Created
August 13, 2014 22:43
-
-
Save bogsio/dffc807cefb22afbe8e7 to your computer and use it in GitHub Desktop.
Parsing utilities
This file contains 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
from nltk import Tree | |
import logging | |
def corpus2trees(text): | |
""" Parse the corpus and return a list of Trees """ | |
rawparses = text.split("\n\n") | |
trees = [] | |
for rp in rawparses: | |
if not rp.strip(): | |
continue | |
try: | |
t = Tree.parse(rp) | |
trees.append(t) | |
except ValueError: | |
logging.error('Malformed parse: "%s"' % rp) | |
return trees | |
def trees2productions(trees): | |
""" Transform list of Trees to a list of productions """ | |
productions = [] | |
for t in trees: | |
productions += t.productions() | |
return productions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment