-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenitive_parser.py
74 lines (69 loc) · 2.26 KB
/
genitive_parser.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
import requests
import re
def apply_changes(text,error_text,ruleId,message):
if ruleId == "GENITIVE":
replace_words = message.replace("consider use of genitive: ","")
punct = ""
try:
if text[-1] in [".",",","?","\"","\'",":",";","!"]:
punct = text[-1]
except:
pass
text = replace_words + punct
return text
def process_errors(text,errors):
if errors:
text = text.split("\n")
my = 0
mx = 0
ruleId = ""
message = ""
utterance_parts = [[]]
add = 0
for error in errors:
c_my = int(error["fromy"])
c_mx = int(error["fromx"])
c_tx = int(error["tox"])
if c_my != my:
utterance_parts[my].append(text[my][mx:])
add += 1
utterance_parts.append([])
my = c_my
if c_mx > mx:
utterance_parts[my].append(text[my][mx:c_mx])
add += 1
elif mx < c_mx:
continue
rule_match = re.search("Lingua::GA::Gramadoir/.*",error["ruleId"])
ruleId = rule_match.group(0).replace("Lingua::GA::Gramadoir/","").replace("\"","").strip()
message = error["msg"]
error_text = error["errortext"]
utterance_parts[my].append(apply_changes(text[my][c_mx:c_tx+1],error_text,ruleId,message))
add += 1
mx = c_tx +1
if mx < len(text[my]):
utterance_parts[my].append(text[my][mx:])
add += 1
output_text = ""
for paragraph in utterance_parts:
if output_text != "":
output_text += "\n"
for x in paragraph:
output_text += "".join(x)
return output_text
else:
return text
def genitive_parser(text):
url = f'https://phoneticsrv3.lcs.tcd.ie/gramsrv/api/grammar?text={text}&check=genitive'
errors = requests.get(url).json()
output_text = process_errors(text, errors)
return output_text
def main():
o = genitive_parser("fear mór an post ")
print(o)
o = genitive_parser("hata an fear")
print(o)
o = genitive_parser("fear an post")
print(o)
if __name__ == "__main__":
main()