tags:

views:

91

answers:

2

Say,

term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7].

Now we have this function which counts no. of occurances of any item. This is the function I've having a problem with.

def TF(term, doc):
    idx = InvertedIndex[term].index(doc)
    return next(i  for i, item in enumerate(InvertedIndex[term][idx:])
                if item != doc)

It is giving 1 for TF(term, 1), 3 for TF(term, 2),1 for TF(term, 4). Fine so far.

But it is giving StopIteration error for TF(term, 7). It is also giving same error if I had InvertedIndex[term] = [7] and called TF(term, 7). How to fix it?

Edit: Clarification about aim of the function. that function is supposed to count no. of occurances of an item. Considering the used example TF(term, 2) must return 3 because it occured 3 times in InvertedIndex[term]

Solution:

def TF(term, doc):
    return InvertedIndex[term].count(doc)
+2  A: 

At the language-level, your problem is that you're calling 'next' on a sequence, and when the sequence is empty it raises StopIteration.

Otherwise, it's not clear how to help you, since it's not obvious what the function you've written is supposed to do. You may want something like this:

def uniq_docs(inverted_index):
    last = None
    for i, doc in enumerate(inverted_index):
        if doc != last:
            yield i, doc
            last = doc

and where you're currently calling TF, use something like:

for index, doc in uniq_docs(InvertedIndex[term]):
    ...
Paul Hankin
Sorry, for not being clear about the aim of the function. that function is supposed to count no. of occurances of an item. Considering the used example `TF(term, 2)` must return 3 because it occured 3 times in InvertedIndex[term]
claws
+3  A: 

I feel like I wrote that loop on another answer but the correct answer for what you want to do is InvertedIndex[term].count(doc)

This will count the number of times that doc occurs in the list.

aaronasterling
Damn it! I didn't know there was a count() method. Also small correction its `InvertedIndex[term].count(doc)`
claws
This is why I should probably spend enough time on learning the language before starting coding for projects.
claws
@claws, I did write that function. Sorry for the stupid bug in it. It happens anytime I try to write something even slightly tricky without testing it (although I am getting better). Apparently, I wasn't thinking about the `count` method when I first wrote it either. I've updated my answer to the original question.
aaronasterling