ó
    ´OÂfS@  ã                   ó®   • S SK r S SKr S SKrS SKJr  Su  rrSu  rr	S /r
 " S S\5      r " S S5      r " S S	5      rSS
 jrSS jrg! \ a     NEf = f)é    N)Ú
TokenizerI)r   é   c            	       ój   • \ rS rSrSrSS\S\SS\S4	S	 jrS
 r	S r
S rS rS rS rS rS rS rSrg)ÚTextTilingTokenizeré   as  Tokenize a document into topical sections using the TextTiling algorithm.
This algorithm detects subtopic shifts based on the analysis of lexical
co-occurrence patterns.

The process starts by tokenizing the text into pseudosentences of
a fixed size w. Then, depending on the method used, similarity
scores are assigned at sentence gaps. The algorithm proceeds by
detecting the peak differences between these scores and marking
them as boundaries. The boundaries are normalized to the closest
paragraph break and the segmented text is returned.

:param w: Pseudosentence size
:type w: int
:param k: Size (in sentences) of the block used in the block comparison method
:type k: int
:param similarity_method: The method used for determining similarity scores:
   `BLOCK_COMPARISON` (default) or `VOCABULARY_INTRODUCTION`.
:type similarity_method: constant
:param stopwords: A list of stopwords that are filtered out (defaults to NLTK's stopwords corpus)
:type stopwords: list(str)
:param smoothing_method: The method used for smoothing the score plot:
  `DEFAULT_SMOOTHING` (default)
:type smoothing_method: constant
:param smoothing_width: The width of the window used by the smoothing method
:type smoothing_width: int
:param smoothing_rounds: The number of smoothing passes
:type smoothing_rounds: int
:param cutoff_policy: The policy used to determine the number of boundaries:
  `HC` (default) or `LC`
:type cutoff_policy: constant

>>> from nltk.corpus import brown
>>> tt = TextTilingTokenizer(demo_mode=True)
>>> text = brown.raw()[:4000]
>>> s, ss, d, b = tt.tokenize(text)
>>> b
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]
é   é
   Né   r   Fc
                 ó˜   • Uc  SSK Jn  UR                  S5      nU R                  R	                  [        5       5        U R                  S	 g )Nr   )Ú	stopwordsÚenglishÚself)Únltk.corpusr   ÚwordsÚ__dict__ÚupdateÚlocals)
r   ÚwÚkÚsimilarity_methodr   Úsmoothing_methodÚsmoothing_widthÚsmoothing_roundsÚcutoff_policyÚ	demo_modes
             Ú:/usr/lib/python3/dist-packages/nltk/tokenize/texttiling.pyÚ__init__ÚTextTilingTokenizer.__init__@   s;   € ð ÑÝ-à!Ÿ™¨	Ó2ˆIØ�‰×ÑœV›XÔ&Ø�M‰M˜&Ñ!ó    c                 óæ  • UR                  5       nU R                  U5      n[        U5      nSR                  S U 5       5      nU R                  U5      nU R	                  U5      nU H9  nUR
                   V	s/ s H  o™S   U R                  ;  d  M  U	PM     sn	Ul        M;     U R                  Xv5      n
U R                  [        :X  a  U R                  Xz5      nO8U R                  [        :X  a  [        S5      e[        SU R                   S35      eU R                  [        :X  a  U R!                  U5      nO[        SU R                   S35      eU R#                  U5      nU R%                  U5      nU R'                  XU5      n/ nSnU H!  nUS:X  a  M  UR)                  UUU 5        UnM#     UU:  a  UR)                  UUS 5        U(       d  U/nU R*                  (       a  X¼XÞ4$ U$ s  sn	f )	zRReturn a tokenized copy of *text*, where each "token" represents
a separate topic.Ú c              3   ób   #   • U  H%  n[         R                  " S U5      (       d  M!  Uv •  M'     g7f)z[a-z\-' \n\t]N)ÚreÚmatch)Ú.0Úcs     r   Ú	<genexpr>Ú/TextTilingTokenizer.tokenize.<locals>.<genexpr>^   s#   é € ð 
Ú%�!¬¯ªÐ2BÀA×)F�A‰A’~ùs   ‚ /¦	/r   z'Vocabulary introduction not implementedzSimilarity method z not recognizedzSmoothing method N)ÚlowerÚ_mark_paragraph_breaksÚlenÚjoinÚ_divide_to_tokensequencesÚwrdindex_listr   Ú_create_token_tabler   ÚBLOCK_COMPARISONÚ_block_comparisonÚVOCABULARY_INTRODUCTIONÚNotImplementedErrorÚ
ValueErrorr   ÚDEFAULT_SMOOTHINGÚ_smooth_scoresÚ_depth_scoresÚ_identify_boundariesÚ_normalize_boundariesÚappendr   )r   ÚtextÚlowercase_textÚparagraph_breaksÚtext_lengthÚnopunct_textÚnopunct_par_breaksÚtokseqsÚtsÚwiÚtoken_tableÚ
gap_scoresÚsmooth_scoresÚdepth_scoresÚsegment_boundariesÚnormalized_boundariesÚsegmented_textÚprevbÚbs                      r   ÚtokenizeÚTextTilingTokenizer.tokenizeS   s
  € ð Ÿ™›ˆØ×6Ñ6°tÓ<ÐÜ˜.Ó)ˆð
 —w‘wñ 
Ù%ó
ó 
ˆð "×8Ñ8¸ÓFÐà×0Ñ0°Ó>ˆó ˆBà×-Ò-ó Ú-�r°A±¸d¿n¹nÑ1L—Ñ-ñ ˆBÖñ ð
 ×.Ñ.¨wÓKˆð ×!Ñ!Ô%5Ó5Ø×/Ñ/°ÓE‰JØ×#Ñ#Ô'>Ó>Ü%Ð&OÓPÐPäØ$ T×%;Ñ%;Ð$<¸OÐLóð ð × Ñ Ô$5Ó5Ø ×/Ñ/°
Ó;‰MäÐ0°×1FÑ1FÐ0GÀÐWÓXÐXð ×)Ñ)¨-Ó8ˆØ!×6Ñ6°|ÓDÐà $× :Ñ :ØÐ&6ó!
Ðð ˆØˆã&ˆAØ�A‹vÙØ×!Ñ! $ u¨Q -Ô0ØŠEñ	 'ð �;ÓØ×!Ñ! $ u v ,Ô/æØ"˜VˆNà�>�>Ø¨lÐNÐNØÐùòa s   Á:G.ÂG.c                 ól  ^• U4S jn/ n[        U5      S-
  n[        U5       Hó  nSu  pxn	Sn
X`R                  S-
  :  a  US-   nO#XeU R                  -
  :”  a  XV-
  nOU R                  nXU-
  S-   US-     Vs/ s H  oÌR                  PM     nnXS-   Xk-   S-     Vs/ s H  oÌR                  PM     nnT H0  nXs" Xý5      U" Xþ5      -  -  nXƒ" Xý5      S-  -  nX“" Xþ5      S-  -  n	M2      U[        R
                  " X‰-  5      -  n
UR                  U
5        Mõ     U$ s  snf s  snf ! [         a     N-f = f)z&Implements the block comparison methodc                 óh   >^• [        U4S jTU    R                  5      n[        S U 5       5      nU$ )Nc                 ó   >• U S   T;   $ ©Nr   © )ÚoÚblocks    €r   Ú<lambda>ÚHTextTilingTokenizer._block_comparison.<locals>.blk_frq.<locals>.<lambda>¤   s   ø€  q¨¡t¨u¢}r   c              3   ó*   #   • U  H	  oS    v •  M     g7f)r   NrS   )r%   Útsoccs     r   r'   ÚITextTilingTokenizer._block_comparison.<locals>.blk_frq.<locals>.<genexpr>¥   s   é € Ð5ªW E˜Q–xªWùs   ‚)ÚfilterÚts_occurencesÚsum)ÚtokrU   Úts_occsÚfreqrD   s    `  €r   Úblk_frqÚ6TextTilingTokenizer._block_comparison.<locals>.blk_frq£   s0   ù€ ÜÔ4°kÀ#Ñ6F×6TÑ6TÓUˆGÜÑ5©WÓ5Ó5ˆDØˆKr   r   )ç        rc   rc   rc   r
   )r+   Úranger   ÚindexÚmathÚsqrtÚZeroDivisionErrorr:   )r   rA   rD   ra   rE   ÚnumgapsÚcurr_gapÚscore_dividendÚscore_divisor_b1Úscore_divisor_b2ÚscoreÚwindow_sizerB   Úb1Úb2Úts     `             r   r1   Ú%TextTilingTokenizer._block_comparison    sb  ø€ õ	ð
 ˆ
Ü�g“, Ñ"ˆä˜gžˆHØANÑ>ˆNÐ.>ØˆEàŸ&™& 1™*Ó$Ø&¨™l‘Ø d§f¡fÑ,Ó,Ø%Ñ0‘à"Ÿf™f�à%,¸Ñ-CÀaÑ-GÈ(ÐUVÉ,Ñ%WÓXÒ%W˜r—(”(Ñ%WˆBÐXØ%,¸©\¸HÑ<RÐUVÑ<VÑ%WÓXÒ%W˜r—(”(Ñ%WˆBÐXã �Ø '¨!£.±7¸1³>Ñ"AÑA�Ø  G¨A£N°aÑ$7Ñ7Ð Ø  G¨A£N°aÑ$7Ñ7Ò ñ !ðØ&¬¯ªÐ3CÑ3VÓ)WÑW�ð ×Ñ˜eÖ$ñ/ 'ð2 Ðùò YùÚXøô %ó Ùðús   Á7DÂD!Ã*D&Ä&
D3Ä2D3c           	      óp   • [        [        [        R                  " USS 5      U R                  S-   S95      $ )z1Wraps the smooth function from the SciPy CookbookNr   )Ú
window_len)ÚlistÚsmoothÚnumpyÚarrayr   )r   rE   s     r   r6   Ú"TextTilingTokenizer._smooth_scoresÆ   s2   € äÜ”5—;’;˜z©!˜}Ó-¸$×:NÑ:NÐQRÑ:RÑSó
ð 	
r   c                 ó  • Sn[         R                  " S5      nUR                  U5      nSnS/nU HK  nUR                  5       U-
  U:  a  M  UR	                  UR                  5       5        UR                  5       nMM     U$ )zFIdentifies indented text or line breaks as the beginning of
paragraphséd   z[ 	]*
[ 	]*
[ 	]*r   )r#   ÚcompileÚfinditerÚstartr:   )r   r;   ÚMIN_PARAGRAPHÚpatternÚmatchesÚ
last_breakÚpbreaksÚpbs           r   r*   Ú*TextTilingTokenizer._mark_paragraph_breaksÌ   sx   € ð ˆÜ—*’*ÐGÓHˆØ×"Ñ" 4Ó(ˆàˆ
Ø�#ˆÛˆBØ�x‰x‹z˜JÑ&¨Ó6Ùà—‘˜rŸx™x›zÔ*ØŸX™X›Z’
ñ ð ˆr   c           
      ó4  • U R                   n/ n[        R                  " SU5      nU H2  nUR                  UR	                  5       UR                  5       45        M4     [        S[        U5      U5       Vs/ s H  n[        Xb-  X6Xb-    5      PM     sn$ s  snf )z3Divides the text into pseudosentences of fixed sizez\w+r   )	r   r#   r~   r:   Úgroupr   rd   r+   ÚTokenSequence)r   r;   r   r.   r‚   r$   Úis          r   r-   Ú-TextTilingTokenizer._divide_to_tokensequencesÞ   sŽ   € à�F‰FˆØˆÜ—+’+˜f dÓ+ˆÛˆEØ× Ñ  %§+¡+£-°·±³Ð!?Ö@ñ ô ˜1œc -Ó0°!Ô4ó
â4�ô ˜!™% °1±5Ð!9Ö:Ù4ñ
ð 	
ùò 
s   Á6Bc                 ó¾  • 0 nSnSnUR                  5       n[        U5      nUS:X  a   [        U5      nU Hõ  n	U	R                   HÝ  u  p« X·:”  a  [        U5      nUS-  nX·:”  a  M  X£;   a£  X:   =R
                  S-  sl        X:   R                  U:w  a   XCU
   l        X:   =R                  S-  sl        X:   R                  U:w  a*  XSU
   l        X:   R                  R                  US/5        M¬  X:   R                  S   S==   S-  ss'   MÊ  [        UUS//SSUUS9X:'   Mß     US-  nM÷     U$ ! [         a  n[        S5      UeSnAff = f! [         a     Nôf = f)z#Creates a table of TokenTableFieldsr   z7No paragraph breaks were found(text too short perhaps?)Nr   éÿÿÿÿ)Ú	first_posr\   Útotal_countÚ	par_countÚlast_parÚlast_tok_seq)Ú__iter__ÚnextÚStopIterationr4   r.   r�   r‘   r�   r’   r\   r:   ÚTokenTableField)r   Útoken_sequencesÚ
par_breaksrD   Úcurrent_parÚcurrent_tok_seqÚpb_iterÚcurrent_par_breakÚerB   Úwordre   s               r   r/   Ú'TextTilingTokenizer._create_token_tableê   sž  € àˆØˆØˆØ×%Ñ%Ó'ˆÜ  ›MÐØ Ó!ðÜ$(¨£MÐ!ó
 "ˆBØ!×/Ô/‘�ðØÓ3Ü,0°«MÐ)Ø# qÑ(˜ð  Õ3ð Ó&ØÑ%×1Ò1°QÑ6Õ1à"Ñ(×1Ñ1°[Ó@Ø5@ DÑ)Ô2Ø#Ñ)×3Ò3°qÑ8Õ3à"Ñ(×5Ñ5¸ÓHØ9H DÑ)Ô6Ø#Ñ)×7Ñ7×>Ñ>ÀÐQRÐ?SÖTà#Ñ)×7Ñ7¸Ñ;¸AÓ>À!ÑCÕ>ä(7Ø"'Ø(7¸Ð';Ð&<Ø$%Ø"#Ø!,Ø%4ñ)�KÓ%ñ-  0ð> ˜qÑ ŠOñA "ðD ÐøôM !ó Ü ØMóàðûðûô %ó áðús)   ©D1 ÁEÄ1
EÄ;EÅEÅ
EÅEc           
      ó  ^
• U Vs/ s H  nSPM     nn[        U5      [        U5      -  n[        R                  " U5      nU R                  [
        :X  a  XE-
  m
OXES-  -
  m
[        [        U[        [        U5      5      5      5      nUR                  5         [        [        U
4S jU5      5      nU HO  nSX8S   '   U H?  n	US   U	S   :w  d  M  [        U	S   US   -
  5      S:  d  M+  X9S      S:X  d  M8  SX8S   '   MA     MQ     U$ s  snf )zBIdentifies boundaries at the peaks of similarity score
differencesr   g       @c                 ó   >• U S   T:„  $ rR   rS   )ÚxÚcutoffs    €r   rV   Ú:TextTilingTokenizer._identify_boundaries.<locals>.<lambda>,  s   ø€  1 Q¡4¨&¢=r   r   é   )r]   r+   rx   Ústdr   ÚLCÚsortedÚziprd   Úreverserv   r[   Úabs)r   rG   r¢   Ú
boundariesÚavgÚstdevÚdepth_tuplesÚhpÚdtÚdt2r£   s             @r   r8   Ú(TextTilingTokenizer._identify_boundaries  s  ø€ ñ ".Ó.¢˜A“a¡ˆ
Ð.ä�,Ó¤# lÓ"3Ñ3ˆÜ—	’	˜,Ó'ˆà×Ñ¤Ó#Ø‘[‰Fà 3™;Ñ&ˆFäœc ,´´c¸,Ó6GÓ0HÓIÓJˆØ×ÑÔÜ”&Ô0°,Ó?Ó@ˆãˆBØ !ˆJ˜!‘uÑÛ�à�q‘E˜S ™V•OÜ˜C ™F R¨¡U™NÓ+¨aÕ/Ø" q¡6Ñ*¨aÕ/à()�J !™uÓ%ó ñ ð Ðùò/ /s   †Dc                 ó  • U Vs/ s H  nSPM     nn[        [        [        U5      S-  S5      S5      nUnXU*   HC  nUnXSS2    H  nX‡:¼  a  UnM    O   Un	XS  H  nX‰:¼  a  Un	M    O   Xy-   SU-  -
  X5'   US-  nME     U$ s  snf )zrCalculates the depth of each gap, i.e. the average difference
between the left and right peaks and the gap's scorer   r	   r
   é   Nr�   r   )ÚminÚmaxr+   )
r   Úscoresr¢   rG   Úclipre   ÚgapscoreÚlpeakrn   Úrpeaks
             r   r7   Ú!TextTilingTokenizer._depth_scores9  s¾   € ñ $*Ó*¢6˜a›¡6ˆÐ*ô
 ”3”s˜6“{ bÑ(¨!Ó,¨aÓ0ˆØˆà T EÓ*ˆHØˆEØ 	 r 	Ô*�Ø“>Ø!’Eáñ	 +ð
 ˆEØ ›�Ø“>Ø!’Eáñ	 (ð
 #(¡-°!°h±,Ñ">ˆLÑØ�Q‰JŠEñ +ð  Ðùò1 +s   …Bc                 ó   • / nSu  pVnSnU H¾  n	US-  nU	S;   a  U(       a  SnUS-  nU	S;  a	  U(       d  SnU[        U5      :  d  M<  U[        XpR                  -  U R                  5      :”  d  Md  X'   S:X  aM  [        U5      n
U H%  nU
[        Xµ-
  5      :”  a  [        Xµ-
  5      n
UnM%    O   WU;  a  UR	                  U5        US-  nMÀ     U$ )zKNormalize the boundaries identified to the original text's
paragraph breaks)r   r   r   Fr   z 	
T)r+   r·   r   r«   r:   )r   r;   r¬   r=   Únorm_boundariesÚ
char_countÚ
word_countÚ	gaps_seenÚ	seen_wordÚcharÚbest_fitÚbrÚbestbrs                r   r9   Ú)TextTilingTokenizer._normalize_boundariesW  så   € ð ˆØ,3Ñ)ˆ
 	Øˆ	ãˆDØ˜!‰OˆJØ�w‹¦9Ø!�	Ø˜a‘�
Ø˜7Ó"®9Ø �	Øœ3˜z›?Õ*¨zÜ�I§¡Ñ&¨¯©Ó/õ0ð Ñ(¨AÓ-ä" 4›y�HÛ.˜Ø#¤c¨"©/Ó&:Ó:Ü'*¨2©?Ó';˜HØ%'šFá!ñ /ð  _Ó4Ø'×.Ñ.¨vÔ6Ø˜Q‘’	ñ+ ð. Ðr   rS   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r0   r5   ÚHCr   rM   r1   r6   r*   r-   r/   r8   r7   r9   Ú__static_attributes__rS   r   r   r   r      s[   † ñ%ðR Ø
Ø*ØØ*ØØØØô"ò&KòZ$òL
òò$

ò0òdò:õ<r   r   c                   ó*   • \ rS rSrSr    SS jrSrg)r–   iy  zWA field in the token table holding parameters for each token,
used later in the processNc                 ód   • U R                   R                  [        5       5        U R                   S	 g ©Nr   )r   r   r   )r   rŽ   r\   r�   r�   r‘   r’   s          r   r   ÚTokenTableField.__init__}  s$   € ð 	�‰×ÑœV›XÔ&Ø�M‰M˜&Ñ!r   rS   )r   r   r   N©rÉ   rÊ   rË   rÌ   rÍ   r   rÏ   rS   r   r   r–   r–   y  s   † ñ!ð ØØØ÷
"r   r–   c                   ó"   • \ rS rSrSrSS jrSrg)r‰   iŠ  z3A token list with its original length and its indexNc                 óŒ   • U=(       d    [        U5      nU R                  R                  [        5       5        U R                  S	 g rÒ   )r+   r   r   r   )r   re   r.   Úoriginal_lengths       r   r   ÚTokenSequence.__init__�  s1   € Ø)×?¬S°Ó-?ˆØ�‰×ÑœV›XÔ&Ø�M‰M˜&Ñ!r   rS   ©NrÔ   rS   r   r   r‰   r‰   Š  s
   † Ù9÷"r   r‰   c                 óÈ  • U R                   S:w  a  [        S5      eU R                  U:  a  [        S5      eUS:  a  U $ US;  a  [        S5      e[        R                  SU S   -  XSS	2   -
  U SU S	   -  U S	U* S	2   -
  4   nUS
:X  a  [        R
                  " US5      nO[        SU-   S-   5      n[        R                  " XDR                  5       -  USS9nXQS-
  U* S-    $ )a„  smooth the data using a window with requested size.

This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the beginning and end part of the output signal.

:param x: the input signal
:param window_len: the dimension of the smoothing window; should be an odd integer
:param window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
    flat window will produce a moving average smoothing.

:return: the smoothed signal

example::

    t=linspace(-2,2,0.1)
    x=sin(t)+randn(len(t))*0.1
    y=smooth(x)

:see also: numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve,
    scipy.signal.lfilter

TODO: the window parameter could be the window itself if an array instead of a string
r   z'smooth only accepts 1 dimension arrays.z1Input vector needs to be bigger than window size.é   )ÚflatÚhanningÚhammingÚbartlettÚblackmanzDWindow is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'r
   r   r�   rÜ   Údznumpy.z(window_len)Úsame)Úmode)	Úndimr4   Úsizerx   Úr_ÚonesÚevalÚconvolver]   )r¢   ru   ÚwindowÚsr   Úys         r   rw   rw   ”  sþ   € ð6 	‡v�v�ƒ{ÜÐBÓCÐCà‡v�v�
ÓÜÐLÓMÐMà�Aƒ~ØˆàÐKÓKÜØRó
ð 	
ô 	�‰��Q�q‘T‘˜A¨¨2˜oÑ.Ñ.°°1°q¸±u±9¸qÀÀZÀKÐPRÐARÑ?SÑ3SÐSÑT€Að �ÓÜ�JŠJ�z 3Ó'‰ä�˜FÑ" ^Ñ3Ó4ˆä�Š�qŸ5™5›7‘{ A¨FÑ3€Aà˜!‰^˜z˜k¨A™oÐ.Ð.r   c                 ó   • SSK Jn  SSKJn  [	        SS9nU c  UR                  5       S S n UR                  U 5      u  pEpgUR                  S5        UR                  S5        UR                  [        [        U5      5      USS	9  UR                  [        [        U5      5      US
S	9  UR                  [        [        U5      5      USS	9  UR                  [        [        U5      5      U5        UR                  5         UR                  5         g )Nr   )Úpylab)ÚbrownT)r   i'  zSentence Gap indexz
Gap Scores)ÚlabelzSmoothed Gap scoreszDepth scores)Ú
matplotlibrî   r   rï   r   ÚrawrM   ÚxlabelÚylabelÚplotrd   r+   ÚstemÚlegendÚshow)r;   rî   rï   Úttrë   Ússrá   rL   s           r   Údemorû   Ê  sË   € Ý å!ä	 tÑ	,€BØ�|Ø�y‰y‹{˜6˜EÐ"ˆØ—+‘+˜dÓ#�K€Aˆ1Ø	‡L�LÐ%Ô&Ø	‡L�L�ÔØ	‡J�JŒu”S˜“V‹}˜a |€JÑ4Ø	‡J�JŒu”S˜“W‹~˜rÐ)>€JÑ?Ø	‡J�JŒu”S˜“V‹}˜a ~€JÑ6Ø	‡J�JŒu”S˜“V‹}˜aÔ Ø	‡L�L„NØ	‡J�J…Lr   )é   rÜ   rÙ   )rf   r#   rx   ÚImportErrorÚnltk.tokenize.apir   r0   r2   r§   rÎ   r5   r   r–   r‰   rw   rû   rS   r   r   Ú<module>rÿ      sz   ðó Û 	ð	Ûõ )à,0Ñ )Ð Ð)Ø	�€€BØ�CÐ ô^˜*ô ^÷B"ñ "÷""ñ "ô3/õløðy ó 	Ùð	ús   ŠA ÁAÁA