我尝试做一个基于7000字的冠词的情感分析。代码在Python中工作,但它选择所有的组合而不是不同的单词。鲜明的词情感分析
例如,字典中说enter
和文字说enterprise
。我怎样才能改变它没有看到这个匹配的代码?
dictfile = sys.argv[1]
textfile = sys.argv[2]
a = open(textfile)
text = string.split(a.read())
a.close()
a = open(dictfile)
lines = a.readlines()
a.close()
dic = {}
scores = {}
current_category = "Default"
scores[current_category] = 0
for line in lines:
if line[0:2] == '>>':
current_category = string.strip(line[2:])
scores[current_category] = 0
else:
line = line.strip()
if len(line) > 0:
pattern = re.compile(line, re.IGNORECASE)
dic[pattern] = current_category
for token in text:
for pattern in dic.keys():
if pattern.match(token):
categ = dic[pattern]
scores[categ] = scores[categ] + 1
for key in scores.keys():
print key, ":", scores[key]
如果你的字典有* *的话,为什么重新使用呢?为什么不'如果行==令牌? –
谢谢你的亲友Robin Koch。问题在于字典来自一个单独的文件。我们不能在文件中包含分离的术语,我们正在测量情绪。我们没有做一个字数。预先感谢您 – Guido
我仍然不确定您与什么相匹配。你能提供一些例子吗?如果你真的做了're.compile('enter')。match('entprise')',你不需要正则表达式。如果你的字典实际上包含正则表达式,那么你应该把它添加到问题中。 –