I'm currently coding a TF-IDF program in python. I followed a code from this, however it's not working.
The problem is 'int' object is not iterable.
Traceback (most recent call last):
File "C:/Users/Try Arie/PycharmProjects/TF-IDF/tf-idf.py", line 106, in <module>
TF_scores = computeTF(doc_info, freqDict_list)
File "C:/Users/Try Arie/PycharmProjects/TF-IDF/tf-idf.py", line 67, in computeTF
for k in tempDict['freq_dict']:
TypeError: 'int' object is not iterable
I haven't tried anything yet because I just followed the code in the link.
def create_freq_dict(sents):
i = 0
freqDict_list = []
for sent in sents:
i += 1
freq_dict = {}
words = word_tokenize(sent)
for word in words:
word.lower()
if word in freq_dict:
freq_dict[word] += 1
else:
freq_dict = 1
temp = {'doc_id': i, 'freq_dict': freq_dict}
freqDict_list.append(temp)
return freqDict_list
def computeTF(doc_info, freqDict_list):
TF_scores = []
for tempDict in freqDict_list:
id = tempDict['doc_id']
for k in tempDict['freq_dict']:
temp = {'doc_id': id,
'TF_score': tempDict['freq_dict'][k]/doc_info[id-1]['doc_length'],
'key': k}
TF_scores.append(temp)
return TF_scores
I expect the output to be like this: