LoginSignup
2
2

More than 5 years have passed since last update.

キーワード抽出をpythonでする。(yahooさんの力を借りて)

Posted at

1 動機

yahooさんのapiを使ってのキーワード抽出をしたいと思ったがサンプルコードがPHPしかなかった。
(https://developer.yahoo.co.jp/sample/jlp/sample3.html)
pythonでしたいよ〜

2 できること

文章を入れると、キーワード単語とその重要度が出力される。

3 サンプルリクエストURL

https://jlp.yahooapis.jp/KeyphraseService/V1/extractappid="yahoocliantID"&sentence=<対象のテキスト>
yahooのcliant ID取得:(https://developer.yahoo.co.jp/yconnect/v2/registration.html)

4 コード

keyword.py
import urllib.request
import sys
import xml.etree.ElementTree as ET
#sentence=解析したい文章
def key(sentence):
    url="https://jlp.yahooapis.jp/KeyphraseService/V1/extract?appid=<yahooのcliant ID>&sentence="+urllib.parse.quote(sentence)
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req) as res:
        body = res.read()
        root = ET.fromstring(body)
    output=[]
    for child in root:
        sen=child[0].text+":"+child[1].text
        output.append(sen)
    return output

[参照]
・URLに日本語を含む時の対処(https://note.nkmk.me/python-urllib-parse-quote-unquote/)
・xmlを取得(https://qiita.com/Takaki_/items/4ba7f5e327296d403e65)

2
2
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
2
2