[英]Context most frequent words corpus in python
使用以下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'對象沒有屬性'concordance'有人知道為什么我不能在第二個def中使用我的第一個代碼塊的結果嗎?我認為通過使用返回語句應該能工作。
有誰知道為什么我不能在第二個def中使用第一個代碼塊的結果? 我認為通過使用返回語句,它應該可以工作。
因為函數不返回變量,所以它們返回value 。
您在context
中使用的popular_words
並非來自meest_freq
; 它來自某個地方的一些全局變量。 在meest_freq
, popular_words
是本地的。 這是因為規則:如果在函數內部分配名稱,則該名稱是局部的,除非您對global
語句另有說明。 在context
,沒有分配給popular_words
,因此Python會查找具有該名稱的全局popular_words
。 這個全局包含了您不希望看到的東西,可能是因為您正在測試解釋器中的功能(也許您已經在測試和修復功能的先前版本中遺留下來了……)。
請不要為此嘗試使用全局變量 。 您已經正確地學習了這一課,從函數中獲取信息的方法是通過返回值。 與此相對的; 將信息獲取到函數中的方法是將其作為參數傳遞。 meest_freq
知道語料庫的方式相同(因為您將其作為mycorpus
),因此應該使context
知道流行的詞。
在某個地方,您必須具有調用這兩個函數的代碼。 該代碼應采用從meest_freq
返回的值 ,並將其傳遞給context
,就像將語料庫傳遞給meest_freq
。
或者,如果將語料庫傳遞給context
,則可以在其中進行調用。 由於您的名字,很難知道什么是正確的組織方式。 我不知道cat
應該是什么意思,或什么context
與任何事物有關,或在這種情況下的concordance
是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.