def find_node_by_index_range(graph, start, end): """ find a node which covers the span if no such node exists returns False """ # acc = accessibility(graph) ret = False nodeSpan = -1 for node in graph.nodes(): if node.is_implicit(): continue minInd, maxInd = get_min_max_span(graph=graph, node=node) if (start >= minInd) and (end <= maxInd): curSpan = maxInd - minInd if ((not ret) or (nodeSpan > curSpan)) or ((curSpan == nodeSpan) and list( set.intersection(set([w.index for w in node.surface_form]), set(range(start, end + 1))))): ret = node nodeSpan = curSpan flag = bool( list( set.intersection(set([ w.index for w in ret.text ]), set(range( start, end + 1))))) #indicates if the returned node covers the entity return (ret, flag)
def getPropositions(self, outputType): ret = [] for topNode in [n for n in self.nodes() if n.features.get("top", False)]: # if "dups" in topNode.features: dups = topNode.features.get("dups", []) allDups = reduce(lambda x, y:list(x) + list(y), dups, []) rest = [n for n in self.neighbors(topNode) if n not in allDups] neigboursList = [] for combination in product(*dups): curNeigbourList = [] ls = list(combination) + rest for curNode in ls: curNeigbourList.append((self.edge_label((topNode, curNode)), curNode)) neigboursList.append(curNeigbourList) for nlist in neigboursList: argList = [] all_neighbours = [n for _, n in nlist] for k, curNeighbour in sorted(nlist, key=lambda(k, n):get_min_max_span(self, n)[0]): curExclude = [n for n in all_neighbours if n != curNeighbour] + [topNode] argList.append([k, subgraph_to_string(self, curNeighbour, exclude=curExclude)]) curProp = Proposition(topNode.get_original_text(), argList, outputType) ret.append(curProp) return ret
def do_conj(self): edges = find_edges(self, lambda((u, v)):self.edge_label((u, v)).startswith("conj_"))# and (not u.isPredicate) and (not v.isPredicate)) nodes = set([u for (u,_) in edges]) for conj1 in nodes: curStartIndex = conj1.minIndex()+1 curNeighbours = conj1.neighbors() isModifier = (not bool([father for father in self.incidents(conj1) if not self.is_aux_edge((father.uid, conj1.uid))])) and bool(self.incidents(conj1)) for rel in [rel for rel in curNeighbours if rel.startswith("conj_")]: marker = rel.split("conj_")[1] markerNode = newNode.Node(text=[Word(curStartIndex+1,marker)], #TODO: how to find marker's index isPredicate=True, features={"conj":True}, gr=self) #decide how to connect it to the rest of the graph, based on its type if isModifier: duplicate_all_incidents(gr=self, source=conj1, target=markerNode) else: for father in self.incidents(conj1): for conj2 in curNeighbours[rel]: duplicateEdge(graph=self, orig=((father,conj1)), new=((father,conj2))) duplicateEdge(graph=self, orig=((father,conj1)), new=((father,markerNode))) if conj1.isPredicate: for neighbor in self.neighbors(conj1): if get_min_max_span(self, neighbor)[0] < curStartIndex: for conj2 in curNeighbours[rel]: if (self.edge_label((conj1,neighbor)) == SOURCE_LABEL) or (not self.is_aux_edge((conj1.uid, neighbor.uid))): duplicateEdge(graph=self, orig=(conj1,neighbor), new=(conj2,neighbor)) # create the coordination construction, headed by the marker self.add_edge(edge=(markerNode,conj1),label=rel) for conj2 in curNeighbours[rel]: self.del_edge((conj1,conj2)) self.add_edge(edge=(markerNode,conj2),label=rel) if conj1.isPredicate: conj2.isPredicate = conj1.isPredicate conj1.surface_form = [w for w in conj1.surface_form if (w not in conj2.surface_form) and (w not in conj1.text) ] for w in conj1.text: if w not in conj1.surface_form: conj1.surface_form.append(w) if conj1.features.get("conjType",False): conj1.text = [w for w in conj1.text if w.index not in conj1.features["conjType"][1]] self.types.add(rel)
def fixProps(self): """ Fix cases of conjunction of properties in indefinite nominals """ edges = find_edges(graph = self.gr, filterFunc = lambda (u,v): (not isDefinite(u)) and (isProp(v)or isRcmodProp(v)) and (not v.is_prenominal())) for counter,(u,v) in enumerate(sorted(edges,key= lambda (_,propNode):get_min_max_span(self.gr,propNode)[0])): curLabel = self.gr.edge_label((u,v)) self.gr.del_edge((u,v)) self.gr.add_edge(edge =(u,v), label = ";".join([curLabel,str(counter+1)]))
def fixProps(self): """ Fix cases of conjunction of properties in indefinite nominals """ edges = find_edges(graph=self.gr, filterFunc=lambda (u, v): (not isDefinite(u)) and (isProp(v) or isRcmodProp(v)) and (not v.is_prenominal())) for counter, (u, v) in enumerate( sorted(edges, key=lambda (_, propNode): get_min_max_span(self.gr, propNode)[0])): curLabel = self.gr.edge_label((u, v)) self.gr.del_edge((u, v)) self.gr.add_edge(edge=(u, v), label=";".join([curLabel, str(counter + 1)]))
def find_node_by_index_range(graph, start, end): """ find a node which covers the span if no such node exists returns False """ # acc = accessibility(graph) ret = False nodeSpan = -1 for node in graph.nodes(): if node.is_implicit(): continue minInd,maxInd = get_min_max_span(graph=graph, node=node) if (start >= minInd) and (end <= maxInd): curSpan = maxInd-minInd if ((not ret) or (nodeSpan > curSpan)) or ((curSpan==nodeSpan) and list(set.intersection(set([w.index for w in node.surface_form]), set(range(start,end+1))))): ret = node nodeSpan = curSpan flag = bool(list(set.intersection(set([w.index for w in ret.text]), set(range(start,end+1))))) #indicates if the returned node covers the entity return (ret,flag)