emilmont / pyStatParser

Simple Python Statistical Parser
Apache License 2.0
109 stars 49 forks source link

Is nltk.grammar.CFG supported? #11

Open loretoparisi opened 7 years ago

loretoparisi commented 7 years ago

Supposed to have

from nltk.grammar import CFG
    grammar = CFG.fromstring("""
    # Grammatical productions.
        S -> NP VP
        NP -> Det N PP | Det N
        VP -> V NP PP | V NP | V
        PP -> P NP
    # Lexical productions.
        NP -> 'I'
        Det -> 'the' | 'a'
        N -> 'man' | 'park' | 'dog' | 'telescope'
        V -> 'ate' | 'saw'
        P

will pyStatParser output a CFG string for the grammar?

loretoparisi commented 7 years ago

Like getting the PCFG grammar

grammar=parser.pcfg

The problem is that I get this error

AttributeError: PCFG instance has no attribute 'productions'

when passing this grammar to the RecursiveDescentApp NLTK example: http://www.nltk.org/_modules/nltk/app/rdparser_app.html

like

    parser = Parser()
    text="How can the net amount of entropy of the universe be massively decreased?"
    sent=text.split()
    tree = parser.parse(text)
    print tree
    grammar = parser.pcfg

    RecursiveDescentApp(grammar, sent).mainloop()

that fails with

Traceback (most recent call last):
  File "wordstree.py", line 906, in <module>
    app()
  File "wordstree.py", line 903, in app
    RecursiveDescentApp(grammar, sent).mainloop()
  File "wordstree.py", line 120, in __init__
    self._init_grammar(self._top)
  File "wordstree.py", line 166, in _init_grammar
    self._productions = list(self._parser.grammar().productions())