-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
186 lines (155 loc) · 5.58 KB
/
check.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import argparse
import polib
import language_tool_python
from colorama import Fore, Style
import os
import json
from jsonschema import validate
import jsonschema_default
from Levenshtein import distance
schema = {
"type": "object",
"properties": {
"checkSourceString": {"type": "boolean", "default": True},
"checkTranslationString": {"type": "boolean", "default": False},
"customDictionary": {
"type": "array",
"items": {"type": "string"},
"default": [],
},
"disabledRules": {"type": "array", "items": {"type": "string"}, "default": []},
},
"required": [],
}
class poChecker:
def __init__(
self,
path,
language="en-US",
check_source=True,
check_translation=False,
dict=[],
disabledRules=[],
verbose=False,
):
self.pofile = polib.pofile(path)
self.tool = language_tool_python.LanguageTool(
language,
config={
"cacheSize": 1000,
"pipelineCaching": True,
"maxSpellingSuggestions": 10,
"disabledRuleIds": ",".join(disabledRules),
},
)
self.check_source = check_source
self.check_translation = check_translation
self.dict = dict
self.verbose = verbose
def process(self):
for entry in self.pofile:
if self.check_source:
issues = self.tool.check(entry.msgid)
if len(issues) > 0:
for issue in issues:
if self.isIssueValid(issue):
self.outputIssue(issue)
if self.check_translation and entry.msgstr:
issues = self.tool.check(entry.msgstr)
if len(issues) > 0:
for issue in issues:
if self.isIssueValid(issue):
self.outputIssue(issue)
self.tool.close()
def suggestCorrectionsFromCustomDic(self, typo, distance_limit=3):
min_dist = distance_limit + 1
suggested_word = None
for knownWord in self.dict:
dist = distance(typo, knownWord, score_cutoff=distance_limit)
if dist < min_dist:
min_dist = dist
suggested_word = knownWord
return suggested_word
def isIssueValid(self, issue):
context = issue.context[
issue.offsetInContext : issue.offsetInContext + issue.errorLength
]
if context in self.dict:
return False
else:
return True
def outputIssue(self, issue):
offset = " " * issue.offsetInContext
pointer = "^" * issue.errorLength
context = issue.context.strip()
suggestion = None
print(Fore.RED + f"{issue.message.strip()}")
print(Fore.YELLOW + f"{context}")
print(f"{offset}{pointer}")
typo = context[
issue.offsetInContext : issue.offsetInContext + issue.errorLength
]
hasSuggestion = True if issue.replacements and issue.replacements[0] else False
# Our method for giving suggestions is less robust, we should reduce the allowed edit distance if a suggestion was already provided
if hasSuggestion:
suggestionFromCustomDict = self.suggestCorrectionsFromCustomDic(typo, 2)
else:
suggestionFromCustomDict = self.suggestCorrectionsFromCustomDic(typo, 3)
if hasSuggestion:
# Ensure we are providing the suggestion with the smallest edit distance
if suggestionFromCustomDict:
suggestion = (
issue.replacements[0]
if distance(typo, issue.replacements[0], score_cutoff=3)
< distance(typo, suggestionFromCustomDict, score_cutoff=3)
else suggestionFromCustomDict
)
else:
suggestion = issue.replacements[0]
else:
suggestion = suggestionFromCustomDict
if suggestion:
print(Fore.GREEN + f"{offset}{suggestion}")
if self.verbose:
print(Fore.WHITE + f"Triggered rule ID: {issue.ruleId}")
print(Style.RESET_ALL)
def main():
parser = argparse.ArgumentParser(description="Process a .po file path")
parser.add_argument("--path", type=str, help="Path to the .po file", required=True)
parser.add_argument(
"--language",
type=str,
default="en-US",
help="The language of the input file (Examples: en-US, en, es, ca-ES)",
)
parser.add_argument(
"--config",
type=str,
default="potLanguageChecker.json",
help="The PotLangueChecker config file to read.",
)
parser.add_argument(
"--verbose",
action="store_true",
help="Displays additional info such as what rule ID was striggered",
)
args = parser.parse_args()
if not os.path.exists(args.config):
print(Fore.RED + f"Config file {args.config} does not exist")
exit(1)
with open(args.config, "r") as f:
config = json.load(f)
validate(instance=config, schema=schema)
default_config = jsonschema_default.create_from(schema)
config = {**default_config, **config}
checker = poChecker(
args.path,
language=args.language,
check_source=config["checkSourceString"],
check_translation=config["checkTranslationString"],
dict=config["customDictionary"],
verbose=args.verbose,
)
checker.process()
if __name__ == "__main__":
main()