Error while running unit level test for 'create_transition_matrix'


TypeError Traceback (most recent call last)
in
1 # Test your function
----> 2 w2_unittest.test_create_transition_matrix(create_transition_matrix, tag_counts, transition_counts)

~/work/w2_unittest.py in test_create_transition_matrix(target, tag_counts, transition_counts)
457
458 for test_case in test_cases:
→ 459 result = target(**test_case[“input”])
460
461 try:

in create_transition_matrix(alpha, tag_counts, transition_counts)
10 ‘’’
11 # Get a sorted list of unique POS tags
—> 12 all_tags = sorted(tag_counts.keys())
13
14 # Count the number of unique POS tags

TypeError: ‘<’ not supported between instances of ‘int’ and ‘str’

Some more detail:

Looks like the tag_counts get modified in the unittest function. Sort on the tag_counts before and after the call to create_transition_matrix works fine but same sort after call to unittest failes with above error.

Resolved:

I was using wrong key as access to the dict. It looks like if the key does not exist it adds it to the dictionary if it’s defaultdict from collections. Reading through official python docs it does not stand out this behavior as a warning …

1 Like