-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translator.py
67 lines (61 loc) · 1.43 KB
/
Translator.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
import re
Dictionary = {
"hello": "kamusta",
"goodbye": "paalam",
"cat": "pusa",
"dog": "aso",
"food": "pagkain",
"there": "diyan",
"hi": "kamusta ka",
"you":"ka",
"what":"ano",
"who":"sino",
"which":"alin",
"how": "paano",
"why":"bakit",
"where":"saan",
"the":"ang",
"is":"ang",
# "":"",
}
NonReturnables = {
"are",
"is",
"am",
"was",
"were",
"have",
"has",
"had",
"will",
"would",
"shall",
"should",
"can",
"could",
"might",
"may",
"does",
"do"
}
# Translates from English to Tagalog
def Translate(word):
# Remove question mark, and other unnecessary characters
AlphaNumericWord = re.sub(r'[^a-zA-Z0-9\s]', '', word).lower()
if AlphaNumericWord in Dictionary:
return Dictionary[AlphaNumericWord]
elif AlphaNumericWord in NonReturnables:
return None # Skip non-returnable words
else:
return AlphaNumericWord # Return the original word if no translation is found
print("-----------------------------------")
UI = input("Enter an English word or phrase to translate to Tagalog: \n")
# Translating word by word
words = UI.split()
word_by_word = []
for word in words:
translated_word = Translate(word)
if translated_word: # Only add to the result if it's not None
word_by_word.append(translated_word)
translated = " ".join(word_by_word)
print(translated)