Change language

NLP | Smoothing deep tree

| |

Code # 1: Class for deep tree alignment

from nltk.tree import Tree

 

def flatten_childtrees (trees):

children = []

 

for t in trees:

if t.height () & lt;  3 :

children.extend (t.pos ())

 

elif t.height () = = 3 :

children.append (Tree (t.label (), t.pos ()))

 

else :

children.extend (

flatten_childtrees ())

 

return children

 

 

def flatten_deeptree (tree):

return Tree (tree.label (), 

flatten_childtrees ())

 

Code # 2 : Rating flatten_deeptree()

from nltk.corpus import treebank

from transforms import   flatten_deeptree

 

print ( "Deep Tree:" , treebank.parsed_sents () [ 0 ])

 

print ( "Flattened Tree:"

flatten_deeptree (treebank.parsed_sents () [ 0 ])) 

Output:

 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)))) (. .)) Flattened Tree: Tree (’S’, [Tree (’ NP’, [(’Pierre’,’ NNP’), (’Vinken’,’ NNP’)]), (’,’, ’,’ ), Tree (’NP’, [(’ 61’, ’CD’), (’ years’, ’NNS’)]), (’ old’, ’JJ’), (’, ’,’, ’) , (’will’,’ MD’), (’join’,’ VB’), Tree (’NP’, [(’ the’, ’DT’), (’ board’, ’NN’)]), (’as’,’ IN’), Tree (’NP’, [(’ a’, ’DT’), (’ nonexecutive’, ’JJ’), (’ director’, ’NN’)]), Tree (’NP-TMP’, [(’ Nov.’, ’NNP’), (’ 29’, ’CD’)]), (’ .’, ’.’)]) 

В the result is a much flatter tree containing only NP phrases. Words that are not part of the NP phrase are separated

How does this work?

  • flatten_deeptree (): returns a new tree from the given tree, calling flatten_childtrees () on each of the children of the given tree.
  • flatten_childtrees (): recursively deepens into the tree until it finds child trees whose height () is equal to or less than 3.

Code # 3: height ()

from nltk.corpus import treebank

from transforms import flatten_deeptree

 

from nltk.tree import Tree

 

print ( "Height:"

Tree ( ’NNP’ , [ ’ Pierre’ ]). height ())

  

print ( "Height:" , Tree (

’NP’ , [Tree ( ’NNP’ , [ ’ Pierre’ ]), 

Tree ( ’NNP’ , [ ’ Vinken’ ])]). height ())

Output:

 Height: 2 Height: 3 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically