ó
    ´OÂf‡k  ã                   óâ   • S SK Jr  S SKJr  S SKJr  S SKJrJrJ	r	  S SK
Jr  S SKJr  \ " S S5      5       r " S	 S
5      r " S S5      r " S S5      rS rS rS rS r\S:X  a  \" 5         gg)é    )Údefaultdict)Útotal_ordering)Úchain)ÚDependencyGrammarÚDependencyProductionÚProbabilisticDependencyGrammar)Úraise_unorderable_types)ÚDependencyGraphc                   óH   • \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)ÚDependencySpané   a0  
A contiguous span over some part of the input string representing
dependency (head -> modifier) relationships amongst words.  An atomic
span corresponds to only one word so it isn't a 'span' in the conventional
sense, as its _start_index = _end_index = _head_index for concatenation
purposes.  All other spans are assumed to have arcs between all nodes
within the start and end indexes of the span, and one head index corresponding
to the head word for the entire span.  This is the same as the root node if
the dependency structure were depicted as a graph.
c                 óš   • Xl         X l        X0l        X@l        XPl        XU[        U5      4U l        [        U R                  5      U l        g ©N)	Ú_start_indexÚ
_end_indexÚ_head_indexÚ_arcsÚ_tagsÚtupleÚ_comparison_keyÚhashÚ_hash)ÚselfÚstart_indexÚ	end_indexÚ
head_indexÚarcsÚtagss         ÚG/usr/lib/python3/dist-packages/nltk/parse/projectivedependencyparser.pyÚ__init__ÚDependencySpan.__init__(   sB   € Ø'ÔØ#ŒØ%ÔØŒ
ØŒ
Ø +¸
ÄEÈ$ÃKÐPˆÔÜ˜$×.Ñ.Ó/ˆ�
ó    c                 ó   • U R                   $ )zS
:return: An value indexing the head of the entire ``DependencySpan``.
:rtype: int
)r   ©r   s    r   r   ÚDependencySpan.head_index1   s   € ð
 ×ÑÐr"   c                 óN   • SU R                   U R                  U R                  4-  $ )zR
:return: A concise string representatino of the ``DependencySpan``.
:rtype: str.
úSpan %d-%d; Head Index: %d)r   r   r   r$   s    r   Ú__repr__ÚDependencySpan.__repr__8   s/   € ð
 ,Ø×ÑØ�O‰OØ×Ñð/
ñ 
ð 	
r"   c                 óä   • SU R                   U R                  U R                  4-  n[        [	        U R
                  5      5       H'  nUSX R
                  U   U R                  U   4-  -  nM)     U$ )zQ
:return: A verbose string representation of the ``DependencySpan``.
:rtype: str
r'   z
%d <- %d, %s)r   r   r   ÚrangeÚlenr   r   )r   ÚstrÚis      r   Ú__str__ÚDependencySpan.__str__C   so   € ð
 +Ø×ÑØ�O‰OØ×Ñð.
ñ 
ˆô
 ”s˜4Ÿ:™:“Ö'ˆAØÐ# q¯*©*°Q©-¸¿¹ÀA¹Ð&GÑGÑGŠCñ (àˆ
r"   c                 óp   • [        U 5      [        U5      :H  =(       a    U R                  UR                  :H  $ r   )Útyper   ©r   Úothers     r   Ú__eq__ÚDependencySpan.__eq__Q   s-   € ä�‹Jœ$˜u›+Ñ%×W¨$×*>Ñ*>À%×BWÑBWÑ*Wð	
r"   c                 ó   • X:X  + $ r   © r3   s     r   Ú__ne__ÚDependencySpan.__ne__V   s   € ØÒ Ð r"   c                 óv   • [        U[        5      (       d  [        SX5        U R                  UR                  :  $ )NÚ<)Ú
isinstancer   r	   r   r3   s     r   Ú__lt__ÚDependencySpan.__lt__Y   s0   € Ü˜%¤×0Ñ0Ü# C¨Ô5Ø×#Ñ# e×&;Ñ&;Ñ;Ð;r"   c                 ó   • U R                   $ )z5
:return: The hash value of this ``DependencySpan``.
)r   r$   s    r   Ú__hash__ÚDependencySpan.__hash__^   s   € ð �z‰zÐr"   )r   r   r   r   r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r    r   r(   r/   r5   r9   r>   rA   Ú__static_attributes__r8   r"   r   r   r      s/   † ñ	ò0ò ò	
òò
ò
!ò<õ
r"   r   c                   ó0   • \ rS rSrSrS rS rS rS rSr	g)	Ú	ChartCelléj   zÕ
A cell from the parse chart formed when performing the CYK algorithm.
Each cell keeps track of its x and y coordinates (though this will probably
be discarded), and a list of spans serving as the cell's entries.
c                 ó:   • Xl         X l        [        5       U l        g)ze
:param x: This cell's x coordinate.
:type x: int.
:param y: This cell's y coordinate.
:type y: int.
N)Ú_xÚ_yÚsetÚ_entries)r   ÚxÚys      r   r    ÚChartCell.__init__q   s   € ð ŒØŒÜ›ˆ�r"   c                 ó:   • U R                   R                  U5        g)zŽ
Appends the given span to the list of spans
representing the chart cell's entries.

:param span: The span to add.
:type span: DependencySpan
N)rP   Úadd)r   Úspans     r   rU   ÚChartCell.add|   s   € ð 	�‰×Ñ˜$Õr"   c                 óN   • SU R                   U R                  U R                  4-  $ )zN
:return: A verbose string representation of this ``ChartCell``.
:rtype: str.
zCC[%d,%d]: %s)rM   rN   rP   r$   s    r   r/   ÚChartCell.__str__†   s"   € ð
  $§'¡'¨4¯7©7°D·M±MÐ!BÑBÐBr"   c                 ó   • SU -  $ )zN
:return: A concise string representation of this ``ChartCell``.
:rtype: str.
z%sr8   r$   s    r   r(   ÚChartCell.__repr__�   s   € ð
 �d‰{Ðr"   )rP   rM   rN   N)
rC   rD   rE   rF   rG   r    rU   r/   r(   rH   r8   r"   r   rJ   rJ   j   s   † ñò	ò òCõr"   rJ   c                   ó*   • \ rS rSrSrS rS rS rSrg)ÚProjectiveDependencyParseréš   a~  
A projective, rule-based, dependency parser.  A ProjectiveDependencyParser
is created with a DependencyGrammar, a set of productions specifying
word-to-word dependency relations.  The parse() method will then
return the set of all parses, in tree representation, for a given input
sequence of tokens.  Each parse must meet the requirements of the both
the grammar and the projectivity constraint which specifies that the
branches of the dependency tree are not allowed to cross.  Alternatively,
this can be understood as stating that each parent node and its children
in the parse tree form a continuous substring of the input sequence.
c                 ó   • Xl         g)zÛ
Create a new ProjectiveDependencyParser, from a word-to-word
dependency grammar ``DependencyGrammar``.

:param dependency_grammar: A word-to-word relation dependencygrammar.
:type dependency_grammar: DependencyGrammar
N)Ú_grammar)r   Údependency_grammars     r   r    Ú#ProjectiveDependencyParser.__init__§   s	   € ð +�r"   c              #   ó  #   • [        U5      U l        / n[        S[        U R                  5      S-   5       H�  nUR	                  / 5        [        S[        U R                  5      S-   5       HS  nX#   R	                  [        X45      5        X4S-   :X  d  M)  X#   U   R                  [        US-
  X3S-
  S/S/5      5        MU     M�     [        S[        U R                  5      S-   5       H�  n[        US-
  SS5       Hx  n[        US-
  US5       Ha  nX%   U   R                   HI  nX#   U   R                   H1  nU R                  Xg5       H  nX#   U   R                  U5        M     M3     MK     Mc     Mz     M‘     U[        U R                  5         S   R                   Hf  n	Sn
[        [        U5      5       H,  nU
SUS-   X   X   SSSU	R                  U   S-   SS	S	4
-  -  n
M.     [        U
5      nUR                  5       v •  Mh     g
7f)a  
Performs a projective dependency parse on the list of tokens using
a chart-based, span-concatenation algorithm similar to Eisner (1996).

:param tokens: The list of input tokens.
:type tokens: list(str)
:return: An iterator over parse trees.
:rtype: iter(Tree)
r   é   éÿÿÿÿÚnullé   Ú ú	%d	%s	%s	%s	%s	%s	%d	%s	%s	%s
ÚROOTÚ-N)ÚlistÚ_tokensr+   r,   ÚappendrJ   rU   r   rP   Úconcatenater   r
   Útree)r   ÚtokensÚchartr.   ÚjÚkÚspan1Úspan2ÚnewspanÚparseÚconll_formatÚdgs               r   rx   Ú ProjectiveDependencyParser.parse±   sé  é € ô ˜F“|ˆŒØˆÜ�qœ#˜dŸl™lÓ+¨aÑ/Ö0ˆAØ�L‰L˜ÔÜ˜1œc $§,¡,Ó/°!Ñ3Ö4�Ø‘—‘¤	¨!£Ô0Ø˜A™•:Ø‘H˜Q‘K—O‘O¤N°1°q±5¸!À¹UÀRÀDÈ6È(Ó$SÖTó 5ñ 1ô �qœ#˜dŸl™lÓ+¨aÑ/Ö0ˆAÜ˜1˜q™5 " bÖ)�Ü˜q 1™u a¨Ö,�AØ!&¡¨!¡×!5Ô!5˜Ø%*¡X¨a¡[×%9Ô%9˜EØ+/×+;Ñ+;¸EÖ+I Ø %¡¨¡§¡°Ö 8ó ,Jó &:ó "6ó -ó *ñ 1ð œ3˜tŸ|™|Ó,Ñ-¨aÑ0×9Ô9ˆEØˆLäœ3˜v›;Ö'�ð Ð LØ˜‘EØ‘IØ‘IØØØØ—K‘K ‘N QÑ&ØØØðPñ !ñ ’ñ	 (ô  ! Ó.ˆBà—'‘'“)ŒOò+ :ùs   ‚BHÂE7Hc           	      óê  • / nUR                   UR                   :X  a  [        S5        UR                   UR                   :”  a  UnUnUnUR                  UR                  -   nUR                  UR                  -   nU R                  R                  U R                  UR                     U R                  UR                     5      (       aa  UR                  XRR                  UR                   -
  '   UR                  [        UR                   UR                  UR                  UU5      5        UR                  UR                  -   nU R                  R                  U R                  UR                     U R                  UR                     5      (       aa  UR                  XQR                  UR                   -
  '   UR                  [        UR                   UR                  UR                  UU5      5        U$ ©aÉ  
Concatenates the two spans in whichever way possible.  This
includes rightward concatenation (from the leftmost word of the
leftmost span to the rightmost word of the rightmost span) and
leftward concatenation (vice-versa) between adjacent spans.  Unlike
Eisner's presentation of span concatenation, these spans do not
share or pivot on a particular word/word-index.

:return: A list of new spans formed through concatenation.
:rtype: list(DependencySpan)
z8Error: Mismatched spans - replace this with thrown error©r   Úprintr   r   r`   Úcontainsrm   r   rn   r   r   ©r   ru   rv   ÚspansÚ	temp_spanÚnew_arcsÚnew_tagss          r   ro   Ú&ProjectiveDependencyParser.concatenateã   s¬  € ð ˆØ×Ñ ×!3Ñ!3Ó3ÜÐLÔMØ×Ñ × 2Ñ 2Ó2ØˆIØˆEØˆEà—;‘; §¡Ñ,ˆØ—;‘; §¡Ñ,ˆØ�=‰=×!Ñ!Ø�L‰L˜×*Ñ*Ñ+¨T¯\©\¸%×:KÑ:KÑ-L÷
ñ 
ð @E×?PÑ?PˆH×&Ñ&¨×);Ñ);Ñ;Ñ<Ø�L‰LÜØ×&Ñ&Ø×$Ñ$Ø×%Ñ%ØØóôð —;‘; §¡Ñ,ˆØ�=‰=×!Ñ!Ø�L‰L˜×*Ñ*Ñ+¨T¯\©\¸%×:KÑ:KÑ-L÷
ñ 
ð @E×?PÑ?PˆH×&Ñ&¨×);Ñ);Ñ;Ñ<Ø�L‰LÜØ×&Ñ&Ø×$Ñ$Ø×%Ñ%ØØóôð ˆr"   ©r`   rm   N)	rC   rD   rE   rF   rG   r    rx   ro   rH   r8   r"   r   r]   r]   š   s   † ñ
ò+ò0õd4r"   r]   c                   ó6   • \ rS rSrSrS rS rS rS rS r	Sr
g	)
Ú'ProbabilisticProjectiveDependencyParseri  ag  A probabilistic, projective dependency parser.

This parser returns the most probable projective parse derived from the
probabilistic dependency grammar derived from the train() method.  The
probabilistic model is an implementation of Eisner's (1996) Model C, which
conditions on head-word, head-tag, child-word, and child-tag.  The decoding
uses a bottom-up chart-based span concatenation algorithm that's identical
to the one utilized by the rule-based projective parser.

Usage example

>>> from nltk.parse.dependencygraph import conll_data2

>>> graphs = [
... DependencyGraph(entry) for entry in conll_data2.split('\n\n') if entry
... ]

>>> ppdp = ProbabilisticProjectiveDependencyParser()
>>> ppdp.train(graphs)

>>> sent = ['Cathy', 'zag', 'hen', 'wild', 'zwaaien', '.']
>>> list(ppdp.parse(sent))
[Tree('zag', ['Cathy', 'hen', Tree('zwaaien', ['wild', '.'])])]

c                 ó   • g)zX
Create a new probabilistic dependency parser.  No additional
operations are necessary.
Nr8   r$   s    r   r    Ú0ProbabilisticProjectiveDependencyParser.__init__:  s   � r"   c                 óÀ  • [        U5      U l        / n[        S[        U R                  5      S-   5       Hê  nUR	                  / 5        [        S[        U R                  5      S-   5       H°  nX#   R	                  [        X45      5        X4S-   :X  d  M)  XS-
     U R                  R                  ;   aQ  U R                  R                  XS-
         H-  nX#   U   R                  [        US-
  X3S-
  S/U/5      5        M/     M™  [        SXS-
     -  5        / s  s  $    Mì     [        S[        U R                  5      S-   5       H�  n[        US-
  SS5       Hx  n[        US-
  US5       Ha  nX&   U   R                   HI  nX#   U   R                   H1  nU R                  Xx5       H  n	X#   U   R                  U	5        M     M3     MK     Mc     Mz     M‘     / n
SnSnU[        U R                  5         S   R                   HÀ  nSnSn[        [        U5      5       Hd  nUSX   S	UR                  U   S-   S	4-  -  nUS
US-   X   X   UR                  U   UR                  U   S	UR                  U   S-   SSS4
-  -  nMf     [        U5      nU R                  U5      nU
R	                  UUR!                  5       45        MÂ     U
R#                  5         S U
 5       $ )a(  
Parses the list of tokens subject to the projectivity constraint
and the productions in the parser's grammar.  This uses a method
similar to the span-concatenation algorithm defined in Eisner (1996).
It returns the most probable parse derived from the parser's
probabilistic dependency grammar.
r   rd   re   z7No tag found for input token '%s', parse is impossible.rg   Nrh   z%s	%s	%d	%s
rf   ri   rj   rk   c              3   ó*   #   • U  H	  u  pUv •  M     g 7fr   r8   )Ú.0Úscorerp   s      r   Ú	<genexpr>Ú@ProbabilisticProjectiveDependencyParser.parse.<locals>.<genexpr>€  s   é € Ð0ª%™˜%•ª%ùs   ‚)rl   rm   r+   r,   rn   rJ   r`   r   rU   r   r   rP   ro   r   r
   Úcompute_probrp   Úsort)r   rq   rr   r.   rs   Útagrt   ru   rv   rw   ÚtreesÚ	max_parseÚ	max_scorerx   ry   Úmalt_formatrz   r�   s                     r   rx   Ú-ProbabilisticProjectiveDependencyParser.parse@  sË  € ô ˜F“|ˆŒØˆÜ�qœ#˜dŸl™lÓ+¨aÑ/Ö0ˆAØ�L‰L˜ÔÜ˜1œc $§,¡,Ó/°!Ñ3Ö4�Ø‘—‘¤	¨!£Ô0Ø˜A™•:Ø !™e‘}¨¯©×(;Ñ(;Ó;Ø#'§=¡=×#6Ñ#6°vÀ!¹e±}Ô#E˜CØ!™H Q™KŸO™OÜ .¨q°1©u°a¸Q¹ÀÀÀsÀeÓ Löó $Fô
 ØUØ$¨¡U™mñ,ôð  "œ	ó 5ñ 1ô  �qœ#˜dŸl™lÓ+¨aÑ/Ö0ˆAÜ˜1˜q™5 " bÖ)�Ü˜q 1™u a¨Ö,�AØ!&¡¨!¡×!5Ô!5˜Ø%*¡X¨a¡[×%9Ô%9˜EØ+/×+;Ñ+;¸EÖ+I Ø %¡¨¡§¡°Ö 8ó ,Jó &:ó "6ó -ó *ñ 1ð ˆØˆ	Øˆ	Øœ3˜tŸ|™|Ó,Ñ-¨aÑ0×9Ô9ˆEØˆLØˆKÜœ3˜v›;Ö'�ØÐ1Ø‘IØØ—K‘K ‘N QÑ&Øð	5ñ  ñ �ð Ð LØ˜‘EØ‘IØ‘IØ—K‘K ‘NØ—K‘K ‘NØØ—K‘K ‘N QÑ&ØØØðPñ !ñ ’ñ (ô* ! Ó.ˆBØ×%Ñ% bÓ)ˆEØ�L‰L˜% §¡£Ð+Ö,ñ5 :ð6 	�
‰
ŒÙ0©%Ó0Ð0r"   c           	      ó  • / nUR                   UR                   :X  a  [        S5        UR                   UR                   :”  a  UnUnUnUR                  UR                  -   nUR                  UR                  -   nU R                  R                  U R                  UR                     U R                  UR                     5      (       aa  UR                  XRR                  UR                   -
  '   UR                  [        UR                   UR                  UR                  UU5      5        UR                  UR                  -   nUR                  UR                  -   nU R                  R                  U R                  UR                     U R                  UR                     5      (       aa  UR                  XQR                  UR                   -
  '   UR                  [        UR                   UR                  UR                  UU5      5        U$ r}   r~   r�   s          r   ro   Ú3ProbabilisticProjectiveDependencyParser.concatenate‚  s¾  € ð ˆØ×Ñ ×!3Ñ!3Ó3ÜÐLÔMØ×Ñ × 2Ñ 2Ó2ØˆIØˆEØˆEà—;‘; §¡Ñ,ˆØ—;‘; §¡Ñ,ˆØ�=‰=×!Ñ!Ø�L‰L˜×*Ñ*Ñ+¨T¯\©\¸%×:KÑ:KÑ-L÷
ñ 
ð @E×?PÑ?PˆH×&Ñ&¨×);Ñ);Ñ;Ñ<Ø�L‰LÜØ×&Ñ&Ø×$Ñ$Ø×%Ñ%ØØóôð —;‘; §¡Ñ,ˆØ—;‘; §¡Ñ,ˆØ�=‰=×!Ñ!Ø�L‰L˜×*Ñ*Ñ+¨T¯\©\¸%×:KÑ:KÑ-L÷
ñ 
ð @E×?PÑ?PˆH×&Ñ&¨×);Ñ);Ñ;Ñ<Ø�L‰LÜØ×&Ñ&Ø×$Ñ$Ø×%Ñ%ØØóôð ˆr"   c           
      ó¦  • / n[        [        5      n0 nU GH¦  n[        S[        UR                  5      5       GH~  n[        [        R                  " UR                  U   S   R                  5       5      5      nUR                  U5      nUR                  U5      n	X‰-   n
[        SUS-   -
  U	S-   5       GHý  nUR                  U   S   nUR                  U   S   nXÄ;   a  XL   R                  U5        OU1XL'   SnSnSnSnUS:  aÐ  X¸-   nUS:¼  a*  UR                  UU      S   nUR                  UU      S   nUS	:w  a0  UR                  UUS-         S   nUR                  UUS-         S   nUS:w  a  UR                  [        XÎ/5      5        S
R                  UUUUU5      nSR                  UUU5      nUU==   S-  ss'   UU==   S-  ss'   GM$  US:”  d  GM-  X¸-   S-
  nUU
:  a*  UR                  UU      S   nUR                  UU      S   nUS:w  a0  UR                  UUS-
        S   nUR                  UUS-
        S   nUS:w  a  UR                  [        XÎ/5      5        SR                  UUUUU5      nSR                  UUU5      nUU==   S-  ss'   UU==   S-  ss'   GM      GM�     GM©     [        X#U5      U l        g)aK  
Trains a ProbabilisticDependencyGrammar based on the list of input
DependencyGraphs.  This model is an implementation of Eisner's (1996)
Model C, which derives its statistics from head-word, head-tag,
child-word, and child-tag relationships.

:param graphs: A list of dependency graphs to train from.
:type: list(DependencyGraph)
rd   Údepsr   rg   Úwordr”   ÚSTOPÚSTARTre   ú'(head ({} {}) (mods ({}, {}, {}) left))ú(mods ({}, {}, {}) left))ú((head ({} {}) (mods ({}, {}, {}) right))ú(mods ({}, {}, {}) right))N)r   Úintr+   r,   Únodesrl   r   Úfrom_iterableÚvaluesÚleft_childrenÚright_childrenrU   rn   r   Úformatr   r`   )r   ÚgraphsÚproductionsÚeventsr   rz   Ú
node_indexÚchildrenÚnr_left_childrenÚnr_right_childrenÚnr_childrenÚchild_indexÚ	head_wordÚhead_tagÚchildÚ	child_tagÚ	prev_wordÚprev_tagÚarray_indexÚ
head_eventÚ	mod_events                        r   ÚtrainÚ-ProbabilisticProjectiveDependencyParser.train·  s/  € ð ˆÜœSÓ!ˆØˆÜˆBÜ# A¤s¨2¯8©8£}×5�
äÜ×'Ò'¨¯©°Ñ(<¸VÑ(D×(KÑ(KÓ(MÓNó�ð $&×#3Ñ#3°JÓ#?Ð Ø$&×$5Ñ$5°jÓ$AÐ!Ø.ÑB�Ü#(ØÐ)¨AÑ-Ñ.Ð0AÀAÑ0E÷$�Kð !#§¡¨Ñ 4°VÑ <�IØ!Ÿx™x¨
Ñ3°EÑ:�HØ Ó(Ø™×+Ñ+¨HÕ5à+3¨*˜™Ø"�EØ &�IØ '�IØ&�HØ" Q“Ø&1Ñ&D˜Ø&¨!Ó+Ø$&§H¡H¨X°kÑ-BÑ$CÀFÑ$K˜EØ(*¯©°¸+Ñ1FÑ(GÈÑ(N˜IØ&¨"Ó,Ø(*¯©°¸+È¹/Ñ1JÑ(KÈFÑ(S˜IØ')§x¡x°¸Àq¹Ñ0IÑ'JÈ5Ñ'Q˜HØ  F›?Ø'×.Ñ.Ô/CÀIÈwÓ/WÔXØ%N×%UÑ%UØ!Ø%Ø$Ø%Ø$ó&˜
ð %@×$FÑ$FØ$Ø%Ø$ó%˜	ð
 ˜zÓ*¨aÑ/Ó*Ø˜yÓ)¨QÑ.Ö)Ø$ qžØ&1Ñ&DÀqÑ&H˜Ø&¨Ó4Ø$&§H¡H¨X°kÑ-BÑ$CÀFÑ$K˜EØ(*¯©°¸+Ñ1FÑ(GÈÑ(N˜IØ&¨!Ó+Ø(*¯©°¸+È¹/Ñ1JÑ(KÈFÑ(S˜IØ')§x¡x°¸Àq¹Ñ0IÑ'JÈ5Ñ'Q˜HØ  F›?Ø'×.Ñ.Ô/CÀIÈwÓ/WÔXØ%O×%VÑ%VØ!Ø%Ø$Ø%Ø$ó&˜
ð %A×$GÑ$GØ$Ø%Ø$ó%˜	ð
 ˜zÓ*¨aÑ/Ó*Ø˜yÓ)¨QÑ.Ö)ôy$ô 6ñ ôN 7°{ÈDÓQˆ�r"   c           	      ó(  • Sn[        S[        UR                  5      5       GHk  n[        [        R
                  " UR                  U   S   R                  5       5      5      nUR                  U5      nUR                  U5      nXV-   n[        SUS-   -
  US-   5       GHê  nUR                  U   S   n	UR                  U   S   n
SnSnS	nS	nUS:  aÖ  X…-   nUS:¼  a(  UR                  XO      S   nUR                  XO      S   nUS
:w  a.  UR                  XOS-         S   nUR                  XOS-         S   nSR                  UUUU	U
5      nSR                  UU	U
5      nU R                  R                  U   nU R                  R                  U   nUS:w  a  UUU-  -  nGM  SnGM  US:”  d  GM  X…-   S-
  nX÷:  a(  UR                  XO      S   nUR                  XO      S   nUS:w  a.  UR                  XOS-
        S   nUR                  XOS-
        S   nSR                  UUUU	U
5      nSR                  UU	U
5      nU R                  R                  U   nU R                  R                  U   nUS:w  a  UUU-  -  nGMè  SnGMí     GMn     U$ )a  
Computes the probability of a dependency graph based
on the parser's probability model (defined by the parser's
statistical dependency grammar).

:param dg: A dependency graph to score.
:type dg: DependencyGraph
:return: The probability of the dependency graph.
:rtype: int
g      ð?rd   r�   r   rg   rž   r”   rŸ   r    re   r¡   r¢   g:Œ0âŽyE>r£   r¤   )r+   r,   r¦   rl   r   r§   r¨   r©   rª   r«   r`   Ú_events)r   rz   Úprobr¯   r°   r±   r²   r³   r´   rµ   r¶   r·   r¸   r¹   rº   r»   r¼   r½   Úh_countÚm_counts                       r   r’   Ú4ProbabilisticProjectiveDependencyParser.compute_prob  sà  € ð ˆÜ ¤3 r§x¡x£=×1ˆJäœE×/Ò/°·±¸Ñ0DÀVÑ0L×0SÑ0SÓ0UÓVÓWˆHà!×/Ñ/°
Ó;ÐØ "× 1Ñ 1°*Ó =ÐØ*Ñ>ˆKÜ$ QÐ*:¸QÑ*>Ñ%?ÐARÐUVÑAV×W�ØŸH™H ZÑ0°Ñ8�	ØŸ8™8 JÑ/°Ñ6�Ø�Ø"�	Ø#�	Ø"�Ø “?Ø"-Ñ"@�KØ" aÓ'Ø "§¡¨Ñ)>Ñ ?ÀÑ G˜Ø$&§H¡H¨XÑ-BÑ$CÀEÑ$J˜	Ø" bÓ(Ø$&§H¡H¨XÀA±oÑ-FÑ$GÈÑ$O˜	Ø#%§8¡8¨HÀ1±_Ñ,EÑ#FÀuÑ#M˜Ø!J×!QÑ!QØØ!Ø Ø!Ø ó"�Jð !<× BÑ BØ Ø!Ø ó!�Ið
 #Ÿm™m×3Ñ3°JÑ?�GØ"Ÿm™m×3Ñ3°IÑ>�Gð  !“|Ø ¨'Ñ 1Ñ1›à)›à  1–_Ø"-Ñ"@À1Ñ"D�KØ"Ó0Ø "§¡¨Ñ)>Ñ ?ÀÑ G˜Ø$&§H¡H¨XÑ-BÑ$CÀEÑ$J˜	Ø" aÓ'Ø$&§H¡H¨XÀA±oÑ-FÑ$GÈÑ$O˜	Ø#%§8¡8¨HÀ1±_Ñ,EÑ#FÀuÑ#M˜Ø!K×!RÑ!RØØ!Ø Ø!Ø ó"�Jð !=× CÑ CØ Ø!Ø ó!�Ið
 #Ÿm™m×3Ñ3°JÑ?�GØ"Ÿm™m×3Ñ3°IÑ>�Gà !“|Ø ¨'Ñ 1Ñ1›à)›ô}  Xñ 2ðN ˆr"   r‡   N)rC   rD   rE   rF   rG   r    rx   ro   r¾   r’   rH   r8   r"   r   r‰   r‰     s'   † ñò4ò@1òD3òjTRõlSr"   r‰   c                  ó,   • [        5         [        5         g r   )Úprojective_rule_parse_demoÚprojective_prob_parse_demor8   r"   r   ÚdemorÉ   h  s   € ÜÔ äÕ r"   c                  óª   • [         R                  " S5      n [        U 5        [        U 5      nUR	                  / SQ5      nU H  n[        U5        M     g)zs
A demonstration showing the creation and use of a
``DependencyGrammar`` to perform a projective dependency
parse.
zP
    'scratch' -> 'cats' | 'walls'
    'walls' -> 'the'
    'cats' -> 'the'
    )ÚtheÚcatsÚscratchrË   ÚwallsN)r   Ú
fromstringr   r]   rx   ©ÚgrammarÚpdpr•   rp   s       r   rÇ   rÇ   n  sL   € ô  ×*Ò*ð	ó€Gô 
ˆ'„NÜ
$ WÓ
-€CØ�I‰IÒ@ÓA€EÛˆÜˆdŽò r"   c                  ó|  • [        5         [        S5        [        S5        [        S5        [        R                  " S5      n [        U 5        [        5         [        S5        [        S5        [        U 5      nUR	                  / SQ5      nU H  n[        U5        M     [        5         [        S5        [        S	5        [        S
5        [        S5        [        R                  " S5      n [        U 5        [        5         [        S5        [        U 5      nUR	                  / SQ5      nU H  n[        U5        M     g)zì
A demonstration showing the creation of a ``DependencyGrammar``
in which a specific number of modifiers is listed for a given
head.  This can further constrain the number of possible parses
created by a ``ProjectiveDependencyParser``.
z>A grammar with no arity constraints. Each DependencyProductionz;specifies a relationship between one head word and only onezmodifier word.zj
    'fell' -> 'price' | 'stock'
    'price' -> 'of' | 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    z<For the sentence 'The price of the stock fell', this grammarz(will produce the following three parses:)rË   ÚpriceÚofrË   ÚstockÚfellz.By contrast, the following grammar contains a z2DependencyProduction that specifies a relationshipz5between a single head word, 'price', and two modifierzwords, 'of' and 'the'.zh
    'fell' -> 'price' | 'stock'
    'price' -> 'of' 'the'
    'of' -> 'stock'
    'stock' -> 'the'
    z:This constrains the number of possible parses to just one:N)r   r   rÏ   r]   rx   rÐ   s       r   Úarity_parse_demorØ   ‚  s  € ô 
„GÜ	Ð
JÔKÜ	Ð
GÔHÜ	Ð
ÔÜ×*Ò*ð	ó€Gô 
ˆ'„Nä	„GÜ	Ð
HÔIÜ	Ð
4Ô5Ü
$ WÓ
-€CØ�I‰IÒDÓE€EÛˆÜˆdŽñ ô 
„GÜ	Ð
:Ô;Ü	Ð
>Ô?Ü	Ð
AÔBÜ	Ð
"Ô#Ü×*Ò*ð	ó€Gô 
ˆ'„Nä	„GÜ	ØDôô % WÓ
-€CØ�I‰IÒDÓE€EÛˆÜˆdŽò r"   c                  óf  • SSK Jn   U R                  S5       Vs/ s H  o(       d  M  [        U5      PM     nn[	        5       n[        S5        UR                  U5        / SQn[        SSR                  U5      S5        [        S	5        UR                  U5       H  n[        U5        M     g
s  snf )zH
A demo showing the training and use of a projective
dependency parser.
r   )Úconll_data2z

z6Training Probabilistic Projective Dependency Parser...)ÚCathyÚzagÚhenÚwildÚzwaaienÚ.z	Parsing 'Ú z'...zParse:N)	Únltk.parse.dependencygraphrÚ   Úsplitr
   r‰   r   r¾   Újoinrx   )rÚ   Úentryr¬   ÚppdpÚsentrp   s         r   rÈ   rÈ   ¸  s�   € õ
 7à2=×2CÑ2CÀFÔ2KÓUÒ2K¨ËuÓ$Œo˜eÖ$Ñ2K€FÐUÜ2Ó4€DÜ	Ð
BÔCØ‡J�JˆvÔâ:€DÜ	ˆ+�s—x‘x “~ vÔ.Ü	ˆ(„OØ—
‘
˜4Ö ˆÜˆdŽò !ùò Vs
   š
B.¨B.Ú__main__N)Úcollectionsr   Ú	functoolsr   Ú	itertoolsr   Únltk.grammarr   r   r   Únltk.internalsr	   râ   r
   r   rJ   r]   r‰   rÉ   rÇ   rØ   rÈ   rC   r8   r"   r   Ú<module>rî      s�   ðõ $Ý $Ý ÷ñ õ
 3Ý 6ð ÷Fð Fó ðF÷\(ñ (÷`}ñ }÷JAñ AòR
!òò(3òlð& ˆzÓÙ…Fð r"   