2つのファイルの共通用語について
解決したいこと
小文字,大文字関係なく共通用語としてカウントさせるようにしたい.
このソースコードでは、csvのファイルとテキストファイルを比較して、共通用語があったらカウントするプログラムです.
Chinaとacuteを出力結果でカウントを2としたいのですが分からなかったので教えて頂きたいです.
ソースコード
with open('1.csv','r') as f:
rows = f.readlines()
with open('1.txt','r') as f:
text = f.read()
id_count = {}
with open('re_1.txt','w') as f:
f.write('用語, 回数, id\n')
for row in rows:
tmp = row.split(',')
id = tmp[0]
word = tmp[1].strip()
count = text.count(word)
if count==0:
pass
else:
f.write('%s,%d,%s\n' % (word, count, id))
if id in id_count:
id_count[id] += count
else:
id_count[id] = count
1.txt
Severe Acute respiratory distress syndrome due to acute coronavirus (SARS-CoV-2), which was first diagnosed in china, China in December 2019.
1.csv
0000,acute
0000,distress
1111,coronavirus
1111,China
実行結果
用語, 回数, id
acute,1,0000
distress,1,0000
coronavirus,1,1111
China,1,1111
0 likes