[Tutor] about NLTK parser

Magnus Lyckå magnus@thinkware.se
Sat May 24 11:24:06 2003


At 07:58 2003-05-24 -0700, Abdirizak abdi wrote:
> >>> import nltk.parser
> >>> import nltk.token
> >>> parser = nltk.parser.ShiftReduceParser(G)
> >>> tokenizer = nltk.token.WSTokenizer()
> >>> tokens = tokenizer.tokenize('the man saw a woman in the garden')
> >>> parse = parser.parse(tokens)

The docs say:
Return the best parse for the given text, or None if no parses are available.

I know nothing about NLTK, but is your grammer right? Is it able
to parse that sentence? You have left that part out. parse() obviously
returned None here. It seems to me that you just wasn't able to parse
the tokens, and that it behaves as it should then.

You might need something like

parse = parser.parse(tokens)
if parse:
     parse.draw()
else:
     print "Can't parse", tokens

> >>> parse.draw()  <----------------even if parser module loaded
>                                             it is giving me an error
>

--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program