前提
pythonでCaboChaを使用して文章の処理をしたいと考えています
MeCabで使用している辞書をNEologdに変更したところ、動くコードと動かないコードが出てきました。
エラーの解決方法をご教授いただきたいです。
該当のソースコード
python
1import CaboCha 2 3c = CaboCha.Parser() 4sentence = "日本では呪術廻戦が人気だ" 5 6tree = c.parse(sentence) 7chunkId = 0 8for i in range(0, tree.size()): 9 token = tree.token(i) 10 if token.chunk != None: 11 print(chunkId, token.chunk.link, token.chunk.head_pos, 12 token.chunk.func_pos, token.chunk.score) 13 chunkId += 1 14 15 print(token.surface, token.feature, token.ne)
こちらのコードは問題なく動くのですが
python
1import CaboCha 2import graphviz 3import matplotlib 4from tkinter import * 5from tkinter import ttk 6import tkinter 7 8#ウィンドウを作成 9root = Tk() 10 11#ウィンドウサイズを指定 12root.geometry("10000x10000") 13 14#ウィンドウタイトルを指定 15root.title('入力フォーム') 16 17#フレームを作成 18frame1 = ttk.Frame(root, padding=(32)) 19frame1.grid() 20 21#文字を入力する欄 22label1 = ttk.Label(frame1, text='解析する文章',padding=(10,2)) 23label1.grid(row=0,column=0,sticky=E) 24 25#入力フォーム作成 26name = StringVar() 27name_txt = ttk.Entry( 28 frame1, 29 textvariable=name, 30 width=100) 31name_txt.grid(row=0, column=1) 32 33#抜き取る品詞 34select_conditions = ['名詞','動詞','形容詞'] 35 36button1 = ttk.Button( 37 frame1, text='OK', 38 command=lambda: (create_graph()) 39) 40button1.grid(row=3, column=0) 41 42c = CaboCha.Parser() 43 44 45 46 47def create_graph(): 48 49 x = name.get() 50 51 if x == "": 52 x ="エレベーターのドアが開く" 53 cabocha_graph() 54 55# Graphvizのオブジェクトを作成 --- (*1) 56 57 58def cabocha_graph(): 59 60 g = graphviz.Digraph(format='svg', filename='test') 61 62 g.attr('node', fontname = 'Meiryo UI') 63 g.attr('edge', fontname = 'Meiryo UI') 64 65 c = CaboCha.Parser() 66 sentence = "お金を投入すると商品の販売可能" 67 68 tree = c.parse(sentence) 69 x = [] 70 count = 0 71 count_list = [] 72 chunk_count = [] 73 y = [] 74 chunkId = 0 75 for i in range(0, tree.size()): 76 token = tree.token(i) 77 x = token.feature.split(",") 78 if token.chunk != None: 79 if x[0] in select_conditions: 80 count += 1 81 count_list.append(x) 82 83 print(token.chunk.link,x) 84 y.append(token.chunk.link) 85 86 chunkId += 1 87 chunk_count.append(chunkId) 88 89 for i in range(0,count): 90 g.node(f'{chunk_count[i]}',count_list[i][6]) 91 92 for i in range(0,count - 1): 93 g.edge(f'{chunk_count[i]}',f'{chunk_count[i+1]}',arrowhead="none") 94 95 for i in range(0,count - 1): 96 if chunk_count[i] != y[i]: 97 g.edge(f'{chunk_count[i]}',f'{y[i]}',color="red") 98 99 100 101 # 表示 102 g.view() 103 104root.mainloop()
こちらのコードでは以下のエラーが出てしまいます
補足情報(FW/ツールのバージョンなど)
python 3.11.1 MeCab 0.996 CaboCha 0.69
回答1件