-1

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:

enter image description here

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Try
  • 41
  • 2
  • 9
  • Tangential, but you should really consider a style guide. Some of your variables seem to be doing something vaguely Hungarian (`freqDict_list`), some are in `camelCase`, some are in `snake_case`, and some are... both? PEP8 recommends `snake_case`, but at the very least you should stick to one style. – Silvio Mayolo Jul 17 '19 at 00:33
  • I followed the code made by the author. So it's just like that. – Try Jul 17 '19 at 00:35

1 Answers1

2

Update this line

for k in range(tempDict['freq_dict']):
pranjal0819
  • 270
  • 2
  • 17