forked from jxmorris12/language_tool_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_major_functionality.py
More file actions
166 lines (136 loc) · 10.4 KB
/
Copy pathtest_major_functionality.py
File metadata and controls
166 lines (136 loc) · 10.4 KB
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
import re
import time
import pytest
from language_tool_python.utils import LanguageToolError
def test_langtool_load():
import language_tool_python
tool = language_tool_python.LanguageTool("en-US")
matches = tool.check('ain\'t nothin but a thang')
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter.', 'replacements': ['Ai'], 'offsetInContext': 0, 'context': "ain't nothin but a thang", 'offset': 0, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical', 'sentence': "ain't nothin but a thang"}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['nothing', 'no thin'], 'offsetInContext': 6, 'context': "ain't nothin but a thang", 'offset': 6, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': "ain't nothin but a thang"}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['than', 'thing', 'hang', 'thank', 'Chang', 'tang', 'thong', 'twang', 'Thant', 'thane', 'Thanh', 'Jhang', 'Shang', 'Zhang'], 'offsetInContext': 19, 'context': "ain't nothin but a thang", 'offset': 19, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': "ain't nothin but a thang"})]"""
tool.close()
def test_process_starts_and_stops_in_context_manager():
import language_tool_python
with language_tool_python.LanguageTool("en-US") as tool:
proc: subprocess.Popen = tool._server
# Make sure process is running before killing language tool object.
assert proc.poll() is None, "tool._server not running after creation"
# Make sure process stopped after close() was called.
assert proc.poll() is not None, "tool._server should stop running after deletion"
def test_process_starts_and_stops_on_close():
import language_tool_python
tool = language_tool_python.LanguageTool("en-US")
proc: subprocess.Popen = tool._server
# Make sure process is running before killing language tool object.
assert proc.poll() is None, "tool._server not running after creation"
tool.close() # Explicitly close() object so process stops before garbage collection.
del tool
# Make sure process stopped after close() was called.
assert proc.poll() is not None, "tool._server should stop running after deletion"
# remember --> if poll is None: # p.subprocess is alive
def test_local_client_server_connection():
import language_tool_python
tool1 = language_tool_python.LanguageTool('en-US', host='0.0.0.0')
url = 'http://{}:{}/'.format(tool1._host, tool1._port)
tool2 = language_tool_python.LanguageTool('en-US', remote_server=url)
assert len(tool2.check('helo darknes my old frend'))
tool1.close()
tool2.close()
def test_config_text_length():
import language_tool_python
tool = language_tool_python.LanguageTool('en-US', config={ 'maxTextLength': 12 })
# With this config file, checking text with >12 characters should raise an error.
error_msg = re.escape("Error: Your text exceeds the limit of 12 characters (it's 27 characters). Please submit a shorter text.")
with pytest.raises(LanguageToolError, match=error_msg):
tool.check('Hello darkness my old frend')
# But checking shorter text should work fine.
# (should have 1 match for this one)
assert len(tool.check('Hello darkne'))
tool.close()
def test_config_caching():
import language_tool_python
tool = language_tool_python.LanguageTool('en-US', config={ 'cacheSize': 1000, 'pipelineCaching': True })
s = 'hello darkness my old frend'
t1 = time.time()
tool.check(s)
t2 = time.time()
tool.check(s)
t3 = time.time()
print(t3 - t2, t2 - t1)
# This is a silly test that says: caching should speed up a grammary-checking by a factor
# of speed_factor when checking the same sentence twice. It theoretically could be very flaky.
# But in practice I've observed speedup of around 250x (6.76s to 0.028s).
speedup_factor = 10.0
assert (t2 - t1) / speedup_factor > (t3 - t2)
tool.close()
def test_langtool_languages():
import language_tool_python
tool = language_tool_python.LanguageTool("en-US")
assert tool._get_languages() == {'es-AR', 'ta-IN', 'en-CA', 'da', 'eo', 'pt-AO', 'de', 'gl', 'ru-RU', 'de-DE', 'en', 'br', 'en-ZA', 'pt-MZ', 'ast-ES', 'sk-SK', 'en-AU', 'ta', 'ga', 'be', 'pl', 'tl-PH', 'sl', 'ar', 'es', 'sl-SI', 'en-NZ', 'el', 'el-GR', 'ru', 'zh-CN', 'en-GB', 'be-BY', 'pl-PL', 'km-KH', 'pt', 'uk-UA', 'ca', 'de-DE-x-simple-language', 'ro', 'ca-ES', 'de-CH', 'ja-JP', 'tl', 'pt-PT', 'gl-ES', 'pt-BR', 'km', 'ga-IE', 'ja', 'sv', 'sk', 'en-US', 'de-AT', 'ca-ES-valencia', 'uk', 'it', 'zh', 'br-FR', 'da-DK', 'ast', 'fr', 'fa', 'nl', 'ro-RO', 'nl-BE', 'auto'}
tool.close()
def test_match():
import language_tool_python
tool = language_tool_python.LanguageTool('en-US')
text = u'A sentence with a error in the Hitchhiker’s Guide tot he Galaxy'
matches = tool.check(text)
assert len(matches) == 2
assert str(matches[0]) == 'Offset 16, length 1, Rule ID: EN_A_VS_AN\nMessage: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.\nSuggestion: an\nA sentence with a error in the Hitchhiker’s Guide tot he ...\n ^'
tool.close()
def test_uk_typo():
import language_tool_python
tool = language_tool_python.LanguageTool("en-UK")
sentence1 = "If you think this sentence is fine then, your wrong."
results1 = tool.check(sentence1)
assert len(results1) == 1
assert language_tool_python.utils.correct(sentence1, results1) == "If you think this sentence is fine then, you're wrong."
results2 = tool.check("You're mum is called Emily, is that right?")
assert len(results2) == 0
tool.close()
def test_remote_es():
import language_tool_python
tool = language_tool_python.LanguageToolPublicAPI('es')
es_text = 'Escriba un texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escritura. Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales. También algunos errores de estilo, a grosso modo.'
matches = tool.check(es_text)
assert str(matches) == """[Match({'ruleId': 'AFRENTAR_DIFICULTADES', 'message': 'Confusión entre «afrontar» y «afrentar».', 'replacements': ['afrontar'], 'offsetInContext': 43, 'context': '...n texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escr...', 'offset': 49, 'errorLength': 8, 'category': 'INCORRECT_EXPRESSIONS', 'ruleIssueType': 'grammar', 'sentence': 'LanguageTool le ayudará a afrentar algunas dificultades propias de la escritura.'}), Match({'ruleId': 'PRON_HABER_PARTICIPIO', 'message': 'El v. ‘haber’ se escribe con hache.', 'replacements': ['ha'], 'offsetInContext': 43, 'context': '...ificultades propias de la escritura. Se a hecho un esfuerzo para detectar errores...', 'offset': 107, 'errorLength': 1, 'category': 'MISSPELLING', 'ruleIssueType': 'misspelling', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'MORFOLOGIK_RULE_ES', 'message': 'Se ha encontrado un posible error ortográfico.', 'replacements': ['ortográficos', 'ortográficas', 'ortográfico', 'orográficos', 'ortografiaos', 'ortografíeos'], 'offsetInContext': 43, 'context': '...rzo para detectar errores tipográficos, ortograficos y incluso gramaticales. También algunos...', 'offset': 163, 'errorLength': 12, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'Y_E_O_U', 'message': 'Cuando precede a palabras que comienzan por ‘i’, la conjunción ‘y’ se transforma en ‘e’.', 'replacements': ['e'], 'offsetInContext': 43, 'context': '...ctar errores tipográficos, ortograficos y incluso gramaticales. También algunos e...', 'offset': 176, 'errorLength': 1, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'Se a hecho un esfuerzo para detectar errores tipográficos, ortograficos y incluso gramaticales.'}), Match({'ruleId': 'GROSSO_MODO', 'message': 'Esta expresión latina se usa sin preposición.', 'replacements': ['grosso modo'], 'offsetInContext': 43, 'context': '...les. También algunos errores de estilo, a grosso modo.', 'offset': 235, 'errorLength': 13, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'También algunos errores de estilo, a grosso modo.'})]"""
tool.close()
def test_correct_en_us():
import language_tool_python
tool = language_tool_python.LanguageTool('en-US')
matches = tool.check('cz of this brand is awsome,,i love this brand very much')
assert len(matches) == 4
assert tool.correct('cz of this brand is awsome,,i love this brand very much') == 'Cz of this brand is awesome,I love this brand very much'
tool.close()
def test_spellcheck_en_gb():
import language_tool_python
s = 'Wat is wrong with the spll chker'
# Correct a sentence with spell-checking
tool = language_tool_python.LanguageTool('en-GB')
assert tool.correct(s) == "Was is wrong with the sell cheer"
# Correct a sentence without spell-checking
tool.disable_spellchecking()
assert tool.correct(s) == "Wat is wrong with the spll chker"
tool.close()
def test_session_only_new_spellings():
import os
import hashlib
import language_tool_python
library_path = language_tool_python.utils.get_language_tool_directory()
spelling_file_path = os.path.join(library_path, "org/languagetool/resource/en/hunspell/spelling.txt")
with open(spelling_file_path, 'r') as spelling_file:
initial_spelling_file_contents = spelling_file.read()
initial_checksum = hashlib.sha256(initial_spelling_file_contents.encode())
new_spellings = ["word1", "word2", "word3"]
with language_tool_python.LanguageTool('en-US', newSpellings=new_spellings, new_spellings_persist=False) as tool:
tool.enabled_rules_only = True
tool.enabled_rules = {'MORFOLOGIK_RULE_EN_US'}
matches = tool.check(" ".join(new_spellings))
with open(spelling_file_path, 'r') as spelling_file:
subsequent_spelling_file_contents = spelling_file.read()
subsequent_checksum = hashlib.sha256(subsequent_spelling_file_contents.encode())
if initial_checksum != subsequent_checksum:
with open(spelling_file_path, 'w') as spelling_file:
spelling_file.write(initial_spelling_file_contents)
assert not matches
assert initial_checksum.hexdigest() == subsequent_checksum.hexdigest()
def test_debug_mode():
from language_tool_python.server import DEBUG_MODE
assert DEBUG_MODE is False