Skip to content

Instantly share code, notes, and snippets.

@moxuse
Created October 23, 2013 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moxuse/7114253 to your computer and use it in GitHub Desktop.
Save moxuse/7114253 to your computer and use it in GitHub Desktop.
YAHoo API KeyphraseService
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import optparse
import httplib
from lxml import etree
# Yahoo!API config
appId = 'APPLICATION ID'
session = httplib.HTTPConnection('jlp.yahooapis.jp')
analiseStr = '夏が終わってしまう、鬱だ'
# set thresould if you want
thresould = 0
def request(reqstStr):
analiseStr = reqstStr
session.request('GET', '/KeyphraseService/V1/extract?appid='+appId+'&sentence='+analiseStr)
response = session.getresponse()
if response.status == httplib.OK:
data = response.read()
# make etree from deta
root = etree.fromstring(data)
# parse XML
if len(root) > 0:
for item in root:
if int(item[1].text) > thresould:
print"result: " + item[0].text + " point: " + item[1].text
def main():
parser = optparse.OptionParser('Usage: %prog [options]')
parser.add_option('-r', '--request',
dest='reqstStr',
help='request with strings')
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error('too many arguments')
if options.reqstStr:
request(options.reqstStr)
else:
request( analiseStr )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment