nltk cant interpret grammar category PRP$ output by stanford parser

前端 未结 1 1130
暗喜
暗喜 2021-01-24 06:53

I want to generate sentence from grammar retrived from stanford parser, but NLTK is not able to interpret PRP$.

from nltk.parse.stanford import StanfordParser
f         


        
相关标签:
1条回答
  • 2021-01-24 07:36

    ValueError: Unable to parse line 11: NP -> PRP$ NNS
    Expected a nonterminal, found: $ NNS

    I've no idea why you are trying to combine a hand-built CFG with the output of the Stanford parser, but here's a solution to this problem:

    I quick inspection of nltk/grammar.py shows that $ is not a legal character for a non-terminal name. This can be easily corrected by patching the module like this:

    import nltk
    import re
    nltk.grammar._STANDARD_NONTERM_RE = re.compile('( [\w/][\w$/^<>-]* ) \s*', re.VERBOSE)
    

    In the above I just added $ to the regexp that you'll find in nltk/grammar.py. You can then create and use grammars that have $ in their productions:

    grammar = nltk.grammar.CFG.fromstring("NP -> PRP$ NNS")
    
    0 讨论(0)
提交回复
热议问题