我嘗試做一個基於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')',你不需要正則表達式。如果你的字典實際上包含正則表達式,那麼你應該把它添加到問題中。 –