This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonglog.py
71 lines (61 loc) · 2.12 KB
/
konglog.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
from nltk.probability import KneserNeyProbDist
from swiplserver import PrologMQI
from nltk.corpus import cmudict
from unicode import join_jamos
def eng_to_kong(eng_word: str)-> list[str]:
"""
Translate given English word into Korean into matching pronounciation,
matching the English Loanword Orthography.
For example, "hello" will be translated into 헐로.
# Panics
When given a english word that it cannot translate, `eng_to_kong` will raise a KeyError.
Example
```python
import konglog
def main():
word = "shrimp"
print(konglog.eng_to_kong(word))
```
"""
# Parse word into phonemes string for passing to Prolog Query.
prolog_arg_aras = "]"
for phe in cmudict.dict()[eng_word.lower().strip()][0]:
if phe[-1] == '0' or phe[-1] == '1' or phe[-1] == '2':
phe = phe[:-1]
prolog_arg_aras = "," + phe.lower() + prolog_arg_aras
prolog_arg_aras = "[" + prolog_arg_aras[1:]
# Execute Prolog query
with PrologMQI() as mqi:
with mqi.create_thread() as prolog_thread:
assert(prolog_thread.query("consult(\"ipa.pl\")"))
prolog_res = prolog_thread.query(f"ipa_to_kr(X,{prolog_arg_aras})")
# Parse results
jamo_lists = []
try:
for jls in prolog_res:
temp = jls['X']
temp.reverse()
jamo_lists.append(temp)
except TypeError:
raise KeyError
jamo_all = []
for jamo_list in jamo_lists:
temp_jamo_all = [""]
for jamos in jamo_list:
if isinstance(jamos, str):
for i in range(len(temp_jamo_all)):
temp_jamo_all[i] += jamos
else:
temp = []
for jamo in jamos:
for s in temp_jamo_all:
temp.append(s + jamo)
temp_jamo_all = temp
jamo_all.extend(temp_jamo_all)
# Combine jamos into Konglish word
jamo_all.sort(key = lambda x : len(x))
for jamos in jamo_all:
try:
return join_jamos(jamos, False)
except ValueError:
continue