Python 2.7 Python—类型为'的参数;功能';这不是不可原谅的错误
因此,我应该编写一些代码,接收一些文本行,通过删除所有非关键字、标点符号等重新编写它们,最后打印每个关键字及其出现的行。有一次,我的代码在工作,似乎没有改变任何东西,我开始得到这个错误,我不知道为什么。我不是在寻找缩短代码的方法,我意识到这远远不够理想。我只是想知道如何修复这个错误 编辑-如果word不在索引中,则createindex中的第58行出现错误: 代码如下:Python 2.7 Python—类型为'的参数;功能';这不是不可原谅的错误,python-2.7,Python 2.7,因此,我应该编写一些代码,接收一些文本行,通过删除所有非关键字、标点符号等重新编写它们,最后打印每个关键字及其出现的行。有一次,我的代码在工作,似乎没有改变任何东西,我开始得到这个错误,我不知道为什么。我不是在寻找缩短代码的方法,我意识到这远远不够理想。我只是想知道如何修复这个错误 编辑-如果word不在索引中,则createindex中的第58行出现错误: 代码如下: from string import * # Program to index sentences stopWords =
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)
好像你忘了初始化索引字典
def createindex(reworkedtext): #creates index and prints it
linecount=1
index = {} # <----------- add this line and see what's what :)
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]
def createindex(reworkedtext):#创建索引并打印它
行数=1
index={}#真的吗?你甚至不告诉我们是哪一行?该死,对不起!它是createindex(reworkedtext)文件“C:\Program Files(x86)\Python\indexer.py”第75行的文件“C:\Program Files(x86)\Python\indexer.py”,第58行的createindex,如果word不在索引中:是的,就是它。当我删除一些测试代码时,我一定是无意中删除了它,但是有了这样一个错误代码,我根本不会想到它!谢谢