Last active
December 11, 2015 05:39
-
-
Save admackin/4554074 to your computer and use it in GitHub Desktop.
Pretty-print PTB trees from a file
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
#!/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