Skip to content
Advertisement

Counting specific words in a sentence

I am currently trying to solve this homework question.

My task is to implement a function that returns a vector of word counts in a given text. I am required to split the text into words then use NLTK's tokeniser to tokenise each sentence.

This is the code I have so far:

JavaScript

There are two doctests that should give the result of: [2, 1, 0] and [4842, 3001]

This is the error message I am getting from my code enter image description here

I’ve spent all day trying to tackle this and I feel I’m getting close but I don’t know what I’m doing wrong, the script is giving me an error every time.

Any help will be very appreciated. Thank you.

Advertisement

Answer

This is how I would use nltk to get to the result your homework wants:

JavaScript

Your code does multiple things that look just wrong:

JavaScript
  • sent_tokenize() takes a string and returns a list of sentences from it – you store the results in 2 variables text, words and then you try to iterate over tuple of them? words is not a text with sentences to begin, this makes not much sense to me
  • wordList is a list, if you use the .append() on it, append() returns None. Nonehas no .split() function.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement