LoginSignup
26
29

More than 5 years have passed since last update.

Pythonで日本語形態素解析

Last updated at Posted at 2016-01-29

Pythonで日本語形態素解析をしてみようと思います。
APIがYahooから出ているのでそれを利用していきます。

まず、POSTする際に必須であるパラメーターは
・appid
・sentence
・results
の3点です。
詳しくPOSTしたい人はパラメーター追加してください。

appidはYahooで登録すれば取得できます。
sentenceは形態素解析する本文です。
resultsはレスポンスの形式指定用です。

形態素解析.py
#coding: utf-8

import requests
from xml.etree.ElementTree import *

def POST(body):
    request_URL = "http://jlp.yahooapis.jp/MAService/V1/parse"

    parameter = {'appid': 'Your appid!',
                'sentence': body,
                'results': 'ma'}
    r = requests.get(request_URL, params=parameter)
    yield (r, r.text)

def XML_parse(body):
    elem = fromstring(body)
    for e in elem.getiterator("{urn:yahoo:jp:jlp}surface"):
        print e.text

if __name__ == '__main__':
    for response in POST(body="今日はいい天気ですね"):
        r = response[0]
        text = response[1]
    XML_parse(text.encode('utf-8'))#encodeしないとUnicode-error
26
29
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
26
29