Reputation: 11
I have a sentnece tree like this:
[{'ROOT': ['S']}, {'S': ['VPS']}, {'VPS': ['N', 'VP']}, {'N': ['#']}, {'VP': ['PP', 'MV']}, {'PP': ['PREP_EZ', 'N']}, {'PREP_EZ': ['#']}, {'N': ['#']}, {'MV': ['N', 'V']}, {'N': ['#']}, {'V': ['#']}]
How I can print its corresponding tree without using nltk.tree? (for some reasons I cannot use the nltk library)
I expect something like this: The desired output
Upvotes: 1
Views: 83
Reputation: 10263
Ok let's see if this can help you:
class Node:
def __init__(self, value, children=[]):
self.value = value
self.children = children
def __str__(self, level=0):
str = " " * level + " |--" + self.value + "\n"
for child in self.children:
str += child.__str__(level + 1)
return str
tree = Node('ROOT', children=[
Node('S', children=[
Node('VPS', children=[
Node('N'),
Node('VP', children=[
Node('PP', children=[
Node('PREP_EZ')
]),
Node('MV', children=[
Node('N'),
Node('V')
])
])
])
])
])
>>> print(tree)
|--'ROOT'
|--'S'
|--'VPS'
|--'N'
|--'VP'
|--'PP'
|--'PREP_EZ'
|--'MV'
|--'N'
|--'V'
Upvotes: 0