social-ime が出す変換候補を elisp で取得する

昨日のつづきだったり。
Web 上から変換候補を取得する事で流行語やネットのスラングにも対応できる social-ime から elisp で変換候補を取得してみました。
# まあ、既に SKK で social-ime を使うための elisp があったりしますが、まあ、昨日のネタの延長ということで…。



social-ime には2種類の API が存在し、1つが通常の「かな漢字変換API」もう1つが「予測変換API」です。
HTTP GET する関数は分離し、以下のように作ってみました。

(require 'url-http)
(require 'json)

;; HTTP GET し、その結果を返すだけの関数
(defun my-url-get-candidates (url &optional coding)
  (let (buf p)
    (unwind-protect
        (progn
          (setq buf
                (let ((url-request-extra-headers
                       `(("Accept-Encoding" . "identity")
                         ("Content-type"    .
                          "application/x-www-form-urlencoded")))
                      (url-request-method "GET")
                      (url-max-redirections 0))
                  (url-retrieve-synchronously url)))
          (when (setq p (url-http-symbol-value-in-buffer
                         'url-http-end-of-headers
                         buf))
            (with-current-buffer buf
              (decode-coding-string
               (buffer-substring (1+ p) (point-max))
               (if coding coding 'utf8)))))
      (when buf
        (kill-buffer buf)))))

;; かな漢字変換 API
(defun my-social-ime-get-candidates (&optional input coding)
  (let* ((url (concat "http://www.social-ime.com/api/?string="
                      (url-hexify-string (if (null input) (current-word) input))))
         (dsv (my-url-get-candidates url (if coding coding 'euc-jp)))
         (candidates (split-string (substring dsv
                                              0
                                              (- (length dsv) 2))
                                   "\t")))
    candidates))
;; 予測変換 API
(defun my-social-ime-predict-get-candidates (&optional input coding)
  (let* ((url (concat "http://www.social-ime.com/api2/predict.php?string="
                      (url-hexify-string (if (null input) (current-word) input))))
         (dsv (my-url-get-candidates url (if coding coding 'utf-8)))
         (candidates (split-string (substring dsv
                                              0
                                              (- (length dsv) 2))
                                   "\t")))
    candidates))

それで実行結果が以下です。

;; かな漢字変換
(my-social-ime-get-candidates "あ")     ; => ("亜" "唖" "娃" "阿" "蛙" "吾" "亞" "呀" "堊" "婀" "椏" "痾" "錏" "鐚" "閼" "鴉" "あ" "ア")
(my-social-ime-get-candidates "すずみや") ; => ("涼宮" "涼みや" "涼宮ハルヒ" "涼宮ハルヒの憂鬱" "すずみや" "スズミヤ")

(my-social-ime-predict-get-candidates "とりあ") ; => ("とりあえず" "取り扱い" "とりあえず、" "取扱い" "取扱" "取り扱いについて" "取り上げ" "取り扱いについて|" "取り扱って" "取り扱っ" "取り扱っております。" "取り扱いについて|特定商取引法" "取り扱いについて|特定商取引" "取扱い商品一覧" "取扱い商品" "取り扱っており" "取扱いについて" "取り上げられ" "とリアルタイムの登録データが表示" "とリアルタイムの登録データが" "取り扱う" "とリアルタイムの登録データ" "取り上げて" "取扱い中!" "取扱商品" "取扱い中" "とリアルタイムの登録" "取扱状況について" "取り扱ってい" "取り扱いが" "とりあえず系" "取扱状況")

問題点としては、特にタイムアウトの仕組みなんかを入れてないので、向こうのサーバからの反応が遅くなっていたり、死んでいたりした時、
結果を得るのにかなり待たされる事になる可能性があるんじゃないかなーという事があげられると思います。


まあそんなこんなで。

更新時刻

  • 2010/01/29/20:25