def index_dir(self, base_path):
        num_files_indexed = 0
        allfiles = os.listdir(base_path)
        #print allfiles
        num_files_indexed = len(allfiles)
        #print num_files_indexed
        docnumber = 0
        self._inverted_index = {} #dictionary
        for file in allfiles: 
                self.documents = [base_path+file] #list of all text files
                f = open(base_path+file, 'r')
                lines = f.read()
		# Tokenize the file into words
                tokens = self.tokenize(lines)
                docnumber = docnumber + 1
                print 'docnumber', docnumber
                for term in tokens:  
# check if the key already exists in the dictionary, if yes, 
# just add a new value for the key
                    #if self._inverted_index.has_key(term)
                    if term in sorted(self._inverted_index.keys()):
                        docnumlist = self._inverted_index.get(term)
                        docnumlist = docnumlist.append(docnumber)
# if the key doesn't exist in dictionary, add the key (term) 
# and associate the docnumber value with it.	
                        self._inverted_index = self._inverted_index.update({term: docnumber})
			#self._inverted_index[term] = docnumber 
        print 'dictionary', self._inverted_index 
        print 'keys', self._inverted_index.keys()
        return num_files_indexed

I'm working on an information retrieval project where we are supposed to crawl through multiple text file, tokenize the files and store the words in an inverted list (dictionary) data structure.

doc1.txt: "the dog ran"
doc2.txt: "the cat slept"
_inverted_index = {
'the': [0,1],
'dog': [0],
'ran': [0],
'cat': [1],
'slept': [1]
where 0,1 are docIDs

I'm getting the following error:
'Nontype' object has no attribute keys. line#95

All help is highly appreciated.

7 Years
Discussion Span
Last Post by pyTony

The error message, and not the error type, were of great help.

And a code usage sample also

And wheres line 95?

Cheers and Happy coding


The error message, and not the error type, were of great help.

i.e. include the entire error message (which usually shows the offending line).

""" ------------------------------------------------
Also, this code can be simplified 
Dictionary key lookup is simply
if key in dictionary_name:
""" ------------------------------------------------
    self._inverted_index = {} #dictionary
## ...
## ...
# check if the key already exists in the dictionary, if yes, 
# just add a new value for the key
    if term in sorted(self._inverted_index.keys()):

Edited by woooee: n/a


I did this code to get words unique to files listed by id dictionary:

import string
import random
# Sherlock Holmes and Alice in Wonderland
filenames = {'doc001':'advsh12.txt', 'doc002' : '11.txt'}

words = dict()
words_and_docids = dict()
allwords = set()

notthese = string.punctuation + string.digits
samplesize = 64

for docid in filenames:
    words[docid] = set(
                ## read in and replace the--without spaces with spaces
                for word in open(filenames[docid]).read().replace('--', '  ').split())
    allwords |= words[docid]

for word in allwords:
    words_and_docids[word] = set(docid for docid in filenames if word in words[docid])

unique = [word for word in allwords if len(words_and_docids[word]) == 1 ]

print 'Words unique in one file:',len(unique),'\nSample of %i:' % samplesize

print '\n'.join("%s: %s" % (fileid, word) for word in random.sample(unique,40)
                for fileid in words_and_docids[word])

raw_input('Ready! Push Enter.')
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.