python拼写检查_python中的languagetool语法和拼写检查器python拼写检查
语⾔⼯具(LanguageTool)
is an open-source grammar tool, also known as the spellchecker for OpenOffice. This library allows you to detect grammar errors and spelling mistakes through a Python script or through a command-line interface. We will work with the python package which can be installed with the pip install language-tool-python command. By default, language_tool_python will
download a LanguageTool server .jar and run that in the background to detect grammar errors locally. However, LanguageTool also offers a that is supported as well but there is a restriction in the number of calls.
是⼀种开源语法⼯具,也称为OpenOffice的拼写检查器。 该库使您可以通过Python脚本或命令⾏界⾯检测语法错误和拼写错误。 我们将使⽤ python软件包,该软件包可通过pip install language-tool-python命令pip install language-tool-python 。 默认情况下,
language_tool_python将下载LanguageTool服务器.jar并在后台运⾏该服务器以在本地检测语法错误。 但是,LanguageTool还提供了受
⽀持的 ,但调⽤次数受到限制。
Python语⾔⼯具 (LanguageTool in Python)
We will provide a practical example of how you can detect your grammar mistakes and also correct them. We will work with the following text:
我们将提供⼀个实⽤的⽰例,说明如何检测语法错误并进⾏纠正。 我们将处理以下⽂本:
“ LanguageTool offers spell and grammar checking. Just paste your text here and click the ‘Check Text’ button. Click
the colored phrases for details on potential errors. or use this text too see an few of of the problems that LanguageTool can detecd. What do you thinks of grammar checkers? Please not that they are not perfect. Style issues get a blue marker: It’s 5 P.M. in the afternoon. The weather was nice on Thursday, 27 June 2017 “.
“ LanguageTool提供拼写和语法检查。只需在此处粘贴⽂本,然后单击“检查⽂本”按钮即可。单击彩⾊短语以获取有关潜在错误的详细信息。 或使⽤该⽂本也看到少数的问题,LanguageTool可以detecd。您如何看待语法检查器?请不要说它们不是完美的。样式问题
是⼀个蓝⾊标记:下午5点。 2017年6⽉27⽇,星期四,天⽓很好。
I made bold the grammar issues. Let’s see how we can detect them with Python:
我把语法问题加粗了。 让我们看看如何使⽤Python检测它们:
import tool = text = # get the matchesmatches = matches
And we get:
我们得到:
[Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter', 'replacements': ['Or'], 'context': '...
As we can see we get a detailed dictionary that shows the ruleId, the message etc. You can find a detailed explanation of
every rule id in the . It is interesting to see the error that it captured about the date where it returns a message that: The date 27 June 2017 is not a Thursday, but a Tuesday. However, for this case, it does not have a correction because it cannot guess
what did the author mean by entering this date
如我们所见,我们获得了⼀个详细的字典,其中显⽰了ruleId , message等。您可以在到每个规则ID的详细说明。 有趣的是看到它在返
回消息的⽇期中捕获到的错误: The date 27 June 2017 is not a Thursday, but a Tuesday. 但是,在这种情况下,它没有更正,因为它⽆法猜测输⼊此⽇期后作者的意思
Since we detect the mistakes now we can correct them.
由于我们发现了错误,因此我们可以纠正它们。
my_mistakes =
my_corrections =
start_positions =
end_positions = for in
if
start_positions.append(rules.offset)
end_positions.Length+rules.offset)
my_mistakes.append(
text[rules.Length+rules.offset])
my_corrections.placements[0])my_new_text = for in
for in
my_new_text[start_positions[m]] =
if and
my_new_text[i]=""my_new_text = my_new_text
And we get (In bold you can see the corrections):
我们得到(粗体字,您可以看到更正):
“ LanguageTool offers spell and grammar checking. Just paste your text here and click the ‘Check Text’ button. Click the colored phrases for details on potential errors. Or use this text to see a few of the problems that LanguageTool can detect. What do you think of grammar checkers? Please note that they are not perfect. Style issues get a blue marker: It’s 5 P.M. The weather was nice on Thursday, 27 June 2017 “
“ LanguageTool提供拼写和语法检查。只需在此处粘贴⽂本,然后单击“检查⽂本”按钮即可。单击彩⾊短语以获取有关潜在错误的详细信息。 或使⽤此⽂字来查看 少数⼈的问题,LanguageTool可以检测。您如何看待语法检查器?请注意,它们并不完美。样式问题有⼀个蓝⾊标记:现在是下午5点。2017年6⽉27⽇,星期四,天⽓很好。
拼写和语法错误 (Spelling and Grammar Mistakes)
Let’s see the mistakes that we captured and their corresponding corrections.
让我们看看我们捕获的错误及其相应的更正。
list(zip(my_mistakes,my_corrections))[('or', 'Or'),
('too see', 'to see'),
('an', 'a'),
('of of', 'of'),
('detecd', 'detect'),
('thinks', 'think'),
('not', 'note'),
('P.M. in the afternoon.', 'P.M.')]
详细的例⼦ (Detailed Example)
We will provide a detailed example by taking into consideration a simple example of just one sentence and we will have a look at the output we get from the LanguageTool. Our sentence:
我们将通过仅考虑⼀个句⼦的简单⽰例来提供⼀个详细的⽰例,并查看从LanguageTool获得的输出。 我们的句⼦:
“Your the best but their are allso good!“
“您最好,但他们都很好! “
text =
matches = len(matches)
# 4
The LanguageTool detected 4 issues. We can focus on each issue. Let’s have a look at the first one.
LanguageTool检测到4个问题。 我们可以专注于每个问题。 让我们来看看第⼀个。
matches[0]
And we get:
我们得到:
Match({'ruleId': 'YOUR_YOU_RE', 'message': 'Did you mean "You\'re"?', 'replacements': ["You're"], 'context': 'Your the best but their are allso good !', 'offs
As we can see it mentions the ruleId, a message to the end your which is “Did you mean “You’re“, the suggested replacements, the context which is the input, the offset which is the position of the start of the issue, the errorLength which is
the number of characters of the issue, in our case 4 characters, the category of the mistake which is “TYPOS” in our case and the releIssueType which is “misspelling”.
我们可以看到它提到的ruleId ,⼀个message到最后你的,这是“你的意思是‘你’,建议replacements的context这是输⼊的offset这是问
题的开始位置,即errorLength ,它是问题的字符数,在我们的案例中为4个字符,在我们的案例中,错误的category为“ TYPOS” ,
⽽releIssueType为“ releIssueType” 。
We can show how we can call each element of the language_tool_python.match.Match type with the name followed by a period. Let’s say that we want to call the replacements.
我们可以演⽰如何调⽤名称后⾯带有句点的language_tool_python.match.Match类型的每个元素。 假设我们要调⽤替换项。
matches[0].replacements# ["You're"]
Let’s have a look at the other issues detected by LanguageTool. The second detected issue was the “their” which is corrected to there
让我们看⼀下LanguageTool检测到的其他问题。 检测到的第⼆个问题是“他们的”,并在那⾥进⾏了更正。
matches[1]
And we get:
我们得到:
Match({'ruleId': 'THEIR_IS', 'message': 'Did you mean "there"?', 'replacements': ['there'], 'context': 'Your the best but their are allso good !', 'offset': 18, 'err The third detected issue was the “allso” which is corrected to also
第三个检测到的问题是“allso”,这被修正为也
matches[2]
And we get:
我们得到:
Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['also', 'all so'], 'context': 'Your the best but t Finally, the last detected issue was the double spaces which is corrected to a single space.
最后,最后检测到的问题是将双空格纠正为单个空格。
matches[3]
能运行python的软件And we get:
我们得到:
Match({'ruleId': 'WHITESPACE_RULE', 'message': 'Possible typo: you repeated a whitespace', 'replacements': [' '], 'context': 'Your the best but their are allso goo 讨论区 (Discussion)
If we want a free tool in Python that does similar work with and supports more than 20 languages, then the LanguageTool is
a good option. Of course, none tool is perfect and we cannot rely solely on grammar and spell checkers but for sure is
something that we can use mainly in NLP tasks and projects.
如果我们想要⼀个免费的Python⼯具,可以与类似的⼯作并⽀持20多种语⾔,那么LanguageTool是⼀个不错的选择。 当然,没有⼀种⼯
具是完美的,我们不能仅仅依靠语法和拼写检查器,但是可以肯定的是,我们可以将其主要⽤于NLP任务和项⽬中。
python拼写检查
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论