Python中的语法检查器使用语言检查
Python是一种开源编程语言。它提供了种类繁多的库,这些库提供了更多的功能。一个这样的库是language_check 。 language_check 库没有与Python 3 捆绑在一起。相反,您必须从命令行手动下载它或从 pypi.org 下载它,然后手动安装它。
安装
通过 pip 安装:
$ pip install --upgrade language-check
如果您使用的是Python 2,则需要预先安装 3to2:
$ pip install --upgrade 3to2
要求
- Python v3.3+(或 2.7)
- Java v6.0 或更高版本。
language_check 指定错误以及文档中的规则 ID、消息、建议和行号。同样使用它我们可以直接更正文件中的错误。它准确地指出错误,但不能保证 100% 成功地发现错误。有时它可能会错过一些重要的错误。所以完全依赖它是不可取的。
下面的Python代码演示了在文本文档上使用 language_check。
import language_check
# Mention the language keyword
tool = language_check.LanguageTool('en-US')
i = 0
# Path of file which needs to be checked
with open(r'transcript1.txt', 'r') as fin:
for line in fin:
matches = tool.check(line)
i = i + len(matches)
pass
# prints total mistakes which are found
# from the document
print("No. of mistakes found in document is ", i)
print()
# prints mistake one by one
for mistake in matches:
print(mistake)
print()
输出 :
No. of mistakes found in document is 3
Line 1, column 1, Rule ID: UPPERCASE_SENTENCE_START
Message: This sentence does not start with an uppercase letter
Suggestion: So
so as you can see buying a new car doesn’t…
^^
Line 1, column 102, Rule ID: ENGLISH_WORD_REPEAT_RULE
Message: Possible typo: you repeated a word
Suggestion: research
…ing if you’ll follow three simple steps research research research read and find out everything y…
^^^^^^^^^^^^^^^^^
Line 1, column 1025, Rule ID: MORFOLOGIK_RULE_EN_US
Message: Possible spelling mistake found
Suggestion: speech; spec; specs; speck; spec h
…unny way to start the speech and in the spech and now use the listener knew that I’m …