Page 431 - Computer Science V2.0 Class 11
P. 431

Sample output:
                      Want to add more word and synonyms?(Y/y):n
                      beautiful ('alluring', 'delightful', 'charming', 'pleasing', 'attractive')
                      lovely ('cute', 'dazzling')
                      pretty ('nice-looking', 'beautiful')
                      Do you want synonyms of a word,Reply Y/y of N/n?y
                  >>> Enter a word:furious
                      No synonyms found
                      Want to add furious  to thesaurus?,Reply Y/y of N/n?
                      y
                  >>> Enter synonyms as a tuple('angry', 'wild', 'stormy')
                      Do you want synonyms of a word,Reply Y/y of N/n?y
                  >>> Enter a word:irate
                      No synonyms found
                      Want to add irate  to thesaurus?,Reply Y/y of N/n?
                      y
                  >>> Enter synonyms as a tuple('boiling', 'furious', 'heated')
                      Do you want synonyms of a word,Reply Y/y of N/n?y
                  >>> Enter a word:furious
                        synonyms of  furious : {'angry', 'furious', 'irate', 'boiling', 'wild', 'stormy', 'heated'}
                      Do you want synonyms of a word,Reply Y/y of N/n?y
                  >>> Enter a word:heated
                      synonyms of  heated : {'furious', 'irate', 'boiling', 'heated'}
                      Do you want synonyms of a word,Reply Y/y of N/n?

                 Solved Programming Questions

                 1.  Write a function wordCountDict(lst)to find the number of occurrences of each word in a list of words in the
                   form of a dictionary. Apply the function wordCountDict(lst)to a list entered by the user.
                   01 def wordCountDict(lst):
                   02     '''
                   03     Objective: To find word frequency of words in a list.
                   04     Input Parameter: lst - list containing words
                   05     Return Value: Dictionary of words and their frequencies
                   06     '''
                   07     '''
                   08     Approach:
                   09     For each word w in list
                   10        if w is in dictionary, increment its count
                   11        else add w to the dictionary with 1 as its count
                   12     '''
                   13     wordCount = dict()
                   14     for w in lst:
                   15         if w in wordCount:
                   16             wordCount[w] += 1
                   17         else:
                   18             wordCount[w] = 1
                   19     return wordCount
                   20
                   21 #Objective: To find frequency of words given in a list.
                   22 lst = eval(input('Enter the list: '))
                   23 print('Dictionary of word count:: ', wordCountDict(lst))
                 Sample Output:
                  >>> Enter the list: ["apple","banana","apple","apple","banana"]
                      Dictionary of word count::  {'apple': 3, 'banana': 2}




                                                                                                        Dictionaries  417
   426   427   428   429   430   431   432   433   434   435   436