Week 1 Programming asignment. Natural Language Processing with Probabilistic Models

Hello everyone, I have encountered error in my function get_count(word_l).
It works fine but gives a different answer compared to expected output. When testing the function, it results in error.

AssertionError Traceback (most recent call last)
~/work/w1_unittest.py in test_get_count(target, word_l)
160 try:
→ 161 assert len(result) == test_case[“expected”][“expected_key_value_pairs”]
162 successful_cases += 1

AssertionError:

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in
1 # Test your function
----> 2 w1_unittest.test_get_count(get_count, word_l)

~/work/w1_unittest.py in test_get_count(target, word_l)
165 {
166 “name”: test_case[“name”],
→ 167 “expected”: test_case[“expected”][“expected_n_words”],
168 “got”: n_words,
169 }

KeyError: ‘expected_n_words’

What the get_count function does is create a dictionary with the keys as the words in the input list and the value for each word being the count of the number of occurrences of that word in the input list.

The only tricky part of the code is handling the case that you’re seeing a given word for the first time and the dictionary entry doesn’t exist yet.

But there must be something incorrect about your implementation if the tests fail.

Please show us the complete output that you get, not just the failure message. What is the evidence you have that your code “works fine”? Can you show us a case that you claim is correct?

Here’s my output for that function, which includes the test before the imported one:

What do you see for the “Expected Output”? Does your function get 6116 key value pairs?

{moderator edit - solution code removed}

Your logic looks correct to me. I think what that means is that the input word_l is wrong. That was produced by your previous function process_data. Are you sure that passed the tests and produced 6116 unique words?

The way it is wrong must be interesting: you show more unique words, but fewer occurrences of “thee”. Hmmmm. Maybe you forgot to convert everything to lower case? :nerd_face:

{moderator edit - solution code removed}

Once this conversation is resolved, please delete the images of your code. Posting your code is not allowed by the Code of Conduct.

Will do. Thanks for the reminder.

1 Like

Notice that you have words that still have trailing commas on them in your output. That requires more logic to strip those. “fire,” is not the same as “fire”.

But you have also turned up some bugs in the test cases. The first one threw an assertion while trying to decode your error.

But also note that you got an explicit failure on the test for process_data. It’s not a good strategy to just ignore that and cruise ahead.

Thank you. I added some more code in the function definition. All is well now.

1 Like