{"id":336,"date":"2012-07-15T01:29:18","date_gmt":"2012-07-14T16:29:18","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2012\/07\/15\/post_93\/"},"modified":"2012-07-15T01:29:18","modified_gmt":"2012-07-14T16:29:18","slug":"post_93","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2012\/07\/15\/post_93\/","title":{"rendered":"\u5f62\u614b\u7d20\u89e3\u6790\u3092\u884c\u3044\u540d\u8a5e\u306e\u51fa\u73fe\u56de\u6570\u3092\u8abf\u3079\u308b"},"content":{"rendered":"<p><a href=\"http:\/\/developer.yahoo.co.jp\/\">Yahoo \u30c7\u30d9\u30ed\u30c3\u30d1\u30fc\u30cd\u30c3\u30c8\u30ef\u30fc\u30af<\/a>&#160; \u3092\u5229\u7528\u3059\u308b\u3068\u3001<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E5%BD%A2%E6%85%8B%E7%B4%A0%E8%A7%A3%E6%9E%90\">\u5f62\u614b\u7d20\u89e3\u6790<\/a>\u3092\u884c\u3063\u3066\u304f\u308c\u308bWeb\u30b5\u30fc\u30d3\u30b9\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<p><a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E5%BD%A2%E6%85%8B%E7%B4%A0%E8%A7%A3%E6%9E%90\">\u5f62\u614b\u7d20\u89e3\u6790<\/a>\u304c\u4f55\u304b\u3068\u304b\u77e5\u308a\u305f\u3044\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u3055\u3093\u306f\u3001<\/p>\n<p><iframe style=\"width: 120px; height: 240px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4873114705\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><iframe style=\"width: 120px; height: 240px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4873113644\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\u3042\u305f\u308a\u306e\u672c\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3067\u3001\u3061\u3087\u3063\u3068\u30a2\u30f3\u30b1\u30fc\u30c8\u7d50\u679c\u307f\u305f\u3044\u306a\u3082\u306e\u304b\u3089\u5358\u8a9e\u3092\u62bd\u51fa\u3057\u305f\u3044\u306a\u30fc\u3068\u601d\u3044\u3001\u307e\u3041\u305d\u3093\u306a\u306b\u4ef6\u6570\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u30d7\u30ed\u30b0\u30e9\u30de\u305f\u308b\u3082\u306e\u3001<a href=\"https:\/\/www.google.co.jp\/webhp?sourceid=chrome-instant&amp;ie=UTF-8#hl=ja&amp;gs_nf=1&amp;gs_is=1&amp;pq=%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%20%E6%80%A0%E3%81%91%E8%80%85&amp;cp=10&amp;gs_id=4b&amp;xhr=t&amp;q=%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E+%E3%82%81%E3%82%93%E3%81%A9%E3%81%8F%E3%81%95%E3%81%8C%E3%82%8A&amp;pf=p&amp;sclient=psy-ab&amp;oq=%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E+%E3%82%81%E3%82%93%E3%81%A9%E3%81%8F&amp;gs_l=&amp;pbx=1&amp;bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&amp;fp=729efe3ab5e5d226&amp;biw=1024&amp;bih=651\">\u3081\u3093\u3069\u304f\u3055\u304c\u308a\u3084\u3067\u7121\u3051\u308c\u3070\u306a\u3089\u306a\u3044<\/a> \u3068\u601d\u3044\u7acb\u3061\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u7c21\u5358\u306b\u66f8\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u518d\u5229\u7528\u3067\u304d\u305d\u3046\u306a\u306e\u3067\u3001\u30e1\u30e2\u3082\u304b\u306d\u3066\u30d6\u30ed\u30b0\u306b\u4e0a\u3052\u3068\u3053\u3046\u304b\u3068\u3002<\/p>\n<p>\u3042\u3001\u5ff5\u306e\u305f\u3081\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306f Python \u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\"># -*- coding: utf-8 -*-\r\nimport sys\r\nimport getopt\r\nimport codecs\r\nimport urllib\r\nimport urllib2\r\nimport xml.dom.minidom\r\n\r\nAPPID = r'yahoo \u30c7\u30d9\u30ed\u30c3\u30d1\u30fc\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3067ID\u3092\u53d6\u5f97\u3057\u3066\u304f\u3060\u3055\u3044'\r\nREQUEST_URL = r'http:\/\/jlp.yahooapis.jp\/MAService\/V1\/parse'\r\n\r\ndef main():\r\n    try:\r\n        opts, args = getopt.getopt(sys.argv[1:],&quot;i:o:c:&quot;)\r\n    except getopt.GetoptError:\r\n        usage()\r\n        sys.exit(2)\r\n    content_file = None\r\n    output_file = None\r\n    code_set = None\r\n    print opts\r\n    for o, a in opts:\r\n        if o == '-i':\r\n            content_file = a\r\n        if o == '-o':\r\n            output_file = a\r\n        if o == '-c':\r\n            code_set = a\r\n    if content_file == None:        \r\n        usage()\r\n        sys.exit(2)\r\n    else:\r\n        if output_file == None:\r\n            output_file = content_file + &quot;.out&quot;\r\n        if code_set == None:\r\n            code_set = 'utf-8'\r\n    analyze(content_file, output_file, code_set)\r\n        \r\ndef usage():\r\n    print 'usage: questionnaire -i {content_file} -o {output_file} -c {code_set}'\r\n    \r\ndef analyze(content_file, output_file, code_set):\r\n    sentence = make_sentence(content_file, code_set)\r\n    param_map = {}\r\n    param_map['sentence'] = sentence\r\n    param_map['results'] = r'uniq' #r'uniq'\r\n    param_map['filter'] = r'9'\r\n    request = make_request(param_map)\r\n    \r\n    xml = urllib2.urlopen(request)\r\n    ret = parse(xml)\r\n    \r\n    output_report(output_file, ret, code_set)\r\n    \r\ndef output_report(output_file, ret, code_set):\r\n    out_cols_ja = {'count':u'\u51fa\u73fe\u6570','surface':u'\u8a9e\u53e5'}\r\n    ret.insert(0, out_cols_ja)\r\n    \r\n    out = codecs.open(output_file, 'w', code_set)\r\n    \r\n    for item in ret:\r\n        line = '%s,%s' % (item['surface'],item['count'])\r\n        print line\r\n        out.write(line)\r\n\r\ndef parse(xml_response):\r\n    result = []\r\n    \r\n    dom = xml.dom.minidom.parse(xml_response)\r\n    root = dom.documentElement\r\n    words = root.getElementsByTagName('word')\r\n\r\n    for word in words:\r\n        result_item = {'count':0,'surface':''}\r\n        result.append(result_item)\r\n        for child in word.childNodes:\r\n            if child.nodeName == 'count': # \u5f62\u614b\u7d20\u306e\u51fa\u73fe\u6570\r\n                result_item[child.nodeName] = get_node_text(child) \r\n            elif child.nodeName == 'surface': # \u5f62\u614b\u7d20\u306e\u8868\u8a18\r\n                result_item[child.nodeName] = get_node_text(child)\r\n    \r\n    return result\r\n    \r\ndef get_node_text(node):\r\n    ret = ''\r\n    for child in node.childNodes:\r\n        if child.nodeType in (child.TEXT_NODE, child.CDATA_SECTION_NODE):\r\n            ret = ret + child.data\r\n    return ret\r\n    \r\ndef make_request(param_map):\r\n    param_map['appid'] = APPID\r\n    return REQUEST_URL + '?' + urllib.urlencode(param_map) \r\n    \r\ndef make_sentence(content_file, code_set):\r\n    f = codecs.open(content_file, 'r', code_set)\r\n    return ''.join([l.replace('\\r',' ').replace('\\n',' ') for l in f])\r\n\r\nif __name__ == '__main__':\r\n    main()<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3057\u3066\u4e0a\u3052\u308b\u3068\u3001\u30c6\u30ad\u30b9\u30c8\u4e2d\u306b\u542b\u307e\u308c\u308b\u540d\u8a5e\u3068\u305d\u306e\u51fa\u73fe\u6570\u3092\u3001CSV\u5f62\u5f0f\u3067\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<p>-i \u306f \u30c6\u30ad\u30b9\u30c8\u3092\u542b\u3093\u3060\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3001-c \u306f\u305d\u306e\u30d5\u30a1\u30a4\u30eb&amp;\u51fa\u529b\u30d5\u30a1\u30a4\u30eb\u306e\u30a8\u30f3\u30b3\u30fc\u30c9\u3001-o \u3067\u51fa\u529b\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">-i &quot;c:\\work\\test.txt&quot; -c &quot;shift-jis&quot;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yahoo \u30c7\u30d9\u30ed\u30c3\u30d1\u30fc\u30cd\u30c3\u30c8\u30ef\u30fc\u30af&#160; \u3092\u5229\u7528\u3059\u308b\u3068\u3001\u5f62\u614b\u7d20\u89e3\u6790\u3092\u884c\u3063\u3066\u304f\u308c\u308bWeb\u30b5\u30fc\u30d3\u30b9\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002 \u5f62\u614b\u7d20\u89e3\u6790\u304c\u4f55\u304b\u3068\u304b\u77e5\u308a\u305f\u3044\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u3055\u3093\u306f\u3001 \u3042\u305f\u308a\u306e\u672c\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[20],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-python"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}