def get_semantics_from_parse_tree(parse_tree_string): """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.""" parse_tree = Tree.parse(parse_tree_string) # Split clauses to handle them separately split_clause_dict = frames.split_clauses(parse_tree) # Activize clauses for key, (clause, conjunction) in split_clause_dict.items(): activized_clause = frames.activize_clause(clause) split_clause_dict[key] = (activized_clause, conjunction) result_list = [] for (clause, conjunction) in split_clause_dict.values(): # Split conjunctions and duplicate arguments if necessary split_tree_dict = frames.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 frames.is_existential(str(tree)): tag_list.append('ex') # Transformational grammar stuff tree = frames.existential_there_insertion(tree) tree = frames.invert_clause(tree) tree = frames.wh_movement(tree) # Regex for finding verbs verb_finder = re.compile(r'(?<=VB[ DGNPZ]) *\w*(?=\))') # Get the lemma of the verb for searching verbnet verbs = (word.strip().lower() for word in verb_finder.findall(str(tree))) # Create VFOs for each verb, then match them to the parse tree for verb in verbs: lemmatized_verb = morphy(verb,'v') vfo_list = frames.create_VerbFrameObjects(lemmatized_verb) match_list = [] for vfo in vfo_list: match = vfo.match_parse(tree) if match: match_list.append((match, vfo.classid)) (best_match, sense) = frames.pick_best_match(match_list) if not best_match is None: result_list.append((best_match, tree, tag_list, sense)) return result_list
def extract_frames_from_parse(parse_tree_string): """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 # Split clauses to handle them separately split_clause_dict = frames.split_clauses(parse_tree) # Activize clauses for key, (clause, conjunction) in split_clause_dict.items(): activized_clause = frames.activize_clause(clause) split_clause_dict[key] = (activized_clause, conjunction) for (clause, conjunction) in split_clause_dict.values(): # Split conjunctions and duplicate arguments if necessary split_tree_dict = frames.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 frames.is_existential(str(tree)): tag_list.append('ex') # Transformational grammar stuff tree = frames.existential_there_insertion(tree) tree = frames.invert_clause(tree) tree = frames.wh_movement(tree) if EXTRACT_DEBUG: print 'Transformed tree:' print str(tree) verbs = frames.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 = frames.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) = frames.pick_best_match(match_list) if EXTRACT_DEBUG: print 'Chose: ' if best_match: 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