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)