treeprettyprinter.doctest 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. .. Copyright (C) 2001-2020 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ========================================================
  4. Unit tests for nltk.treeprettyprinter.TreePrettyPrinter
  5. ========================================================
  6. >>> from nltk.tree import Tree
  7. >>> from nltk.treeprettyprinter import TreePrettyPrinter
  8. Tree nr 2170 from nltk.corpus.treebank:
  9. >>> tree = Tree.fromstring(
  10. ... '(S (NP-SBJ (PRP I)) (VP (VBP feel) (ADJP-PRD (RB pretty) '
  11. ... '(JJ good)) (PP-CLR (IN about) (NP (PRP it)))) (. .))')
  12. >>> tpp = TreePrettyPrinter(tree)
  13. >>> print(tpp.text())
  14. S
  15. __________________________|_____________________
  16. | VP |
  17. | ____________________|___________ |
  18. | | | PP-CLR |
  19. | | | _____|_____ |
  20. NP-SBJ | ADJP-PRD | NP |
  21. | | _______|______ | | |
  22. PRP VBP RB JJ IN PRP .
  23. | | | | | | |
  24. I feel pretty good about it .
  25. >>> print(tpp.text(unicodelines=True))
  26. S
  27. ┌──────────────────────────┼─────────────────────┐
  28. │ VP │
  29. │ ┌─────────────┬──────┴───────────┐ │
  30. │ │ │ PP-CLR │
  31. │ │ │ ┌─────┴─────┐ │
  32. NP-SBJ │ ADJP-PRD │ NP │
  33. │ │ ┌───────┴──────┐ │ │ │
  34. PRP VBP RB JJ IN PRP .
  35. │ │ │ │ │ │ │
  36. I feel pretty good about it .
  37. A tree with long labels:
  38. >>> tree = Tree.fromstring(
  39. ... '(sentence (plural-noun-phrase (plural-noun Superconductors)) '
  40. ... '(verb-phrase (plural-verb conduct) '
  41. ... '(noun-phrase (singular-noun electricity))))')
  42. >>> tpp = TreePrettyPrinter(tree)
  43. >>> print(tpp.text(abbreviate=8, nodedist=2))
  44. sentence
  45. __________|__________
  46. | verb-phr.
  47. | __________|__________
  48. plural-n. | noun-phr.
  49. | | |
  50. plural-n. plural-v. singular.
  51. | | |
  52. Supercon. conduct electric.
  53. >>> print(tpp.text(maxwidth=8, nodedist=2))
  54. sentence
  55. _________|________
  56. | verb-
  57. | phrase
  58. | ________|_________
  59. plural- | noun-
  60. noun- | phrase
  61. phrase | |
  62. | | |
  63. plural- plural- singular-
  64. noun verb noun
  65. | | |
  66. Supercon conduct electric
  67. ductors ity
  68. A discontinuous tree:
  69. >>> tree = Tree.fromstring(
  70. ... '(top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) (inf (verb 6) '
  71. ... '(conj (inf (pp (prep 2) (np (det 3) (noun 4))) (verb 7)) (inf (verb 9)) '
  72. ... '(vg 10) (inf (verb 11)))))) (punct 12))', read_leaf=int)
  73. >>> sentence = ('Ze had met haar moeder kunnen gaan winkelen ,'
  74. ... ' zwemmen of terrassen .'.split())
  75. >>> tpp = TreePrettyPrinter(tree, sentence)
  76. >>> print(tpp.text())
  77. top
  78. _____|______________________________________________
  79. smain | |
  80. _______________________________|_____ | |
  81. | | inf | |
  82. | | _____|____ | |
  83. | | | inf | |
  84. | | | ____|_____ | |
  85. | | | | conj | |
  86. | | _____ | ___ | _________|______ | __________________ |
  87. | | inf | | | | | | |
  88. | | _________|_____ | ___ | _________ | | | | |
  89. | | pp | | | | | | | |
  90. | | ____|____ | | | | | | | |
  91. | | | np | | | | inf | inf |
  92. | | | ____|____ | | | | | | | |
  93. noun verb prep det noun verb verb verb punct verb vg verb punct
  94. | | | | | | | | | | | | |
  95. Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .
  96. >>> print(tpp.text(unicodelines=True))
  97. top
  98. ┌─────┴──────────────────┬───────────────────────────┐
  99. smain │ │
  100. ┌────┬──────────────────────────┴─────┐ │ │
  101. │ │ inf │ │
  102. │ │ ┌─────┴────┐ │ │
  103. │ │ │ inf │ │
  104. │ │ │ ┌────┴─────┐ │ │
  105. │ │ │ │ conj │ │
  106. │ │ ┌───── │ ─── │ ─────────┴────── │ ─────┬─────┬──────┐ │
  107. │ │ inf │ │ │ │ │ │ │
  108. │ │ ┌─────────┴───── │ ─── │ ─────────┐ │ │ │ │ │
  109. │ │ pp │ │ │ │ │ │ │ │
  110. │ │ ┌────┴────┐ │ │ │ │ │ │ │ │
  111. │ │ │ np │ │ │ │ inf │ inf │
  112. │ │ │ ┌────┴────┐ │ │ │ │ │ │ │ │
  113. noun verb prep det noun verb verb verb punct verb vg verb punct
  114. │ │ │ │ │ │ │ │ │ │ │ │ │
  115. Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .