Page 378 - Computer Science Class 11 With Functions
P. 378
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():
07 '''
08 Input Parameter: None
09 Return Value: None
10 '''
11
12 cont = input('Want to add more word and synonyms?(Y/y):')
13
14 while cont in ['Y', 'y']:
15 wordMeans = eval(input('enter word, tuple of synonyms as a tuple:'))
16 thesaurus[wordMeans[0]] = wordMeans[1]
17 cont = input('Want to add more word meanings? (Y/y):')
18
19 printDict()
20
21 def printDict():
22 '''
23 Input Parameter: None
24 Return Value: None
25 '''
26
27 for key in thesaurus:
28 print(key, thesaurus[key])
29
30 findSynonyms()
31
32 def findSynonyms():
33 '''
34 Input Parameter: None
35 Return Value: None
36 '''
37 moreWords = input('Do you want synonyms of a word,Reply Y/y of N/n?')
38 while moreWords in ['Y', 'y']:
39 synonyms = set()
40 word = input('Enter a word:')
41 keys = thesaurus.keys()
376 Touchpad Computer Science-XI

