为什么我的Python程序无法进行不包含停用词的字数统计?

时间:2018-11-18 03:47:41

标签: python word

这是我第一次编程。 这是我写的代码。我在文本文件上执行此操作以计算内容单词,但不计算停用词(例如“ the”或“ a”)。 每当我打印progran时,我都会写上所有单词,包括停用词。 这是我写的程序。

 from collections import Counter


with open("words.py") as input_file:
   count = Counter(word for line in input_file
                     for word in line.split())

stop_words = frozenset(['the', 'a', 'is'])
def mostCommonWords(concordanceList):
    finalCount = Counter()
    for line in concordanceList:
        words = [w for w in line.split(" ") if w not in stop_words]
        finalCount.update(words)  # update final count using the words list
    return finalCount
print(count.most_common(10))

1 个答案:

答案 0 :(得分:0)

定义 mostCommonWords函数,但您从未调用该函数。

程序将打印count.most_common(10),这是整个文件,包括停用词。