【发布时间】:2013-12-21 20:15:58
【问题描述】:
我在 NLTK 中有一个函数可以生成一个索引列表,它看起来像
concordanceList = ["this is a concordance string something",
"this is another concordance string blah"]
我还有另一个函数,它返回一个计数器字典,其中包含 concordanceList 中每个单词的计数
def mostCommonWords(concordanceList):
finalCount = Counter()
for line in concordanceList:
words = line.split(" ")
currentCount = Counter(words)
finalCount.update(currentCount)
return finalCount
我遇到的问题是如何最好地从生成的计数器中删除停用词,这样,当我调用
mostCommonWords(concordanceList).most_common(10)
结果不只是 {"the": 100, "is": 78, "that": 57}。
我认为预处理文本以删除停用词已经过时了,因为我仍然需要索引字符串作为语法语言的实例。基本上,我在问是否有比为停用词创建停用词计数器、将值设置为低然后再像这样创建另一个计数器更简单的方法:
stopWordCounter = Counter(the=1, that=1, so=1, and=1)
processedWordCounter = mostCommonWords(concordanceList) & stopWordCounter
这应该将所有停用词的计数值设置为 1,但它看起来很老套。
编辑:此外,我在实际制作这样一个 stopWordCounter 时遇到了麻烦,因为如果我想包含像“and”这样的保留字,我会收到一个无效的语法错误。计数器具有易于使用的联合和交集方法,这将使任务相当简单;字典有等效的方法吗?
【问题讨论】:
-
RE:您对无效语法错误的编辑。
and是保留的,但"and"是一个字符串。您应该使用Counter(["and"])创建一个带有字符串"and"的计数器。
标签: python dictionary counter nltk