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

dictionary.               Returns  the  value  associated  with  the  >>>dictionary_d1=
                 setdefault(key,           specified  key.  If  the  key  does  not  exist,  {1:'A',2:'B',3:'C'}
                 [value])                  it  is  inserted  into  the  dictionary  and  >>>dictionary_

                                           associated with the value passed as the  d1.setdefault(4,'T')
                                           second parameter.
                                                                                 T
                 sorted(dictionary)  Sort the dictionary by key or by value.     >>>dictionary_d1=
                                                                                 {4:'A',2:'B',1:'C'}
                                                                                 >>>sorted(dictionary_d1)

                                                                                 [1, 2, 4]


                 14.7 Building a Thesaurus
                 In this section, we will build a thesaurus. For this purpose, we write a program that creates and manages a thesaurus
                 in the form of a dictionary. More specifically, the program should do the following:
                 •  Begin with a thesaurus of a few words, such as the following thesaurus

                      thesaurus     =    {'beautiful':('alluring',       'delightful',      'charming',     'pleasing',
                      'attractive'),\
                      'lovely':('cute', 'dazzling'), \
                      'pretty': ('nice-looking', 'beautiful')}
                 •  opulate the thesaurus with the word: synonyms pairs. For each word, synonyms is a tuple of its synonyms.
                   P
                   The program should repeatedly prompt the user with a message Want to add more words and synonyms?
                   (Y/y) and continue to accept word: synonyms so long as  the user responds with Y or y.
                 •  When no more word: synonyms are to be added, display  the thesaurus constructed in step b.

                 •  epeatedly ask the user, whether he/she would like to continue with the thesaurus operations. So long as the user
                   R
                   responds with Y or y, do the following:
                    i. Ask the user to enter a word  (w) whose synonyms are required.

                   ii. Prompt the user to enter w with the message 'Enter a word:'
                  iii. Find the synonyms of the word w (entered by the user) in  thesaurus.
                  iv. The program should yield all synonyms of w.
                     A word s is considered the synonym of w, if
                     a. either s is in the tuple corresponding to the key w in thesaurus, that is,  s is in thesaurus[w]
                     b.  if w and s belong to the same tuple of synonyms for some other key k. That is, if w and s are in thesaurus[k]
                       for some key k, then all words in  thesaurus[k] are synonyms of w.
                       We do not consider indirect relationships for synonyms. For instance, in the example thesaurus, we do not
                     think of 'delightful' as a synonym for 'pretty' even though 'beautiful' is a synonym for 'pretty'
                     and 'delightful' is a synonym for 'beautiful'.
                  v.  If there are no synonyms for a word, ask the user whether to add the word and its synonyms to the thesaurus.
                     If the answer is no, continue asking for more words to look in the thesaurus. If the answer is yes, accept a tuple of
                     synonyms for the given word and enter into the thesaurus.
                   01 # Global dictionary to store the thesaurus so far
                   02
                   03  thesaurus = {'beautiful':('alluring', 'delightful', 'charming', 'pleasing', 'attractive'),\
                   04              'lovely':('cute', 'dazzling'), 'pretty': ('nice-looking', 'beautiful')}
                   05
                   06 def engDict():


                                                                                                        Dictionaries  415
   424   425   426   427   428   429   430   431   432   433   434