forked from myint/language-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·151 lines (131 loc) · 5.35 KB
/
test.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test suite for language_check."""
from __future__ import unicode_literals
import unittest
import warnings
from collections import namedtuple
import language_check
class TestLanguageTool(unittest.TestCase):
CheckTest = namedtuple('CheckTest', ('text', 'matches'))
Match = namedtuple('Match', ('fromy', 'fromx', 'ruleId'))
check_tests = {
'en': [
CheckTest(
('Paste your own text here... or check this text too see '
'a few of the problems that that LanguageTool can detect. '
'Did you notice that their is no spelcheckin included?'),
[
Match(0, 47, 'TOO_TO'),
Match(0, 132, 'THEIR_IS'),
]
),
],
'fr': [
CheckTest(
('Se texte est un exemple pour pour vous montrer '
'le fonctionnement de LanguageTool. '
'notez que LanguageTool ne comporte pas '
'de correcteur orthographique.'),
[
Match(0, 0, 'SE_CE'),
Match(0, 3, 'TE_NV'),
Match(0, 24, 'FRENCH_WORD_REPEAT_RULE'),
Match(0, 82, 'UPPERCASE_SENTENCE_START'),
]
),
CheckTest(
'je me rappelle de tout sans aucun soucis!',
[
Match(0, 0, 'UPPERCASE_SENTENCE_START'),
Match(0, 6, 'RAPPELER_DE'),
Match(0, 28, 'ACCORD_NOMBRE'),
Match(0, 34, 'FRENCH_WHITESPACE'),
]
),
],
}
correct_tests = {
'en-US': {
'that would of been to impressive.':
'That would have been too impressive.',
},
'fr': {
'il monte en haut si il veut.':
'Il monte s’il veut.',
},
}
def test_check(self):
lang_check = language_check.LanguageTool()
for language, tests in self.check_tests.items():
try:
lang_check.language = language
except ValueError:
version = language_check.get_version()
warnings.warn(
'LanguageTool {} doesn’t support language {!r}'
.format(version, language)
)
for text, expected_matches in tests:
matches = lang_check.check(text)
for expected_match in expected_matches:
for match in matches:
if (
(match.fromy, match.fromx, match.ruleId) ==
(expected_match.fromy, expected_match.fromx,
expected_match.ruleId)
):
break
else:
raise IndexError(
'can’t find {!r}'.format(expected_match))
def test_correct(self):
lang_check = language_check.LanguageTool()
for language, tests in self.correct_tests.items():
try:
lang_check.language = language
except ValueError:
version = language_check.get_version()
warnings.warn(
'LanguageTool {} doesn’t support language {!r}'
.format(version, language)
)
for text, result in tests.items():
self.assertEqual(lang_check.correct(text), result)
def test_languages(self):
self.assertIn('en', language_check.get_languages())
def test_version(self):
self.assertTrue(language_check.get_version())
def test_get_build_date(self):
self.assertTrue(language_check.get_build_date())
def test_get_directory(self):
path = language_check.get_directory()
language_check.set_directory(path)
self.assertEqual(path, language_check.get_directory())
def test_disable_spellcheck(self):
sentence_with_misspelling = 'This is baad.'
lang_check = language_check.LanguageTool()
self.assertTrue(lang_check.check(sentence_with_misspelling))
lang_check.disable_spellchecking()
self.assertFalse(lang_check.check(sentence_with_misspelling))
lang_check.enable_spellchecking()
self.assertTrue(lang_check.check(sentence_with_misspelling))
def test_README_with_unicode(self):
tool = language_check.LanguageTool('en-US')
text = ('A sentence with a error in the '
'Hitchhiker’s Guide tot he Galaxy')
matches = tool.check(text)
self.assertEqual(len(matches), 2)
self.assertEqual((matches[0].fromy, matches[0].fromx),
(0, 16))
self.assertEqual((matches[0].ruleId, matches[0].replacements),
('EN_A_VS_AN', ['an']))
self.assertEqual((matches[1].fromy, matches[1].fromx),
(0, 50))
self.assertEqual((matches[1].ruleId, matches[1].replacements),
('TOT_HE', ['to the']))
corrected = language_check.correct(text, matches)
self.assertEqual(corrected, 'A sentence with an error in the '
'Hitchhiker’s Guide to the Galaxy')
if __name__ == '__main__':
unittest.main()