grammar.doctest 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ===============
  4. Grammar Parsing
  5. ===============
  6. Grammars can be parsed from strings:
  7. >>> from nltk import CFG
  8. >>> grammar = CFG.fromstring("""
  9. ... S -> NP VP
  10. ... PP -> P NP
  11. ... NP -> Det N | NP PP
  12. ... VP -> V NP | VP PP
  13. ... Det -> 'a' | 'the'
  14. ... N -> 'dog' | 'cat'
  15. ... V -> 'chased' | 'sat'
  16. ... P -> 'on' | 'in'
  17. ... """)
  18. >>> grammar
  19. <Grammar with 14 productions>
  20. >>> grammar.start()
  21. S
  22. >>> grammar.productions() # doctest: +NORMALIZE_WHITESPACE
  23. [S -> NP VP, PP -> P NP, NP -> Det N, NP -> NP PP, VP -> V NP, VP -> VP PP,
  24. Det -> 'a', Det -> 'the', N -> 'dog', N -> 'cat', V -> 'chased', V -> 'sat',
  25. P -> 'on', P -> 'in']
  26. Probabilistic CFGs:
  27. >>> from nltk import PCFG
  28. >>> toy_pcfg1 = PCFG.fromstring("""
  29. ... S -> NP VP [1.0]
  30. ... NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15]
  31. ... Det -> 'the' [0.8] | 'my' [0.2]
  32. ... N -> 'man' [0.5] | 'telescope' [0.5]
  33. ... VP -> VP PP [0.1] | V NP [0.7] | V [0.2]
  34. ... V -> 'ate' [0.35] | 'saw' [0.65]
  35. ... PP -> P NP [1.0]
  36. ... P -> 'with' [0.61] | 'under' [0.39]
  37. ... """)
  38. Chomsky Normal Form grammar (Test for bug 474)
  39. >>> g = CFG.fromstring("VP^<TOP> -> VBP NP^<VP-TOP>")
  40. >>> g.productions()[0].lhs()
  41. VP^<TOP>