
    gp                         d dl Z d dlZd dlmZ  e j                  e      Z G d d      Z G d de      Z G d de      Z	 G d	 d
      Z
 G d d      Zd Zd Zd Zd Zedk(  r e        yy)    N)DependencyGraphc                   "    e Zd ZdZd Zd Zd Zy)DependencyScorerIa  
    A scorer for calculated the weights on the edges of a weighted
    dependency graph.  This is used by a
    ``ProbabilisticNonprojectiveParser`` to initialize the edge
    weights of a ``DependencyGraph``.  While typically this would be done
    by training a binary classifier, any class that can return a
    multidimensional list representation of the edge weights can
    implement this interface.  As such, it has no necessary
    fields.
    c                 @    | j                   t        k(  rt        d      y )Nz*DependencyScorerI is an abstract interface)	__class__r   	TypeErrorselfs    ]/var/www/openai/venv/lib/python3.12/site-packages/nltk/parse/nonprojectivedependencyparser.py__init__zDependencyScorerI.__init__"   s     >>..HII /    c                     t               )a&  
        :type graphs: list(DependencyGraph)
        :param graphs: A list of dependency graphs to train the scorer.
            Typically the edges present in the graphs can be used as
            positive training examples, and the edges not present as negative
            examples.
        NotImplementedErrorr
   graphss     r   trainzDependencyScorerI.train&   s     "##r   c                     t               )ae  
        :type graph: DependencyGraph
        :param graph: A dependency graph whose set of edges need to be
            scored.
        :rtype: A three-dimensional list of numbers.
        :return: The score is returned in a multidimensional(3) list, such
            that the outer-dimension refers to the head, and the
            inner-dimension refers to the dependencies.  For instance,
            scores[0][1] would reference the list of scores corresponding to
            arcs from node 0 to node 1.  The node's 'address' field can be used
            to determine its number identification.

        For further illustration, a score list corresponding to Fig.2 of
        Keith Hall's 'K-best Spanning Tree Parsing' paper::

              scores = [[[], [5],  [1],  [1]],
                       [[], [],   [11], [4]],
                       [[], [10], [],   [5]],
                       [[], [8],  [8],  []]]

        When used in conjunction with a MaxEntClassifier, each score would
        correspond to the confidence of a particular edge being classified
        with the positive training examples.
        r   r
   graphs     r   scorezDependencyScorerI.score0   s    2 "##r   N__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    	J$$r   r   c                   "    e Zd ZdZd Zd Zd Zy)NaiveBayesDependencyScorera  
    A dependency scorer built around a MaxEnt classifier.  In this
    particular class that classifier is a ``NaiveBayesClassifier``.
    It uses head-word, head-tag, child-word, and child-tag features
    for classification.

    >>> from nltk.parse.dependencygraph import DependencyGraph, conll_data2

    >>> graphs = [DependencyGraph(entry) for entry in conll_data2.split('\n\n') if entry]
    >>> npp = ProbabilisticNonprojectiveParser()
    >>> npp.train(graphs, NaiveBayesDependencyScorer())
    >>> parses = npp.parse(['Cathy', 'zag', 'hen', 'zwaaien', '.'], ['N', 'V', 'Pron', 'Adj', 'N', 'Punc'])
    >>> len(list(parses))
    1

    c                      y Nr   r	   s    r   r   z#NaiveBayesDependencyScorer.__init__c   s    r   c                 @   ddl m} g }|D ]{  }|j                  j                         D ]\  }|j                  j	                         D ]=  \  }}||d   v rd}nd}|j                  t        |d   |d   |d   |d         |f       ? ^ } |j                  |      | _        y	)
as  
        Trains a ``NaiveBayesClassifier`` using the edges present in
        graphs list as positive examples, the edges not present as
        negative examples.  Uses a feature vector of head-word,
        head-tag, child-word, and child-tag.

        :type graphs: list(DependencyGraph)
        :param graphs: A list of dependency graphs to train the scorer.
        r   )NaiveBayesClassifierdepsTFwordtagabcdN)	nltk.classifyr#   nodesvaluesitemsappenddictr   
classifier)	r
   r   r#   labeled_examplesr   	head_nodechild_index
child_nodelabels	            r   r   z NaiveBayesDependencyScorer.trainf   s     	7 E"[[//1	/4{{/@/@/B+K"i&77 # #$++ "+F"3"+E"2",V"4",U"3	 "
 0C 2 & /445EFr   c                 d   g }|j                   j                         D ]K  }|j                   j                         D ],  }|j                  t        |d   |d   |d   |d                . M g }g }d}| j                  j                  |      D ]  }t        j                  d|j                  d      |j                  d             |j                  t        j                  |j                  d      dz         g       |d	z  }|t        |j                         k(  s|j                  |       g }d} |S )
a  
        Converts the graph into a feature-based representation of
        each edge, and then assigns a score to each based on the
        confidence of the classifier in assigning it to the
        positive label.  Scores are returned in a multidimensional list.

        :type graph: DependencyGraph
        :param graph: A dependency graph to score.
        :rtype: 3 dimensional list
        :return: Edge scores for the graph parameter.
        r'   r(   r)   r   z	%.4f %.4fr%   r&   gdy=   )r/   r0   r2   r3   r4   prob_classify_manyloggerdebugprobmathloglen)	r
   r   edgesr6   r8   edge_scoresrowcountpdists	            r   r   z NaiveBayesDependencyScorer.score   s    ++-I#kk002
#F+#E*$V,$U+	 3 . __77>ELLejjouzz#GJJC=!@ABCQJEEKK((""3' ? r   Nr   r   r   r   r   r   Q   s    ""GH&r   r   c                       e Zd Zd Zd Zy)
DemoScorerc                     t        d       y )NzTraining...)printr   s     r   r   zDemoScorer.train   s
    mr   c                 @    g dgdgdggg g dgdggg dgg dggg dgdgg ggS )N   r;         
      r   r   s     r   r   zDemoScorer.score   sP     !qcA3bTA3"rA3!qc2	
 	
r   N)r   r   r   r   r   r   r   r   rI   rI      s    
r   rI   c                   L    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy) ProbabilisticNonprojectiveParsera	  A probabilistic non-projective dependency parser.

    Nonprojective dependencies allows for "crossing branches" in the parse tree
    which is necessary for representing particular linguistic phenomena, or even
    typical parses in some languages.  This parser follows the MST parsing
    algorithm, outlined in McDonald(2005), which likens the search for the best
    non-projective parse to finding the maximum spanning tree in a weighted
    directed graph.

    >>> class Scorer(DependencyScorerI):
    ...     def train(self, graphs):
    ...         pass
    ...
    ...     def score(self, graph):
    ...         return [
    ...             [[], [5],  [1],  [1]],
    ...             [[], [],   [11], [4]],
    ...             [[], [10], [],   [5]],
    ...             [[], [8],  [8],  []],
    ...         ]


    >>> npp = ProbabilisticNonprojectiveParser()
    >>> npp.train([], Scorer())

    >>> parses = npp.parse(['v1', 'v2', 'v3'], [None, None, None])
    >>> len(list(parses))
    1

    Rule based example

    >>> from nltk.grammar import DependencyGrammar

    >>> grammar = DependencyGrammar.fromstring('''
    ... 'taught' -> 'play' | 'man'
    ... 'man' -> 'the' | 'in'
    ... 'in' -> 'corner'
    ... 'corner' -> 'the'
    ... 'play' -> 'golf' | 'dachshund' | 'to'
    ... 'dachshund' -> 'his'
    ... ''')

    >>> ndp = NonprojectiveDependencyParser(grammar)
    >>> parses = ndp.parse(['the', 'man', 'in', 'the', 'corner', 'taught', 'his', 'dachshund', 'to', 'play', 'golf'])
    >>> len(list(parses))
    4

    c                 .    t        j                  d       y)z6
        Creates a new non-projective parser.
        z#initializing prob. nonprojective...N)loggingr>   r	   s    r   r   z)ProbabilisticNonprojectiveParser.__init__   s     	;<r   c                 H    || _         | j                   j                  |       y)a  
        Trains a ``DependencyScorerI`` from a set of ``DependencyGraph`` objects,
        and establishes this as the parser's scorer.  This is used to
        initialize the scores on a ``DependencyGraph`` during the parsing
        procedure.

        :type graphs: list(DependencyGraph)
        :param graphs: A list of dependency graphs to train the scorer.
        :type dependency_scorer: DependencyScorerI
        :param dependency_scorer: A scorer which implements the
            ``DependencyScorerI`` interface.
        N)_scorerr   )r
   r   dependency_scorers      r   r   z&ProbabilisticNonprojectiveParser.train  s     )6"r   c                 D    | j                   j                  |      | _        y)a(  
        Assigns a score to every edge in the ``DependencyGraph`` graph.
        These scores are generated via the parser's scorer which
        was assigned during the training process.

        :type graph: DependencyGraph
        :param graph: A dependency graph to assign scores to.
        N)rW   r   scoresr   s     r   initialize_edge_scoresz7ProbabilisticNonprojectiveParser.initialize_edge_scores  s     ll((/r   c                     t         j                  d       |D ]  }|j                  |        |j                  |       |j	                  ||d          y)aL  
        Takes a list of nodes that have been identified to belong to a cycle,
        and collapses them into on larger node.  The arcs of all nodes in
        the graph must be updated to account for this.

        :type new_node: Node.
        :param new_node: A Node (Dictionary) to collapse the cycle nodes into.
        :type cycle_path: A list of integers.
        :param cycle_path: A list of node addresses, each of which is in the cycle.
        :type g_graph, b_graph, c_graph: DependencyGraph
        :param g_graph, b_graph, c_graph: Graphs which need to be updated.
        zCollapsing nodes...addressN)r=   r>   remove_by_addressadd_noderedirect_arcs)r
   new_node
cycle_pathg_graphb_graphc_graphcycle_node_indexs          r   collapse_nodesz/ProbabilisticNonprojectiveParser.collapse_nodes  sN     	*+ *%%&67 !+"j(9*=>r   c                    t         j                  d|       | j                  |      }t         j                  d|       t         j                  d| j                         t	        | j                        D ]  \  }}t	        | j                  |         D ]  \  }}t         j                  | j                  |   |          ||v s0||vs5| j                  |   |   sH| j                  ||      }t         j                  d| j                  |   |   |       g }| j                  |   |   D ]  }	|j                  |	|z
          || j                  |   |<     t	        | j                        D ]A  \  }}t	        | j                  |         D ]!  \  }}
||v s||v sg | j                  |   |<   # C t         j                  d| j                         y)aE  
        Updates the edge scores to reflect a collapse operation into
        new_node.

        :type new_node: A Node.
        :param new_node: The node which cycle nodes are collapsed into.
        :type cycle_path: A list of integers.
        :param cycle_path: A list of node addresses that belong to the cycle.
        zcycle %szold cycle %szPrior to update: %sz%s - %szAfter update: %sN)r=   r>   compute_original_indexesrZ   	enumeratecompute_max_subtract_scorer2   )r
   ra   rb   irE   jcolumnsubtract_valnew_valscur_valcells              r   update_edge_scoresz3ProbabilisticNonprojectiveParser.update_edge_scores1  s    	Z,22:>
^Z0*DKK8,FAs&t{{1~6	6T[[^A./
?q
':t{{1~a?P#'#B#B1j#QLLLDKKN1,=|L!H#';;q>!#4 ,(>? $5 )1DKKN1% 7 -  ,FAs$T[[^44
?qJ(*DKKN1% 5 -
 	'5r   c                     d}|r\g }d}|D ]N  }|| j                   v r-| j                   |   D ]  }||vs|j                  |       d} >|j                  |       P |}|r\|S )a  
        As nodes are collapsed into others, they are replaced
        by the new node in the graph, but it's still necessary
        to keep track of what these original nodes were.  This
        takes a list of node addresses and replaces any collapsed
        node addresses with their original addresses.

        :type new_indexes: A list of integers.
        :param new_indexes: A list of node addresses to check for
            subsumed nodes.
        TF)inner_nodesr2   )r
   new_indexesswapped	originals	new_indexold_vals         r   ri   z9ProbabilisticNonprojectiveParser.compute_original_indexesW  s     IG(	 0 00#'#3#3I#>")3%,,W5&*G $?
 $$Y/ ) $K  r   c                 V    d}|D ]!  }| j                   |   |   D ]
  }||kD  s	|} # |S )a  
        When updating scores the score of the highest-weighted incoming
        arc is subtracted upon collapse.  This returns the correct
        amount to subtract from that edge.

        :type column_index: integer.
        :param column_index: A index representing the column of incoming arcs
            to a particular node being updated
        :type cycle_indexes: A list of integers.
        :param cycle_indexes: Only arcs from cycle nodes are considered.  This
            is a list of such nodes addresses.
        i`y)rZ   )r
   column_indexcycle_indexes	max_score	row_indexro   s         r   rk   z;ProbabilisticNonprojectiveParser.compute_max_subtract_scorer  sA     	&I $I 6| D)+ ,I !E ' r   c                    | j                  |g      }t        j                  d|       d}d}t        t	        | j
                              D ]p  }t        t	        | j
                  |               D ]J  }||v s|| j
                  |   |   |kD  s | j
                  |   |   }|}t        j                  d||       L r t        j                  |       | j                  D ]  }| j                  |   }||v s|c S  |S )z
        Returns the source of the best incoming arc to the
        node with address: node_index

        :type node_index: integer.
        :param node_index: The address of the 'destination' node,
            the node that is arced to.
        zoriginals: %sNz%s, %s)ri   r=   r>   rangerB   rZ   ru   )	r
   
node_indexrx   max_arcr~   r   	col_indexkeyreplaced_nodess	            r   best_incoming_arcz2ProbabilisticNonprojectiveParser.best_incoming_arc  s     11:,?	_i0	s4;;/0I"3t{{9'=#>?		)%Y)?	)JY)V $I 6y AI'GLL9i@ @ 1 	Y##C!--c2N.(
 $
 r   c                 2   | j                  |g      }d }d }d }t        t        | j                              D ][  }t        t        | j                  |               D ]5  }||v s|| j                  |   |   |kD  s | j                  |   |   }|}|}7 ] ||gS r!   )ri   r   rB   rZ   )r
   r   rx   r   r~   max_origr   r   s           r   original_best_arcz2ProbabilisticNonprojectiveParser.original_best_arc  s    11:,?		s4;;/0I"3t{{9'=#>?		)%Y)?	)JY)V $I 6y AI'G(H @ 1 ""r   c              #     K   i | _         t               }t        |      D ]1  \  }}|j                  |dz      j	                  |||   d|dz   d       3 |j                          t               }t        |      D ]1  \  }}|j                  |dz      j	                  |||   d|dz   d       3 t               }t               }t        |      D ]1  \  }}|j                  |dz      j	                  |||   d|dz   d       3 | j                  |       t        j                  | j                         |j                  j                         D 	cg c]  }	|	d   	 }
}	t        |      }i }|
r|
j                  d      }t        j                  d|       |j                  |      }t        j                  d|       | j                  |      }| j                  |      ||<   t        j                  d||       ||fD ]$  }|j                  |   j	                  d	d|d
       & |j!                  ||       |j#                         }|rdd|dz   d
}|j%                  |       | j'                  ||       | j)                  |||||       |D ]  }|j!                  |d   |        || j                   |d   <   |
j+                  d|dz          |dz  }|D ]  }|j-                  |        t        j                  d|       t        j                  d|       t        j                  d|       t        j                  d|       t        j                  d| j                          |
rt        j                  d| j                         t        j                  d       t/        t        |      dz   |dz         D ]  }||   |||   d   <    t        j                  d|       |j                  j                         D ]  }i |d<   	 t/        dt        |      dz         D ]   }|j!                  ||   d   ||   d          " t        j                  d       | yc c}	w w)a  
        Parses a list of tokens in accordance to the MST parsing algorithm
        for non-projective dependency parses.  Assumes that the tokens to
        be parsed have already been tagged and those tags are provided.  Various
        scoring methods can be used by implementing the ``DependencyScorerI``
        interface and passing it to the training algorithm.

        :type tokens: list(str)
        :param tokens: A list of words or punctuation to be parsed.
        :type tags: list(str)
        :param tags: A list of tags corresponding by index to the words in the tokens list.
        :return: An iterator of non-projective parses.
        :rtype: iter(DependencyGraph)
        r;   NTOP)r'   r(   relr]   r]   r   zcurrent_vertex: %szcurrent_node: %szbest in arc: %s --> %sTEMP)r'   r   r]   NONEzg_graph: %szb_graph: %szc_graph: %sz	Betas: %szreplaced nodes %szFinal scores: %szRecovering parse...r$   zDone.N)ru   r   rj   r/   updateconnect_graphr[   r=   r>   rZ   r0   rB   popget_by_addressr   r   add_arccontains_cycler_   rs   rg   insertr^   r   )r
   tokenstagsrc   indextokenoriginal_graphrd   re   vertexunvisited_verticesnr_verticesbetascurrent_vertexcurrent_nodebest_in_edge
new_vertexrb   ra   cycle_indexcycle_node_addressrl   nodes                          r   parsez&ProbabilisticNonprojectiveParser.parse  s      "#%f-LE5MM%!)$++tE{6eVWiX . 	(*%f-LE5  +22tE{6eVWiX .
 "#!#%f-LE5MM%!)$++tE{6eVWiX . 	##G,T[[!>Emm>R>R>TU>TFfY/>TU&k /33A6NLL-~>"11.ALLL+\:11.AL$($:$:>$JE.!LL1<P-|<
j)00#FzJ = OOL.9 !//1J$*6kTUoV  *''*=##Hj'7GT#-KOOHY$7E $. 9C  )!45 #))![1_= q  +5&--.@A +5 LL0LL0LL0LLe,LL,d.>.>?a !f 	'5*+s6{Qa8A!&qE%(1+ 9 	[%("((//1D
 DL 2 q#f+/*A""58A;a< + 	WQ Vs   E	QQHQC7QN)r   r   r   r   r   r   r[   rg   rs   ri   rk   r   r   r   r   r   r   rS   rS      s=    /b=# 	0?($6L6(@#tr   rS   c                       e Zd ZdZd Zd Zy)NonprojectiveDependencyParserau  
    A non-projective, rule-based, dependency parser.  This parser
    will return the set of all possible non-projective parses based on
    the word-to-word relations defined in the parser's dependency
    grammar, and will allow the branches of the parse tree to cross
    in order to capture a variety of linguistic phenomena that a
    projective parser will not.
    c                     || _         y)z
        Creates a new ``NonprojectiveDependencyParser``.

        :param dependency_grammar: a grammar of word-to-word relations.
        :type dependency_grammar: DependencyGrammar
        N)_grammar)r
   dependency_grammars     r   r   z&NonprojectiveDependencyParser.__init__;  s     +r   c              #     K   t               | _        t        |      D ]#  \  }}|g d|d| j                  j                  |<   % | j                  j                  j	                         D ]u  }g }| j                  j                  j	                         D ]E  }| j
                  j                  |d   |d         s&|d   |d   k7  s2|j                  |d          G ||d<   w g }g }t        |      D ]~  \  }	}
g }t        |      D ]9  \  }}|	|k7  s| j
                  j                  ||
      s)|j                  |       ; t        |      dk(  r|j                  |	       |j                  |        t        |      dk  rt        |      dk(  r*t        t        |            D ]  }	|j                  |	        g }|D ]%  }g }t        t        |            D 	cg c]  }	g  }}	' d}	d}|	dk\  rN|r_t        ||	         d	k(  r||	   d   |	<   nBt        ||	         dk(  rd
|	<   n+||	   j                         }||	<   j                  |	|g       |sd}D ]  }|d   |	k(  sd} t        ||	         }|rX|dk(  rSt        t        |      d	z
  d
d
      D ]6  }||   }|d   |	k(  s||	   j                  |j                  |      d	          8 n4|r2|dkD  r-||	   j                         }||	<   |j                  |	|g       d}|	d	z   t        |      k(  r|j                  dd        d}|r|	d	z  }	n|	d	z  }	|	dk\  rND ]  }|j                  d
      d	kD  rt               }|j                  |j                  d
      d	z      |_        t        t        ||      d	      D ]a  \  }\  }}|d	z   }|j                  |   }|j                  ||d       |dk(  rd}nd}|j                  |d	z      d   |   j                  |       c |  yc c}	w w)a  
        Parses the input tokens with respect to the parser's grammar.  Parsing
        is accomplished by representing the search-space of possible parses as
        a fully-connected directed graph.  Arcs that would lead to ungrammatical
        parses are removed and a lattice is constructed of length n, where n is
        the number of input tokens, to represent all possible grammatical
        traversals.  All possible paths through the lattice are then enumerated
        to produce the set of non-projective parses.

        param tokens: A list of tokens to parse.
        type tokens: list(str)
        return: An iterator of non-projective parses.
        rtype: iter(DependencyGraph)
        r   )r'   r$   r   r]   r'   r]   r$   r      Tr;   FN)start)r'   r]   ROOT )r   _graphrj   r/   r0   r   containsr2   rB   r   r   rF   r   rootzipr   )r
   r   r   r   r6   r$   dep_noderootspossible_headsrl   r'   headsrm   headanalyses_stackanalysisforwardindex_on_stack
stack_itemorig_lengthr   r]   
head_indexhead_addressr   r   s                               r   r   z#NonprojectiveDependencyParser.parseD  s.      &'%f-LE5 	(DKKe$ . **113ID KK--446MM**9V+<hv>NO!&)Xf-==KK 34 7 !%If 4  (GAtE$V,4F 6 6tT BLLO - 5zQQ!!%( ) u:>5zQs6{+ALLO , H(-c..A(BC(B1B(BC  AGq&>!,-2&4Q&7&:^A./14&(-a0446&*aY/%*N&+
%a=A--1N ', #&nQ&7"8K%+*:!&s5zA~r2!>A).qJ)!}1 .q 1 8 81a I "?
 (K!O-a0446&*aY/"&q5C//OOHQK0#GFAFAG q&N !H~~b!A%#%EX^^B%7!%;<EJ09FH%Q1,,%  *A~{{7+Uw?@1$ CCJN+F3C8??H1 K/ !U DsB   B3P6PA
PP*B+P	PB PAP"BP:CPN)r   r   r   r   r   r   r   r   r   r   r   1  s    +{r   r   c                  ,    t                t                y r!   )nonprojective_conll_parse_demorule_based_demor   r   r   demor     s    "$r   c                      t               } | j                  g t                      | j                  g dg d      D ]  }t	        |        y )N)v1v2v3)NNN)rS   r   rI   r   rK   )nppparse_graphs     r   	hall_demor     s:    
*
,CIIb*,yy!35GHk Ir   c                     ddl m}  | j                  d      D cg c]  }|st        |       }}t	               }|j                  |t                      |j                  g dg d      D ]  }t        |        y c c}w )Nr   )conll_data2z

)Cathyzaghenzwaaien.)NVPronAdjr   Punc)	nltk.parse.dependencygraphr   splitr   rS   r   r   r   rK   )r   entryr   r   r   s        r   r   r     sp    62=2C2CF2KU2Kuoe$2KFU
*
,CIIf023yy/1W 	k Vs
   A<A<c                      ddl m}  | j                  d      }t        |       t	        |      }|j                  g d      }t        d       |D ]  }t        |        y )Nr   )DependencyGrammarz
    'taught' -> 'play' | 'man'
    'man' -> 'the' | 'in'
    'in' -> 'corner'
    'corner' -> 'the'
    'play' -> 'golf' | 'dachshund' | 'to'
    'dachshund' -> 'his'
    )themaninr   cornertaughthis	dachshundtoplaygolfzGraphs:)nltk.grammarr   
fromstringrK   r   r   )r   grammarndpr   r   s        r   r   r     s[    .**		G 
'N
'
0CYY	
F 
)e r   __main__)rU   r@   r   r   	getLoggerr   r=   r   r   rI   rS   r   r   r   r   r   r   r   r   <module>r      s      6			8	$3$ 3$v_!2 _L
" 
&_ _NN Nl	 F zF r   