-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.py
32 lines (26 loc) · 1.03 KB
/
grammar.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
import language_tool_python
tool = language_tool_python.LanguageTool('en-US')
def checkGrammar(text):
''' Checks for grammatical errors and returns corrections'''
matches = tool.check(text)
my_mistakes = []
my_corrections = []
start_positions = []
end_positions = []
for rules in matches:
if len(rules.replacements) > 0:
start_positions.append(rules.offset)
end_positions.append(rules.errorLength+rules.offset)
my_mistakes.append(
text[rules.offset:rules.errorLength+rules.offset])
my_corrections.append(rules.replacements[0])
my_new_text = list(text)
for m in range(len(start_positions)):
for i in range(len(text)):
my_new_text[start_positions[m]] = my_corrections[m]
if (i > start_positions[m] and i < end_positions[m]):
my_new_text[i] = ""
my_new_text = "".join(my_new_text)
print(my_new_text)
print(list(zip(my_mistakes, my_corrections)))
return [my_new_text, my_mistakes]