<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import PETRglobals <EOL> import PETRreader <EOL> import time <EOL> import utilities <EOL> import types <EOL> class Phrase : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , label , date , sentence ) : <EOL> """<STR_LIT>""" <EOL> self . label = label if not label == "<STR_LIT>" else "<STR_LIT>" <EOL> self . children = [ ] <EOL> self . phrasetype = label [ <NUM_LIT:0> ] <EOL> self . annotation = "<STR_LIT>" <EOL> self . text = "<STR_LIT>" <EOL> self . parent = None <EOL> self . meaning = "<STR_LIT>" <EOL> self . verbclass = "<STR_LIT>" <EOL> self . date = date <EOL> self . index = - <NUM_LIT:1> <EOL> self . head = None <EOL> self . head_phrase = None <EOL> self . color = False <EOL> self . sentence = sentence <EOL> def get_meaning ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . label in "<STR_LIT>" : <EOL> lower = map ( lambda b : b . get_meaning ( ) , filter ( lambda a : a . label in "<STR_LIT>" , self . children ) ) <EOL> events = [ ] <EOL> for item in lower : <EOL> events += item <EOL> if events : <EOL> self . meaning = events <EOL> return events <EOL> return self . meaning <EOL> def get_text ( self ) : <EOL> if self . color : <EOL> return "<STR_LIT>" <EOL> text = self . text <EOL> for child in self . children : <EOL> if isinstance ( child , NounPhrase ) : <EOL> text += "<STR_LIT:U+0020>" + child . get_text ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> text += "<STR_LIT:U+0020>" + child . get_text ( ) <EOL> return text <EOL> def resolve_codes ( self , codes ) : <EOL> """<STR_LIT>""" <EOL> if not codes : <EOL> return [ ] , [ ] <EOL> actorcodes = [ ] <EOL> agentcodes = [ ] <EOL> for code in codes : <EOL> if not code : <EOL> continue <EOL> """<STR_LIT>""" <EOL> if code . startswith ( "<STR_LIT>" ) : <EOL> agentcodes . append ( code ) <EOL> else : <EOL> actorcodes . append ( code ) <EOL> return actorcodes , agentcodes <EOL> def mix_codes ( self , agents , actors ) : <EOL> """<STR_LIT>""" <EOL> codes = set ( ) <EOL> mix = lambda a , b : a + b if not b in a else a <EOL> actors = actors if actors else [ '<STR_LIT>' ] <EOL> for ag in agents : <EOL> if ag == '<STR_LIT>' and len ( agents ) > <NUM_LIT:1> : <EOL> continue <EOL> actors = map ( lambda a : mix ( a , ag [ <NUM_LIT:1> : ] ) , actors ) <EOL> return filter ( lambda a : a not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] , actors ) <EOL> codes = set ( ) <EOL> print ( '<STR_LIT>' ) <EOL> for act in ( actors if actors else [ '<STR_LIT>' ] ) : <EOL> for ag in ( agents if agents else [ '<STR_LIT>' ] ) : <EOL> if ag == "<STR_LIT>" and len ( agents ) > <NUM_LIT:1> : <EOL> continue <EOL> code = act <EOL> if not ag [ <NUM_LIT:1> : ] in act : <EOL> code += ag [ <NUM_LIT:1> : ] <EOL> if not code in [ '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ] : <EOL> codes . add ( code ) <EOL> return list ( codes ) <EOL> def return_head ( self ) : <EOL> return self . head , self . head_phrase <EOL> def get_head ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_head = self . return_head <EOL> try : <EOL> if self . label == '<STR_LIT:S>' : <EOL> self . head , self . head_phrase = map ( lambda b : b . get_head ( ) , filter ( lambda a : a . label == '<STR_LIT>' , self . children ) ) [ <NUM_LIT:0> ] <EOL> return ( self . head , self . head_phrase ) <EOL> elif self . label == '<STR_LIT>' : <EOL> return self . children [ <NUM_LIT:0> ] . text , self <EOL> if ( not self . label [ <NUM_LIT:1> ] == '<STR_LIT:P>' ) : <EOL> return ( self . text , self . parent ) <EOL> head_children = filter ( lambda child : child . label . startswith ( self . label [ <NUM_LIT:0> ] ) and not child . label [ <NUM_LIT:1> ] == '<STR_LIT:P>' , self . children ) <EOL> if head_children : <EOL> possibilities = filter ( None , map ( lambda a : a . get_head ( ) , head_children ) ) <EOL> else : <EOL> other_children = filter ( lambda child : child . label . startswith ( self . label [ <NUM_LIT:0> ] ) , self . children ) <EOL> possibilities = filter ( None , map ( lambda a : a . get_head ( ) , other_children ) ) <EOL> self . head_phrase = possibilities [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> self . head = possibilities [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> return possibilities [ - <NUM_LIT:1> ] <EOL> except : <EOL> return ( None , None ) <EOL> def print_to_stdout ( self , indent ) : <EOL> print ( indent , self . label , self . text , self . get_meaning ( ) ) <EOL> for child in self . children : <EOL> child . print_to_stdout ( indent + "<STR_LIT:\t>" ) <EOL> class NounPhrase ( Phrase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , label , date , sentence ) : <EOL> Phrase . __init__ ( self , label , date , sentence ) <EOL> def return_meaning ( self ) : <EOL> return self . meaning <EOL> def get_text ( self ) : <EOL> """<STR_LIT>""" <EOL> PPcodes = [ ] <EOL> text = "<STR_LIT>" <EOL> for child in self . children : <EOL> if isinstance ( child , PrepPhrase ) : <EOL> m = self . resolve_codes ( child . get_meaning ( ) ) <EOL> if m [ <NUM_LIT:0> ] : <EOL> PPcodes += child . get_meaning ( ) <EOL> else : <EOL> text += "<STR_LIT:U+0020>" + child . get_text ( ) <EOL> if isinstance ( child , NounPhrase ) : <EOL> value = child . get_text ( ) <EOL> text += value [ <NUM_LIT:0> ] <EOL> PPcodes += value [ <NUM_LIT:1> ] <EOL> if child . label [ : <NUM_LIT:2> ] in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> text += "<STR_LIT:U+0020>" + child . text <EOL> return text , PPcodes <EOL> def check_date ( self , match ) : <EOL> """<STR_LIT>""" <EOL> code = None <EOL> try : <EOL> for j in match : <EOL> dates = j [ <NUM_LIT:1> ] <EOL> date = [ ] <EOL> code = "<STR_LIT>" <EOL> for d in dates : <EOL> if d [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> date . append ( d [ <NUM_LIT:0> ] + str ( PETRreader . dstr_to_ordate ( d [ <NUM_LIT:1> : ] ) ) ) <EOL> else : <EOL> date . append ( str ( PETRreader . dstr_to_ordate ( d ) ) ) <EOL> curdate = self . date <EOL> if not date : <EOL> code = j [ <NUM_LIT:0> ] <EOL> elif len ( date ) == <NUM_LIT:1> : <EOL> if date [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:<>' : <EOL> if curdate < int ( date [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) : <EOL> code = j [ <NUM_LIT:0> ] <EOL> else : <EOL> if curdate >= int ( date [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) : <EOL> code = j [ <NUM_LIT:0> ] <EOL> else : <EOL> if curdate < int ( date [ <NUM_LIT:1> ] ) : <EOL> if curdate >= int ( date [ <NUM_LIT:0> ] ) : <EOL> code = j [ <NUM_LIT:0> ] <EOL> if code : <EOL> return code <EOL> except Exception as e : <EOL> return code <EOL> return code <EOL> def get_meaning ( self ) : <EOL> def recurse ( path , words , length , so_far = "<STR_LIT>" ) : <EOL> if words and words [ <NUM_LIT:0> ] in path : <EOL> match = recurse ( path [ words [ <NUM_LIT:0> ] ] , words [ <NUM_LIT:1> : ] , length + <NUM_LIT:1> , so_far + "<STR_LIT:U+0020>" + words [ <NUM_LIT:0> ] ) <EOL> if match : <EOL> return match <EOL> if '<STR_LIT:#>' in path : <EOL> if isinstance ( path [ "<STR_LIT:#>" ] , list ) : <EOL> code = self . check_date ( path [ '<STR_LIT:#>' ] ) <EOL> if not code is None : <EOL> """<STR_LIT>""" <EOL> return [ code ] , so_far , length , [ path [ '<STR_LIT:#>' ] [ - <NUM_LIT:1> ] ] <EOL> else : <EOL> return [ path [ '<STR_LIT:#>' ] ] , so_far , length <EOL> return False <EOL> text_children = [ ] <EOL> PPcodes = [ ] <EOL> VPcodes = [ ] <EOL> NPcodes = [ ] <EOL> codes = [ ] <EOL> roots = [ ] <EOL> matched_txt = [ ] <EOL> for child in self . children : <EOL> if isinstance ( child , NounPhrase ) : <EOL> value = child . get_text ( ) <EOL> text_children += value [ <NUM_LIT:0> ] . split ( ) <EOL> NPcodes += value [ <NUM_LIT:1> ] <EOL> elif child . label [ : <NUM_LIT:2> ] in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> text_children += child . get_text ( ) . split ( ) <EOL> elif child . label == "<STR_LIT>" : <EOL> m = self . resolve_codes ( child . get_meaning ( ) ) <EOL> if m [ <NUM_LIT:0> ] : <EOL> PPcodes += child . get_meaning ( ) <EOL> else : <EOL> text_children += child . get_text ( ) . split ( ) <EOL> elif child . label == "<STR_LIT>" : <EOL> m = child . get_meaning ( ) <EOL> if m and isinstance ( m [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , basestring ) : <EOL> m = self . resolve_codes ( m [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> if m [ <NUM_LIT:0> ] : <EOL> VPcodes += child . get_theme ( ) <EOL> else : <EOL> pass <EOL> elif child . label == "<STR_LIT>" : <EOL> not_found = True <EOL> level = self . parent <EOL> reflexive = child . text . endswith ( "<STR_LIT>" ) or child . text . endswith ( "<STR_LIT>" ) <EOL> local = True <EOL> while not_found and level . parent : <EOL> if level . label . startswith ( "<STR_LIT>" ) and reflexive : <EOL> break <EOL> if local and level . label in [ "<STR_LIT:S>" , "<STR_LIT>" ] : <EOL> local = False <EOL> level = level . parent <EOL> continue <EOL> if ( not local ) or ( reflexive and local ) : <EOL> for child in level . parent . children : <EOL> if isinstance ( child , NounPhrase ) : <EOL> if not child . get_meaning ( ) == "<STR_LIT>" : <EOL> not_found = False <EOL> codes += child . get_meaning ( ) <EOL> break <EOL> level = level . parent <EOL> index = <NUM_LIT:0> <EOL> while index < len ( text_children ) : <EOL> match = recurse ( PETRglobals . ActorDict , text_children [ index : ] , <NUM_LIT:0> ) <EOL> if match : <EOL> codes += match [ <NUM_LIT:0> ] <EOL> roots += match [ <NUM_LIT:3> ] <EOL> index += match [ <NUM_LIT:2> ] <EOL> matched_txt += [ match [ <NUM_LIT:1> ] ] <EOL> continue <EOL> match = recurse ( PETRglobals . AgentDict , text_children [ index : ] , <NUM_LIT:0> ) <EOL> if match : <EOL> codes += match [ <NUM_LIT:0> ] <EOL> roots += [ [ '<STR_LIT>' ] ] <EOL> index += match [ <NUM_LIT:2> ] <EOL> matched_txt += [ match [ <NUM_LIT:1> ] ] <EOL> """<STR_LIT>""" <EOL> continue <EOL> index += <NUM_LIT:1> <EOL> """<STR_LIT>""" <EOL> actorcodes , agentcodes = self . resolve_codes ( codes ) <EOL> PPactor , PPagent = self . resolve_codes ( PPcodes ) <EOL> NPactor , NPagent = self . resolve_codes ( NPcodes ) <EOL> VPactor , VPagent = self . resolve_codes ( VPcodes ) <EOL> if not actorcodes : <EOL> actorcodes += NPactor <EOL> if not actorcodes : <EOL> actorcodes += PPactor <EOL> if not actorcodes : <EOL> actorcodes += VPactor <EOL> if not agentcodes : <EOL> agentcodes += NPagent <EOL> if not agentcodes : <EOL> agentcodes += PPagent <EOL> if not agentcodes : <EOL> agentcodes += VPagent <EOL> """<STR_LIT>""" <EOL> self . meaning = self . mix_codes ( agentcodes , actorcodes ) <EOL> self . get_meaning = self . return_meaning <EOL> """<STR_LIT>""" <EOL> if matched_txt : <EOL> self . sentence . metadata [ '<STR_LIT>' ] += [ ( matched_txt , self . meaning , roots [ : len ( matched_txt ) ] ) ] <EOL> return self . meaning <EOL> def convert_existential ( self ) : <EOL> parent = self . parent <EOL> sister = parent . children [ <NUM_LIT:1> ] <EOL> neice = sister . children [ <NUM_LIT:1> ] <EOL> subject = neice . children [ <NUM_LIT:0> ] <EOL> predicate = neice . children [ <NUM_LIT:1> ] <EOL> parent . children [ <NUM_LIT:0> ] = subject <EOL> subject . parent = parent <EOL> sister . children [ <NUM_LIT:1> ] = predicate <EOL> predicate . parent = sister <EOL> self . parent = None <EOL> return subject <EOL> class PrepPhrase ( Phrase ) : <EOL> def __init__ ( self , label , date , sentence ) : <EOL> Phrase . __init__ ( self , label , date , sentence ) <EOL> self . meaning = "<STR_LIT>" <EOL> self . prep = "<STR_LIT>" <EOL> self . head = "<STR_LIT>" <EOL> def get_meaning ( self ) : <EOL> """<STR_LIT>""" <EOL> self . prep = self . children [ <NUM_LIT:0> ] . text <EOL> if len ( self . children ) > <NUM_LIT:1> and not self . children [ <NUM_LIT:1> ] . color : <EOL> if isinstance ( self . children [ <NUM_LIT:1> ] , NounPhrase ) and len ( self . children [ <NUM_LIT:1> ] . get_text ( ) [ <NUM_LIT:0> ] ) > <NUM_LIT:0> : <EOL> self . meaning = self . children [ <NUM_LIT:1> ] . get_meaning ( ) <EOL> else : <EOL> self . meaning = "<STR_LIT>" <EOL> self . head = self . children [ <NUM_LIT:1> ] . get_head ( ) [ <NUM_LIT:0> ] <EOL> return self . meaning <EOL> def get_prep ( self ) : <EOL> return self . children [ <NUM_LIT:0> ] . text <EOL> class VerbPhrase ( Phrase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , label , date , sentence ) : <EOL> Phrase . __init__ ( self , label , date , sentence ) <EOL> self . meaning = "<STR_LIT>" <EOL> self . upper = "<STR_LIT>" <EOL> self . lower = "<STR_LIT>" <EOL> self . passive = False <EOL> self . code = <NUM_LIT:0> <EOL> self . valid = self . is_valid ( ) <EOL> self . S = None <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . children [ <NUM_LIT:0> ] . label == "<STR_LIT>" : <EOL> helping = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if ( ( not ( self . parent . get_head ( ) [ <NUM_LIT:0> ] in helping or self . parent . children [ <NUM_LIT:0> ] . text in helping ) ) and <EOL> len ( filter ( lambda a : isinstance ( a , VerbPhrase ) , self . parent . children ) ) <= <NUM_LIT:1> and <EOL> not self . check_passive ( ) ) : <EOL> self . valid = False <EOL> np_replacement = NounPhrase ( "<STR_LIT>" , self . date , self . sentence ) <EOL> np_replacement . children = self . children <EOL> np_replacement . parent = self . parent <EOL> np_replacement . index = self . index <EOL> self . parent . children . remove ( self ) <EOL> self . parent . children . insert ( self . index , np_replacement ) <EOL> del ( self ) <EOL> self = np_replacement <EOL> return False <EOL> self . valid = True <EOL> except IndexError as e : <EOL> self . valid = True <EOL> return True <EOL> def get_theme ( self ) : <EOL> """<STR_LIT>""" <EOL> m = self . get_meaning ( ) <EOL> if isinstance ( m [ <NUM_LIT:0> ] , basestring ) : <EOL> return [ m [ <NUM_LIT:0> ] ] <EOL> if m [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return m [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> return [ m [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ] <EOL> def return_meaning ( self ) : <EOL> return self . meaning <EOL> def get_meaning ( self ) : <EOL> """<STR_LIT>""" <EOL> time1 = time . time ( ) <EOL> self . get_meaning = self . return_meaning <EOL> c , passive , meta = self . get_code ( ) <EOL> s_options = filter ( lambda a : a . label in "<STR_LIT>" , self . children ) <EOL> def resolve_events ( event ) : <EOL> """<STR_LIT>""" <EOL> returns = [ ] <EOL> first , second , third = [ up , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if not ( up or c ) : <EOL> return [ event ] <EOL> if not isinstance ( event , tuple ) : <EOL> second = event <EOL> third = c <EOL> if passive : <EOL> for item in first : <EOL> e2 = ( [ second ] , item , passive ) <EOL> self . sentence . metadata [ id ( e2 ) ] = [ event , meta , <NUM_LIT:7> ] <EOL> returns . append ( e2 ) <EOL> elif event [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> first = event [ <NUM_LIT:0> ] <EOL> third = utilities . combine_code ( c , event [ <NUM_LIT:2> ] ) <EOL> if up : <EOL> returns = [ ] <EOL> for source in up : <EOL> e = ( first , source , third ) <EOL> self . sentence . metadata [ id ( e ) ] = [ event , up , <NUM_LIT:1> ] <EOL> returns . append ( e ) <EOL> return returns <EOL> second = '<STR_LIT>' <EOL> elif not event [ <NUM_LIT:0> ] in [ '<STR_LIT>' , [ ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] : <EOL> second = event <EOL> third = c <EOL> else : <EOL> second = event [ <NUM_LIT:1> ] <EOL> third = utilities . combine_code ( c , event [ <NUM_LIT:2> ] ) <EOL> e = ( first , second , third ) <EOL> self . sentence . metadata [ id ( e ) ] = [ event , c , meta , <NUM_LIT:2> ] <EOL> return returns + [ e ] <EOL> events = [ ] <EOL> up = self . get_upper ( ) <EOL> if self . check_passive ( ) or ( passive and not c ) : <EOL> source_options = [ ] <EOL> target_options = up <EOL> for child in self . children : <EOL> if isinstance ( child , PrepPhrase ) : <EOL> if child . get_prep ( ) in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> source_options += child . get_meaning ( ) <EOL> meta . append ( ( child . prep , child . get_meaning ( ) ) ) <EOL> elif child . get_prep ( ) in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> target_options += child . get_meaning ( ) <EOL> meta . append ( ( child . prep , child . get_meaning ( ) ) ) <EOL> if not target_options : <EOL> target_options = [ "<STR_LIT>" ] <EOL> if source_options or c : <EOL> for i in target_options : <EOL> e = ( source_options , i , c if self . check_passive ( ) else passive ) <EOL> events . append ( e ) <EOL> self . sentence . metadata [ id ( e ) ] = [ None , e , meta , <NUM_LIT:3> ] <EOL> self . meaning = events <EOL> return events <EOL> up = "<STR_LIT>" if up in [ '<STR_LIT>' , [ ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] else up <EOL> low , neg = self . get_lower ( ) <EOL> if not low : <EOL> low = "<STR_LIT>" <EOL> if neg : <EOL> c = <NUM_LIT:0> <EOL> if isinstance ( low , list ) : <EOL> for event in low : <EOL> events += resolve_events ( event ) <EOL> elif not s_options : <EOL> if up or c : <EOL> e = ( up , low , c ) <EOL> self . sentence . metadata [ id ( e ) ] = [ None , e , <NUM_LIT:4> ] <EOL> events . append ( e ) <EOL> elif low : <EOL> events . append ( low ) <EOL> lower = map ( lambda a : a . get_meaning ( ) , s_options ) <EOL> sents = [ ] <EOL> for item in lower : <EOL> sents += item <EOL> if sents and not events : <EOL> for event in sents : <EOL> if isinstance ( event , tuple ) and ( event [ <NUM_LIT:1> ] or event [ <NUM_LIT:2> ] ) : <EOL> for ev in resolve_events ( event ) : <EOL> if isinstance ( ev [ <NUM_LIT:1> ] , list ) : <EOL> for item in ev [ <NUM_LIT:1> ] : <EOL> local = ( ev [ <NUM_LIT:0> ] , item , ev [ <NUM_LIT:2> ] ) <EOL> self . sentence . metadata [ id ( local ) ] = [ ev , item , <NUM_LIT:5> ] <EOL> events . append ( local ) <EOL> else : <EOL> events += resolve_events ( event ) <EOL> maps = [ ] <EOL> for i in events : <EOL> evs = self . match_transform ( i ) <EOL> if isinstance ( evs , tuple ) : <EOL> for j in evs [ <NUM_LIT:0> ] : <EOL> maps . append ( j ) <EOL> self . sentence . metadata [ id ( j ) ] = [ i , evs [ <NUM_LIT:1> ] , <NUM_LIT:6> ] <EOL> else : <EOL> maps += evs <EOL> self . meaning = maps <EOL> return maps <EOL> def return_upper ( self ) : <EOL> return self . upper <EOL> def return_passive ( self ) : <EOL> return self . passive <EOL> def check_passive ( self ) : <EOL> """<STR_LIT>""" <EOL> self . check_passive = self . return_passive <EOL> if True : <EOL> if self . children [ <NUM_LIT:0> ] . label in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> level = self . parent <EOL> if level . label == "<STR_LIT>" : <EOL> self . passive = True <EOL> return True <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> if level and isinstance ( level , VerbPhrase ) : <EOL> if level . children [ <NUM_LIT:0> ] . text in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . passive = True <EOL> return True <EOL> level = level . parent <EOL> if not level : <EOL> break <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> self . passive = False <EOL> return False <EOL> def return_S ( self ) : <EOL> return self . S <EOL> def get_S ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_S = self . return_S <EOL> not_found = True <EOL> level = self <EOL> while not_found and not level . parent is None : <EOL> if isinstance ( level . parent , VerbPhrase ) : <EOL> level = level . parent <EOL> elif level . parent . label == "<STR_LIT:S>" : <EOL> if level . children [ <NUM_LIT:0> ] . label == "<STR_LIT>" and level . index == <NUM_LIT:0> : <EOL> level = level . parent <EOL> continue <EOL> self . S = level . parent <EOL> return level . parent <EOL> else : <EOL> return level <EOL> return None <EOL> def get_upper ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_upper = self . return_upper <EOL> for child in self . parent . children : <EOL> if isinstance ( child , NounPhrase ) and not child . get_meaning ( ) == [ "<STR_LIT>" ] : <EOL> self . upper = child . get_meaning ( ) <EOL> return self . upper <EOL> return [ ] <EOL> def return_lower ( self ) : <EOL> return self . lower <EOL> def get_lower ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_lower = self . return_lower <EOL> lower = [ ] <EOL> v_options = filter ( lambda a : ( isinstance ( a , VerbPhrase ) and a . is_valid ( ) ) , self . children ) <EOL> lower = map ( lambda a : a . get_meaning ( ) , v_options ) <EOL> events = [ ] <EOL> if len ( self . children ) > <NUM_LIT:1> : <EOL> negated = ( lower and self . children [ <NUM_LIT:1> ] . text ) == "<STR_LIT>" <EOL> else : <EOL> negated = False <EOL> for item in lower : <EOL> if negated : <EOL> adjusted = [ ] <EOL> for event in item : <EOL> if isinstance ( event , tuple ) : <EOL> adjusted . append ( ( event [ <NUM_LIT:0> ] , event [ <NUM_LIT:1> ] , event [ <NUM_LIT:2> ] - <NUM_LIT> ) ) <EOL> else : <EOL> adjusted . append ( event ) <EOL> item = adjusted <EOL> events += item <EOL> if events : <EOL> self . lower = events <EOL> return events , negated <EOL> NPcodes = [ ] <EOL> PPcodes = [ ] <EOL> VPcodes = [ ] <EOL> for child in self . children : <EOL> if isinstance ( child , NounPhrase ) : <EOL> NPcodes += child . get_meaning ( ) <EOL> elif isinstance ( child , PrepPhrase ) : <EOL> PPcodes += ( child . get_meaning ( ) ) <EOL> elif False and child . label in "<STR_LIT>" : <EOL> for ch in ( child . children [ - <NUM_LIT:1> ] . children if child . label == "<STR_LIT>" else child . children ) : <EOL> if isinstance ( ch , NounPhrase ) : <EOL> Scodes += ch . get_meaning ( ) <EOL> elif isinstance ( ch , PrepPhrase ) : <EOL> Scodes += ch . get_meaning ( ) <EOL> elif isinstance ( ch , VerbPhrase ) : <EOL> Scodes += ch . get_meaning ( ) [ <NUM_LIT:1> ] <EOL> actorcodes , agentcodes = ( [ ] , [ ] ) <EOL> NPactor , NPagent = self . resolve_codes ( NPcodes ) <EOL> PPactor , PPagent = self . resolve_codes ( PPcodes ) <EOL> VPactor , VPagent = self . resolve_codes ( VPcodes ) <EOL> actorcodes += NPactor <EOL> if not actorcodes : <EOL> actorcodes += PPactor <EOL> if not actorcodes : <EOL> actorcodes += VPactor <EOL> agentcodes += NPagent <EOL> if not agentcodes : <EOL> agentcodes += PPagent <EOL> if not agentcodes : <EOL> agentcodes += VPagent <EOL> self . lower = self . mix_codes ( agentcodes , actorcodes ) <EOL> return self . lower , negated <EOL> def return_code ( self ) : <EOL> return self . code <EOL> def get_code ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_code = self . return_code <EOL> meta = [ ] <EOL> dict = PETRglobals . VerbDict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in map ( lambda a : a . text , self . children ) : <EOL> return <NUM_LIT:0> , <NUM_LIT:0> , [ '<STR_LIT>' ] <EOL> patterns = PETRglobals . VerbDict [ '<STR_LIT>' ] <EOL> verb = "<STR_LIT>" if self . children [ <NUM_LIT:0> ] . label == "<STR_LIT>" else self . get_head ( ) [ <NUM_LIT:0> ] <EOL> meta . append ( verb ) <EOL> meaning = "<STR_LIT>" <EOL> path = dict <EOL> passive = False <EOL> if verb in dict : <EOL> code = <NUM_LIT:0> <EOL> path = dict [ verb ] <EOL> if [ '<STR_LIT:#>' ] == path . keys ( ) : <EOL> path = path [ '<STR_LIT:#>' ] <EOL> if True or path . keys ( ) == [ '<STR_LIT:#>' ] : <EOL> try : <EOL> code = path [ '<STR_LIT:#>' ] [ '<STR_LIT:code>' ] <EOL> meaning = path [ '<STR_LIT:#>' ] [ '<STR_LIT>' ] <EOL> self . verbclass = meaning if not meaning == "<STR_LIT>" else verb <EOL> if not code == '<STR_LIT>' : <EOL> active , passive = utilities . convert_code ( code ) <EOL> self . code = active <EOL> except : <EOL> self . code = ( <NUM_LIT:0> , <NUM_LIT:0> , [ ] ) <EOL> else : <EOL> for child in self . children : <EOL> if child . label in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if child . children [ <NUM_LIT:0> ] . text in path : <EOL> meta . append ( child . children [ <NUM_LIT:0> ] . text ) <EOL> path = path [ child . children [ <NUM_LIT:0> ] . text ] <EOL> if "<STR_LIT:#>" in path : <EOL> try : <EOL> code = path [ '<STR_LIT:#>' ] [ '<STR_LIT:#>' ] [ '<STR_LIT:code>' ] <EOL> meaning = path [ '<STR_LIT:#>' ] [ '<STR_LIT:#>' ] [ '<STR_LIT>' ] <EOL> self . verbclass = meaning if not meaning == "<STR_LIT>" else verb <EOL> if not code == '<STR_LIT>' : <EOL> active , passive = utilities . convert_code ( code ) <EOL> self . code = active <EOL> except : <EOL> pass <EOL> match = self . match_pattern ( ) <EOL> if match : <EOL> meta . append ( match [ '<STR_LIT>' ] ) <EOL> active , passive = utilities . convert_code ( match [ '<STR_LIT:code>' ] ) <EOL> self . code = active <EOL> if passive and not active : <EOL> self . check_passive = lambda : True <EOL> self . code = passive <EOL> return self . code , passive , meta <EOL> def match_transform ( self , e ) : <EOL> """<STR_LIT>""" <EOL> def recurse ( pdict , event , a2v = { } , v2a = { } ) : <EOL> path = pdict <EOL> if isinstance ( pdict , list ) : <EOL> line = pdict [ <NUM_LIT:1> ] <EOL> path = pdict [ <NUM_LIT:0> ] <EOL> verb = utilities . convert_code ( path [ <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] if not path [ <NUM_LIT:2> ] == "<STR_LIT>" else v2a [ "<STR_LIT>" ] <EOL> if isinstance ( v2a [ path [ <NUM_LIT:1> ] ] , tuple ) : <EOL> results = [ ] <EOL> for item in v2a [ path [ <NUM_LIT:1> ] ] : <EOL> results . append ( ( list ( v2a [ path [ <NUM_LIT:0> ] ] ) , item , verb ) ) <EOL> return results , line <EOL> return [ ( list ( v2a [ path [ <NUM_LIT:0> ] ] ) , v2a [ path [ <NUM_LIT:1> ] ] , verb ) ] , line <EOL> if isinstance ( event , tuple ) : <EOL> actor = None if not event [ <NUM_LIT:0> ] else tuple ( event [ <NUM_LIT:0> ] ) <EOL> masks = filter ( lambda a : a in pdict , [ event [ <NUM_LIT:2> ] , event [ <NUM_LIT:2> ] - event [ <NUM_LIT:2> ] % <NUM_LIT> , <EOL> event [ <NUM_LIT:2> ] - event [ <NUM_LIT:2> ] % <NUM_LIT> , event [ <NUM_LIT:2> ] - event [ <NUM_LIT:2> ] % <NUM_LIT> ] ) <EOL> if masks : <EOL> path = pdict [ masks [ <NUM_LIT:0> ] ] <EOL> elif - <NUM_LIT:1> in pdict : <EOL> v2a [ "<STR_LIT>" ] = event [ <NUM_LIT:2> ] <EOL> path = pdict [ - <NUM_LIT:1> ] <EOL> else : <EOL> return False <EOL> else : <EOL> actor = event <EOL> if actor in a2v : <EOL> actor = a2v [ actor ] <EOL> if not actor : <EOL> actor = "<STR_LIT:_>" <EOL> if actor in path : <EOL> return recurse ( path [ actor ] , event [ <NUM_LIT:1> ] , a2v , v2a ) <EOL> elif not actor == '<STR_LIT:_>' : <EOL> for var in sorted ( path . keys ( ) ) [ : : - <NUM_LIT:1> ] : <EOL> if var in v2a : <EOL> continue <EOL> if not var == '<STR_LIT:.>' : <EOL> v2a [ var ] = actor <EOL> a2v [ actor ] = var <EOL> return recurse ( path [ var ] , event [ <NUM_LIT:1> ] , a2v , v2a ) <EOL> return False <EOL> try : <EOL> t = recurse ( PETRglobals . VerbDict [ '<STR_LIT>' ] , e ) <EOL> if t : <EOL> return t <EOL> else : <EOL> if e [ <NUM_LIT:0> ] and e [ <NUM_LIT:2> ] and isinstance ( e [ <NUM_LIT:1> ] , tuple ) and e [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] and not e [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] / ( <NUM_LIT:16> ** <NUM_LIT:3> ) : <EOL> if isinstance ( e [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , list ) : <EOL> results = [ ] <EOL> for item in e [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] : <EOL> event = ( e [ <NUM_LIT:0> ] , item , utilities . combine_code ( e [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] , e [ <NUM_LIT:2> ] ) ) <EOL> results . append ( event ) <EOL> return results <EOL> event = ( e [ <NUM_LIT:0> ] , e [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , utilities . combine_code ( e [ <NUM_LIT:2> ] , e [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] ) ) <EOL> return [ event ] <EOL> except Exception as ex : <EOL> pass <EOL> return [ e ] <EOL> def match_pattern ( self ) : <EOL> """<STR_LIT>""" <EOL> meaning = self . verbclass <EOL> code = self . code <EOL> def match_phrase ( path , phrase ) : <EOL> if not phrase : <EOL> return False <EOL> for item in filter ( lambda b : b . text in path , phrase . children ) : <EOL> subpath = path [ item . text ] <EOL> match = reroute ( subpath , lambda a : match_phrase ( a , item . head_phrase ) ) <EOL> if match : <EOL> item . color = True <EOL> return match <EOL> return reroute ( path , lambda a : match_phrase ( a , phrase . head_phrase ) ) <EOL> def match_noun ( path , phrase = self if not self . check_passive ( ) else self . get_S ( ) , preplimit = <NUM_LIT:0> ) : <EOL> noun_phrases = [ ] <EOL> if not phrase : <EOL> return False <EOL> if preplimit : <EOL> for sib in phrase . children : <EOL> if isinstance ( sib , PrepPhrase ) and len ( sib . children ) > <NUM_LIT:1> and sib . get_prep ( ) in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> noun_phrases . append ( sib . children [ <NUM_LIT:1> ] ) <EOL> else : <EOL> for child in phrase . children : <EOL> if child . label in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> noun_phrases . append ( child ) <EOL> if isinstance ( phrase , NounPhrase ) : <EOL> noun_phrases . append ( phrase ) <EOL> for item in noun_phrases : <EOL> head , headphrase = item . get_head ( ) <EOL> if head and head in path : <EOL> subpath = path [ head ] <EOL> skip = lambda a : False <EOL> match = reroute ( subpath , skip , skip , lambda a : match_prep ( a , item ) , skip , <NUM_LIT:0> ) <EOL> if match : <EOL> headphrase . children [ - <NUM_LIT:1> ] . color = True <EOL> return match <EOL> match = reroute ( subpath , ( lambda a : match_phrase ( a , item . head_phrase ) ) <EOL> if isinstance ( item , NounPhrase ) else None ) <EOL> if match : <EOL> headphrase . children [ - <NUM_LIT:1> ] . color = True <EOL> return match <EOL> if '<STR_LIT>' in path : <EOL> phrase . color = True <EOL> return reroute ( path [ '<STR_LIT>' ] , lambda a : match_phrase ( a , phrase . head_phrase ) ) <EOL> return reroute ( path , lambda a : match_phrase ( a , phrase . head_phrase ) ) <EOL> def match_prep ( path , phrase = self ) : <EOL> for item in filter ( lambda b : isinstance ( b , PrepPhrase ) , phrase . children ) : <EOL> prep = item . children [ <NUM_LIT:0> ] . text <EOL> if prep in path : <EOL> subpath = path [ prep ] <EOL> match = reroute ( subpath , <EOL> lambda a : match_noun ( a , item . children [ <NUM_LIT:1> ] ) if len ( item . children ) > <NUM_LIT:1> else False , <EOL> match_prep ) <EOL> if match : <EOL> return match <EOL> return reroute ( path , o2 = match_prep ) <EOL> def reroute ( subpath , o1 = match_noun , o2 = match_noun , o3 = match_prep , o4 = match_noun , exit = <NUM_LIT:1> ) : <EOL> if not o1 : <EOL> return False <EOL> if '<STR_LIT:->' in subpath : <EOL> match = o1 ( subpath [ '<STR_LIT:->' ] ) <EOL> if match : <EOL> return match <EOL> if '<STR_LIT:U+002C>' in subpath : <EOL> match = o2 ( subpath [ '<STR_LIT:U+002C>' ] ) <EOL> if match : <EOL> return match <EOL> if '<STR_LIT:|>' in subpath : <EOL> match = o3 ( subpath [ '<STR_LIT:|>' ] ) <EOL> if match : <EOL> return match <EOL> if '<STR_LIT:*>' in subpath : <EOL> match = o4 ( subpath [ '<STR_LIT:*>' ] ) <EOL> if match : <EOL> return match <EOL> if '<STR_LIT:#>' in subpath and exit : <EOL> return subpath [ '<STR_LIT:#>' ] <EOL> return False <EOL> if meaning in PETRglobals . VerbDict [ '<STR_LIT>' ] : <EOL> path = PETRglobals . VerbDict [ '<STR_LIT>' ] [ meaning ] <EOL> if self . check_passive ( ) : <EOL> return match_noun ( path , self , <NUM_LIT:1> ) <EOL> else : <EOL> return match_noun ( path , self . get_S ( ) ) <EOL> return False <EOL> class Sentence : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parse , text , date ) : <EOL> self . treestr = parse . replace ( '<STR_LIT:)>' , '<STR_LIT>' ) <EOL> self . parse = parse <EOL> self . agent = "<STR_LIT>" <EOL> self . ID = - <NUM_LIT:1> <EOL> self . actor = "<STR_LIT>" <EOL> self . date = date <EOL> self . longlat = ( - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> self . verbs = [ ] <EOL> self . txt = "<STR_LIT>" <EOL> self . tree = self . str_to_tree ( parse . strip ( ) ) <EOL> self . verb_analysis = { } <EOL> self . events = [ ] <EOL> self . metadata = { '<STR_LIT>' : [ ] } <EOL> def str_to_tree ( self , str ) : <EOL> """<STR_LIT>""" <EOL> segs = str . split ( ) <EOL> root = Phrase ( segs [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] , self . date , self ) <EOL> level_stack = [ root ] <EOL> existentials = [ ] <EOL> for element in segs [ <NUM_LIT:1> : ] : <EOL> if element . startswith ( "<STR_LIT:(>" ) : <EOL> lab = element [ <NUM_LIT:1> : ] <EOL> if lab == "<STR_LIT>" : <EOL> new = NounPhrase ( lab , self . date , self ) <EOL> elif lab == "<STR_LIT>" : <EOL> new = VerbPhrase ( lab , self . date , self ) <EOL> self . verbs . append ( new ) <EOL> elif lab == "<STR_LIT>" : <EOL> new = PrepPhrase ( lab , self . date , self ) <EOL> else : <EOL> new = Phrase ( lab , self . date , self ) <EOL> if lab == "<STR_LIT>" : <EOL> existentials . append ( new ) <EOL> new . parent = level_stack [ - <NUM_LIT:1> ] <EOL> new . index = len ( level_stack [ - <NUM_LIT:1> ] . children ) <EOL> level_stack [ - <NUM_LIT:1> ] . children . append ( new ) <EOL> level_stack . append ( new ) <EOL> elif element . endswith ( "<STR_LIT:)>" ) : <EOL> try : <EOL> level_stack . pop ( ) <EOL> except : <EOL> break <EOL> else : <EOL> level_stack [ - <NUM_LIT:1> ] . text = element <EOL> self . txt += "<STR_LIT:U+0020>" + element <EOL> for element in existentials : <EOL> try : <EOL> element . parent . convert_existential ( ) <EOL> except : <EOL> pass <EOL> return root <EOL> def print_nouns ( self , label ) : <EOL> """<STR_LIT>""" <EOL> print ( label ) <EOL> for la in self . metadata [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT:U+0020>' , la ) <EOL> def get_metadata ( self , entry ) : <EOL> metadict = self . metadata <EOL> next = entry <EOL> store = "<STR_LIT>" <EOL> meta_total = [ ] <EOL> while id ( next ) in metadict : <EOL> store = metadict [ id ( next ) ] <EOL> meta_total . append ( store ) <EOL> next = store [ <NUM_LIT:0> ] <EOL> return map ( lambda a : a [ - <NUM_LIT:2> ] if len ( a ) > <NUM_LIT:1> else a [ <NUM_LIT:0> ] , meta_total [ : : - <NUM_LIT:1> ] ) <EOL> def return_events ( self ) : <EOL> return self . events <EOL> def get_events ( self , require_dyad = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> events = map ( lambda a : a . get_meaning ( ) , filter ( lambda b : b . label in "<STR_LIT>" , self . tree . children ) ) <EOL> meta = { '<STR_LIT>' : self . metadata [ '<STR_LIT>' ] } <EOL> """<STR_LIT>""" <EOL> valid = [ ] <EOL> try : <EOL> for sent in events : <EOL> for event in sent : <EOL> if event [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> event = ( event [ <NUM_LIT:0> ] , None , event [ <NUM_LIT:2> ] ) <EOL> if isinstance ( event , tuple ) and isinstance ( event [ <NUM_LIT:1> ] , basestring ) : <EOL> code = utilities . convert_code ( event [ <NUM_LIT:2> ] , <NUM_LIT:0> ) <EOL> print ( '<STR_LIT>' , event , hex ( event [ <NUM_LIT:2> ] ) ) <EOL> if event [ <NUM_LIT:0> ] and event [ <NUM_LIT:1> ] and code : <EOL> for source in event [ <NUM_LIT:0> ] : <EOL> valid . append ( ( source . replace ( '<STR_LIT>' , '<STR_LIT>' ) , event [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) , code ) ) <EOL> meta [ ( source . replace ( '<STR_LIT>' , '<STR_LIT>' ) , event [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) , code ) ] = self . get_metadata ( event ) <EOL> elif ( not require_dyad ) and event [ <NUM_LIT:0> ] and code and not event [ <NUM_LIT:1> ] : <EOL> for source in event [ <NUM_LIT:0> ] : <EOL> valid . append ( ( source . replace ( '<STR_LIT>' , '<STR_LIT>' ) , "<STR_LIT>" , code ) ) <EOL> elif ( not require_dyad ) and event [ <NUM_LIT:1> ] and code and not event [ <NUM_LIT:0> ] : <EOL> valid . append ( ( "<STR_LIT>" , event [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) , code ) ) <EOL> if len ( event [ <NUM_LIT:0> ] ) > <NUM_LIT:1> and ( not event [ <NUM_LIT:1> ] ) and code and code [ : <NUM_LIT:2> ] in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> for source in event [ <NUM_LIT:0> ] : <EOL> for target in event [ <NUM_LIT:0> ] : <EOL> if not source == target : <EOL> valid . append ( ( source . replace ( '<STR_LIT>' , '<STR_LIT>' ) , target . replace ( '<STR_LIT>' , '<STR_LIT>' ) , code ) ) <EOL> self . events = list ( set ( valid ) ) <EOL> self . get_events = self . return_events <EOL> return valid , meta <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" , e ) <EOL> return None , None <EOL> '''<STR_LIT>''' <EOL> def print_to_file ( self , root , file = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" , self . get_events ( ) , file = file ) <EOL> txtstrg = self . txt . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) <EOL> print ( "<STR_LIT>" , txtstrg , file = file ) <EOL> def print_tree ( self , root , indent = "<STR_LIT>" , f = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" + root . label . replace ( "<STR_LIT:$>" , "<STR_LIT>" ) , ( "<STR_LIT>" + root . text + "<STR_LIT:}>" if not root . text == "<STR_LIT>" else "<STR_LIT>" ) , file = f , end = "<STR_LIT:U+0020>" ) <EOL> if root . label in [ "<STR_LIT>" ] : <EOL> m = root . get_meaning ( ) <EOL> k = "<STR_LIT>" <EOL> for i in m : <EOL> k += "<STR_LIT:+>" + i <EOL> print ( "<STR_LIT>" + k + "<STR_LIT:}>" , file = f , end = "<STR_LIT:U+0020>" ) <EOL> elif root . label in [ "<STR_LIT>" ] : <EOL> m = root . get_meaning ( ) <EOL> print ( "<STR_LIT>" + utilities . code_to_string ( m ) + "<STR_LIT:}>" , file = f , end = "<STR_LIT:U+0020>" ) <EOL> for child in root . children : <EOL> self . print_tree ( child , indent + "<STR_LIT:\t>" , f ) <EOL> if root . label in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> print ( "<STR_LIT>" , file = f , end = "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" , file = f , end = "<STR_LIT:U+0020>" ) </s>
