You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
8.2 KiB
Plaintext

.. Copyright (C) 2001-2020 NLTK Project
.. For license information, see LICENSE.TXT
========================================================
Unit tests for nltk.treeprettyprinter.TreePrettyPrinter
========================================================
>>> from nltk.tree import Tree
>>> from nltk.treeprettyprinter import TreePrettyPrinter
Tree nr 2170 from nltk.corpus.treebank:
>>> tree = Tree.fromstring(
... '(S (NP-SBJ (PRP I)) (VP (VBP feel) (ADJP-PRD (RB pretty) '
... '(JJ good)) (PP-CLR (IN about) (NP (PRP it)))) (. .))')
>>> tpp = TreePrettyPrinter(tree)
>>> print(tpp.text())
S
__________________________|_____________________
| VP |
| ____________________|___________ |
| | | PP-CLR |
| | | _____|_____ |
NP-SBJ | ADJP-PRD | NP |
| | _______|______ | | |
PRP VBP RB JJ IN PRP .
| | | | | | |
I feel pretty good about it .
>>> print(tpp.text(unicodelines=True))
S
┌──────────────────────────┼─────────────────────┐
│ VP │
│ ┌─────────────┬──────┴───────────┐ │
│ │ │ PP-CLR │
│ │ │ ┌─────┴─────┐ │
NP-SBJ │ ADJP-PRD │ NP │
│ │ ┌───────┴──────┐ │ │ │
PRP VBP RB JJ IN PRP .
│ │ │ │ │ │ │
I feel pretty good about it .
A tree with long labels:
>>> tree = Tree.fromstring(
... '(sentence (plural-noun-phrase (plural-noun Superconductors)) '
... '(verb-phrase (plural-verb conduct) '
... '(noun-phrase (singular-noun electricity))))')
>>> tpp = TreePrettyPrinter(tree)
>>> print(tpp.text(abbreviate=8, nodedist=2))
sentence
__________|__________
| verb-phr.
| __________|__________
plural-n. | noun-phr.
| | |
plural-n. plural-v. singular.
| | |
Supercon. conduct electric.
>>> print(tpp.text(maxwidth=8, nodedist=2))
sentence
_________|________
| verb-
| phrase
| ________|_________
plural- | noun-
noun- | phrase
phrase | |
| | |
plural- plural- singular-
noun verb noun
| | |
Supercon conduct electric
ductors ity
A discontinuous tree:
>>> tree = Tree.fromstring(
... '(top (punct 8) (smain (noun 0) (verb 1) (inf (verb 5) (inf (verb 6) '
... '(conj (inf (pp (prep 2) (np (det 3) (noun 4))) (verb 7)) (inf (verb 9)) '
... '(vg 10) (inf (verb 11)))))) (punct 12))', read_leaf=int)
>>> sentence = ('Ze had met haar moeder kunnen gaan winkelen ,'
... ' zwemmen of terrassen .'.split())
>>> tpp = TreePrettyPrinter(tree, sentence)
>>> print(tpp.text())
top
_____|______________________________________________
smain | |
_______________________________|_____ | |
| | inf | |
| | _____|____ | |
| | | inf | |
| | | ____|_____ | |
| | | | conj | |
| | _____ | ___ | _________|______ | __________________ |
| | inf | | | | | | |
| | _________|_____ | ___ | _________ | | | | |
| | pp | | | | | | | |
| | ____|____ | | | | | | | |
| | | np | | | | inf | inf |
| | | ____|____ | | | | | | | |
noun verb prep det noun verb verb verb punct verb vg verb punct
| | | | | | | | | | | | |
Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .
>>> print(tpp.text(unicodelines=True))
top
┌─────┴──────────────────┬───────────────────────────┐
smain │ │
┌────┬──────────────────────────┴─────┐ │ │
│ │ inf │ │
│ │ ┌─────┴────┐ │ │
│ │ │ inf │ │
│ │ │ ┌────┴─────┐ │ │
│ │ │ │ conj │ │
│ │ ┌───── │ ─── │ ─────────┴────── │ ─────┬─────┬──────┐ │
│ │ inf │ │ │ │ │ │ │
│ │ ┌─────────┴───── │ ─── │ ─────────┐ │ │ │ │ │
│ │ pp │ │ │ │ │ │ │ │
│ │ ┌────┴────┐ │ │ │ │ │ │ │ │
│ │ │ np │ │ │ │ inf │ inf │
│ │ │ ┌────┴────┐ │ │ │ │ │ │ │ │
noun verb prep det noun verb verb verb punct verb vg verb punct
│ │ │ │ │ │ │ │ │ │ │ │ │
Ze had met haar moeder kunnen gaan winkelen , zwemmen of terrassen .