-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.py
More file actions
36 lines (29 loc) · 1.03 KB
/
Query.py
File metadata and controls
36 lines (29 loc) · 1.03 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
# Query.py
import json
import sys
# This method is used to normalize and replace any of the three characters with a regular "ا"
# Did the same during SetDictionary
def normalizeAleph(word: str) -> str:
ALEF_KEY_MAP = {"آ":"ا", "أ":"ا", "إ":"ا"}
return "".join(ALEF_KEY_MAP.get(ch, ch) for ch in word)
# 'sorted' sorts the characters of the word 'احدم': ['حامد', 'احمد']
def generateKey(word:str) -> str:
return "".join(sorted(word))
if (len(sys.argv) <= 1):
print("you need to enter at least one argument\n" \
"Hint: Expected\n" \
"Query.py <word1> , <word2>...\n"
"Example: \n"
"َQuery.py احمد\n"
"Query.py حامد احمد محمد")
sys.exit()
with open("Dictionary.json", "r", encoding="utf-8") as file:
data = json.load(file)
querys = sys.argv[1:]
for originalQuery in querys:
query = normalizeAleph(originalQuery)
key = generateKey(query)
if(key in data):
print (data[key])
else:
print (originalQuery + " doesn't appear in the database")