Prerequisite: Flattening Deep Tree
We have flattened a Deep Tree by only keeping the lowest level subtrees. But here we can keep the highest level subtrees instead.
Code #1 : Lets’ understand
Code #2 : Evaluating
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 it works ?
- shallow_tree() function creates new child trees by iterating over each of the top-level subtrees.
- The subtree is replaced by a list of its part-of-speech tagged children, if the height() of a subtree is less than 3.
- If children of a tree are the part-of-speech tagged leaves, the All other subtrees are replaced by a new Tree.
- Thus, eliminates all the nested subtrees while still retaining the top-level subtrees.
Code #3 : height
height of tree : 7 height of shallow tree :3
- copy in Python (Deep Copy and Shallow Copy)
- Creating a Pandas DataFrame
- Creating a Pandas Series
- Python | Creating a 3D List
- Django - Creating apps | Set - 2
- Django - Creating Apps | Set - 1
- Creating views on Pandas DataFrame | Set - 2
- Creating views on Pandas DataFrame
- Creating a Proxy Webserver in Python | Set 2
- Python | Creating Multidimensional dictionary
- Creating a Pandas Series from Dictionary
- Creating a dataframe using Excel files
- Creating a Pandas Series from Lists
- Creating a dataframe from Pandas series
- Creating linear kernel SVM in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.