def get_events(self, require_dyad=1): """ Take the coding of the highest verb phrase and return that, given: 1) Target and source are both present 2) Code is non-zero 3) Target isn't another event Parameters ----------- self: Sentence object calling the method Returns ------- valid: list List of coded events that satisfy the above conditions, of form (source, target, code) where the code has been converted from an int back into CAMEO """ events = map(lambda a: a.get_meaning(), filter(lambda b: b.label in "SVP", self.tree.children)) meta = {"nouns": self.metadata["nouns"]} # print(events) valid = [] try: for sent in events: for event in sent: if event[1] == "passive": event = (event[0], None, event[2]) if isinstance(event, tuple) and isinstance(event[1], basestring): code = utilities.convert_code(event[2], 0) print("checking event", event, hex(event[2])) if event[0] and event[1] and code: for source in event[0]: valid.append((source.replace("~", "---"), event[1].replace("~", "---"), code)) meta[ (source.replace("~", "---"), event[1].replace("~", "---"), code) ] = self.get_metadata(event) elif (not require_dyad) and event[0] and code and not event[1]: for source in event[0]: valid.append((source.replace("~", "---"), "---", code)) elif (not require_dyad) and event[1] and code and not event[0]: valid.append(("---", event[1].replace("~", "---"), code)) # If there are multiple actors in a cooperation scenario, code their cooperation as well if len(event[0]) > 1 and (not event[1]) and code and code[:2] in ["03", "04", "05", "06"]: for source in event[0]: for target in event[0]: if not source == target: valid.append((source.replace("~", "---"), target.replace("~", "---"), code)) self.events = list(set(valid)) self.get_events = self.return_events return valid, meta except Exception as e: print("Error in parsing:", e) return None, None
def write_events_demo(sent, events, meta, output_file): print("the output_path" + output_file) if output_file: f = codecs.open(output_file, encoding='utf-8', mode='a') f.write('\n\n') f.write(sent.txt + '\n') f.write(sent.treestr + '\n') print(events) f.write( str(events[0][0]) + " " + str(utilities.convert_code(events[0][0][2], forward=0)) + '\n') # f.write(str(meta) + '\n\n') f.close()
def recurse(pdict, event, a2v={}, v2a={}): path = pdict if isinstance(pdict, list): line = pdict[1] path = pdict[0] verb = utilities.convert_code(path[2])[0] if not path[2] == "Q" else v2a["Q"] if isinstance(v2a[path[1]], tuple): results = [] for item in v2a[path[1]]: results.append((list(v2a[path[0]]), item, verb)) return results, line return [(list(v2a[path[0]]), v2a[path[1]], verb)], line if isinstance(event, tuple): actor = None if not event[0] else tuple(event[0]) masks = filter( lambda a: a in pdict, [event[2], event[2] - event[2] % 0x10, event[2] - event[2] % 0x100, event[2] - event[2] % 0x1000], ) if masks: path = pdict[masks[0]] elif -1 in pdict: v2a["Q"] = event[2] path = pdict[-1] else: return False else: actor = event if actor in a2v: actor = a2v[actor] if not actor: actor = "_" if actor in path: return recurse(path[actor], event[1], a2v, v2a) elif not actor == "_": for var in sorted(path.keys())[::-1]: if var in v2a: continue if not var == ".": v2a[var] = actor a2v[actor] = var return recurse(path[var], event[1], a2v, v2a) return False
def get_code(self): """ Match the codes from the Verb Dictionary. Step 1. Check for compound verb matches Step 2. Check for pattern matches via match_pattern() method Parameters ----------- self: VerbPhrase object that called the method Returns ------- code: int Code described by this verb, best read in hex """ self.get_code = self.return_code meta = [] dict = PETRglobals.VerbDict["verbs"] if "AND" in map(lambda a: a.text, self.children): return 0, 0, ["and"] patterns = PETRglobals.VerbDict["phrases"] verb = "TO" if self.children[0].label == "TO" else self.get_head()[0] meta.append(verb) meaning = "" path = dict passive = False if verb in dict: code = 0 path = dict[verb] if ["#"] == path.keys(): path = path["#"] if True or path.keys() == ["#"]: # Pre-compounds are weird try: code = path["#"]["code"] meaning = path["#"]["meaning"] self.verbclass = meaning if not meaning == "" else verb if not code == "": active, passive = utilities.convert_code(code) self.code = active except: self.code = (0, 0, []) else: # Post - compounds for child in self.children: if child.label in ["PRT", "ADVP"]: if child.children[0].text in path: meta.append(child.children[0].text) path = path[child.children[0].text] if "#" in path: try: code = path["#"]["#"]["code"] meaning = path["#"]["#"]["meaning"] self.verbclass = meaning if not meaning == "" else verb if not code == "": active, passive = utilities.convert_code(code) self.code = active except: pass match = self.match_pattern() if match: meta.append(match["line"]) print(match) active, passive = utilities.convert_code(match["code"]) self.code = active if passive and not active: self.check_passive = lambda: True self.code = passive return self.code, passive, meta