當我創建的本地使用的嵌套字典存儲文本文件的倒排索引不海峽。倒排索引的抽象結構在下面(值是整數)。在鍵'0'的任何字值中,鍵'1'的idf和值是tf。類型錯誤:列表索引必須是整數或切片,使用嵌套的字典
inverted_index={'word1':{'0':idf_value, '1': 2 , 'filename1': frequency_value, 'filename2': frequency_value},'word2':{'0':idf_value, '1': 2, 'filename1': frequency_value, 'filename2': frequency_value}}
這是代碼:
import textract, math, os
docs=[]
#Read the files and store them in docs
folder = os.listdir("./input/")
for file in folder:
if file.endswith("txt"):
docs.append ([file,textract.process("./input/"+file)])
inverted_index={}
for doc in docs:
words=doc[1].decode()
words=words.split(" ")
#loop through and build the inverted index
for word in words:
temp={}
#to remove initial white space
if (word == " ") or (word==""):
continue
if word not in inverted_index:
temp[doc[0]]=1
temp['0']=0 #idf
temp['1']=1 #tf
inverted_index[word]=temp
else:
if doc[0] not in inverted_index[word].keys():
inverted_index[word][doc[0]]=1
inverted_index[word]['1']=inverted_index[word]['1']+1
else:
inverted_index[word][doc[0]]=inverted_index[word][doc[0]]+1
# to sort and print values with calculating the the tf and idf on the fly
for key, value in sorted(inverted_index.items()): # to sort words alphabitically
inverted_index[key]=sorted(inverted_index[key]) # to sort the filenames where the word occured.
inverted_index[key]['0']=math.log2(len(docs)/value['1']) # the error in this line
print(key, value)
,但我得到這個錯誤在倒數第二行:
Traceback (most recent call last):
File "aaaa.py", line 34, in <module>
inverted_index[key]['0']=math.log2(len(docs)/value['1'])
TypeError: list indices must be integers or slices, not str
能否請你幫我解決這個bug。謝謝
請張貼滿'Traceback' – ksai
是inverted_index列出的值是多少?如果是這樣,inverted_index [key] ['1']可能是問題。嘗試將該行中的索引從「1」更改爲1,將「0」更改爲0,而不加引號。如果你有一個列表'a = [1,2,3]',那麼你可以通過'a [0]'而不是'a ['0']來訪問列表中的項目, ]'。字符串索引是不允許的。 –
我不知道inverted_index'的'的內容,但根據錯誤嘗試改變'值「1」]''到值[1]'在最後第二行。 – ksai