我用python写了如下程序,可是后来最后出了问题。有谁帮我解决一下??
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我用python写了如下程序,可是后来最后出了问题。有谁帮我解决一下??相关的知识,希望对你有一定的参考价值。
import math
from operator import itemgetter
import nltk.data
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import os
import collections
from string import punctuation
aynur=open('f:\\guli3.txt')
ahmat=aynur.read()
import nltk.data
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import os
tokenizer=nltk.data.load('tokenizers/punkt/english.pickle')
words=word_tokenize(ahmat)
uygur_stops=set(stopwords.words('uygur'))
words1=words
words2=[word for word in words1 if word not in uygur_stops]
def freq(word, document):
return document.split(None).count(word)
def wordCount(document):
return len(document.split(None))
def numDocsContaining(word,documentList):
count = 0
for document in documentList:
if freq(word,document) > 0:
count += 1
return count
def tf(word, document):
return (freq(word,document) / float(wordCount(document)))
def idf(word, documentList):
return math.log(len(documentList) / numDocsContaining(word,documentList))
def tfidf(word, document, documentList):
return (tf(word,document) * idf(word,documentList))
if __name__ == '__main__':
documentList = []
documentList.append("f:\\guli3.txt")
documentList.append("""DOCUMENT #2 TEXT""")
documentList.append("""DOCUMENT #3 TEXT""")
words6 =
words6=words2
documentNumber = 0
for word5 in documentList[documentNumber].split(None):
words6[word5] = tfidf(word5,documentList[documentNumber],documentList)
for item in sorted(words.items(), key=itemgetter(1), reverse=True):
print "%f <= %s" % (item[1], item[0])
出现问题如下:
>>>
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\1.py", line 55, in <module>
words6[word5] = tfidf(word5,documentList[documentNumber],documentList)
TypeError: list indices must be integers
>>>
error作为提示已经很清楚了,list中必须是整型变量
希望对你有帮助
以上是关于我用python写了如下程序,可是后来最后出了问题。有谁帮我解决一下??的主要内容,如果未能解决你的问题,请参考以下文章
我家猫老喜欢和我躲猫猫,我用Python赶忙写了个猫脸检测器。在哪里都逃不出我的手心。
分治法解决凸包问题到底咋回事?为了弄懂,我用python写了个可视化窗体程序