Skip to content
Advertisement

Calculating the semantic descriptor of a nested list

I am trying to calculate the semantic description of a nested list to turn it into a nested dictionary. First I got distinct_words, each word of it will be the keys of my final dictionary.

JavaScript

EXPECTED OUTPUT: {'i': {'am': 3, 'a': 2, 'sick': 1, 'man': 3, 'spiteful': 1, 'an': 1, 'unattractive': 1, 'believe': 1, 'my': 2, 'liver': 1, 'is': 1, 'diseased': 1, 'however': 1, 'know': 1, 'nothing': 1, 'at': 1, 'all': 1, 'about': 1, 'disease': 1, 'and': 1, 'do': 1, 'not': 1, 'for': 1, 'certain': 1, 'what': 1, 'ails': 1, 'me': 1}, 'am': {'i': 3, 'a': 2, 'sick': 1, 'man': 3, 'spiteful': 1, 'an': 1, 'unattractive': 1}, etc…}

At this moment this is my code. I already got the words I want as the keys, but I don’t know how to count the words related to them and put into the final dictionary, I’ve tried using the counter above, but what it does is calculate the overall value of appearences.

Thanks in advance for any help.

Advertisement

Answer

Try this:

JavaScript

You need to loop each sentence twice, in order to get each word for each key. For this you can use itertools.product.

Also note that I use here collections.defaultdict which you should read about, it is a nice utility that sets the dictionary with a default if the key does not exist (allowing to skip the check that you had)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement