-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexam.py
135 lines (118 loc) · 5.03 KB
/
exam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Code written by Abhinav Masalia
import json
from collections import OrderedDict
lex_dict = OrderedDict()
print("Enter a word to check if Gina knows it:", end = " ")
exam_question = input("")
with open('JSON/lexicon.json', 'r') as f: #calling lexicon.json for read and storing it into a dictionary
lex_dict = json.loads(f.read())
# store where the word is found
category = ""
# Introducing found to see if the word was found and also to have a better way to ask the user in the end if the word asked in the exam is correct
found = True
while True:
# Iterates through the noun dictionary of lexicon.json to print the information if the input word is found
for noun, noun_info in lex_dict["nouns"].items():
if exam_question == noun:
category = "nouns"
print("Noun: " + noun)
print("Gender:", end = " ")
if noun_info[1] == "F":
print("Female")
if noun_info[1] == "M":
print("Male")
if noun_info[1] == "U":
print("Unknown")
print("Nominative Inflection:", end = " ")
if noun_info[0] == "":
print("Unknown")
else:
print(noun_info[0])
print("Accusative Inflection:", end = " ")
if noun_info[2] == "":
print("Unknown")
else:
print(noun_info[2])
found = False # This helps check in the end if the word was found or not
break
# Iterates through the verb dictionary of lexicon.json to print the information if the input word is found
for verb, verb_info in lex_dict["verbs"].items():
if exam_question == verb:
category = "verbs"
print("Verb: " + verb)
print("Person:", end = " ")
if verb_info[2] == "":
print("Unknown")
else:
print(verb_info[2])
print("Male Inflection:", end = " ")
if verb_info[0] == "":
print("Unknown")
else:
print(verb_info[0])
print("Female Inflection:", end = " ")
if verb_info[1] == "":
print("Unknown")
else:
print(verb_info[1])
found = False
break
# Iterates through the adjectives dictionary of lexicon.json to print the information if the input word is found
for adj, adj_info in lex_dict["adjectives"].items():
if exam_question == adj:
category = "adjectives"
print("Adjective: " + adj)
print("Male Inflection:", end = " ")
if adj_info[0] == "":
print("Unknown")
else:
print(adj_info[0])
print("Female Inflection:", end = " ")
if adj_info[1] == "":
print("Unknown")
else:
print(adj_info[1])
print("Accusative Inflection:", end = " ")
if adj_info[2] == "":
print("Unknown")
else:
print(adj_info[2])
found = False
break
# Iterates through the adpositions dictionary of lexicon.json to print the information if the input word is found
for adp, adp_info in lex_dict["adpositions"].items():
if exam_question == adp:
category = "adpositions"
print("Preposition: " + adp)
print("Postposition:", end = " ")
if adp_info == "":
print("Unknown")
else:
print(adp_info)
found = False
break
# We want the while loop only to run once and break out of it on finding a word or if the entire dictionary is traversed, so this break statement serves the purpose
break
# This means that found = False wasn't encountered i.e. the word wasn't found in Gina's lexicon
if found:
print("Word: Unknown")
else:
print("\nIs the category of the word correct?\n(y) - yes or (n) or no")
categ_check = input("")
if categ_check == 'n' or categ_check == 'no':
# Deleting the word from Gina's lexicon if the user feels the information learnt by Gina is wrong
del lex_dict[category][exam_question]
# Writing back the updated lexicon to lexicon.json
with open('JSON/lexicon.json', 'w') as f:
json.dump(lex_dict, f, indent = 2)
print("Gina removed the word, \"" + exam_question + "\" from her lexicon!")
else:
print("\nIs the information learned about the word correct?\n(y) - yes or (n) or no")
info_check = input("")
if info_check == 'n' or info_check == 'no':
# Deleting the word from Gina's lexicon if the user feels the information learnt by Gina is wrong
del lex_dict[category][exam_question]
# Writing back the updated lexicon to lexicon.json
with open('JSON/lexicon.json', 'w') as f:
json.dump(lex_dict, f, indent = 2)
print("Gina removed the word, \"" + exam_question + "\" from her lexicon!")