Page 329 - Computer Science Class 11 Without Functions
P. 329
A woad s ie coneidiaid thi eynonym of w, if
iithia s ie in thi tupli coaaieponding to thi kiy w in thesaurus, thnt ie, s ie in thesaurus[w]
if w nnd s bilong to thi enmi tupli of eynonyme foa eomi othia kiy k. Thnt ie, if w nnd s nai in thesaurus[k]
foa eomi kiy k, thin nll woade in thesaurus[k] nai eynonyme of w.
Wi do not coneidia indiaict ailntonehipe foa eynonyme. Foa inetnnci, in thi ixnmpli thesaurus, wi do
not think of 'delightful' ne n eynonym foa 'pretty' ivin though 'beautiful' ie n eynonym foa
'pretty' nnd 'delightful' ie n eynonym foa 'beautiful'.
v. If thiai nai no eynonyme foa n woad, nek thi ueia whithia to ndd thi woad nnd ite eynonyme to thi thesaurus.
If thi nnewia ie no, contnui neking foa moai woade to look in thi thienuaue. If thi nnewia ie yie, nccipt n tupli
of eynonyme foa thi givin woad nnd intia into thi thienuaue.
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')
Snmpli 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
Python Dictonnaiie 327

