因此,我應該編寫一些代碼,它需要一些文本行,通過刪除所有非關鍵詞,標點符號等來重新編譯它們,最後打印每個代碼關鍵字和它出現在哪些行上。有一次,我的代碼正在工作,看起來沒有改變任何東西,我開始得到這個錯誤,我不知道爲什麼。我並沒有想方設法讓代碼更短或什麼,我意識到這遠非理想。我只想知道如何解決這個錯誤。Python - 類型'函數'的參數不是可迭代的錯誤
編輯 - 錯誤是58行,CreateIndex中如果Word沒有在指數:
下面是代碼:
from string import *
# Program to index sentences
stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
"of", "from", "here", "even", "the", "but", "and", "is", "my", \
"them", "then", "this", "that", "than", "though", "so", "are" ]
punctuation = [".",",",":",";","!","?","&","'"]
stemming=["s","es","ed","er","ly","ing"]
text={}
reworkedtext={}
def inserttext(text): #Function to insert lines of text
linecount=1
print "Please insert text here:"
line = ""
while line!=".":
line = raw_input()
text[linecount]=line
linecount+=1
def reworktext(text): #Reworks the text by removing punctuation and making everything lowercase
for line in text:
reworkedtext[line]=""
for character in range(0,len(text[line])):
if text[line][character] not in punctuation:
reworkedtext[line]=reworkedtext[line]+lower(text[line][character])
def removestopwords(reworkedtext): #Removes stopwords
for line in reworkedtext:
wordcount=0
reworkedtext[line]=split(reworkedtext[line])
for word in range(0,len(reworkedtext[line])):
if reworkedtext[line][wordcount] in stopWords:
del(reworkedtext[line][wordcount])
else:
wordcount+=1
def stemwords(reworkedtext): #Stems all words
for line in reworkedtext:
for word in range(0,len(reworkedtext[line])):
if reworkedtext[line][word][-2:] in stemming:
reworkedtext[line][word]=reworkedtext[line][word][:-2]
if reworkedtext[line][word][-3:] in stemming:
reworkedtext[line][word]=reworkedtext[line][word][:-3]
if reworkedtext[line][word][-1:] in stemming:
reworkedtext[line][word]=reworkedtext[line][word][:-1]
def createindex(reworkedtext): #creates index and prints it
linecount=1
for line in reworkedtext:
for word in range(0,len(reworkedtext[line])):
if word not in index:
index[word]=""
index[word]=str(line)
linecount+=1
elif index[word]!=str(line):
index[word]=index[word]+", "+str(line)
for words in index:
print words, index[words]
inserttext(text)
reworktext(text)
removestopwords(reworkedtext)
stemwords(reworkedtext)
createindex(reworkedtext)
認真嗎?你甚至不會告訴我們這是哪一行? –
哪個功能? – jramirez
廢話,對不起!它是 文件「C:\ Program Files(x86)\ Python \ indexer.py」,行75,在 createindex(reworkedtext) 文件「C:\ Program Files文件58,在createindex 如果單詞不在索引中: –