Exemplo n.º 1
0
def spans_intersection_from_sexp(sexp1, sexp2):
    """
    computes the span difference between two s-expressions
    """
    tree1 = nltk.bracket_parse(sexp1)
    tree2 = nltk.bracket_parse(sexp2)
    return spans_intersection(tree1, tree2)
Exemplo n.º 2
0
def spans_intersection_from_sexp(sexp1, sexp2):
    """
    computes the span difference between two s-expressions
    """
    tree1 = nltk.bracket_parse(sexp1)
    tree2 = nltk.bracket_parse(sexp2)
    return spans_intersection(tree1, tree2)
Exemplo n.º 3
0
 def load(self):
     from nltk import bracket_parse
     s = "(S (NP (N I)) (VP1 (VP2 (V saw) (NP (ART the) (N man))) (PP (P with) (NP (ART a) (N telescope)))))"
     t = bracket_parse(s)
     self.root = TreeModel.importNltkLiteTree(t)
     self.treeview.setData(self.root, 'label')
     self.vp1 = self.root.children[1]
     self.pp = self.vp1.children[1]
     self.vp2 = self.vp1.children[0]
     self.stage = 0
     self.button2.setText('splice VP1')
Exemplo n.º 4
0
        def load(self):
            from nltk import bracket_parse

            s = "(S (NP (N I)) (VP1 (VP2 (V saw) (NP (ART the) (N man))) (PP (P with) (NP (ART a) (N telescope)))))"
            t = bracket_parse(s)
            self.root = TreeModel.importNltkLiteTree(t)
            self.treeview.setData(self.root, "label")
            self.vp1 = self.root.children[1]
            self.pp = self.vp1.children[1]
            self.vp2 = self.vp1.children[0]
            self.stage = 0
            self.button2.setText("splice VP1")
Exemplo n.º 5
0
def demo():
    """
    A demonstration showing how each tree transform can be used.
    """  
      
    from nltk.draw.tree import draw_trees
    from nltk import treetransforms, bracket_parse
    from copy import deepcopy
    
    # original tree from WSJ bracketed text
    sentence = """(TOP
  (S
    (S
      (VP
        (VBN Turned)
        (ADVP (RB loose))
        (PP
          (IN in)
          (NP
            (NP (NNP Shane) (NNP Longman) (POS 's))
            (NN trading)
            (NN room)))))
    (, ,)
    (NP (DT the) (NN yuppie) (NNS dealers))
    (VP (AUX do) (NP (NP (RB little)) (ADJP (RB right))))
    (. .)))"""
    tree = bracket_parse(sentence)
    
    # collapse subtrees with only one child
    collapsedTree = deepcopy(tree)
    treetransforms.collapse_unary(collapsedTree)
    
    # convert the tree to CNF
    cnfTree = deepcopy(collapsedTree)
    treetransforms.chomsky_normal_form(cnfTree)
    
    # convert the tree to CNF with parent annotation (one level) and horizontal smoothing of order two
    parentTree = deepcopy(collapsedTree)
    treetransforms.chomsky_normal_form(parentTree, horzMarkov=2, vertMarkov=1)
    
    # convert the tree back to its original form (used to make CYK results comparable)
    original = deepcopy(parentTree)
    treetransforms.un_chomsky_normal_form(original)
    
    # convert tree back to bracketed text
    sentence2 = original.pprint()
    print sentence
    print sentence2
    print "Sentences the same? ", sentence == sentence2
    
    draw_trees(tree, collapsedTree, cnfTree, parentTree, original)
Exemplo n.º 6
0
def demo():
    """
    A demonstration showing how each tree transform can be used.
    """  
      
    from nltk.draw.tree import draw_trees
    from nltk import treetransforms, bracket_parse
    from copy import deepcopy
    
    # original tree from WSJ bracketed text
    sentence = """(TOP
  (S
    (S
      (VP
        (VBN Turned)
        (ADVP (RB loose))
        (PP
          (IN in)
          (NP
            (NP (NNP Shane) (NNP Longman) (POS 's))
            (NN trading)
            (NN room)))))
    (, ,)
    (NP (DT the) (NN yuppie) (NNS dealers))
    (VP (AUX do) (NP (NP (RB little)) (ADJP (RB right))))
    (. .)))"""
    tree = bracket_parse(sentence)
    
    # collapse subtrees with only one child
    collapsedTree = deepcopy(tree)
    treetransforms.collapse_unary(collapsedTree)
    
    # convert the tree to CNF
    cnfTree = deepcopy(collapsedTree)
    treetransforms.chomsky_normal_form(cnfTree)
    
    # convert the tree to CNF with parent annotation (one level) and horizontal smoothing of order two
    parentTree = deepcopy(collapsedTree)
    treetransforms.chomsky_normal_form(parentTree, horzMarkov=2, vertMarkov=1)
    
    # convert the tree back to its original form (used to make CYK results comparable)
    original = deepcopy(parentTree)
    treetransforms.un_chomsky_normal_form(original)
    
    # convert tree back to bracketed text
    sentence2 = original.pprint()
    print sentence
    print sentence2
    print "Sentences the same? ", sentence == sentence2
    
    draw_trees(tree, collapsedTree, cnfTree, parentTree, original)
Exemplo n.º 7
0
def main():
    import sys
    from optparse import OptionParser, OptionGroup
    usage = """%%prog [options] [grammar_file]""" % globals()

    opts = OptionParser(usage=usage)
    opts.add_option("-c", "--components",
        action="store_true", dest="show_components", default=0,
        help="show hole semantics components")
    opts.add_option("-r", "--raw",
        action="store_true", dest="show_raw", default=0,
        help="show the raw hole semantics expression")
    opts.add_option("-d", "--drawtrees",
        action="store_true", dest="draw_trees", default=0,
        help="show formula trees in a GUI window")
    opts.add_option("-v", "--verbose",
        action="count", dest="verbosity", default=0,
        help="show more information during parse")

    (options, args) = opts.parse_args()

    if len(args) > 0:
        filename = args[0]
    else:
        filename = 'grammars/hole.fcfg'

    print 'Reading grammar file', filename
    #grammar = data.load(filename)
    parser = load_earley(filename, trace=options.verbosity)

    # Prompt the user for a sentence.
    print 'Sentence: ',
    line = sys.stdin.readline()[:-1]

    # Parse the sentence.
    tokens = line.split()
    trees = parser.nbest_parse(tokens)
    print 'Got %d different parses' % len(trees)

    for tree in trees:
        # Get the semantic feature from the top of the parse tree.
        sem = tree[0].node['sem'].simplify()

        # Skolemise away all quantifiers.  All variables become unique.
        sem = sem.skolemise()

        # Reparse the semantic representation from its bracketed string format.
        # I find this uniform structure easier to handle.  It also makes the
        # code mostly independent of the lambda calculus classes.
        usr = bracket_parse(str(sem))

        # Break the hole semantics representation down into its components
        # i.e. holes, labels, formula fragments and constraints.
        hole_sem = HoleSemantics(usr)

        # Maybe print the raw semantic representation.
        if options.show_raw:
            print
            print 'Raw expression'
            print usr

        # Maybe show the details of the semantic representation.
        if options.show_components:
            print
            print 'Holes:       ', hole_sem.holes
            print 'Labels:      ', hole_sem.labels
            print 'Constraints: ', hole_sem.constraints
            print 'Top hole:    ', hole_sem.top_hole
            print 'Top labels:  ', hole_sem.top_most_labels
            print 'Fragments:'
            for (l,f) in hole_sem.fragments.items():
                print '\t%s: %s' % (l, f)

        # Find all the possible ways to plug the formulas together.
        pluggings = hole_sem.pluggings()

        # Build FOL formula trees using the pluggings.
        trees = map(hole_sem.formula_tree, pluggings)

        # Print out the formulas in a textual format.
        n = 1
        for tree in trees:
            print
            print '%d. %s' % (n, tree)
            n += 1

        # Maybe draw the formulas as trees.
        if options.draw_trees:
            draw_trees(*trees)

        print
        print 'Done.'
Exemplo n.º 8
0
def spans_from_sexp(sexp):
    """
    computes the set of spans given the s-expression
    """
    tree = nltk.bracket_parse(sexp)
    return spans_from_tree(tree)
Exemplo n.º 9
0
def tree_from_sexp(sexp):
    """
    parses an s-expression into a tree
    """
    return nltk.bracket_parse(sexp)
Exemplo n.º 10
0
def spans_from_sexp(sexp):
    """
    computes the set of spans given the s-expression
    """
    tree = nltk.bracket_parse(sexp)
    return spans_from_tree(tree)
Exemplo n.º 11
0
def tree_from_sexp(sexp):
    """
    parses an s-expression into a tree
    """
    return nltk.bracket_parse(sexp)
Exemplo n.º 12
0
def draw_file(filename):
    f = open(filename)
    tree_str = f.read().strip()
    f.close()
    tree = nltk.bracket_parse(tree_str)
    view = TreeView([tree])
Exemplo n.º 13
0
def draw_file(filename):
    f = open(filename)
    tree_str = f.read().strip()
    f.close()
    tree = nltk.bracket_parse(tree_str)
    view = TreeView([tree])
Exemplo n.º 14
0
def draw_file1(filename):
    f = open(filename)
    for tree_str in f.readlines():        
        tree = nltk.bracket_parse(tree_str.strip())
        view = TreeView([tree])
    f.close()
Exemplo n.º 15
0
def demo():
    import random

    def fill(cw):
        cw['fill'] = '#%06d' % random.randint(0, 999999)

    cf = CanvasFrame(width=550, height=450, closeenough=2)

    t = bracket_parse('''
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))''')

    tc = TreeWidget(cf.canvas(),
                    t,
                    draggable=1,
                    node_font=('helvetica', -14, 'bold'),
                    leaf_font=('helvetica', -12, 'italic'),
                    roof_fill='white',
                    roof_color='black',
                    leaf_color='green4',
                    node_color='blue2')
    cf.add_widget(tc, 10, 10)

    def boxit(canvas, text):
        big = ('helvetica', -16, 'bold')
        return BoxWidget(canvas,
                         TextWidget(canvas, text, font=big),
                         fill='green')

    def ovalit(canvas, text):
        return OvalWidget(canvas, TextWidget(canvas, text), fill='cyan')

    treetok = bracket_parse('(S (NP this tree) (VP (V is) (AdjP shapeable)))')
    tc2 = TreeWidget(cf.canvas(), treetok, boxit, ovalit, shapeable=1)

    def color(node):
        node['color'] = '#%04d00' % random.randint(0, 9999)

    def color2(treeseg):
        treeseg.node()['fill'] = '#%06d' % random.randint(0, 9999)
        treeseg.node().child()['color'] = 'white'

    tc.bind_click_trees(tc.toggle_collapsed)
    tc2.bind_click_trees(tc2.toggle_collapsed)
    tc.bind_click_nodes(color, 3)
    tc2.expanded_tree(1).bind_click(color2, 3)
    tc2.expanded_tree().bind_click(color2, 3)

    paren = ParenWidget(cf.canvas(), tc2)
    cf.add_widget(paren, tc.bbox()[2] + 10, 10)

    tree3 = bracket_parse('''
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment)))))''')
    tc3 = tree_to_treesegment(cf.canvas(),
                              tree3,
                              tree_color='green4',
                              tree_xspace=2,
                              tree_width=2)
    tc3['draggable'] = 1
    cf.add_widget(tc3, 10, tc.bbox()[3] + 10)

    def orientswitch(treewidget):
        if treewidget['orientation'] == 'horizontal':
            treewidget.expanded_tree(1, 1).subtrees()[0].set_text('vertical')
            treewidget.collapsed_tree(1, 1).subtrees()[0].set_text('vertical')
            treewidget.collapsed_tree(1).subtrees()[1].set_text('vertical')
            treewidget.collapsed_tree().subtrees()[3].set_text('vertical')
            treewidget['orientation'] = 'vertical'
        else:
            treewidget.expanded_tree(1, 1).subtrees()[0].set_text('horizontal')
            treewidget.collapsed_tree(1,
                                      1).subtrees()[0].set_text('horizontal')
            treewidget.collapsed_tree(1).subtrees()[1].set_text('horizontal')
            treewidget.collapsed_tree().subtrees()[3].set_text('horizontal')
            treewidget['orientation'] = 'horizontal'

    text = """
Try clicking, right clicking, and dragging
different elements of each of the trees.
The top-left tree is a TreeWidget built from
a Tree.  The top-right is a TreeWidget built
from a Tree, using non-default widget
constructors for the nodes & leaves (BoxWidget
and OvalWidget).  The bottom-left tree is
built from tree_to_treesegment."""
    twidget = TextWidget(cf.canvas(), text.strip())
    textbox = BoxWidget(cf.canvas(), twidget, fill='white', draggable=1)
    cf.add_widget(textbox, tc3.bbox()[2] + 10, tc2.bbox()[3] + 10)

    tree4 = bracket_parse('(S (NP this tree) (VP (V is) (Adj horizontal)))')
    tc4 = TreeWidget(cf.canvas(),
                     tree4,
                     draggable=1,
                     line_color='brown2',
                     roof_color='brown2',
                     node_font=('helvetica', -12, 'bold'),
                     node_color='brown4',
                     orientation='horizontal')
    tc4.manage()
    cf.add_widget(tc4, tc3.bbox()[2] + 10, textbox.bbox()[3] + 10)
    tc4.bind_click(orientswitch)
    tc4.bind_click_trees(tc4.toggle_collapsed, 3)

    # Run mainloop
    cf.mainloop()