We flattened the Deep Tree, keeping only the lowest level subtrees. But here we can save the highest level subtrees.
Code # 1: Let`s
t.height () & lt;
children.extend (t.pos ())
children.append ( Tree (t.label (), t.pos ()))
Tree (tree.label (), children)
Code # 2: Rating
, treebank.parsed_sents () [
" Shallow Tree: "
, shallow_tree (treebank.parsed_sents () [
Deep Tree: (S (NP-SBJ (NP (NNP Pierre) (NNP Vinken)) (,,) (ADJP (NP (CD 61) (NNS years)) (JJ old)) (, ,)) (VP (MD will) (VP (VB join) (NP (DT the) (NN board)) (PP-CLR (IN as) (NP (DT a) (JJ nonexecutive) (NN director))) (NP-TMP (NNP Nov.) (CD 29)))) (. .)) Shallow Tree: Tree (`S`, [Tree (` NP-SBJ`, [(`Pierre`,` NNP`), (`Vinken`,` NNP`), (`,`, `,` ), (`61`,` CD`), (`years`,` NNS`), (`old`,` JJ`), (`,`, `,`)]), Tree (`VP`, [(`will`,` MD`), (`join`,` VB`), (`the`,` DT`), (`board`,` NN`), (`as`,` IN`) , (`a`,` DT`), (`nonexecutive`,` JJ`), (`director`,` NN`), (`Nov.`,` NNP`), (`29`,` CD` )]), (`.`,` .`)])
How does it work?
- The shallow_tree () function creates new children trees by iterating over each of the top-level subtrees.
- The subtree is replaced with a list of its child tags if the height () of the subtree is less than 3.
- If the children of the tree are tagged part-of-speech leaves, all the rest of the subtrees are replaced by the new tree.
- This removes all nested subtrees while keeping the top-level subtrees.
Code # 3: height strong >
"height of tree:" code >
treebank.parsed_sents () [
]. height ())
"height of shallow tree: "
shallow_tree (treebank. parsed_sents () [
]). height ())
height of tree: 7 height of shallow tree: 3