Skip to content

Instantly share code, notes, and snippets.

@admackin
Last active December 11, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save admackin/4554074 to your computer and use it in GitHub Desktop.
Save admackin/4554074 to your computer and use it in GitHub Desktop.
Pretty-print PTB trees from a file
#!/usr/bin/env python
import sys
from contextlib import nested
from nltk import tree
def pretty_print(filenames):
for f in filenames:
with nested(open(f), open(f + '.pretty', 'w')) as (inf, outf):
for line in inf:
try:
t = tree.Tree.parse(line)
pretty = t.pprint(margin=50) + '\n'
except ValueError:
pretty = line
outf.write(pretty)
def main():
pretty_print(sys.argv)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment