Esempio n. 1
0
def process(parse,verbose=True):
    """Show the steps of transformation for a parse."""
    # Original parse
    parse_tree = Tree.parse(parse)
    print_parse(parse_tree, "Parse")

    frames = extract_frames_from_parse(parse, verbose=verbose)
    if verbose:
        print
        for frame in frames:
            print frame.pprint()
            if frame.condition:
                print "Condition:"
                print frame.condition.pprint()
        print

    # Bail if no frames matched
    if not frames:
        if verbose: print "No frames matched."
        return

    # Extract semantic structures
    semantic_structures = create_semantic_structures(frames)
    if semantic_structures:
        if verbose: print semantic_structures
        return semantic_structures
    else:
        if verbose: print "No semantic structures returned."
Esempio n. 2
0
def process(parse):
    """Show the steps of transformation for a parse."""
    # Original parse
    parse = Tree.parse(parse)
    print_parse(parse, "Parse")

    split_trees = split_conjunctions(parse)
    for split_parse, conjunction in split_trees.values():
        for subtree in split_parse:
            print_if_diff(subtree, parse, "Subtree ({})".format(conjunction))
Esempio n. 3
0
def process(parse):
    """Show the steps of transformation for a parse."""
    # Original parse
    parse = Tree.parse(parse)
    print_parse(parse, "Parse")

    split_trees = split_conjunctions(parse)
    for split_parse, conjunction in split_trees.values():
        for subtree in split_parse:
            print_if_diff(subtree, parse, "Subtree ({})".format(conjunction))
Esempio n. 4
0
def extract_frames_from_parse(parse_tree_string, verbose=False):
    """Take a string representing the parse tree as input, and print the
    semantic parse. The result list consists of a list of tuples, with each
    tuple containing the VerbNet frame and its associated tree."""
    result_list = []

    # In case we're handed an bad string, bail somewhat gracefully
    try:
        parse_tree = Tree.parse(parse_tree_string)
    except ValueError:
        print "Warning: semantics could not parse tree", repr(
            parse_tree_string)
        return result_list

    # Temporarily (and maybe permanently) disabled features:
    # 1. Clause splitting: we have not found any example where it does something
    # 2. Activizing clauses: for now, passives do not matter.

    # Split clauses to handle them separately
    #split_clause_dict = split_clauses(parse_tree)

    # Activize clauses
    #for key, (clause, conjunction) in split_clause_dict.items():
    #    activized_clause = activize_clause(clause)
    #    split_clause_dict[key] = (activized_clause, conjunction)

    #for (clause, conjunction) in split_clause_dict.values():
    for clause, conjunction in ((parse_tree, ''), ):
        # Split conjunctions and duplicate arguments if necessary
        split_tree_dict = split_conjunctions(clause)

        if conjunction != '':
            result_list.append(conjunction)

        for (split_tree, conjunction) in split_tree_dict.values():
            if conjunction != '':
                result_list.append(conjunction)

            for tree in split_tree:
                tag_list = []

                # Store whether there was an existential there
                if is_existential(str(tree)):
                    tag_list.append('ex')

                # Transformational grammar stuff
                tree = existential_there_insertion(tree)
                tree = invert_clause(tree)
                tree = wh_movement(tree)

                if EXTRACT_DEBUG:
                    print 'Transformed tree:'
                    print str(tree)

                verbs = find_verbs(tree)

                # Create VFOs for each verb, then match them to the parse tree
                for verb, negation in verbs:
                    lemmatized_verb = morphy(verb, 'v')
                    vfo_list = create_VerbFrameObjects(lemmatized_verb)
                    match_list = []

                    if EXTRACT_DEBUG:
                        print 'VFO list for %s:' % verb
                        print '\n'.join(
                            str(vfo.frame_list) for vfo in vfo_list)

                    for vfo in vfo_list:
                        match = vfo.match_parse(tree)

                        if match:
                            if EXTRACT_DEBUG:
                                print 'Matched:'
                                print '\t', str(vfo.frame_list)
                                print 'with'
                                print '\t', str(tree)
                            match_list.append((match, vfo.classid))

                    if EXTRACT_DEBUG:
                        print 'Match list:'

                        for m in match_list:
                            print 'Sense:', m[1]
                            for a, b in m[0].items():
                                print a, str(b)
                            print '\n\n'

                    (best_match, sense) = pick_best_match(match_list)

                    if EXTRACT_DEBUG:
                        print 'Chose: '
                        if best_match:
                            print sense
                            for a, b in best_match.items():
                                print a, str(b)
                        else:
                            print str(None)
                        print '\n\n'
                    if not best_match is None:
                        result_list.append((best_match, tree, tag_list, sense,
                                            verb, negation))

    return result_list
Esempio n. 5
0
def extract_frames_from_parse(parse_tree_string, verbose=False):
    """Take a string representing the parse tree as input, and print the
    semantic parse. The result list consists of a list of tuples, with each
    tuple containing the VerbNet frame and its associated tree."""
    result_list = []

    # In case we're handed an bad string, bail somewhat gracefully
    try:
        parse_tree = Tree.parse(parse_tree_string)
    except ValueError:
        print "Warning: semantics could not parse tree", repr(parse_tree_string)
        return result_list

    # Temporarily (and maybe permanently) disabled features:
    # 1. Clause splitting: we have not found any example where it does something
    # 2. Activizing clauses: for now, passives do not matter.

    # Split clauses to handle them separately
    #split_clause_dict = split_clauses(parse_tree)

    # Activize clauses
    #for key, (clause, conjunction) in split_clause_dict.items():
    #    activized_clause = activize_clause(clause)
    #    split_clause_dict[key] = (activized_clause, conjunction)

    #for (clause, conjunction) in split_clause_dict.values():
    for clause, conjunction in ((parse_tree, ''),):
        # Split conjunctions and duplicate arguments if necessary
        split_tree_dict = split_conjunctions(clause)

        if conjunction != '':
            result_list.append(conjunction)

        for (split_tree, conjunction) in split_tree_dict.values():
            if conjunction != '':
                result_list.append(conjunction)

            for tree in split_tree:
                tag_list = []

                # Store whether there was an existential there
                if is_existential(str(tree)):
                    tag_list.append('ex')

                # Transformational grammar stuff
                tree = existential_there_insertion(tree)
                tree = invert_clause(tree)
                tree = wh_movement(tree)

                if EXTRACT_DEBUG:
                    print 'Transformed tree:'
                    print str(tree)

                verbs = find_verbs(tree)

                # Create VFOs for each verb, then match them to the parse tree
                for verb, negation in verbs:
                    lemmatized_verb = morphy(verb, 'v')
                    vfo_list = create_VerbFrameObjects(lemmatized_verb)
                    match_list = []

                    if EXTRACT_DEBUG:
                        print 'VFO list for %s:' % verb
                        print '\n'.join(str(vfo.frame_list) for vfo in vfo_list)

                    for vfo in vfo_list:
                        match = vfo.match_parse(tree)

                        if match:
                            if EXTRACT_DEBUG:
                                print 'Matched:'
                                print '\t', str(vfo.frame_list)
                                print 'with'
                                print '\t', str(tree)
                            match_list.append((match, vfo.classid))

                    if EXTRACT_DEBUG:
                        print 'Match list:'

                        for m in match_list:
                            print 'Sense:', m[1]
                            for a, b in m[0].items():
                                print a, str(b)
                            print '\n\n'

                    (best_match, sense) = pick_best_match(match_list)

                    if EXTRACT_DEBUG:
                        print 'Chose: '
                        if best_match:
                            print sense
                            for a, b in best_match.items():
                                print a, str(b)
                        else:
                            print str(None)
                        print '\n\n'
                    if not best_match is None:
                        result_list.append((best_match, tree, tag_list, sense, verb, negation))

    return result_list
Esempio n. 6
0
def extract_frames_from_parse(parse_tree_string, verbose=False):
    """Take a string representing the parse tree as input, and print the
    semantic parse. The result list consists of a list of tuples, with each
    tuple containing the VerbNet frame and its associated tree."""
    # TODO: Use verbose argument
    result_list = []

    # In case we're handed an bad string, bail somewhat gracefully
    try:
        parse_tree = Tree.parse(parse_tree_string)
    except ValueError:
        print "Error: semantics could not parse tree", repr(parse_tree_string)
        return result_list

    # Temporarily (and maybe permanently) disabled features:
    # 1. Clause splitting: we have not found any example where it does something
    # 2. Activizing clauses: for now, passives do not matter.

    # Split clauses to handle them separately
    # split_clause_dict = split_clauses(parse_tree)

    # Activize clauses
    # for key, (clause, conjunction) in split_clause_dict.items():
    #     activized_clause = activize_clause(clause)
    #     split_clause_dict[key] = (activized_clause, conjunction)

    # TODO: This strange loop is because split_clauses may not work
    # for (clause, conjunction) in split_clause_dict.values():
    for clause, conjunction in ((parse_tree, ''), ):
        # Split conjunctions and duplicate arguments if necessary
        #split_tree_dict = split_conjunctions(clause)
        split_tree_dict = split_conjunctions_sparse(clause)

        #DEBUG
        #print "SPLIT TREE DICT: ---------------"
        #print split_tree_dict

        if conjunction != '':
            result_list.append(conjunction)

        for (split_tree, conjunction) in split_tree_dict.values():
            for tree in split_tree:
                if conjunction and verbose:
                    print "Subtree ({}):".format(conjunction)
                    print tree.pprint(force_multiline=True)
                # TODO: Deactivated for now
                # Store whether there was an existential there
                # if is_existential(str(tree)):
                #     tag_list.append('ex')

                # Transformational grammar stuff
                orig_tree = tree
                tree = assertion_filter(tree)
                tree = existential_there_insertion(tree)
                tree = invert_clause(tree)
                tree = wh_movement(tree)

                if verbose and tree != orig_tree:
                    print 'Transformed tree:'
                    print tree.pprint(force_multiline=True)

                match = match_verb(tree, verbose=verbose)
                if match:
                    result_list.append(match)

    return result_list