Commit 90682aee by Steven Bird

svn/trunk@6032

parent 9cfd0689
import nltk
def parse(sent, grammar):
gr = nltk.cfg.parse_cfg(grammar)
parser = nltk.ChartParser(gr, nltk.parse.TD_STRATEGY)
trees = parser.nbest_parse(sent.split())
nltk.draw.draw_trees(*trees)
grammar = """
S -> NP VP
VP -> V NP | VP PP
NP -> Det N | NP PP
PP -> P NP
NP -> 'I'
Det -> 'the' | 'a' | 'my'
N -> 'elephant' | 'pajamas' | 'man' | 'park' | 'telescope'
V -> 'shot' | 'saw'
P -> 'in' | 'on' | 'with'
"""
sent = 'I shot the elephant in my pajamas'
parse(sent, grammar)
import nltk
def parse(sent, grammar):
gr = nltk.cfg.parse_cfg(grammar)
parser = nltk.ChartParser(gr, nltk.parse.TD_STRATEGY)
trees = parser.nbest_parse(sent.split())
nltk.draw.draw_trees(*trees)
grammar = """
S -> NP VP
VP -> V NP | VP PP
NP -> Det N | NP PP
PP -> P NP
NP -> 'I'
Det -> 'the' | 'a' | 'my'
N -> 'elephant' | 'pajamas' | 'man' | 'park' | 'telescope'
V -> 'shot' | 'saw'
P -> 'in' | 'on' | 'with'
"""
sent = 'I saw the man in the park with a telescope'
parse(sent, grammar)
import nltk
def parse(sent, grammar):
gr = nltk.cfg.parse_cfg(grammar)
parser = nltk.ChartParser(gr, nltk.parse.TD_STRATEGY)
trees = parser.nbest_parse(sent.split())
nltk.draw.draw_trees(*trees)
grammar = """
S -> NP V NP
NP -> NP Sbar
Sbar -> NP V
NP -> 'fish' | 'police'
V -> 'fish' | 'police'
"""
sent = 'police police police police police police police police police'
parse(sent, grammar)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment