def process_edge(self, edge, depth): hg = self.system.get_hg(self) if not edge.is_atom(): ct = edge.connector_type() if ct[:2] == 'Pd': pred = edge[0] if (len(edge) > 2 and deep_lemma(hg, pred).root() in CONFLICT_PRED_LEMMAS): subjects = edge.edges_with_argrole('s') objects = edge.edges_with_argrole('o') if len(subjects) == 1 and len(objects) == 1: subject = strip_concept(subjects[0]) obj = strip_concept(objects[0]) if (subject and obj and has_proper_concept(subject) and has_proper_concept(obj)): actor_orig = main_coref(hg, subject) actor_targ = main_coref(hg, obj) conflict_edge = hedge( ('conflict/P/.', actor_orig, actor_targ, edge)) if (is_actor(hg, actor_orig) and is_actor(hg, actor_targ)): yield create_op(conflict_edge) for wedge in self._topics( hg, actor_orig, actor_targ, edge): yield wedge self.conflicts += 1
def actors(hg): global actor_degrees if actor_degrees is None: actor_degrees = Counter() for conflict_edge in hg.search('(conflict/p/. * * *)'): actor1 = main_coref(hg, conflict_edge[1]) actor2 = main_coref(hg, conflict_edge[2]) actor_degrees[actor1] += 1 actor_degrees[actor2] += 1 return actor_degrees
def find_actors(hg, edge): actors = set() if is_actor(hg, edge): actors.add(main_coref(hg, edge)) if not edge.is_atom(): for item in edge: actors |= find_actors(hg, item) return actors
def find_actors(hg, edge): """Returns set of all coreferences to actors found in the edge.""" actors = set() if is_actor(hg, edge): actors.add(main_coref(hg, edge)) if not edge.is_atom(): for item in edge: actors |= find_actors(hg, item) return actors
def input_edge(self, edge): if not edge.is_atom(): ct = edge.connector_type() if ct[:2] == 'pd': subjects = edge.edges_with_argrole('s') if len(subjects) == 1: subject = strip_concept(subjects[0]) if subject and has_proper_concept(subject): actor = main_coref(self.hg, subject) self.actor_counter[actor] += 1
def conflict_topics(hg): global all_topics if all_topics is None: all_topics = Counter() topics = Counter() for conflict_edge in hg.search('(conflict/p/. * * *)'): actor1 = main_coref(hg, conflict_edge[1]) actor2 = main_coref(hg, conflict_edge[2]) if is_valid(hg, actor1) and is_valid(hg, actor2): edge = conflict_edge[3] for item in edge[1:]: if item.type()[0] == 's': if item[0].to_str() in conflict_topic_triggers: for concept in all_concepts(item[1]): topic = main_coref(hg, concept) topics[topic] += 1 for topic, weight in topics.most_common(): if weight > 1 and has_proper_concept(topic): all_topics[topic] = weight return all_topics
def input_edge(self, edge): if not edge.is_atom(): ct = edge.connector_type() if ct[:2] == 'pd': pred = edge[0] if (len(edge) > 2 and deep_lemma(self.hg, pred).root() in CONFLICT_PRED_LEMMAS): subjects = edge.edges_with_argrole('s') objects = edge.edges_with_argrole('o') if len(subjects) == 1 and len(objects) == 1: subject = find_concept(subjects[0]) obj = find_concept(objects[0]) if (subject and obj and is_proper_concept(subject) and is_proper_concept(obj)): actor_orig = main_coref(self.hg, subject) actor_targ = main_coref(self.hg, obj) if (is_actor(self.hg, actor_orig) and is_actor(self.hg, actor_targ)): self.add(('conflict/p/.', actor_orig, actor_targ, edge)) self._topics(actor_orig, actor_targ, edge) self.conflicts += 1
def conflicts_by_topic(hg, topic): conflicts = defaultdict(list) for conflict_edge in hg.search('(conflict/p/. * * *)'): actor1 = main_coref(hg, conflict_edge[1]) actor2 = main_coref(hg, conflict_edge[2]) if is_valid(hg, actor1) and is_valid(hg, actor2): edge = conflict_edge[3] for item in edge[1:]: if item.type()[0] == 's': if item[0].to_str() in conflict_topic_triggers: topics = list(main_coref(hg, concept) for concept in all_concepts(item[1])) if topic in topics: all_topics = conflict_topics(hg) other_topics = set() for t in topics: if t in all_topics and t != topic: other_topics.add(t) text = hg.get_str_attribute(edge, 'text') conflict_data = {'text': text, 'other_topics': other_topics} conflicts[(actor1, actor2)].append(conflict_data) return conflicts
def input_edge(self, edge): if not edge.is_atom(): ct = edge.connector_type() if ct[:2] == 'pd': pred = edge[0] if (len(edge) > 2 and deep_lemma(self.hg, pred).root() in CLAIM_PRED_LEMMAS): subjects = edge.edges_with_argrole('s') claims = edge.edges_with_argrole('r') if len(subjects) == 1 and len(claims) >= 1: subject = strip_concept(subjects[0]) if subject and has_proper_concept(subject): actor = main_coref(self.hg, subjects[0]) if self._is_actor(actor): for claim in claims: self._process_claim(actor, claim, edge)
def input_edge(self, edge): if not edge.is_atom(): ct = edge.connector_type() if ct[:2] == 'pd': subjects = edge.edges_with_argrole('s') if len(subjects) == 1: subject = strip_concept(subjects[0]) if subject and has_proper_concept(subject): pred = edge[0] dlemma = deep_lemma(self.hg, pred).root() if dlemma in ACTOR_PRED_LEMMAS: try: actor = main_coref(self.hg, subject) self.actor_counter[actor] += 1 except Exception as e: print(str(e))
def process_edge(self, edge, depth): hg = self.system.get_hg(self) if not edge.is_atom(): ct = edge.connector_type() if ct[0] == 'P': subjects = edge.edges_with_argrole('s') if len(subjects) == 1: subject = strip_concept(subjects[0]) if subject and has_proper_concept(subject): pred = edge[0] dlemma = deep_lemma(hg, pred).root() if dlemma in ACTOR_PRED_LEMMAS: try: actor = main_coref(hg, subject) self.actor_counter[actor] += 1 except Exception as e: print(str(e))
def test_main_coref(self): concepts = self.concepts self.assertEqual(main_coref(self.hg, concepts[0]), concepts[0]) self.assertEqual(main_coref(self.hg, concepts[1]), concepts[1]) self.assertEqual(main_coref(self.hg, concepts[2]), concepts[2]) make_corefs(self.hg, concepts[0], concepts[1]) make_corefs(self.hg, concepts[1], concepts[2]) self.assertEqual(main_coref(self.hg, concepts[0]), concepts[1]) self.assertEqual(main_coref(self.hg, concepts[1]), concepts[1]) self.assertEqual(main_coref(self.hg, concepts[2]), concepts[1])
def process_edge(self, edge, depth): hg = self.system.get_hg(self) if not edge.is_atom(): ct = edge.connector_type() if ct[0] == 'P': pred = edge[0] if (len(edge) > 2 and deep_lemma(hg, pred, same_if_none=True).root() in CLAIM_PRED_LEMMAS): subjects = edge.edges_with_argrole('s') claims = edge.edges_with_argrole('r') if len(subjects) == 1 and len(claims) >= 1: subject = strip_concept(subjects[0]) if subject and has_proper_concept(subject): actor = main_coref(hg, subject) self.actors.add(actor) for claim in claims: # if specificatin, claim is inside if claim.type()[0] == 'S': self._process_claim(actor, claim[1], edge) else: self._process_claim(actor, claim, edge)
def is_actor(hg, edge): if edge.type()[0] == 'c': return hg.exists(('actor/p/.', main_coref(hg, edge))) else: return False
def is_actor(hg, edge): """Checks if the edge is a coreference to an actor.""" if edge.type()[0] == 'c': return hg.exists(('actor/p/.', main_coref(hg, edge))) else: return False