![](/img/trans.png)
[英]How can i count occurrence of each word in document using Dictionary comprehension
[英]How do I count word occurrence in each line if the word is in a dictionary
我正在嘗試計算每行中正面、負面和中性詞的數量。 我有一個包含名為reviews.txt 的評論行的文本文件。
我的代碼:
poswords = {} #contains positive words
negwords = {} #contains negative words
with open(path + "reviews.txt", 'r') as f:
possum = 0
negsum = 0
neutsum = 0
for line in f.readlines():
lower = line.lower()
for word in lower.split():
if word in poswords:
possum += 1
elif word in negwords:
negsum += 1
else:
neutsum += 1
print(possum)
print(negsum)
print(neutsum)
Output:
1401
633
18351
我如何顯示每行的計數,而不是計算整個文本文件的正面、負面和中性詞?
將最后 3 個打印語句放入 for 循環中。 喜歡
poswords = {} #contains positive words
negwords = {} #contains negative words
with open(path + "reviews.txt", 'r') as f:
for line in f.readlines():
possum = 0
negsum = 0
neutsum = 0
lower = line.lower()
for word in lower.split():
if word in poswords:
possum += 1
elif word in negwords:
negsum += 1
else:
neutsum += 1
print("Line: ", line)
print(possum)
print(negsum)
print(neutsum)
將每行的計數變量設置為零,然后在完成該行后打印變量。
poswords = {} #contains positive words
negwords = {} #contains negative words
with open(path + "reviews.txt", 'r') as f:
for line in f.readlines():
possum = 0
negsum = 0
neutsum = 0
lower = line.lower()
for word in lower.split():
if word in poswords:
possum += 1
elif word in negwords:
negsum += 1
else:
neutsum += 1
print("\n", line)
print(possum)
print(negsum)
print(neutsum)
這也可以用re
來完成:
poswords = {...}
negwords = {...}
pos = '|'.join(poswords)
neg = '|'.join(negwords)
with open("reviews.txt", 'r') as f:
matches = re.findall(f'({pos})|({neg})|(\w+)', f.read())
positive, negitive, neutral = (sum(map(bool, g)) for g in zip(*matches))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.