sentence/grammar checking with python

2,242 views
Skip to first unread message

fxcreation

unread,
Jul 17, 2011, 6:44:51 AM7/17/11
to nltk-users
Hi there....please help me...i need to use python in my research
project and i'm still learning python...

i want to develop a system that can do grammar/sentence checking and
produce the syntax tree..i read and follow instruction provided in
NLTK python..it can parsed the syntax and produce the tree as below:
+++++++++++++++++++++++++++++++++
>>> import nltk
>>> grammar1 = nltk.parse_cfg("""
S -> NP VP
VP -> V NP
V -> "saw"
NP -> "Mary" | "Bob"
""")

>>> sent = "Mary saw Bob".split()
>>> rd_parser = nltk.RecursiveDescentParser(grammar1)
>>> for tree in rd_parser.nbest_parse(sent):
print tree

(S (NP Mary) (VP (V saw) (NP Bob)))

+++++++++++++++++++++++++++++++++++++++++++++

the system will produce the tree if the sentence is match with the
rules/grammar. but if i put the sentence like " Mary saw Bob saw" it
will return blank/ none output...
is there any ideas to produce a sentence or suggestion that can tell
me or user to make correction by providing the error and suggestion,
for example:

#"Mary saw Bob saw" is not match with the grammar rules...maybe u can
change the sentence into "Mary saw Bob" according to the rules NP+VP..

i really need help for this and really appreaciate if there anyone can
help me...tq

Richard Careaga

unread,
Jul 17, 2011, 8:37:30 AM7/17/11
to nltk-...@googlegroups.com
Here's a crude hack at trapping the error (suggesting the correction in a general way doesn't seem trivial):

import nltk
grammar1 = nltk.parse_cfg("""
S -> NP VP
VP -> V NP
V -> "saw"
NP -> "Mary" | "Bob"
""")
sent = "Mary saw Bob saw".split()
rd_parser = nltk.RecursiveDescentParser(grammar1)
if len(rd_parser.nbest_parse(sent)) > 0:
    for tree in rd_parser.nbest_parse(sent):
         print(tree)
else:
    print("Error: %s is not a match for the grammar rule %s") % (sent, grammar1)

But it might be more fruitful to add a chunker (see http://streamhacker.com/2009/02/23/chunk-extraction-with-nltk/) that would isolate the well formed NPs from the surrounding stray POS. Then, if needed, you can deal with the free floating tokens.
Reply all
Reply to author
Forward
0 new messages