Python 中的语法和拼写检查器

2024年8月29日 | 阅读 8 分钟

在接下来的教程中,我们将讨论一个名为 LanguageTool 的 Python 包,并了解如何使用 Python 编程语言创建简单的语法和拼写检查器。

那么,让我们开始吧。

理解 Python 中的 LanguageTool 库

LanguageTool 是一个用于语法和拼写检查的开源工具,它也被称为 OpenOffice 的拼写检查器。该包允许程序员通过 Python 代码片段或命令行界面 (CLI) 检测语法和拼写错误。

如何安装 LanguageTool 库?

要安装 Python 库,我们需要 'pip',这是一个用于管理安装来自受信任公共存储库的模块所需的包的框架。一旦我们有了 'pip',我们就可以使用 Windows 命令提示符 (CMD) 或终端中的命令来安装 LanguageTool 库,如下所示:

语法

language_tool_python 库将默认下载一个 LanguageTool 服务器作为 JAR 文件,并在后台执行该文件以在本地检测语法错误。但是 LanguageTool 也提供了一个支持的公共 HTTP Proofreading API;但是,调用次数有限。

验证安装

库安装完成后,我们可以创建一个空的 Python 程序文件并写入一个 import 语句来验证它,如下所示:

文件:verify.py

现在,保存上述文件并在终端中使用以下命令执行它:

语法

如果上面的 Python 程序文件没有返回任何错误,则表示库已正确安装。但是,如果出现异常,请尝试重新安装该库,并且还建议参考该模块的官方文档。

使用 Python LanguageTool 库

在下一节中,我们将通过一个实际示例来理解 Python 中 LanguageTool 库的工作原理。以下 Python 脚本演示了检测语法错误并对其进行更正。我们将使用以下文本:

文本

LanguageTool 提供检查语法和拼写错误的功能。我们只需在此处粘贴文本,然后点击“检查文本”按钮。点击彩色短语 for for 以获取有关潜在错误的更多信息。 or 我们可以使用这段文字 too see an 一些 LanguageTool 可以 dedect 的问题。 Whot 有人 thinks 对语法检查器有什么看法?请 not 注意它们并不完美。风格问题会得到蓝色标记:现在是晚上 7 P.M. in the evening。天气在 Monday, 22 November 2021 很好。

上面的文本包含一些以粗体突出显示的语法和拼写错误。让我们看下面的 Python 脚本来理解 LanguageTool 工具的工作原理:

示例

输出

[Match({'ruleId': 'ENGLISH_WORD_REPEAT_RULE', 'message': 'Possible typo: you repeated a word', 'replacements': ['for'], 'offsetInContext': 43, 'context': "...Text' button. Click the colored phrases for for information on potential errors. or we ...", 'offset': 165, 'errorLength': 7, 'category': 'MISC', 'ruleIssueType': 'duplication', 'sentence': 'Click the colored phrases for for information on potential errors.'}), Match({'ruleId': 'UPPERCASE_SENTENCE_START', 'message': 'This sentence does not start with an uppercase letter.', 'replacements': ['Or'], 'offsetInContext': 43, 'context': '...or for information on potential errors. or we can use this text too see an some of...', 'offset': 206, 'errorLength': 2, 'category': 'CASING', 'ruleIssueType': 'typographical', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'TOO_TO', 'message': 'Did you mean "to see"?', 'replacements': ['to see'], 'offsetInContext': 43, 'context': '...tential errors. or we can use this text too see an some of the issues that LanguageTool...', 'offset': 230, 'errorLength': 7, 'category': 'CONFUSED_WORDS', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'EN_A_VS_AN', 'message': 'Use "a" instead of 'an' if the following word doesn't start with a vowel sound, e.g. 'a sentence', 'a university'.', 'replacements': ['a'], 'offsetInContext': 43, 'context': '...errors. or we can use this text too see an some of the issues that LanguageTool ca...', 'offset': 238, 'errorLength': 2, 'category': 'MISC', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['detect', 'defect', 'deduct', 'deject'], 'offsetInContext': 43, 'context': '...ome of the issues that LanguageTool can dedect. Whot do someone thinks of grammar chec...', 'offset': 282, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'or we can use this text too see an some of the issues that LanguageTool can dedect.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['Who', 'What', 'Shot', 'Whom', 'Hot', 'WHO', 'Whet', 'Whit', 'Whoa', 'Whop', 'WHT', 'Wot', 'W hot'], 'offsetInContext': 43, 'context': '...he issues that LanguageTool can dedect. Whot do someone thinks of grammar checkers? ...', 'offset': 290, 'errorLength': 4, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Whot do someone thinks of grammar checkers?'}), Match({'ruleId': 'PLEASE_NOT_THAT', 'message': 'Did you mean "note"?', 'replacements': ['note'], 'offsetInContext': 43, 'context': '...eone thinks of grammar checkers? Please not that they are not perfect. Style proble...', 'offset': 341, 'errorLength': 3, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'Please not that they are not perfect.'}), Match({'ruleId': 'PM_IN_THE_EVENING', 'message': 'This is redundant. Consider using "P.M."', 'replacements': ['P.M.'], 'offsetInContext': 43, 'context': '...yle problems get a blue marker: It is 7 P.M. in the evening. The weather was nice on Monday, 22 Nov...', 'offset': 414, 'errorLength': 19, 'category': 'REDUNDANCY', 'ruleIssueType': 'style', 'sentence': 'Style problems get a blue marker: It is 7 P.M. in the evening.'})]

说明

在上面的代码片段中,我们导入了所需的库,并定义了一个使用 LanguageTool 工具来检查文本中语法和拼写错误的工具。然后,我们定义了另一个字符串变量,其中存储了我们要检查的文本段落。然后,我们使用 check() 函数检索匹配项,并为用户打印它们。

结果,我们可以观察到一个详细的字典,显示 ruleId, message, replacements, offsetInContext, context, offset 以及更多内容。我们可以在 LanguageTool 社区中找到每个规则 ID 的详细解释。

既然我们已经检测到了错误,现在是时候更正它们了。让我们看下面的 Python 脚本来演示这一点:

示例

输出

LanguageTool provides utility to check grammar and spelling errors. We just have to paste the text here and click the 'Check Text' button. Click the colored phrases for information on potential errors. Or we can use this text to see a some of the issues that LanguageTool can detect. Who do someone thinks of grammar checkers? Please note that they are not perfect. Style problems get a blue marker: It is 7 P.M.. The weather was nice on Monday, 22 November 2021

说明

我们在上面的代码片段中包含了一些新变量来处理错误、更正、起始位置和结束位置。然后,我们使用 for 循环遍历 my_matches 中的规则,并将错误替换为其更正。然后,我们将这些更正后的文本存储在一个列表中。最后,我们再次使用 for 循环遍历列表中的字符串元素,将它们连接起来,并为用户打印结果文本。

因此,我们已经成功地更正了在上一代码片段中发现的错误。

现在,让我们使用下面的 Python 脚本观察我们之前捕获的错误以及它们各自的更正:

示例

输出

[('for for', 'for'), ('or', 'Or'), ('too see', 'to see'), ('an', 'a'), ('dedect', 'detect'), ('Whot', 'Who'), ('not', 'note'), ('P.M. in the evening', 'P.M.')]

说明

在上面的代码片段中,我们打印了文本中错误及其相应更正的列表。

自动将建议应用于文本

让我们看一个简单的示例,演示如何使用 Python 中的 LanguageTool 库自动将建议应用于文本。

示例

输出

Original Text: A quick broun fox jumpps over a a little lazy dog.
Text after correction: A quick brown fox jumps over a little lazy dog.

说明

在上面的代码片段中,我们导入了所需的库,并定义了 LanguageTool 工具,指定语言为美国英语。然后,我们定义了一个字符串变量并将一些文本存储到其中。然后,我们使用工具的 correct() 函数自动更正文本中的错误,并为用户打印结果文本。