{"id":1249,"date":"2020-09-08T11:42:57","date_gmt":"2020-09-08T02:42:57","guid":{"rendered":"https:\/\/tech.at-iroha.jp\/?p=1249"},"modified":"2021-09-08T14:40:31","modified_gmt":"2021-09-08T05:40:31","slug":"mecab-%e3%81%a8-python-%e3%81%ab%e3%82%88%e3%82%8b%e5%bd%a2%e6%85%8b%e7%b4%a0%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/tech.at-iroha.jp\/?p=1249","title":{"rendered":"\u81ea\u7136\u8a00\u8a9e\u51e6\u7406\uff1aMeCab \u3068 Python \u306b\u3088\u308b\u5f62\u614b\u7d20\u89e3\u6790"},"content":{"rendered":"\n<p>\u5148\u65e5\u3001\u3042\u308b\u5b9f\u9a13\u3067\u5f97\u3089\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u5206\u6790\u3001\u8a55\u4fa1\u3059\u308b\u305f\u3081\u306b\u3001\u5f62\u614b\u7d20\u89e3\u6790\u3092\u884c\u3046\u6a5f\u4f1a\u304c\u3042\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u624b\u9806\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u5f62\u614b\u7d20\u89e3\u6790\u3068\u306f\u81ea\u7136\u8a00\u8a9e\u3067\u66f8\u304b\u308c\u305f\u6587\u7ae0\u3092\u3001\u8f9e\u66f8\u3092\u5143\u306b\u3001\u5f62\u614b\u7d20\uff08\u8a00\u8a9e\u3068\u3057\u3066\u610f\u5473\u3092\u6301\u3064\u6700\u5c0f\u5358\u4f4d\uff09\u306b\u5206\u5272\u3057\u3001\u305d\u308c\u305e\u308c\u306b\u54c1\u8a5e\u3084\u6d3b\u7528\u5f62\u60c5\u5831\u3092\u4ed8\u52a0\u3059\u308b\u4e00\u9023\u306e\u51e6\u7406\u3092\u6307\u3057\u3066\u3044\u307e\u3059\u3002<br>\u5f62\u614b\u7d20\u89e3\u6790\u306e\u6b74\u53f2\u306f\u53e4\u304f\u30011970\u5e74\u4ee3\u304b\u3089\u7814\u7a76\u3055\u308c\u3001\u69d8\u3005\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u63d0\u6848\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>\u4eca\u56de\u306e\u5f62\u614b\u7d20\u89e3\u6790\u306b\u306f MeCab \u3092\u4f7f\u7528\u3057\u307e\u3057\u305f\u3002Mecab \u306f\u4eac\u90fd\u5927\u5b66\u3068NTT\u30b3\u30df\u30e5\u30cb\u30b1\u30fc\u30b7\u30e7\u30f3\u79d1\u5b66\u57fa\u790e\u7814\u7a76\u6240\u306e\u5171\u540c\u7814\u7a76\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u958b\u767a\u3055\u308c\u305f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u5f62\u614b\u7d20\u89e3\u6790\u30a8\u30f3\u30b8\u30f3\u3067\u3001\u65e2\u306b\u591a\u304f\u306e\u5206\u91ce\u3067\u6d3b\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>MeCab \u3092\u6271\u3046\u30b9\u30af\u30ea\u30d7\u30c8\u8a00\u8a9e\u3068\u3057\u3066\u306f Python \u3092\u9078\u629e\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u6790\u74b0\u5883\u306e\u69cb\u7bc9<\/h2>\n\n\n\n<p>\u307e\u305a\u306f Python \u3067 MeCab \u3092\u6271\u3048\u308b\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002\u4eca\u56de\u306f Windows \u4e0a\u306b\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306eURL\u304b\u3089 Python 3.x \u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001Python \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br>https:\/\/pythonlinks.python.jp\/ja\/index.html<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MeCab \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306eURL\u304b\u3089 MeCab (Binary package for MS-Windows) \u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br>https:\/\/taku910.github.io\/mecab\/#download<\/p>\n\n\n\n<p>\u6587\u5b57\u30b3\u30fc\u30c9\u306b\u306fUTF8\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.stack.imgur.com\/Cgc5Y.png\" alt=\"Python\u3067MeCab\u3092\u4f7f\u3046\u3068\u51fa\u529b\u304c\u6587\u5b57\u5316\u3051\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002 - \u30b9\u30bf\u30c3\u30af\u30fb\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">mecab-python \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n\n\n\n<p>\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\u3092\u8d77\u52d5\u3057\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5165\u529b\u3057\u3001mecab-python \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python -m pip install mecab<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MeCab \u306b\u3088\u308b\u89e3\u6790<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u540d\uff1asample.txt<br>\u6587\u5b57\u30b3\u30fc\u30c9\uff1aUTF-8<\/p>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u543e\u8f29\u306f\u732b\u3067\u3042\u308b\u3002\u540d\u524d\u306f\u307e\u3060\u7121\u3044\u3002\n\u3069\u3053\u3067\u751f\u308c\u305f\u304b\u3068\u3093\u3068\u898b\u5f53\u304c\u3064\u304b\u306c\u3002<\/pre>\n\n\n\n<p>\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3001\u4e00\u6642\u7684\u306b\u51fa\u529b\u3092UTF8\u306b\u3057\u307e\u3059\u3002\uff08PowerShell \u3067\u306f\u6b63\u3057\u304f\u51fa\u529b\u3055\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chcp 65001<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3001sample.txt \u306e\u4e2d\u8eab\u306e\u5f62\u614b\u7d20\u89e3\u6790\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mecab sample.txt<\/pre>\n\n\n\n<p>\u6b63\u5e38\u306b\u5b9f\u884c\u3067\u304d\u305f\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"519\" src=\"https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image.png\" alt=\"\" class=\"wp-image-1251\" srcset=\"https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image.png 993w, https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-700x366.png 700w, https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-768x401.png 768w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u6790\u7528\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u4f5c\u6210\u3068\u5b9f\u884c<\/h2>\n\n\n\n<p>\u5f62\u614b\u7d20\u89e3\u6790\u5f8c\u3001\u7279\u5b9a\u306e\u54c1\u8a5e\uff08\u540d\u8a5e\u3001\u5f62\u5bb9\u8a5e\u3001\u52d5\u8a5e\u3001\u526f\u8a5e\uff09\u306e\u5f62\u614b\u7d20\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u540d\uff1acount_words.py<br>\u6587\u5b57\u30b3\u30fc\u30c9\uff1aUTF-8<\/p>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport MeCab\nimport time\nimport sys\n\n# \u5f15\u6570\u3067\u6307\u5b9a\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u53d6\u5f97\nfrom sys import argv\ninput_file_name= sys.argv&#x5B;1]\n\n# \u30d5\u30a1\u30a4\u30eb\u540d\u3092\u51fa\u529b\n#print(input_file_name)\n\n# \u6307\u5b9a\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\nwith open(input_file_name, encoding=&quot;UTF-8&quot;) as f:\n\tdata = f.read()\n\n# \u5f62\u614b\u7d20\u89e3\u6790\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b\ntext = MeCab.Tagger().parse(data)\n\n#print(text)\n\n# \u5f62\u614b\u7d20\u89e3\u6790\u7d50\u679c\u3092\u6539\u884c\u3067\u5206\u5272\nlines = text.split(&quot;\\n&quot;)\n\n\ncnt_word = 0 # \u540d\u8a5e\u306e\u6570\nwords = &#x5B;] # \u5358\u8a9e\u30ea\u30b9\u30c8\n\n# \u5404\u884c\u3054\u3068\u306b\u51e6\u7406\u3092\u884c\u3046\nfor line in lines:\n\t#\u5f62\u614b\u7d20\u89e3\u6790\u7d50\u679c\u3092\u5206\u5272\n\tblocks = line.split(&quot;\\t&quot;)\n\t#print(blocks&#x5B;1])\n\t\n\tif len(blocks) &gt; 1:\n\t\tword = blocks&#x5B;0] #\u5bfe\u8c61\u6587\u5b57\u5217\uff08\u4f8b\uff1a\u3059\u3082\u3082\uff09\n\t\tinfo  = blocks&#x5B;1] #\u54c1\u8a5e\u60c5\u5831\uff08\u4f8b\uff1a\u540d\u8a5e,\u4e00\u822c,*,*,*,*,\u3059\u3082\u3082,\u30b9\u30e2\u30e2,\u30b9\u30e2\u30e2\uff09\n\t\titems = info.split(&quot;,&quot;) #\u54c1\u8a5e\u60c5\u5831\u3092\u5206\u5272\n\t\t\n\t\tif words.count(word) == 0:\n\t\t\tfor item in items:\n\t\t\t\t#print(item)\n\t\t\t\t\n\t\t\t\t# \u5bfe\u8c61\u6587\u5b57\u306e\u54c1\u8a5e\u304c\u540d\u8a5e\u3001\u5f62\u5bb9\u8a5e\u3001\u52d5\u8a5e\u3001\u526f\u8a5e\u3001\u3044\u305a\u308c\u304b\u306e\u5834\u5408\u3001\u30ab\u30a6\u30f3\u30bf\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\n\t\t\t\tif item == &quot;\u540d\u8a5e&quot; or item == &quot;\u5f62\u5bb9\u8a5e&quot; or item == &quot;\u52d5\u8a5e&quot; or item == &quot;\u526f\u8a5e&quot;:\n\t\t\t\t\tcnt_word += 1\n\t\t\t\t\twords.append(word)\n\n# \u7d50\u679c\u3092\u51fa\u529b\u3059\u308b\nprint(&#039;Total : &#039; + str(cnt_word))\nprint(&#039;Words : &#039; + &#039;, &#039;.join(words))\n<\/pre><\/div>\n\n\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python count_words.py input.txt<\/pre>\n\n\n\n<p>\u7279\u5b9a\u306e\u54c1\u8a5e\uff08\u540d\u8a5e\u3001\u5f62\u5bb9\u8a5e\u3001\u52d5\u8a5e\u3001\u526f\u8a5e\uff09\u306e\u5f62\u614b\u7d20\u306e\u5408\u8a08\u3068\u3001\u5bfe\u8c61\u3068\u306a\u3063\u305f\u5f62\u614b\u7d20\u306e\u30ea\u30b9\u30c8\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"519\" src=\"https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-1.png\" alt=\"\" class=\"wp-image-1254\" srcset=\"https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-1.png 993w, https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-1-700x366.png 700w, https:\/\/tech.at-iroha.jp\/wp-content\/uploads\/2020\/09\/image-1-768x401.png 768w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<p>\u3053\u308c\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u4f8b\u3067\u3059\u304c\u3001\u5fdc\u7528\u3059\u308b\u3068\u5927\u91cf\u306e\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u3001\u69d8\u3005\u306a\u89d2\u5ea6\u304b\u3089\u5206\u6790\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u65e5\u3001\u3042\u308b\u5b9f\u9a13\u3067\u5f97\u3089\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u5206\u6790\u3001\u8a55\u4fa1\u3059\u308b\u305f\u3081\u306b\u3001\u5f62\u614b\u7d20\u89e3\u6790\u3092\u884c\u3046\u6a5f\u4f1a\u304c\u3042\u3063\u305f\u305f\u3081\u3001\u305d\u306e\u624b\u9806\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002 \u5f62\u614b\u7d20\u89e3\u6790\u3068\u306f\u81ea\u7136\u8a00\u8a9e\u3067\u66f8\u304b\u308c\u305f\u6587\u7ae0\u3092\u3001\u8f9e\u66f8\u3092\u5143\u306b\u3001\u5f62\u614b\u7d20\uff08\u8a00\u8a9e\u3068\u3057\u3066\u610f\u5473\u3092\u6301\u3064\u6700\u5c0f\u5358\u4f4d\uff09\u306b\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1249","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nlp"],"_links":{"self":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1249"}],"version-history":[{"count":14,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1249\/revisions\/1569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/media\/1251"}],"wp:attachment":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}