Page 216 - Information_Practice_Fliipbook_Class11
P. 216
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',
04 'attractive'),\
05 'lovely':('cute', 'dazzling'), 'pretty': ('nice-looking',
06 'beautiful')}
07
08 cont = input('Want to add more word and synonyms?(Y/y):')
09
10 while cont in ['Y', 'y']:
11 wordMeans = eval(input('enter word, synonyms as a tuple:'))
12 thesaurus[wordMeans[0]] = wordMeans[1]
13 cont = input('Want to add more word meanings? (Y/y):')
14
15 for key in thesaurus:
16 print(key, thesaurus[key])
17
18 moreWords = input('Do you want synonyms of a word? Reply Y/y of N/n')
19 while moreWords in ['Y', 'y']:
20 synonyms = set()
21 word = input('Enter a word:')
22 keys = thesaurus.keys()
23 for key in thesaurus.keys():
24 if word in thesaurus[key]:
25 synonyms.update(set(thesaurus[key]), {key})
26 elif key == word:
27 synonyms.update(set(thesaurus[key]))
28 if synonyms == set():
29 print('No synonyms found')
30 print('Want to add', word, ' to thesaurus? Reply Y/y of N/n')
31 addSynonym = input()
32 if addSynonym in ['Y','y']:
33 synonyms = eval(input('Enter synonyms as a tuple'))
34 thesaurus[word] = synonyms
35 else:
36 print('synonyms of ', word, ':', synonyms)
37
38 moreWords = input('Do you want synonyms of a word? Reply Y/y of N/n')
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/ny
>>> Enter a word:furious
No synonyms found
Want to add furious to thesaurus? Reply Y/y of N/n
202 Touchpad Informatics Practices-XI

