Skip to content

Commit

Permalink
keep track of updates of sources, targets
Browse files Browse the repository at this point in the history
  • Loading branch information
JingL committed Oct 25, 2018
1 parent aba09a2 commit c799067
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
31 changes: 21 additions & 10 deletions UniversalPetrarch/PETRgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,8 @@ def match_lower(path, verb, target):
triple_dict['verbcode'] = verbcode
triple_dict['matched_txt'] = matched_pattern if matched_pattern != None else (" ").join(matched_txts)
triple_dict['meaning'] = (",").join(meanings)

triple_dict['source_id'] = source.headID if not isinstance(source,basestring) else None
triple_dict['target_id'] = target.headID if not isinstance(target,basestring) else None
#raw_input("Press Enter to continue...")
return verbcode, triple_dict['matched_txt'],triple_dict['meaning'],tripleID, triple_dict

Expand Down Expand Up @@ -2147,12 +2148,16 @@ def find_new_target_actor(verblist, pattern, verb):
meaning = target.mix_codes(newagentcodes, newactorcodes)
meaning = meaning if meaning != None else ['---']
target_meaning = meaning
target.matched_txt = no_overlap_text
else:
target_meaning = ['---']
target_meaning = ['---']
triple['target_id']= None


elif overlap and len(target.matched_txt) == len(overlap):
# entire target actor is in the matched pattern
target_meaning = ['---']
triple['target_id']= None

if (target_meaning in [['---'],[]] or isinstance(target, basestring)) or (verb.passive and source_meaning in [['---'],[],'']):
verblist = self.metadata['othernoun'][verb.headID]
Expand All @@ -2174,10 +2179,14 @@ def find_new_target_actor(verblist, pattern, verb):
source = newtarget
self.nouns[source.headID] = source
source_meaning = newtarget_meaning
triple['source_id'] = source.headID

elif target_meaning in [['---'],[]] or isinstance(target, basestring):
target = newtarget
self.nouns[target.headID] = target
target_meaning = newtarget_meaning
triple['target_id'] = target.headID


#print("new target found,",target.text, target_meaning)

Expand Down Expand Up @@ -2221,16 +2230,17 @@ def find_new_target_actor(verblist, pattern, verb):
source = newsource
self.nouns[source.headID] = source
source_meaning = newsource_meaning
triple['source_id'] = source.headID

#'''

#check if verb code updates:
newverbcode, newmatched_txt, newmeanings, _ , _ = self.get_verb_code_per_triplet((source,target,verb))
#print(triple['verbcode'],newverbcode,newmatched_txt,newmeanings)
if triple['verbcode'] != newverbcode:
if newverbcode not in ['---',None] and "*" in newmatched_txt and "*" not in triple['matched_txt']:
triple['verbcode'] = newverbcode
#raw_input()
#'''


if verb.headID in self.rootID and tripleID not in paired_event:
if verb.headID in root_eventID:
Expand Down Expand Up @@ -2335,7 +2345,7 @@ def find_new_target_actor(verblist, pattern, verb):
if transfer_pattern != "None":
triple["before_transfer"] = event_before_transfer
triple["after_transfer"] = event_after_transfer

##need update source, target ID info

elif current_event[0] and current_event[1]:
event_after_transfer = [current_event]
Expand Down Expand Up @@ -2462,19 +2472,20 @@ def find_new_target_actor(verblist, pattern, verb):
logger.debug("paired_event:" + tripleID)
logger.debug(triple['event'])
verbcode = triple['event'][2]
ids = tripleID.split("#")
verbid = tripleID.split("#")[2]
sourid = triple['source_id'] if triple['source_id']!= None else ""
if verbcode not in allactors:
allactors[verbcode] = {}
allactors[verbcode]['vid'] = ids[2]
allactors[verbcode][ids[0]] = triple['event'][0]
allactors[verbcode]['vid'] = verbid
allactors[verbcode][sourid] = triple['event'][0]

for verbcode, actors in allactors.items():
idx = len(self.events)
if len(actors) > 1:
for sid in actors.keys():
for tid in actors.keys():
if sid != tid and sid != 'vid' and tid != 'vid':
tripleID = sid + "#" + tid + "#" + \
if sid != tid and sid != 'vid' and tid != 'vid' and sid != "" and tid != "":
tripleID = str(sid) + "#" + str(tid) + "#" + \
actors['vid'] + "#" + str(idx)
self.events[tripleID] = [
actors[sid], actors[tid], verbcode]
Expand Down
30 changes: 19 additions & 11 deletions UniversalPetrarch/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,36 +334,44 @@ def story_filter(story_dict, story_id):
# if event_tuple[1:] in text_dict: # log an error here if we can't find a
# non-null case?
if event in sent_dict['triplets']:
triple_dict = sent_dict['triplets'][event]
if not event.startswith("p1_"):
# get source, target, matched text for events using Petrarch2 dictionary
indexes = event.split('#')

sourcetext='---'
if indexes[0] !='-':
sourceid = int(indexes[0])
sourcetext='---'
if 'source_id' in triple_dict and triple_dict['source_id'] != None:
sourceid = int(triple_dict['source_id'])
sourcetext = (" ").join(sent_dict['nouns'][sourceid].matched_txt)
#print(sent_dict['nouns'][sourceid].text)
if sourcetext == "":
sourcetext = sent_dict['nouns'][sourceid].text


targettext='---'
if indexes[1] !='-':
targetid = int(indexes[1])
if 'target_id' in triple_dict and triple_dict['target_id'] != None:
targetid = int(triple_dict['target_id'])
targettext = (" ").join(sent_dict['nouns'][targetid].matched_txt)
if targettext == "":
targettext = sent_dict['nouns'][targetid].text
#print(sent_dict['nouns'][targetid].text)


verbtext = sent_dict['triplets'][event]['triple'][2].text
verbtext = triple_dict['triple'][2].text

if PETRglobals.WriteActorText:
filtered[event_tuple]['actortext'] = [sourcetext,targettext]
if PETRglobals.WriteEventText:
#eventtext = str(sent_dict['triplets'][event]['matched_txt'].replace("*",verbtext))
#if eventtext.find('[') != -1:
# eventtext = eventtext[0:eventtext.find('[')]
eventtext = str(sent_dict['triplets'][event]['matched_txt'])
eventtext = str(triple_dict['matched_txt'])
filtered[event_tuple]['eventtext'] = eventtext
else:
# get source, target, matched text for events using Petrarch1 dictionary
if PETRglobals.WriteActorText:
filtered[event_tuple]['actortext'] = [sent_dict['triplets'][event]['source_text'], sent_dict['triplets'][event]['target_text']]
filtered[event_tuple]['actortext'] = [triple_dict['source_text'], triple_dict['target_text']]
if PETRglobals.WriteEventText:
eventtext = str(sent_dict['triplets'][event]['matched_txt'])
eventtext = str(triple_dict['matched_txt'])
filtered[event_tuple]['eventtext'] = eventtext


Expand Down Expand Up @@ -597,4 +605,4 @@ def convert_code(code, forward=1):
if code and code in reverse:
return reverse[code]

return 0 # hex(code)
return 0 # hex(code)

1 comment on commit c799067

@JingL1014
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#47

Please sign in to comment.