我有一个文本文件,其中包含单词,情感类别以及与每种情感对应的分数。
word emotion emotion-intensity-score
outraged anger 0.964
brutality anger 0.959
devil anger 0.953
hateful anger 0.940
terrorize anger 0.939
tragedy sadness 0.906
anguish sadness 0.902
devil sadness 0.900
treat trust 0.422
travel trust 0.422
antiseptic trust 0.422
purr joy 0.500
surreal joy 0.500
...
我试图通过制作字典来检索每个输入单词的情感类别。某些单词可能会出现在多个类别中,但我的代码只能检索到一个。有什么办法吗?
filepath = "lexicon.txt"
f = open(filepath, 'r')
lines = f.readlines()
my_dic = {}
for line in lines:
state_code, state_name = line.split('\t')
my_dic[state_code.strip()] = state_name.strip()
print(my_dic['devil'])
我认为这应该可以满足您的需求
filepath = "lexicon.txt"
f = open(filepath, 'r')
lines = f.readlines()
my_dic = {}
for line in lines:
state_code, state_name, score = line.split('\t')
if state_code in my_dic:
my_dic[state_code.strip()].append((state_name.strip(), float(score))
elif state_code not in my_dic:
my_dic[state_code.strip()] = [(state_name.strip(), float(score))]
print(my_dic['devil'])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句