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()

"""
Explanation: Homework: Competitive Grammar Writing
End of explanation
"""


parse_gram = Pcfg(["S1.gr","S2.gr","Vocab.gr"])

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)

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)

"""
Explanation: Parsing sentences with your grammar
While you are developing your grammar you should parse with your grammar both example sentences and samples from your and other grammars.
End of explanation
"""


ce, trees = parser.parse_file('example_sentences.txt')
print("-cross entropy: {}".format(ce))

"""
Explanation: Use parse_file to parse a file of sentences.
End of explanation
"""


gen_gram = Pcfg(["S1.gr","Vocab.gr"])

gen = PcfgGenerator(gen_gram)
for _ in range(20):
    print(" ".join(gen.generate()))

"""
Explanation: Generating sentences with your grammar
While you are developing your grammar you should generate sentences with your grammar
to check what your grammar is doing. Try to write your grammars to that it will 
generate hard to parse sentences.
End of explanation
"""


