How to check whether a sentence is valid in Python?
Examples:
I love Stackoverflow - Correct
I Stackoverflow love - Incorrect
I would suggest the language-tool-python. For example:
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)
and we get:
4
We can have a look at the 4 issues that it found:
1st Issue:
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 !', 'offset': 0, 'errorLength': 4, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})
2nd Issue:
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, 'errorLength': 5, 'category': 'CONFUSED_WORDS', 'ruleIssueType': 'misspelling'})
3rd Issue:
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 their are allso good !', 'offset': 28, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling'})
4th Issue:
matches[3]
and we get:
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'})
If you are looking for a more detailed example you can have a look at the related post of Predictive Hacks