如何在Python中检查一个句子是否合法?
示例:
I love Stackoverflow - Correct
I Stackoverflow love - Incorrect
有各种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'
服务器端也可以私有运行。
此外,this 是一个粗糙(屏幕抓取)的库,用于 Ginger,可以说是目前最成熟的免费语法检查选项之一。
应该可以编写 Microsoft Word 脚本并使用其语法检查功能。
在 Open Office 网站上有一个由 Patrick 在评论中提到的经过筛选的语法检查器列表curated list of grammar checkers on Open Office website。
language-check
需要 Java。 - Pradhyum R请查看NLTK,他们支持语法分析,你可以定义一种语法或使用已有的语法和一个无上下文的解析器。如果句子能够被解析,则其具有有效的语法;否则则没有。这些语法可能并不具有最广泛的覆盖面(例如,它可能不知道如何处理像 StackOverflow 这样的单词),但这种方法将允许你明确说明语法中什么是有效或无效的。NLTK 书的第8章涵盖了解析,并应该解释你需要知道的内容。Chapter 8
另一种方式是编写一个Python接口,连接大范围覆盖的解析器(例如Stanford 解析器 或 C&C)。这些是统计解析器,即使它们没有看到所有单词或所有语法结构,也能够理解句子。缺点是有时解析器仍会返回具有错误语法的句子的解析结果,因为它会根据统计数据做出最佳猜测。
所以,这真的取决于你的目标是什么。如果你想对被认为具有语法正确性的内容具有非常精确的控制能力,请使用 NLTK 的无上下文解析器。如果你想要鲁棒性和广泛覆盖范围,请使用统计解析器。
其他回答中提到了LanguageTool,它是最大的开源语法检查器。直到现在,它都没有一个可靠、最新的Python端口。
我推荐language_tool_python,这是一个支持Python 3、最新版本的Java和LanguageTool的语法检查器。它是唯一一个最新的、免费的Python语法检查器。(完全披露,我制作了这个库)
我建议使用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
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的相关帖子。
步骤1
pip install Caribe
import Caribe as cb
sentence="I is playing football"
output=cb.caribe_corrector(sentence)
print(output)
根据我的研究,我在这里分享我的分析。
为了更准确和专业的语法和拼写检查,您可以考虑使用专用库和工具,如pyaspeller
、pyspellchecker
或language-tool-python
。这些库是专门设计用于语法和拼写检查任务,与像GPT-3这样的通用语言模型相比,可能提供更高的准确性。
步骤1
pip install pyaspeller
pip install language-tool-python
步骤2
from pyaspeller import YandexSpeller
import language_tool_python
def error_correcting(text):
tool = language_tool_python.LanguageTool('en-US')
datasets = tool.correct(text)
return datasets
def error_correct_pyspeller(sample_text):
speller = YandexSpeller()
fixed = speller.spelled(sample_text)
return fixed
input_text = """
This is a sample paragrap with some incorrect spellings and grammer mistaks.
It's importnt to check larje text chunks for accurcy and improve readibility.
Gingerit is a great library for such tasks, and it can handl larje text as well.
Let's try processing this larje text using Gingerit.
"""
output_data = error_correcting(input_text)
print(output_data)
output_text = error_correct_pyspeller(input_text)
print(output_text)