-1

I have a function which take a dictionary af words and a sentence (sent). The problem is that in this loop :

  for word in s:

It doesn't take words per words (words coposinf the sentence), but character per character.

I don't know why.

Any idea please?

Thanks

def compute_tfidf(word_counter, sent):
    word_counter = {}
    total_count = 0
    no_of_sentences = 0    
    sents_emd = []
    print(sent)

    for s in sent :
        total_count = total_count + len(s)

        no_of_sentences = no_of_sentences +  1



        for word in s:
            print(word)
            print(word_counter[word])

    return sents_emd
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

3

if s is a string you will loop it character by character. If you want to loop each word in a sentence string, try for word in s.split() which will split a string on spaces into a list by default.

if total_count is supposed to count words you probably also want to use len(s.split()).. at that point you might want to just use a variable like sentence = s.split() at the start of the loop.

Zhenhir
  • 1,157
  • 8
  • 13