58

如何检查一个句子在 Python 中是否有效?

例子:

I love Stackoverflow - Correct
I Stackoverflow love - Incorrect
4

4 回答 4

47

有各种提供自动校对和语法检查的 Web 服务。有些有一个 Python 库来简化查询。

据我所知,这些工具中的大多数(当然是 After the Deadline 和 LanguageTool)都是基于规则的。将检查的文本与描述常见错误的大量规则进行比较。如果规则匹配,软件将其称为错误。如果规则不匹配,软件什么也不做(它无法检测到它没有规则的错误)。

截止日期后

import ATD
ATD.setDefaultKey("your API key")
errors = ATD.checkDocument("Looking too the water. Fixing your writing typoss.")
for error in errors:
 print "%s error for: %s **%s**" % (error.type, error.precontext, error.string)
 print "some suggestions: %s" % (", ".join(error.suggestions),)

预期输出:

grammar error for: Looking **too the**
some suggestions: to the
spelling error for: writing **typoss**
some suggestions: typos

可以在您自己的机器上运行服务器应用程序,建议使用 4 GB RAM。

语言工具

https://pypi.python.org/pypi/language-check

>>> import language_check
>>> tool = language_check.LanguageTool('en-US')
>>> text = 'A sentence with a error in the Hitchhiker’s Guide tot he Galaxy'
>>> matches = tool.check(text)

>>> matches[0].fromy, matches[0].fromx
(0, 16)
>>> matches[0].ruleId, matches[0].replacements
('EN_A_VS_AN', ['an'])
>>> matches[1].fromy, matches[1].fromx
(0, 50)
>>> matches[1].ruleId, matches[1].replacements
('TOT_HE', ['to the'])

>>> print(matches[1])
Line 1, column 51, Rule ID: TOT_HE[1]
Message: Did you mean 'to the'?
Suggestion: to the
...

>>> language_check.correct(text, matches)
'A sentence with an error in the Hitchhiker’s Guide to the Galaxy'

也可以私下运行服务器端。

此外,是一个用于 Ginger 的 hacky(屏幕抓取)库,可以说是目前最精美的免费语法检查选项之一。

微软Word

应该可以编写 Microsoft Word 脚本并使用其语法检查功能。

更多的

Open Office 网站上有一个语法检查器的精选列表。帕特里克在评论中指出。

于 2014-08-05T15:22:37.197 回答
24

查看NLTK。它们支持可用于解析句子的语法。您可以定义语法,或使用提供的语法以及上下文无关的解析器。如果句子解析,则它具有有效的语法;如果不是,那么它不会。这些语法可能没有最广泛的覆盖范围(例如,它可能不知道如何处理像 StackOverflow 这样的单词),但是这种方法将允许您具体说明语法中的有效或无效。 NLTK 书的第 8 章涵盖了解析,应该解释你需要知道的内容。

另一种方法是为覆盖范围广泛的解析器(如斯坦福解析器C&C )编写一个 python 接口。这些是统计解析器,即使他们以前没有看过所有的单词或所有的语法结构,也能理解句子。缺点是有时解析器仍然会返回一个语法错误的句子的解析,因为它会使用统计数据来做出最好的猜测。

所以,这真的取决于你的目标是什么。如果您想非常精确地控制被认为是语法的内容,请使用带有 NLTK 的上下文无关解析器。如果您想要稳健性和广泛的覆盖范围,请使用统计解析器。

于 2012-04-20T19:34:56.393 回答
6

其他一些答案提到了最大的开源语法检查器LanguageTool 。直到现在,它还没有可靠的、最新的 Python 端口。

我推荐language_tool_python,一个支持 Python 3 和最新版本的 Java 和 LanguageTool 的语法检查器。它是唯一最新的、免费的 Python 语法检查器。(完全披露,我制作了这个库)

于 2020-05-07T19:46:08.073 回答
5

我建议使用language-tool-python。例如:

import language_tool_python
tool = language_tool_python.LanguageTool('en-US')

text = "Your the best but their are allso  good !"
matches = tool.check(text)
len(matches)

我们得到:

4

我们可以看看它发现的 4 个问题:

第 1 期:

matches[0]

我们得到:

Match({'ruleId': 'YOUR_YOU_RE', 'message': 'Did you mean "You\'re"?', 'replacements': ["You're"], 'context': 'Your the best but their are allso  good !', 'offset': 0, 'errorLength': 4, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})

第二期:

matches[1]

我们得到:

Match({'ruleId': 'THEIR_IS', 'message': 'Did you mean "there"?', 'replacements': ['there'], 'context': 'Your the best but their are allso  good !', 'offset': 18, 'errorLength': 5, 'category': 'CONFUSED_WORDS', 'ruleIssueType': 'misspelling'})

第三期: matches[2] 我们得到:

Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['also', 'all so'], 'context': 'Your the best but their are allso  good !', 'offset': 28, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})

第四期:

matches[3]

我们得到:

Match({'ruleId': 'WHITESPACE_RULE', 'message': 'Possible typo: you repeated a whitespace', 'replacements': [' '], 'context': 'Your the best but their are allso  good!', 'offset': 33, 'errorLength': 2, 'category': 'TYPOGRAPHY', 'ruleIssueType': 'whitespace'})

如果您正在寻找更详细的示例,可以查看Predictive Hacks的相关帖子

于 2020-10-12T08:44:53.350 回答