LoginSignup
1
0

More than 5 years have passed since last update.

Hylang で適当にHTTPサーバを立てる(3) (2018年7月現在)

Last updated at Posted at 2018-07-23

前回

Hylang で適当にHTTPサーバを立てる(1) (2018年7月現在)
Hylang で適当にHTTPサーバを立てる(2) (2018年7月現在)

JSON を返そう

ちょこっと変えるだけで十分だった。
content-type を変えること、dictなデータにjson.dumps 関数をかけることに注意すれば難しいことはない。

(import [http.server [HTTPServer SimpleHTTPRequestHandler BaseHTTPRequestHandler]]
        [urllib.parse [urlparse]]
        json)

(defclass EchoHandler [BaseHTTPRequestHandler]
  (defn do_GET [self]
    (setv content-len (int (.get self.headers "content-length")))
    (setv request-body (-> (.read self.rfile content-len)
                          (.decode "UTF-8")))
    (print request-body)
    (setv json-data (.loads json request-body))
    (print json-data)
    (print (. json-data ["type"]))
    (print (. json-data ["params"]))
    (.send_response self 200)
    (.send_header self "Content-type" "application/json")
    (.end_headers self)
    (setv response-body {"status" 200
                         "message" "JSONデータ"})
    (.wfile.write self (.encode (.dumps json response-body) "utf-8"))))

(setv httpd (HTTPServer (, "localhost" 8887) EchoHandler))

(.serve_forever httpd)

結果

日本語とシェル的な問題でうまく表示できなかったが、正しいデータが返ってきているとおもわれる。
image.png

機械学習的な息吹を感じよう

なんか良いテーマないかなーって見つけたのは、文章の極性判定。

極性判定とは、要するにその話がどんな雰囲気なの?っていうことを考えさせることで、Pythonでは nltk というパッケージがこれをものすごく簡単にやってくれる。

勿論TensorFlowやChainerで実装する方が勉強になるけど、タイトル的にあんまり重いことはしない方が良い、ということでちゃちゃっと書いてしまう。

以下のようなフォルダ構造を作る。

.-
 |- senti_analysis.hy
 |- test.hy <前回を引き継ぐ>

これから、この senti_analysis.hy を書いていく。

3分でできる文章の極性判定

まず、 twython をインストールする。

pip install twython

(普通に nltk が入ったときに入ると思いきや入っておらずエラーが出てしまったため。)

コードを書く

senti_analysis.hy

;; require 'pip install twython'
(import [nltk.sentiment.vader [SentimentIntensityAnalyzer]]
        nltk)

;; (.download nltk "vader_lexicon")
(setv vader-analyzer (SentimentIntensityAnalyzer))

(defn get-senti [text]
  (.polarity_scores vader_analyzer text))

途中で書かれている (.donwload ... は必要なファイルのダウンロードで、初回の一回だけ行えば良い。

具体的な内容を説明すると、
SentimentIntensityAnalyzerというクラスのインスタンス vader-analyzer を作ってこのメンバ関数である polarity_scores 関数に任意のテキストを与えている。

実際に適用して見ると、以下のようなデータが返ってくる。

(get-senti "I'm happy")

;; => {"neg": 0.0, "neu": 0.213, "pos": 0.787, "compound": 0.5719}

8割 positive であることがわかる。9割くらいは欲しかったが、これは別の機会に(求:おすすめの手法)

さて、残るはこれをサーバにくっつけるだけだ。

test.hy をアップデートしよう

前の項で作った get-senti 関数が欲しているデータは英文テキストである。

想定される利用方法は、クライアントから送られてくる英文テキストに対して get-senti 関数を適用しその結果を返すというものだろう。

よってそのようにハンドラを更新する。

test.hy

(defclass EchoHandler [BaseHTTPRequestHandler]
  (defn do_GET [self]
    (setv content-len (int (.get self.headers "content-length")))
    (setv request-body (-> (.read self.rfile content-len)
                          (.decode "UTF-8")))
    (print request-body)
    (setv json-data (.loads json request-body))
    (print json-data)
    (print (. json-data ["type"]))
    (print (. json-data ["params"]))
    (.send_response self 200)
    (.send_header self "Content-type" "application/json")
    (.end_headers self)
    (setv response-body {"status" 400
                         "params" "不適切なパラメータです"})
    (when (= "senti-analysis" (. json-data ["type"]))
      (setv response-body
            {"status" 200
             "params" (senti_analysis.get-senti (. json-data ["params"]))}))
    (.wfile.write self (.encode (.dumps json response-body) "utf-8"))))

内容は、入力されるJSONの type が senti-analysis であれば senti_analysis.hy の get-senti 関数を実行し、それ以外ではエラーを返す、というものだ。

全体のファイルとフォルダ構成

senti_analysis.hy

;; require 'pip install twython'
(import [nltk.sentiment.vader [SentimentIntensityAnalyzer]]
        nltk)

;; (.download nltk "vader_lexicon")
(setv vader-analyzer (SentimentIntensityAnalyzer))

(defn get-senti [text]
  (.polarity_scores vader_analyzer text))

test.hy

(import [http.server [HTTPServer SimpleHTTPRequestHandler BaseHTTPRequestHandler]]
        [urllib.parse [urlparse]]
        json)
(import senti_analysis)

(defclass EchoHandler [BaseHTTPRequestHandler]
  (defn do_GET [self]
    (setv content-len (int (.get self.headers "content-length")))
    (setv request-body (-> (.read self.rfile content-len)
                          (.decode "UTF-8")))
    (print request-body)
    (setv json-data (.loads json request-body))
    (print json-data)
    (print (. json-data ["type"]))
    (print (. json-data ["params"]))
    (.send_response self 200)
    (.send_header self "Content-type" "application/json")
    (.end_headers self)
    (setv response-body {"status" 400
                         "params" "不適切なパラメータです"})
    (when (= "senti-analysis" (. json-data ["type"]))
      (setv response-body
            {"status" 200
             "params" (senti_analysis.get-senti (. json-data ["params"]))}))
    (.wfile.write self (.encode (.dumps json response-body) "utf-8"))))

(setv httpd (HTTPServer (, "localhost" 8887) EchoHandler))

(.serve_forever httpd) 
.-
 |- senti_analysis.hy
 |- test.hy

結果

image.png

次にやること

何かあればやる
でもHylang+有名ライブラリで書くよりCommon Lispのライブラリを利用したり(機械学習ライブラリであるMGLの更新がほぼ止まっていたりする)、Clojureのライブラリを利用したり(coretexかDeeplearning4j)、するのも楽しそう

repository

参考

https://qiita.com/yukinoi/items/c3c4e4e24a66b88d8215

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0