2017-11-11 52 views
0

當我創建的本地使用的嵌套字典存儲文本文件的倒排索引不海峽。倒排索引的抽象結構在下面(值是整數)。在鍵'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。謝謝

+0

請張貼滿'Traceback' – ksai

+0

是inverted_index列出的值是多少?如果是這樣,inverted_index [key] ['1']可能是問題。嘗試將該行中的索引從「1」更改爲1,將「0」更改爲0,而不加引號。如果你有一個列表'a = [1,2,3]',那麼你可以通過'a [0]'而不是'a ['0']來訪問列表中的項目, ]'。字符串索引是不允許的。 –

+0

我不知道inverted_index'的'的內容,但根據錯誤嘗試改變'值「1」]''到值[1]'在最後第二行。 – ksai

回答

0

inverted_index[key]['0']來的錯誤,因爲inverted_index[key] = sorted(inverted_index[key])創建自己內心的字典鍵的列表,你的

print(inverted_index[key]) 
# becomes ['0', '1', 'filename1', 'filename2'] 

並由此觸發類型錯誤,因爲你不能做字符串索引到列表中。

爲了讓你改變每一個字[0]你內心的字典的價值,你可以試試這個代碼:

for key, value in sorted(inverted_index.items()): 
    inverted_index[key] = sorted(inverted_index[key]) 
    current_word_key = inverted_index[key][0] 
    value['0'] = 'some_value' 
    inverted_index[key] = value 

print(inverted_index) 

DEMO

+0

你的代碼只顯示每個單詞的密鑰列表 例如: 'anger':['0','1','y10.txt'] 它應該是'憤怒':{'0':0 ,'1':1,'y10.txt':1} – fsfr23

+0

我所做的只是指出這段代碼有什麼問題,你所需要的只是將值賦給它的字典,不過看看我的更新 –

0

這對我的作品

for key, value in sorted(inverted_index.items()): 
    inverted_index[key]=sorted(inverted_index[key]) 
    value['0']=math.log2(len(docs)/value['1']) # the error in this line 
    inverted_index[key]=value 
    print(key, value) 
+0

什麼?哈哈,你剛剛複製我的答案,並張貼在你自己的? –

+0

不,我沒有複製它,你必須已經更新它。無論如何,這不是一個大問題,我會接受你的答案 – fsfr23

相關問題