Skip to content

Commit 77b6d83

Browse files
committed
2.4.1: add Match.matchedText
1 parent cd8ec98 commit 77b6d83

4 files changed

Lines changed: 13 additions & 7 deletions

File tree

‎language_tool_python/match.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ def get_match_ordered_dict():
66
('ruleId', str),
77
('message', str),
88
('replacements', list),
9+
('offsetInContext', int),
910
('context', str),
1011
('offset', int),
1112
('errorLength', int),
1213
('category', str),
1314
('ruleIssueType', str),
15+
('sentence', str),
1416
])
1517
return slots
1618

@@ -48,6 +50,7 @@ def __init__(self, attrib):
4850
attrib['ruleIssueType'] = attrib['rule']['issueType']
4951
del attrib['rule']
5052
# Process context.
53+
attrib['offsetInContext'] = attrib['context']['offset']
5154
attrib['context'] = attrib['context']['text']
5255
# Process replacements.
5356
attrib['replacements'] = [r['value'] for r in attrib['replacements']]
@@ -85,6 +88,12 @@ def __str__(self):
8588
)
8689
return s
8790

91+
@property
92+
def matchedText(self):
93+
""" Returns the text that garnered the error (without its surrounding context).
94+
"""
95+
return self.context[self.offsetInContext:self.offsetInContext+self.errorLength]
96+
8897
def __eq__(self, other):
8998
return list(self) == list(other)
9099

‎language_tool_python/server.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ def __init__(self, language=None, motherTongue=None, remote_server=None):
4646
language = get_locale_language()
4747
except ValueError:
4848
language = FAILSAFE_LANGUAGE
49-
# if self._url[-1] != '/':
50-
# self._url += '/'
51-
print('self._url:', self._url)
5249
self._language = LanguageTag(language, self._get_languages())
5350
self.motherTongue = motherTongue
5451
self.disabled_rules = set()

‎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.4.0',
10+
version='2.4.1',
1111
description='Checks grammar using LanguageTool.',
1212
long_description_content_type='text/markdown',
1313
long_description=long_description,

‎tests/test_major_functionality.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def test_langtool_load():
33
import language_tool_python
44
lang_tool = language_tool_python.LanguageTool("en-US")
55
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', 'Shang', 'Zhang'], 'context': "ain't nothin but a thang", 'offset': 19, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})]"""
6+
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter', 'replacements': ['Ain'], 'offsetInContext': 0, 'context': "ain't nothin but a thang", 'offset': 0, 'errorLength': 3, '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', 't hang', '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"})]"""
77

88
def test_langtool_languages():
99
import language_tool_python
@@ -26,15 +26,15 @@ def test_remote_es():
2626

2727
print('Spell-checking in [ES] using LanguageTool Public API:')
2828
matches = tool.check(es_text)
29-
assert str(matches) == """[Match({'ruleId': 'AFRENTAR_DIFICULTADES', 'message': 'Confusión entre «afrontar» y «afrentar».', 'replacements': ['afrontar'], 'context': '...n texto aquí. LanguageTool le ayudará a afrentar algunas dificultades propias de la escr...', 'offset': 49, 'errorLength': 8, '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'})]"""
29+
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', 'message': 'Cuando precede a palabras que comienzan por «i-» (o «hi-»), la conjunción «y» se transforma en «e».', 'replacements': ['e incluso'], 'offsetInContext': 43, 'context': '...ctar errores tipográficos, ortograficos y incluso gramaticales. También algunos errores d...', 'offset': 176, 'errorLength': 9, '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.'})]"""
3030

3131
def test_correct_en_us():
3232
import language_tool_python
3333
tool = language_tool_python.LanguageTool('en-US')
3434

3535
matches = tool.check('cz of this brand is awsome,,i love this brand very much')
3636
assert len(matches) == 4
37-
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'})]"""
37+
assert str(matches) == """[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter', 'replacements': ['Cz'], 'offsetInContext': 0, 'context': 'cz of this brand is awsome,,i love this br...', 'offset': 0, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical', 'sentence': 'cz of this brand is awsome,,i love this brand very much'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['awesome', 'aw some'], 'offsetInContext': 20, 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 20, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'cz of this brand is awsome,,i love this brand very much'}), Match({'ruleId': 'DOUBLE_PUNCTUATION', 'message': 'Two consecutive commas', 'replacements': [','], 'offsetInContext': 26, 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 26, 'errorLength': 2, 'category': 'PUNCTUATION', 'ruleIssueType': 'typographical', 'sentence': 'cz of this brand is awsome,,i love this brand very much'}), Match({'ruleId': 'I_LOWERCASE', 'message': 'Is this the personal pronoun "I"? It is spelled uppercase.', 'replacements': ['I'], 'offsetInContext': 28, 'context': 'cz of this brand is awsome,,i love this brand very much', 'offset': 28, 'errorLength': 1, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'cz of this brand is awsome,,i love this brand very much'})]"""
3838

3939
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'
4040

0 commit comments

Comments
 (0)