Skip to content

Commit 5024439

Browse files
committed
spellcheck fixes & testing
1 parent 9d561b6 commit 5024439

10 files changed

Lines changed: 95 additions & 190 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: Test with PyTest
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
python-version: [3.6, 3.7, 3.8]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Set up Python ${{ matrix.python-version }}
23+
uses: actions/setup-python@v2
24+
with:
25+
python-version: ${{ matrix.python-version }}
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip setuptools wheel
29+
pip install pytest pytest-xdist # Testing packages
30+
python setup.py install_egg_info # Workaround https://github.com/pypa/pip/issues/4537
31+
pip install -e . # Run pytest
32+
- name: Test with pytest
33+
run: |
34+
pytest -vx --dist=loadfile -n auto
35+
- name: Run command-line tests
36+
run: |
37+
./tests/test_local.bash # Test command-line with local server
38+
./tests/test_remote.bash # Test command-line with remote server

‎language_tool_python/server.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,15 @@ def _spell_checking_rules(self):
9191
return {'HUNSPELL_RULE', 'HUNSPELL_NO_SUGGEST_RULE',
9292
'MORFOLOGIK_RULE_' + str(self.language).replace('-', '_').upper()}
9393

94-
def check(self, text: str, srctext=None) -> [Match]:
94+
def check(self, text: str) -> [Match]:
9595
"""Match text against enabled rules."""
9696
url = urllib.parse.urljoin(self._url, 'check')
97-
response = self._get_root(url, self._encode(text, srctext))
97+
response = self._get_root(url, self._encode(text))
9898
matches = response['matches']
9999
return [Match(match) for match in matches]
100100

101-
def _encode(self, text, srctext=None):
101+
def _encode(self, text):
102102
params = {'language': self.language, 'text': text.encode('utf-8')}
103-
if srctext is not None:
104-
params['srctext'] = srctext.encode('utf-8')
105103
if self.motherTongue is not None:
106104
params['motherTongue'] = self.motherTongue
107105
if self.disabled_rules:
@@ -116,9 +114,9 @@ def _encode(self, text, srctext=None):
116114
params['enabledCategories'] = ','.join(self.enabled_categories)
117115
return urllib.parse.urlencode(params).encode()
118116

119-
def correct(self, text: str, srctext=None) -> str:
117+
def correct(self, text: str) -> str:
120118
"""Automatically apply suggestions to the text."""
121-
return correct(text, self.check(text, srctext))
119+
return correct(text, self.check(text))
122120

123121
def enable_spellchecking(self):
124122
"""Enable spell-checking rules."""

‎pytest.ini‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
addopts = -ra
3+
testpaths = tests

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='language_tool_python',
10-
version='2.2.4',
10+
version='2.3.0',
1111
description='Checks grammar using LanguageTool.',
1212
long_description_content_type='text/markdown',
1313
long_description=long_description,

‎tests/easy_test.py‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎tests/lang_tool_load_test.py‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎tests/remote_test.py‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎tests/spellcheck_test.py‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎tests/test.py‎

Lines changed: 0 additions & 151 deletions
This file was deleted.

‎tests/test_major_functionality.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
def test_langtool_load():
3+
import language_tool_python
4+
lang_tool = language_tool_python.LanguageTool("en-US")
5+
matches = lang_tool.check('ain\'t nothin but a thang')
6+
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter', 'replacements': ['Ain'], 'context': "ain't nothin but a thang", 'offset': 0, 'errorLength': 3, 'category': 'CASING', 'ruleIssueType': 'typographical'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['nothing', 'no thin'], 'context': "ain't nothin but a thang", 'offset': 6, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['than', 'thing', 'hang', 'thank', 'Chang', 'tang', 'thong', 'twang', 'Thant', 'thane', 'Thanh', 't hang', 'than g', 'Shang', 'Zhang'], 'context': "ain't nothin but a thang", 'offset': 19, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})]"""
7+
8+
def test_match():
9+
import language_tool_python
10+
tool = language_tool_python.LanguageTool('en-US')
11+
text = u'A sentence with a error in the Hitchhiker’s Guide tot he Galaxy'
12+
matches = tool.check(text)
13+
assert len(matches) == 2
14+
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 ^'
15+
16+
17+
def test_remote_es():
18+
import language_tool_python
19+
tool = language_tool_python.LanguageToolPublicAPI('es')
20+
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.'
21+
22+
print('Spell-checking in [ES] using LanguageTool Public API:')
23+
matches = tool.check(es_text)
24+
print('num es matches:', len(matches))
25+
assert str(matches) == """[Match({'ruleId': 'AFRENTAR_DIFICULTADES', 'message': 'La forma correcta es usando el verbo «afrontar»: "afrontar algunas dificultades""', 'replacements': ['afrontar algunas dificultades'], 'context': '...n texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escritura. Se a hecho un ...', 'offset': 49, 'errorLength': 29, 'category': 'INCORRECT_EXPRESSIONS', 'ruleIssueType': 'grammar'}), Match({'ruleId': 'PRON_HABER_PARTICIPIO', 'message': "El v. 'haber' se escribe con hache.", 'replacements': ['ha'], 'context': '...ificultades propias de la escritura. Se a hecho un esfuerzo para detectar errores...', 'offset': 107, 'errorLength': 1, 'category': 'MISSPELLING', 'ruleIssueType': 'misspelling'}), 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'], 'context': '...rzo para detectar errores tipográficos, ortograficos y incluso gramaticales. También algunos...', 'offset': 163, 'errorLength': 12, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'}), Match({'ruleId': 'Y_E', 'message': 'Cuando precede a palabras que comienzan por «i-» (o «hi-»), la conjunción «y» se transforma en «e».', 'replacements': ['e incluso'], 'context': '...ctar errores tipográficos, ortograficos y incluso gramaticales. También algunos errores d...', 'offset': 176, 'errorLength': 9, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar'}), Match({'ruleId': 'GROSSO_MODO', 'message': 'Esta expresión latina se usa sin preposición.', 'replacements': ['grosso modo'], 'context': '...les. También algunos errores de estilo, a grosso modo.', 'offset': 235, 'errorLength': 13, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar'})]"""
26+
27+
def test_correct_en_us():
28+
import language_tool_python
29+
tool = language_tool_python.LanguageTool('en-US')
30+
31+
matches = tool.check('cz of this brand is awsome,,i love this brand very much')
32+
assert len(matches) == 4
33+
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter', 'replacements': ['Cz'], 'context': 'cz of this brand is awsome,,i love this br...', 'offset': 0, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['awesome', 'aw some'], 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 20, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'}), Match({'ruleId': 'DOUBLE_PUNCTUATION', 'message': 'Two consecutive commas', 'replacements': [','], 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 26, 'errorLength': 2, 'category': 'PUNCTUATION', 'ruleIssueType': 'typographical'}), Match({'ruleId': 'I_LOWERCASE', 'message': 'Is this the personal pronoun "I"? It is spelled uppercase.', 'replacements': ['I'], 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 28, 'errorLength': 1, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})]"""
34+
35+
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'
36+
37+
def test_spellcheck_en_gb():
38+
import language_tool_python
39+
40+
s = 'Wat is wrong with the spll chker'
41+
42+
# Correct a sentence with spell-checking
43+
tool = language_tool_python.LanguageTool('en-GB')
44+
assert tool.correct(s) == "Was is wrong with the sell cheer"
45+
46+
# Correct a sentence without spell-checking
47+
tool.disable_spellchecking()
48+
assert tool.correct(s) == "Wat is wrong with the spll chker"

0 commit comments

Comments
 (0)