【发布时间】:2015-06-15 01:07:05
【问题描述】:
在使用以下 def 查找语料库中最常用的 10 个单词后(使用 Python),我必须比较这 10 个单词在所述语料库的不同子类别中的上下文。
def meest_freq(mycorpus):
import string
woorden = mycorpus.words()
zonderhoofdletters = [word.lower() for word in woorden]
filtered = [word for word in zonderhoofdletters if word not in stopList]
no_punct = [s.translate(None, string.punctuation) for s in filtered]
word_counter = {}
D = defaultdict(int)
for word in no_punct:
D[word] +=1
popular_words = sorted(D, key = D.get, reverse = True)
woord1 = popular_words[1]
woord2 = popular_words[2]
woord3 = popular_words[3]
woord4 = popular_words[4]
woord5 = popular_words[5]
woord6 = popular_words[6]
woord7 = popular_words[7]
woord8 = popular_words[8]
woord9 = popular_words[9]
woord10 = popular_words[10]
print "De 10 meest frequente woorden zijn: ", woord1, ",", woord2, ',', woord3, ',', woord4, ',', woord5, ',', woord6, ',', woord7, ',', woord8, ',', woord9, "en", woord10
return popular_words
我想使用以下代码:
def context(cat):
words = popular_words[:10]
context = words.concordance()
print context
不幸的是,我不断收到“AttributeError: 'str' object has no attribute 'concordance' 有谁知道为什么我不能在第二个 def 中使用我的第一个代码块的结果?我认为通过使用返回语句它应该能够工作。
【问题讨论】:
-
您实际上并没有从函数中获取返回值 - 您必须使用
words = meest_freq(yourcorpus)[:10] -
它来自 nltk。我们在课堂上看到过