NLTK Context Free Grammar Genaration

后端 未结 4 1647
花落未央
花落未央 2021-02-06 01:01

I\'m working on a non-English parser with Unicode characters. For that, I decided to use NLTK.

But it requires a predefined context-free grammar as below:



        
4条回答
  •  执念已碎
    2021-02-06 01:35

    Maybe you're looking for CFG.fromstring() (formerly parse_cfg())?

    From Chapter 7 of the NLTK book (updated to NLTK 3.0):

    > grammar = nltk.CFG.fromstring("""
     S -> NP VP
     VP -> V NP | V NP PP
     V -> "saw" | "ate"
     NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
     Det -> "a" | "an" | "the" | "my"
     N -> "dog" | "cat" | "cookie" | "park"
     PP -> P NP
     P -> "in" | "on" | "by" | "with"
     """)
    
    > sent = "Mary saw Bob".split()
    > rd_parser = nltk.RecursiveDescentParser(grammar)
    > for p in rd_parser.parse(sent):
          print p
    (S (NP Mary) (VP (V saw) (NP Bob)))
    

提交回复
热议问题