In [1]:
from pcfg_parse_gen import Pcfg, PcfgGenerator, CkyParse
import nltk
def print_tree(tree_string):
tree_string = tree_string.strip()
tree = nltk.Tree.fromstring(tree_string)
tree.pretty_print()
def draw_tree(tree_string):
tree_string = tree_string.strip()
tree = nltk.Tree.fromstring(tree_string)
tree.draw()
In [2]:
parse_gram = Pcfg(["S1.gr","S2.gr","Vocab.gr"])
In [3]:
parser = CkyParse(parse_gram, beamsize=0.00001)
ce, trees = parser.parse_sentences(["Arthur is the king ."])
print("-cross entropy: {}".format(ce))
for tree_string in trees:
print_tree(tree_string)
In [4]:
ce, trees = parser.parse_sentences(["five strangers are at the Round Table ."])
print("-cross entropy: {}".format(ce))
for tree_string in trees:
print_tree(tree_string)
Use parse_file
to parse a file of sentences.
In [5]:
ce, trees = parser.parse_file('example_sentences.txt')
print("-cross entropy: {}".format(ce))
In [6]:
gen_gram = Pcfg(["S1.gr","Vocab.gr"])
In [7]:
gen = PcfgGenerator(gen_gram)
for _ in range(20):
print(" ".join(gen.generate()))