-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (167 loc) · 7.44 KB
/
Copy pathmain.py
File metadata and controls
198 lines (167 loc) · 7.44 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import json
import os
class ProgrammerDictionary:
def __init__(self, locale='en-US'):
self.locale = locale
self.dictionary_file = f'definitions/{locale}.json'
data = self._load_definitions()
self.menu = data.get('menu', {})
self.definitions = data.get('terms', {})
self.categories = data.get('categories', [])
self.related_terms = self._build_related_terms()
def _load_definitions(self):
"""Load definitions from JSON file."""
if os.path.exists(self.dictionary_file):
with open(self.dictionary_file, 'r', encoding='utf-8') as f:
return json.load(f)
return {"menu": {}, "terms": {}, "categories": []}
def _build_related_terms(self):
"""Build a mapping of search queries to their primary terms."""
related = {}
for term, data in self.definitions.items():
# Only add search queries if they exist
if 'search_queries' in data:
for query in data['search_queries']:
related[query.upper()] = term
# Add the main term as a search query too
related[term] = term
return related
def lookup_term(self, term):
"""Look up a programming term."""
term = term.upper()
# Check if it's a related term
if term in self.related_terms:
primary_term = self.related_terms[term]
return self.definitions[primary_term]
return None
def add_term(self, term, definition, categories, examples=None,
search_queries=None):
"""Add a new term to the dictionary."""
term = term.upper()
self.definitions[term] = {
"definition": definition,
"categories": categories, # Now accepts an array of categories
"examples": examples or [],
"search_queries": search_queries or []
}
self.related_terms = self._build_related_terms()
self._save_definitions()
def lookup_by_category(self, category):
"""Look up all terms in a specific category."""
return {term: data for term, data in self.definitions.items()
if category in data['categories']}
def _save_definitions(self):
"""Save definitions back to JSON file."""
data = {
"menu": self.menu,
"categories": self.categories,
"terms": self.definitions
}
with open(self.dictionary_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
def main():
print("Locale:")
print("1. English (United States) [en-US]")
while True:
locale_choice = input("\nSelect locale (default: en-US): ").strip()
if locale_choice == "" or locale_choice == "1":
locale = "en-US"
break
else:
print("Invalid locale! Please try again.")
dictionary = ProgrammerDictionary(locale)
menu = dictionary.menu # Get menu translations for selected locale
print(f"\n{menu['welcome']}") # Now show welcome message after locale selection
while True:
print(f"\n{menu['title']}")
print(f"1. {menu['options']['lookup']}")
print(f"2. {menu['options']['add']}")
print(f"3. {menu['options']['category_lookup']}")
print(f"4. {menu['options']['exit']}")
choice = input(f"\n{menu['prompts']['choice']}").strip()
if choice == '1':
term = input(menu['prompts']['term_lookup'])
result = dictionary.lookup_term(term)
if result:
print(
f"\n{menu['labels']['definition']}: {result['definition']}")
print(f"{menu['labels']['category']}: {result['category']}")
if result['examples']:
print(
f"{menu['labels']['examples']}: {', '.join(result['examples'])}")
print("\n")
else:
print(menu['messages']['term_not_found'])
elif choice == '2':
term = input(menu['prompts']['term_add']).strip()
definition = input(menu['prompts']['definition']).strip()
# Show available categories
print("\nAvailable categories:")
for i, category in enumerate(dictionary.categories, 1):
print(f"{i}. {category}")
categories = []
while True:
category_input = input(menu['prompts']['categories']).strip()
if not category_input:
break
for cat_choice in category_input.split(','):
cat_choice = cat_choice.strip()
try:
category_index = int(cat_choice) - 1
if 0 <= category_index < len(dictionary.categories):
categories.append(dictionary.categories[category_index])
else:
print(f"Invalid category number: {cat_choice}")
except ValueError:
if cat_choice in dictionary.categories:
categories.append(cat_choice)
else:
print(f"Invalid category: {cat_choice}")
if categories:
break
print("Please enter at least one valid category!")
examples = input(menu['prompts']['examples']).strip()
search_queries = input(menu['prompts']['search_queries']).strip()
examples = [e.strip() for e in examples.split(',')] if examples else []
search_queries = [q.strip()
for q in search_queries.split(',')] if search_queries else [
]
dictionary.add_term(term, definition, categories, examples, search_queries)
print(menu['messages']['term_added'])
elif choice == '3':
# Show available categories
print("\nAvailable categories:")
for i, category in enumerate(dictionary.categories, 1):
print(f"{i}. {category}")
category_choice = input(menu['prompts']['category_lookup']).strip()
try:
category_index = int(category_choice) - 1
if 0 <= category_index < len(dictionary.categories):
category = dictionary.categories[category_index]
else:
print("Invalid category number!")
continue
except ValueError:
if category_choice not in dictionary.categories:
print("Invalid category!")
continue
category = category_choice
results = dictionary.lookup_by_category(category)
if results:
print(f"\nTerms in category '{category}':")
for term, data in results.items():
print(f"\n{term}:")
print(f" {menu['labels']['definition']}: {data['definition']}")
if data['examples']:
print(
f" {menu['labels']['examples']}: {', '.join(data['examples'])}")
print()
else:
print(menu['messages']['no_terms_in_category'])
elif choice == '4':
print(menu['messages']['goodbye'])
break
else:
print(menu['messages']['invalid_choice'])
if __name__ == "__main__":
main()