b.py
python
1# 各種モジュール 2import MeCab 3import random 4from nltk import ngrams 5from collections import Counter, defaultdict 6 7# テストデータのわかち書き、加工 8def parse_words(test_data): 9 10 # テストデータ読み込み 11 with open(test_data, 'r') as f: 12 lines = f.readlines() 13 14 # わかち書き 15 t = MeCab.Tagger('-Owakati') 16 datas = [] 17 for line in lines: 18 data = t.parse(line).strip() 19 datas.append(data) 20 21 # 各行の先頭と末尾に目印を付与 22 datas = [f'__BEGIN__ {data} __END__' for data in datas] 23 datas = [data.split() for data in datas] 24 25 return datas 26 27# 3単語とその出現回数辞書作成 28def create_words_cnt_dic(datas): 29 30 # 行ごとにトライグラム作成し、1つに連結 31 words = [] 32 for data in datas: 33 words.extend(list(ngrams(data, 3))) 34 35 # 3単語とその出現回数辞書 36 words_cnt_dic = Counter(words) 37 38 return words_cnt_dic 39 40# マルコフ用辞書 41def create_m_dic(words_cnt_dic): 42 43 # 空のマルコフ辞書 44 m_dic = {} 45 for k, v in words_cnt_dic.items(): 46 # 先頭2単語、その次の単語 47 two_words, next_word = k[:2], k[2] 48 49 # 存在しなければ作る 50 if two_words not in m_dic: 51 m_dic[two_words] = {'words': [], 'weights': []} 52 53 # 先頭2単語に対し、次に来る単語とその重み 54 m_dic[two_words]['words'].append(next_word) 55 m_dic[two_words]['weights'].append(v) 56 57 return m_dic 58 59# 文章開始単語リストとその重みリスト作成 60def create_begin_words_weights(words_cnt_dic): 61 62 begin_words_dic = defaultdict(int) 63 for k, v in words_cnt_dic.items(): 64 if k[0] == '__BEGIN__': 65 next_word = k[1] 66 begin_words_dic[next_word] = v 67 68 begin_words = [k for k in begin_words_dic.keys()] 69 begin_weights = [v for v in begin_words_dic.values()] 70 71 return begin_words, begin_weights 72 73# 文章生成 74def create_sentences(m_dic, begin_words, begin_weights): 75 76 # 開始単語の抽選 77 begin_word = random.choices(begin_words, weights=begin_weights, k=1)[0] 78 79 # 作成文章の格納 80 sentences = ['__BEGIN__', begin_word] 81 82 # 作成文章の後方2単語をもとに、次の単語を抽選する 83 while True: 84 # 後方2単語 85 back_words = tuple(sentences[-2:]) 86 87 # マルコフ用辞書から抽選 88 words, weights = m_dic[back_words]['words'], m_dic[back_words]['weights'] 89 next_word = random.choices(words, weights=weights, k=1)[0] 90 91 # 終了の目印が出たら抜ける 92 if next_word == '__END__': 93 break 94 95 # 取得単語を追加 96 sentences.append(next_word) 97 98 # 開始マークより後ろを連結 99 return ''.join(sentences[1:]) 100 101# テストデータのわかち書き、加工 102datas = parse_words('osake.txt') 103 104# 3単語の出現回数辞書作成 105words_cnt_dic = create_words_cnt_dic(datas) 106 107# マルコフ用辞書作成 108m_dic = create_m_dic(words_cnt_dic) 109 110# 開始単語とその重みリスト作成 111begin_words, begin_weights = create_begin_words_weights(words_cnt_dic) 112 113# 20回 文章生成 114for i in range(20): 115 text = create_sentences(m_dic, begin_words, begin_weights) 116 print(str(i).zfill(2), text, sep=': ')
osake.text
1酒を1本飲む 22本も飲むと楽しい 35本も飲むと悲しい 4酒は楽しい 5酒は悲しい 6悲しい酒は美空ひばり
上のコードを実行したところ、以下のようなエラーが出ました。どのように解決すれば良いでしょうか。
ちなみに、mecab --versionを実行したところ
mecab of 0.996
と出力された為、mecabはインストール済みです。
Traceback (most recent call last): File "/Users/???/Desktop/Python/b.py", line 2, in <module> import MeCab File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/MeCab/__init__.py", line 10, in <module> from . import _MeCab ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/MeCab/_MeCab.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (__ZN5MeCab11createModelEPKc)
追記
Pythonのmecabバインディングはどうやってインストールしましたか
→Homebrewをインストールした後、brew install mecab mecab-ipadicというふうにインストールしました。以下の動画を参考にしました。
https://youtu.be/mrzPAQOkRlA
PythonはIntelバイナリですか、Apple Silicon バイナリですか
→Apple Silicon バイナリです。
pip show mecab-python3
の結果を質問を編集して書き入れてください
→
Name: mecab-python3
Version: 1.0.5
Summary: Python wrapper for the MeCab morphological analyzer for Japanese
Home-page: https://github.com/SamuraiT/mecab-python3
Author:
Author-email:
License: BSD
Location: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages
Requires:
Required-by:

回答1件
あなたの回答
tips
プレビュー