knpという構文解析システムを使用して、特定の属性を持つ語を抽出し、
辞書に追加するというプログラムを作成しているのですが、何故か
上書きされてしまいます。
python
1# -*- coding: utf-8 -*- 2import jctconv 3import semiformalizer 4 5def getDictforEditor(text): 6 number_of_time_list = [] #回数 7 strength_quantity_qualified_list = [] #強数量修飾 8 number_of_hours_list = [] #時数ノ 9 future_phrase_list = [] #未来句 10 quantity_list = [] #数量 11 strength_time_list = [] #強時間 12 time_list = [] #時間 13 dict_for_editor = {'回数':[],'強数量修飾':[],'時数ノ':[],'未来句':[],'数量':[],'強時間':[],'時間':[]} 14 tag = semiformalizer.MakeDict(text).tag() 15 prop = semiformalizer.MakeDict(text).prop() #tag,propはknpの情報を抜き出している 16 for i in prop.keys(): 17 if not (prop[i]['case'] is 'main' or prop[i]['case'] is 'obj' or prop[i]['type'] is 'action'): 18 if tag[i]['tag'].get('回数'): 19 number_of_time_list.append(prop[i]['midasi']) 20 elif tag[i]['tag'].get('強数量修飾'): 21 strength_quantity_qualified_list.append(prop[i]['midasi']) 22 elif tag[i]['tag'].get('時数ノ'): 23 number_of_hours_list.append(prop[i]['midasi']) 24 elif tag[i]['tag'].get('未来句'): 25 future_phrase_list.append(prop[i]['midasi']) 26 elif tag[i]['tag'].get('数量'): 27 quantity_list.append(prop[i]['midasi']) 28 elif tag[i]['tag'].get('強時間'): 29 strength_time_list.append(prop[i]['midasi']) 30 elif tag[i]['tag'].get('時間'): 31 time_list.append(prop[i]['midasi']) 32 # print(time_list) 33 dict_for_editor.update({'回数':number_of_time_list}) 34 dict_for_editor.update({'強数量修飾':strength_quantity_qualified_list}) 35 dict_for_editor.update({'時数ノ':number_of_hours_list}) 36 dict_for_editor.update({'未来句':future_phrase_list}) 37 dict_for_editor.update({'数量':quantity_list}) 38 dict_for_editor.update({'強時間':strength_time_list}) 39 dict_for_editor.update({'時間':time_list}) 40 # print(dict_for_editor) 41 return dict_for_editor 42 43 44if __name__ == '__main__': 45 dict_for_editor = [] 46 file = 'sample' 47 with open('{}.txt'.format(file),encoding = 'utf-8') as f: 48 text_list = f.readlines() 49 for text in text_list: 50 if not text == '\n' and not text is None: 51 text = ''.join(text.split()) # スペースを除く 52 text = jctconv.z2h(text,kana = False, digit = True, ascii = True) 53 dict_for_editor = getDictforEditor(text) 54 with open('{}の辞書.txt'.format(file),'w',encoding = 'utf-8') as f: 55 f.write(str(dict_for_editor)) 56 print(dict_for_editor)
<sample.txtの中身>
端末及びプリンターは、納入場所に現在設置しているものを使用すること。
献立業務において一ヶ月のカレンダー表示できること。
<出力結果>
{'回数': [], '強数量修飾': [], '時数ノ': ['一ヶ月の'], '未来句': [], '数量': [], '強時間': [], '時間': []}
理想としては、
{'回数': [], '強数量修飾': [], '時数ノ': ['一ヶ月の'], '未来句': [], '数量': [], '強時間': [], '時間': ['現在']}
1行目の結果は出てはいるのですが、2行目を読む際、上書きされるという結果が
起こっています。
原因としては、
・1行ずつknpの情報の判定をしているので、次の行の文章を読んだ際に上書きされる。
・それぞれ用意したリストにappendするときに上書きされる。
この2つが原因の候補になると思っています。
出来れば、助言をいただけると幸いです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/30 13:02
2018/09/30 13:11
2018/10/01 02:22
2018/10/01 17:08