-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickwash.py
53 lines (39 loc) · 1.21 KB
/
quickwash.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
import json
import sys
import spacy
import judicious
import proselint
nlp = spacy.load('en')
judicious.register("http://127.0.0.1:5000")
def error_str_to_json(err_string):
return json.loads(
proselint.tools.errors_to_json(
proselint.tools.lint(err_string)
)
)
def sentence_context(text, position):
"""The sentence in the text that includes the given position."""
doc = nlp(text)
counter = 0
for sentence in doc.sents:
counter += len(sentence.text)
if counter >= position:
return sentence.text
def error_is_valid(text, error):
"""Call out to Judicious and figure out if error is valid."""
return judicious.prosewash(
text=sentence_context(text, error['start']),
error_message=error['message'],
)
def main(text=None):
if text is None:
with open(sys.argv[1], "r") as f:
text = f.read()
errors = error_str_to_json(text)['data']['errors']
valid_errors = [error for error in errors
if error_is_valid(text, error)]
json_output = error_str_to_json(text)
json_output['data']['errors'] = valid_errors
print(json_output)
if __name__ == "__main__":
main()