Last active
February 11, 2024 14:46
-
-
Save interrogator/89eb901f28923ec847fd to your computer and use it in GitHub Desktop.
visualising a parse tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def quicktree(sentence): | |
"""Parse a sentence and return a visual representation""" | |
from nltk import Tree | |
from nltk.draw.util import CanvasFrame | |
from nltk.draw import TreeWidget | |
from stat_parser import Parser | |
from IPython.display import display | |
from IPython.display import Image | |
parser = Parser() | |
parsed = parser.parse(sentence) | |
cf = CanvasFrame() | |
tc = TreeWidget(cf.canvas(),parsed) | |
cf.add_widget(tc,10,10) # (10,10) offsets | |
cf.print_to_file('tree.ps') | |
cf.destroy() | |
! convert tree.ps tree.png | |
! rm tree.ps | |
return Image(filename='tree.png') | |
! rm tree.png | |
quicktree("This is a parse tree.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SELECT name, age FROM customers WHERE age > 25;