You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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)