Drkcore

22 02 2012 coffeescript Node.js Tweet

Node.jsでYahoo!日本語形態素解析APIを使う

紆余曲折の末Yahoo! APIに落ち着いた。

http = require('http')
querystring = require('querystring')

qst = querystring.stringify({
    appid: '################',
    sentence: 'もももすももももものうち',
    results: 'ma'
})

options = {
  host: 'jlp.yahooapis.jp',
  port: 80,
  path: '/MAService/V1/parse?' + qst
};

http.get(options,
  (res) ->
    body = ""
    res.on('data', (data) -> body += data)
    res.on('end', -> console.log(body))
  ).on('error',
  (e) -> console.log("Got error: " + e.message)
)

追記12.02.22

結果がXMLで返ってくるのでパースしないといけないが、node.js で libxml を使うにはどのライブラリをつかうべきかというエントリを参考にしてlibxml-to-jsを選択。

http = require('http')
querystring = require('querystring')
parser = require('libxml-to-js')

qst = querystring.stringify({
    appid: '######',
    sentence: 'あたしはプログラムの女の子です',
    results: 'ma'
})

options = {
  host: 'jlp.yahooapis.jp',
  port: 80,
  path: '/MAService/V1/parse?' + qst
};

http.get(options,
  (res) ->
    body = ""
    res.on('data', (data) -> body += data)
    res.on('end', -> parser(body, (err, result) -> console.log(result.ma_result.word_list)))
  ).on('error',
  (e) -> console.log("Got error: " + e.message)
)

結果

{ word: 
   [ { surface: 'あたし', reading: 'あたし', pos: '名詞' },
     { surface: 'は', reading: 'は', pos: '助詞' },
     { surface: 'プログラム', reading: 'ぷろぐらむ', pos: '名詞' },
     { surface: 'の', reading: 'の', pos: '助詞' },
     { surface: '女の子', reading: 'おんなのこ', pos: '名詞' },
     { surface: 'です', reading: 'です', pos: '助動詞' } ] }

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021