How to check whether a sentence is valid in Python?
Examples:
I love Stackoverflow - Correct
I Stackoverflow love - Incorrect
How to check whether a sentence is valid in Python?
Examples:
I love Stackoverflow - Correct
I Stackoverflow love - Incorrect
There are various Web Services providing automated proofreading and grammar checking. Some have a Python library to simplify querying.
As far as I can tell, most of those tools (certainly After the Deadline and LanguageTool) are rule based. The checked text is compared with a large set of rules describing common errors. If a rule matches, the software calls it an error. If a rule does not match, the software does nothing (it cannot detect errors it does not have rules for).
After the Deadline
Expected output:
It is possible to run the server application on your own machine, 4 GB RAM is recommended.
LanguageTool
https://pypi.python.org/pypi/language-check
It is also possible to run the server side privately.
Ginger
Additionally, this is a hacky (screen scraping) library for Ginger, arguably one of the most polished free-to-use grammar checking options out there.
Microsoft Word
It should be possible to script Microsoft Word and use its grammar checking functionality.
More
There is a curated list of grammar checkers on Open Office website. Noted in comments by Patrick.
Check out NLTK. They have support for grammars that you can use to parse your sentence. You can define a grammar, or use one that is provided, along with a context-free parser. If the sentence parses, then it has valid grammar; if not, then it doesn't. These grammars may not have the widest coverage (eg, it might not know how to handle a word like StackOverflow), but this approach will allow you to say specifically what is valid or invalid in the grammar. Chapter 8 of the NLTK book covers parsing and should explain what you need to know.
An alternative would be to write a python interface to a wide-coverage parser (like the Stanford parser or C&C). These are statistical parsers that will be able to understand sentences even if they haven't seen all the words or all the grammatical constructions before. The downside is that sometimes the parser will still return a parse for a sentence with bad grammar because it will use the statistics to make the best guess possible.
So, it really depends on exactly what your goal is. If you want very precise control over what is considered grammatical, use a context-free parser with NLTK. If you want robustness and wide-coverage, use a statistical parser.