i;sSc@s:ddlmZddlmZdefdYZdS(i(t GraphWrapper(tner_tagstBaseGraphBuildercBsleZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZdZdZd Zd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZedZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(d@dZ*dZ+dZ,dZ-d Z.d!Z/d@d"Z0d@d#d#d$Z1d%Z2d&Z3d'Z4d(Z5d)Z6d*Z7d+Z8d,Z9d-Z:d.Z;d/Z<d0Z=d1Z>d2Z?d3Z@d4ZAed5ZBd6ZCd7ZDd8ZEd9ZFd:ZGd;ZHd<ZIeJd=ZKeJd>d?ZLRS(AtROOTtordertwordtformtentitytreferst named_entityt named_referst constituentt syntactictcontainstterminaltis_headt is_head_wordt dependencyicCsd|_d|_dS(N(tNonetgraphtprevious_sentence(tself((s!corefgraph/graph/graph_builder.pyt__init__3s cCstj}||jd<|S(Nt graph_builder(Rt blank_graphR(RR((s!corefgraph/graph/graph_builder.pyt new_graph7s  cCs||_d|_dS(N(RRR(RR((s!corefgraph/graph/graph_builder.pyt set_graph>s cCs||ds(tsortedRt#get_out_neighbours_by_relation_typeRtword_edge_type(RR3((s!corefgraph/graph/graph_builder.pytget_sentence_wordss c Csj|j}tjd|d|jd|d|}x3|D]+}tj|j|||jd|jq7W|S(NRRRR (RRR$tentity_node_typeR0tentity_edge_typetentity_edge_label(Rt entity_idtmentionsR RR$tmention((s!corefgraph/graph/graph_builder.pyt add_entitys    )cCstjd|d|jS(NRR(RR@RR(RR((s!corefgraph/graph/graph_builder.pytget_all_entitiesscCs"tjd|jd|d|jS(NRR9R;(RRORRS(RR((s!corefgraph/graph/graph_builder.pytget_all_entity_mentionss c Cs:tjd|jd|jd|d|d|d|}|S(NRRRR RR"(RR$Rtnamed_entity_node_type(Rt entity_typeRUR t new_entity((s!corefgraph/graph/graph_builder.pytadd_named_entitys  c Cs5tjd|jd|d|d|jd|jdS(NRtoriginttargett link_typeR (RR0Rtnamed_entity_edge_typetnamed_entity_edge_label(RR3RW((s!corefgraph/graph/graph_builder.pytadd_mention_of_named_entitys'cCs1ttjd|jd|d|jddS(NRR9R;RKcSs|dS(NR!((RL((s!corefgraph/graph/graph_builder.pyRMs(RNRRORRb(Rtroot((s!corefgraph/graph/graph_builder.pytget_sentence_named_entitiess cCsPtjd|jd|jd|d|d|p0|d|}|j|||S(NRRRR"R R!(RR$Rtsyntactic_node_typet link_root(RRR3R"RR R$((s!corefgraph/graph/graph_builder.pytadd_constituents    ic Cstjd|jd|jd|d|d|d|d|d|d | d |d d } |r|jd |d| |jd |d| n| S(NRRRRR R#tlemmatbegintendR!R"tWORDR3Rtelement(RR$Rtword_node_typet link_wordRh( RRRR RjR#RR3RkRlt word_node((s!corefgraph/graph/graph_builder.pytadd_words    c Cs,tjd|jd|d|d|jdS(NRR_R`Ra(RR0RRP(RR3R((s!corefgraph/graph/graph_builder.pyRp scCsK|d|jkr|gStj|j|d|j}t|ddS(NttypeR;RKcSs|dS(NR!((RL((s!corefgraph/graph/graph_builder.pyRM1s(RoRRORRPRN(RRntwords((s!corefgraph/graph/graph_builder.pyt get_words'scCstjd|jd|dS(NRRn(RtremoveR(RRn((s!corefgraph/graph/graph_builder.pyRv3scCs#tjd|jd|d|dS(NRR_R`(RtunlinkR(RR_R`((s!corefgraph/graph/graph_builder.pyRw9scCsItjd|jd|d|d|jd|ddd|jd |dS( NRR_R`RatvaluetweightiR t_(RR0Rtdependency_edge_type(Rtdependency_fromt dependency_totdependency_type((s!corefgraph/graph/graph_builder.pytlink_dependencyAsc Cs.tjd|d|jd|jdt}|S(NR9R;RRK(RROR{RR8(RRtchildren((s!corefgraph/graph/graph_builder.pytget_dependant_wordsKs !c Cs.tjd|d|jd|jdt}|S(NR9R;RRK(Rt"get_in_neighbours_by_relation_typeR{RR8(RRR((s!corefgraph/graph/graph_builder.pytget_governor_wordsSs !c Cs,tjd|jd|d|d|jdS(NRR_R`Ra(RR0Rtroot_edge_type(RR3Rn((s!corefgraph/graph/graph_builder.pyRh\scCssx'|j|D]}|j||qWtjd|jd|d|d|jd|jd|jd|jdS(NRR_R`RaRxR Rz(RuRpRR0Rtsyntactic_edge_typetsyntactic_edge_value_branchtsyntactic_edge_label(RtparenttchildR((s!corefgraph/graph/graph_builder.pytlink_syntax_non_terminalcs cCs_|j||tjd|jd|d|d|jd|jddd|jd |jdS( NRR_R`RaRxRyiR Rz(RpRR0RRtsyntactic_edge_value_terminal(RRR((s!corefgraph/graph/graph_builder.pytlink_syntax_terminaloscCsB|d|jkr"tdntj|j|||jdS(NRssNo word as head word(Rot ExceptionRR0Rthead_word_edge_type(RRnt head_word((s!corefgraph/graph/graph_builder.pyt set_head_wordzscCsX|d|jkr|Stj|j||j}|dkrT|j|d}n|S(NRsi(RoRt"get_out_neighbour_by_relation_typeRRRRu(RRnthead((s!corefgraph/graph/graph_builder.pyt get_head_words  cCsFt||j<|j||j|tj|j|||jdS(N(R8thead_edge_typeRRRR0R(RRR((s!corefgraph/graph/graph_builder.pytset_heads cCs_|d|jkr|Stjd|jd|d|j}|dkr[|jd|dS|S(NRsRR9R;Rni(RoRRRRRtget_syntactic_children(RRnR((s!corefgraph/graph/graph_builder.pytget_heads  cCs|j|ko||jS(N(R(RRn((s!corefgraph/graph/graph_builder.pyRscCs2d|kr|d}ntj|j||jS(NR (Rt!get_in_neighbour_by_relation_typeRR(RRn((s!corefgraph/graph/graph_builder.pyRBs  cCs&|d|dko%|d|dkS(Nii((telement_a_spantelement_b_span((s!corefgraph/graph/graph_builder.pyt is_insidescCs"tjd|jd|d|jS(NRR9R;(RRORR(RRn((s!corefgraph/graph/graph_builder.pyRs cCs2d|kr|d}ntj|j||jS(NR (RRRR(RRn((s!corefgraph/graph/graph_builder.pytget_syntactic_parents  cCsNd|kr|d}n|j|}|j|}|jdd|S(NR RKcSs|dS(Ntspan((tx((s!corefgraph/graph/graph_builder.pyRMs(RRtsort(RRntsyntactic_fathertsiblings((s!corefgraph/graph/graph_builder.pytget_syntactic_siblings   cCstjd|jdS(NR(Rt show_graphR(R((s!corefgraph/graph/graph_builder.pyRscCs|jjd7_dS(Ni(t __class__t word_count(R((s!corefgraph/graph/graph_builder.pytstatistics_word_upscCs|jjd7_dS(Ni(Rtsentence_count(R((s!corefgraph/graph/graph_builder.pytstatistics_sentence_upscCs|jjd7_dS(Ni(Rtdocument_count(R((s!corefgraph/graph/graph_builder.pytstatistics_document_upscCs|j|j|jfS(N(RRR(tcls((s!corefgraph/graph/graph_builder.pyt get_statssRzcCsdj|||S(Ns {1}{0}{2}(R&(Rtsentence_namespacetword_idt separator((s!corefgraph/graph/graph_builder.pytget_compose_idsN(Mt__name__t __module__R%R(R'RR1R2RoRPRRRSRTR[RbRcRgRRRRRRtdependency_node_typeR{RRRRRRt staticmethodRR/R)R<R?RARGRJRQRRXRYRZR^RdRfRiRrRpRuRvRwRRRRhRRRRRRRRBRRRRRRRRt classmethodRR(((s!corefgraph/graph/graph_builder.pyR s                       N(t graph.xutilsRtresources.tagsetRtobjectR(((s!corefgraph/graph/graph_builder.pyts