ó
    ´OÂfÀn  ã                   ó\  • S r SSKrSSKrSSKrSSKJrJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  SS	KJrJr  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Q5      r " S S5      r  " S S5      r! " S S5      r" " S S5      r# " S S\#5      r$S r%\&S:X  a  \%" 5         / SQr'g)a  
This module brings together a variety of NLTK functionality for
text analysis, and provides simple, interactive interfaces.
Functionality includes: concordancing, collocation discovery,
regular expression search over tokenized strings, and
distributional similarity.
é    N)ÚCounterÚdefaultdictÚ
namedtuple)Úreduce)Úlog)ÚBigramCollocationFinder)ÚMLE)Úpadded_everygram_pipeline)ÚBigramAssocMeasuresÚ	f_measure)ÚConditionalFreqDist)ÚFreqDist)Úsent_tokenize)ÚLazyConcatenationÚ
cut_stringÚ	tokenwrapÚConcordanceLine)ÚleftÚqueryÚrightÚoffsetÚ
left_printÚright_printÚlinec                   óZ   • \ rS rSrSr\S 5       rSSS 4S jrS rS r	SS	 jr
SS
 jrSrg)ÚContextIndexé'   zû
A bidirectional index between words and their 'contexts' in a text.
The context of a word is usually defined to be the words that occur
in a fixed window around the word; but other definitions may also
be used by providing a custom context function.
c                 ó”   • US:w  a  XS-
     R                  5       OSnU[        U 5      S-
  :w  a  XS-      R                  5       OSnX#4$ )z;One left token and one right token, normalized to lowercaser   é   ú*START*ú*END*)ÚlowerÚlen)ÚtokensÚir   r   s       Ú+/usr/lib/python3/dist-packages/nltk/text.pyÚ_default_contextÚContextIndex._default_context/   sM   € ð )*¨Q«ˆv˜!‘e‰}×"Ñ"Ô$°IˆØ)*¬c°&«k¸A©oÓ)=�˜1‘u‘×#Ñ#Ô%À7ˆØˆ}Ðó    Nc                 ó   • U $ ©N© ©Úxs    r&   Ú<lambda>ÚContextIndex.<lambda>6   s   € ÉQr)   c                 óN  ^ ^• UT l         TT l        U(       a  UT l        OT R                  T l        U(       a!  T Vs/ s H  oS" U5      (       d  M  UPM     snm[	        U U4S j[        T5       5       5      T l        [	        U U4S j[        T5       5       5      T l        g s  snf )Nc              3   ón   >#   • U  H*  u  pTR                  U5      TR                  TU5      4v •  M,     g 7fr+   )Ú_keyÚ_context_func©Ú.0r%   ÚwÚselfr$   s      €€r&   Ú	<genexpr>Ú(ContextIndex.__init__.<locals>.<genexpr>?   s1   øé € ð %
ÚFW¹d¸aˆT�Y‰Y�q‹\˜4×-Ñ-¨f°aÓ8Õ9ÒFWùó   ƒ25c              3   ón   >#   • U  H*  u  pTR                  TU5      TR                  U5      4v •  M,     g 7fr+   )r4   r3   r5   s      €€r&   r9   r:   B   s1   øé € ð %
ÚFW¹d¸aˆT×Ñ ¨Ó*¨D¯I©I°a«LÕ9ÒFWùr;   )r3   Ú_tokensr4   r'   ÚCFDÚ	enumerateÚ_word_to_contextsÚ_context_to_words)r8   r$   Úcontext_funcÚfilterÚkeyÚts   ``    r&   Ú__init__ÚContextIndex.__init__6   sŽ   ù€ ØˆŒ	ØˆŒÞØ!-ˆDÕà!%×!6Ñ!6ˆDÔÞÙ!'Ó5¢˜A¨6°!¯9—a¡Ñ5ˆFÜ!$õ %
ÜFOÐPVÔFWó%
ó "
ˆÔô "%õ %
ÜFOÐPVÔFWó%
ó "
ˆÕùò	 6s   ¼B"ÁB"c                 ó   • U R                   $ )zW
:rtype: list(str)
:return: The document that this context index was
    created from.
©r=   ©r8   s    r&   r$   ÚContextIndex.tokensF   ó   € ð �|‰|Ðr)   c                 óÐ   • U R                  U5      n[        U R                  U   5      n0 nU R                  R                  5        H  u  pE[	        U[        U5      5      X4'   M     U$ )z€
Return a dictionary mapping from words to 'similarity scores,'
indicating how often these two words occur in the same
context.
)r3   Úsetr@   Úitemsr   )r8   ÚwordÚword_contextsÚscoresr7   Ú
w_contextss         r&   Úword_similarity_dictÚ!ContextIndex.word_similarity_dictN   s_   € ð �y‰y˜‹ˆÜ˜D×2Ñ2°4Ñ8Ó9ˆàˆØ!×3Ñ3×9Ñ9Ö;‰MˆAÜ! -´°Z³ÓAˆF‹Iñ <ð ˆr)   c                 ó0  • [        [        5      nU R                  U R                  U5          HO  nU R                  U    H9  nXQ:w  d  M
  X5==   U R                  U   U   U R                  U   U   -  -  ss'   M;     MQ     [        X3R                  SS9S U $ )NT)rD   Úreverse)r   Úintr@   r3   rA   ÚsortedÚget)r8   rP   ÚnrR   Úcr7   s         r&   Úsimilar_wordsÚContextIndex.similar_words]   s“   € ÜœSÓ!ˆØ×'Ñ'¨¯	©	°$«Ô8ˆAØ×+Ñ+¨AÔ.�Ø•9Ø“IØ×.Ñ.¨qÑ1°$Ñ7¸$×:PÑ:PÐQRÑ:SÐTUÑ:VÑVñ•Ió /ñ 9ô �f§*¡*°dÑ;¸B¸QÐ?Ð?r)   c                 óð  ^ ^• U Vs/ s H  nT R                  U5      PM     nnU Vs/ s H  n[        T R                  U   5      PM     nn[        [	        U5      5       Vs/ s H  oTU   (       a  M  X   PM     nn[        [        R                  U5      mU(       a"  U(       a  [        SSR                  U5      5      eT(       d
  [        5       $ [        UU 4S jU 5       5      nU$ s  snf s  snf s  snf )ag  
Find contexts where the specified words can all appear; and
return a frequency distribution mapping each context to the
number of times that context was used.

:param words: The words used to seed the similarity search
:type words: str
:param fail_on_unknown: If true, then raise a value error if
    any of the given words do not occur at all in the index.
z%The following word(s) were not found:Ú c              3   ód   >#   • U  H%  nTR                   U     H  o"T;   d  M
  Uv •  M     M'     g 7fr+   )r@   )r6   r7   r\   Úcommonr8   s      €€r&   r9   Ú/ContextIndex.common_contexts.<locals>.<genexpr>|   s.   øé € ð Ú �a¨$×*@Ñ*@ÀÕ*C QÈFÁ{—‘Ñ*C‘š5ùs   ƒ0£0)
r3   rN   r@   Úranger#   r   ÚintersectionÚ
ValueErrorÚjoinr   )	r8   ÚwordsÚfail_on_unknownr7   Úcontextsr%   ÚemptyÚfdrb   s	   `       @r&   Úcommon_contextsÚContextIndex.common_contextsg   sÍ   ù€ ñ (-Ó-¢u !�—‘˜1–¡uˆÐ-Ù<AÓBºE°q”C˜×.Ñ.¨qÑ1Ö2¹EˆÐBÜ#(¬¨U«Ô#4ÓHÒ#4˜aÀQ½K“�”Ñ#4ˆÐHÜœ×(Ñ(¨(Ó3ˆÞ–_ÜÐDÀcÇhÁhÈuÃoÓVÐVÞä“:Ðäõ Ù óó ˆBð ˆIùò .ùÚBùÚHs   ‡C)¨"C.Á"C3Á3C3)r4   rA   r3   r=   r@   ©é   )F)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ústaticmethodr'   rF   r$   rT   r]   rm   Ú__static_attributes__r,   r)   r&   r   r   '   s>   † ñð ñó ðð -1¸Á;ô 
ò òô@÷r)   r   c                   óL   • \ rS rSrSrS 4S jrS rS rS rSS jr	SS	 jr
S
rg)ÚConcordanceIndexé‚   zg
An index that can be used to look up the offset locations at which
a given word occurs in a document.
c                 ó   • U $ r+   r,   r-   s    r&   r/   ÚConcordanceIndex.<lambda>ˆ   s   € ©Qr)   c                 óÐ   • Xl          X l         [        [        5      U l         [        U5       H4  u  p4U R                  U5      nU R                  U   R                  U5        M6     g)a¡  
Construct a new concordance index.

:param tokens: The document (list of tokens) that this
    concordance index was created from.  This list can be used
    to access the context of a given word occurrence.
:param key: A function that maps each token to a normalized
    version that will be used as a key in the index.  E.g., if
    you use ``key=lambda s:s.lower()``, then the index will be
    case-insensitive.
N)r=   r3   r   ÚlistÚ_offsetsr?   Úappend)r8   r$   rD   ÚindexrP   s        r&   rF   ÚConcordanceIndex.__init__ˆ   s\   € ð Œð	 ð Œ	ØDä#¤DÓ)ˆŒØLä$ VÖ,‰KˆEØ—9‘9˜T“?ˆDØ�M‰M˜$Ñ×&Ñ& uÖ-ò -r)   c                 ó   • U R                   $ )z[
:rtype: list(str)
:return: The document that this concordance index was
    created from.
rI   rJ   s    r&   r$   ÚConcordanceIndex.tokens¢   rL   r)   c                 óB   • U R                  U5      nU R                  U   $ )z¼
:rtype: list(int)
:return: A list of the offset positions at which the given
    word occurs.  If a key function was specified for the
    index, then given word's key will be looked up.
)r3   r   ©r8   rP   s     r&   ÚoffsetsÚConcordanceIndex.offsetsª   s    € ð �y‰y˜‹ˆØ�}‰}˜TÑ"Ð"r)   c                 ó\   • S[        U R                  5      [        U R                  5      4-  $ )Nz+<ConcordanceIndex for %d tokens (%d types)>)r#   r=   r   rJ   s    r&   Ú__repr__ÚConcordanceIndex.__repr__´   s-   € Ø<Ü�—‘ÓÜ�—‘Óð@
ñ 
ð 	
r)   c           
      ó^  • [        U[        5      (       a  UnOU/nSR                  U5      n[        S U 5       5      nX%-
  S-
  S-  nUS-  n/ nU R	                  US   5      n	[        USS 5       HE  u  p¡U R	                  U5       Vs1 s H
  o»U
-
  S-
  iM     nn[        UR                  U	5      5      n	MG     U	(       aæ  U	 Hà  n
SR                  U R                  Xª[        U5      -    5      nU R                  [        SX§-
  5      U
 nU R                  U
[        U5      -   X§-    n[        SR                  U5      U* 5      R                  U5      n[        SR                  U5      U5      nSR                  UUU/5      n[        UUUU
UUU5      nUR                  U5        Mâ     U$ s  snf )zs
Find all concordance lines given the query word.

Provided with a list of words, these will be found as a phrase.
r`   c              3   ó`   #   • U  H$  n[         R                  " U5      (       a  M   S v •  M&     g7f)r   N)ÚunicodedataÚ	combining)r6   Úchars     r&   r9   Ú4ConcordanceIndex.find_concordance.<locals>.<genexpr>Æ   s   é € ÐU¢z˜t¼×9NÒ9NÈt×9TŸ™¢zùs   ‚.¥	.é   é   r   r   N)Ú
isinstancer~   rg   Úsumr‡   r?   rY   re   r=   r#   Úmaxr   Úrjustr   r€   )r8   rP   ÚwidthÚphraseÚ
phrase_strÚ
phrase_lenÚ
half_widthÚcontextÚconcordance_listr‡   r%   r   Úword_offsetsÚ
query_wordÚleft_contextÚright_contextr   r   Ú
line_printÚconcordance_lines                       r&   Úfind_concordanceÚ!ConcordanceIndex.find_concordanceº   s²  € ô �dœD×!Ñ!Ø‰Fà�VˆFà—X‘X˜fÓ%ˆ
ÜÑU¡zÓUÓUˆ
ØÑ(¨1Ñ,°Ñ2ˆ
Ø˜1‘*ˆð ÐØ—,‘,˜v a™yÓ)ˆÜ  ¨¨ Ö,‰GˆAØ9=¿¹ÀdÔ9KÓLÒ9K¨v Q™J¨œNÑ9KˆLÐLÜ˜\×6Ñ6°wÓ?Ó@ŠGñ -ö Û�Ø ŸX™X d§l¡l°1¼3¸v»;±Ð&GÓH�
à#Ÿ|™|¬C°°1±;Ó,?À!ÐD�Ø $§¡¨Q´°V³©_¸q¹{Ð K�ä'¨¯©°Ó(>ÀÀÓL×RÑRØó�
ô )¨¯©°-Ó)@À*ÓM�à ŸX™X z°:¸{Ð&KÓL�
ä#2Ø ØØ!ØØØØó$Ð ð !×'Ñ'Ð(8Ö9ñ- ð.  Ðùò5 Ms   Â
F*c                 ó   • U R                  XS9nU(       d  [        S5        g[        U[        U5      5      n[        SU S[        U5       S35        [	        USU 5       H  u  pV[        UR
                  5        M     g)aa  
Print concordance lines given the query word.
:param word: The target word or phrase (a list of strings)
:type word: str or list
:param lines: The number of lines to display (default=25)
:type lines: int
:param width: The width of each line, in characters (default=80)
:type width: int
:param save: The option to save the concordance.
:type save: bool
)r˜   z
no matcheszDisplaying z of z	 matches:N)r¥   ÚprintÚminr#   r?   r   )r8   rP   r˜   Úlinesrž   r%   r¤   s          r&   Úprint_concordanceÚ"ConcordanceIndex.print_concordanceê   s}   € ð  ×0Ñ0°Ð0ÐCÐæÜ�,Õä˜œsÐ#3Ó4Ó5ˆEÜ�K ˜w d¬3Ð/?Ó+@Ð*AÀÐKÔLÜ'0Ð1AÀ&À5Ð1IÖ'JÑ#�ÜÐ&×+Ñ+Ö,ò (Kr)   )r3   r   r=   N)éP   )r­   é   )rq   rr   rs   rt   ru   rF   r$   r‡   rŠ   r¥   r«   rw   r,   r)   r&   ry   ry   ‚   s+   † ññ
 $/ô .ò4ò#ò
ô. ÷`-r)   ry   c                   ó$   • \ rS rSrSrS rS rSrg)ÚTokenSearcheri  aÂ  
A class that makes it easier to use regular expressions to search
over tokenized strings.  The tokenized string is converted to a
string where tokens are marked with angle brackets -- e.g.,
``'<the><window><is><still><open>'``.  The regular expression
passed to the ``findall()`` method is modified to treat angle
brackets as non-capturing parentheses, in addition to matching the
token boundaries; and to have ``'.'`` not match the angle brackets.
c                 ó>   • SR                  S U 5       5      U l        g )NÚ c              3   ó2   #   • U  H  nS U-   S-   v •  M     g7f)Ú<Ú>Nr,   )r6   r7   s     r&   r9   Ú)TokenSearcher.__init__.<locals>.<genexpr>  s   é € Ð:²6¨a˜C !™G cžM²6ùs   ‚)rg   Ú_raw)r8   r$   s     r&   rF   ÚTokenSearcher.__init__  s   € Ø—G‘GÑ:±6Ó:Ó:ˆ�	r)   c                 óÚ  • [         R                  " SSU5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " XR                  5      nU H<  nUR	                  S5      (       a  M  UR                  S5      (       d  M3  [        S	5      e   U Vs/ s H  o3S
S R                  S5      PM     nnU$ s  snf )ak  
Find instances of the regular expression in the text.
The text is a list of tokens, and a regexp pattern to match
a single token must be surrounded by angle brackets.  E.g.

>>> from nltk.text import TokenSearcher
>>> from nltk.book import text1, text5, text9
>>> text5.findall("<.*><.*><bro>")
you rule bro; telling you bro; u twizted bro
>>> text1.findall("<a>(<.*>)<man>")
monied; nervous; dangerous; white; white; white; pious; queer; good;
mature; white; Cape; great; wise; wise; butterless; white; fiendish;
pale; furious; better; certain; complete; dismasted; younger; brave;
brave; brave; brave
>>> text9.findall("<th.*>{3,}")
thread through those; the thought that; that the thing; the thing
that; that that thing; through these than through; them that the;
through the thick; them that they; thought that the

:param regexp: A regular expression
:type regexp: str
z\sr²   r´   z(?:<(?:rµ   z)>)z	(?<!\\)\.z[^>]z$Bad regexp for TokenSearcher.findallr   éÿÿÿÿz><)ÚreÚsubÚfindallr·   Ú
startswithÚendswithrf   Úsplit©r8   ÚregexpÚhitsÚhs       r&   r½   ÚTokenSearcher.findall  sÀ   € ô0 —’˜˜r 6Ó*ˆÜ—’˜˜i¨Ó0ˆÜ—’˜˜e VÓ,ˆÜ—’˜ f¨fÓ5ˆô �zŠz˜&§)¡)Ó,ˆó ˆAØ—<‘< ×$Ó$¨¯©°C¯«Ü Ð!GÓHÐHñ ñ
 .2Ó2ªT¨�!�B�—‘˜dÖ#©TˆÐ2Øˆùò 3s   ÃC()r·   N)rq   rr   rs   rt   ru   rF   r½   rw   r,   r)   r&   r°   r°     s   † ñò;õ'r)   r°   c                   óâ   • \ rS rSrSrSrSS jrS rS rSS jr	SS	 jr
SS
 jrSS jrS rS rS rSS jrSS jrS rS S jrS!S jrS rS rS r\R2                  " S5      rS rS rS rSrg)"ÚTexti9  a£  
A wrapper around a sequence of simple (string) tokens, which is
intended to support initial exploration of texts (via the
interactive console).  Its methods perform a variety of analyses
on the text's contexts (e.g., counting, concordancing, collocation
discovery), and display the results.  If you wish to write a
program which makes use of these analyses, then you should bypass
the ``Text`` class, and use the appropriate analysis function or
class directly instead.

A ``Text`` is typically initialized from a given document or
corpus.  E.g.:

>>> import nltk.corpus
>>> from nltk.text import Text
>>> moby = Text(nltk.corpus.gutenberg.words('melville-moby_dick.txt'))

TNc                 ó&  • U R                   (       a  [        U5      nXl        U(       a  X l        gSUSS ;   a5  USS R	                  S5      nSR                  S USU  5       5      U l        gSR                  S USS  5       5      S	-   U l        g)
zV
Create a Text object.

:param tokens: The source text.
:type tokens: sequence of str
Ú]Nrp   r`   c              3   ó8   #   • U  H  n[        U5      v •  M     g 7fr+   ©Ústr©r6   Útoks     r&   r9   Ú Text.__init__.<locals>.<genexpr>b  s   é € Ð C²]¨c¤ S§ ²]ùó   ‚r   c              3   ó8   #   • U  H  n[        U5      v •  M     g 7fr+   rË   rÍ   s     r&   r9   rÏ   d  s   é € Ð @²Z¨c¤ S§ ²ZùrÐ   é   z...)Ú_COPY_TOKENSr~   r$   Únamer�   rg   )r8   r$   rÔ   Úends       r&   rF   ÚText.__init__S  s„   € ð ××Ü˜&“\ˆFØŒæØ�IØ�F˜3˜B�KÓØ˜˜"�+×#Ñ# CÓ(ˆCØŸ™Ñ C°V¸A¸c±]Ó CÓCˆD�IàŸ™Ñ @°V¸B¸Q±ZÓ @Ó@À5ÑHˆD�Ir)   c                 ó    • U R                   U   $ r+   )r$   )r8   r%   s     r&   Ú__getitem__ÚText.__getitem__j  s   € Ø�{‰{˜1‰~Ðr)   c                 ó,   • [        U R                  5      $ r+   )r#   r$   rJ   s    r&   Ú__len__ÚText.__len__m  s   € Ü�4—;‘;ÓÐr)   c                 óŽ   • SU R                   ;  a  [        U R                  S S9U l        U R                  R	                  XU5      $ )a|  
Prints a concordance for ``word`` with the specified context window.
Word matching is not case-sensitive.

:param word: The target word or phrase (a list of strings)
:type word: str or list
:param width: The width of each line, in characters (default=80)
:type width: int
:param lines: The number of lines to display (default=25)
:type lines: int

:seealso: ``ConcordanceIndex``
Ú_concordance_indexc                 ó"   • U R                  5       $ r+   ©r"   ©Úss    r&   r/   Ú"Text.concordance.<locals>.<lambda>„  ó
   € ¨1¯7©7¬9r)   ©rD   )Ú__dict__ry   r$   rÞ   r«   ©r8   rP   r˜   rª   s       r&   ÚconcordanceÚText.concordancet  sB   € ð   t§}¡}Ó4Ü&6Ø—‘Ñ!4ñ'ˆDÔ#ð ×&Ñ&×8Ñ8¸ÀeÓLÐLr)   c                 ó’   • SU R                   ;  a  [        U R                  S S9U l        U R                  R	                  X5      SU $ )a~  
Generate a concordance for ``word`` with the specified context window.
Word matching is not case-sensitive.

:param word: The target word or phrase (a list of strings)
:type word: str or list
:param width: The width of each line, in characters (default=80)
:type width: int
:param lines: The number of lines to display (default=25)
:type lines: int

:seealso: ``ConcordanceIndex``
rÞ   c                 ó"   • U R                  5       $ r+   rà   rá   s    r&   r/   Ú'Text.concordance_list.<locals>.<lambda>™  rä   r)   rå   N)ræ   ry   r$   rÞ   r¥   rç   s       r&   rž   ÚText.concordance_list‰  sG   € ð   t§}¡}Ó4Ü&6Ø—‘Ñ!4ñ'ˆDÔ#ð ×&Ñ&×7Ñ7¸ÓDÀVÀeÐLÐLr)   c                 ó¸  ^• SU R                   ;   a   U R                  U:X  a  U R                  U:X  dž  Xl        X l        SSKJn  UR                  S5      m[        R                  " U R                  U5      nUR                  S5        UR                  U4S j5        [        5       n[        UR                  UR                  U5      5      U l        U R                  $ )aŠ  
Return collocations derived from the text, ignoring stopwords.

    >>> from nltk.book import text4
    >>> text4.collocation_list()[:2]
    [('United', 'States'), ('fellow', 'citizens')]

:param num: The maximum number of collocations to return.
:type num: int
:param window_size: The number of tokens spanned by a collocation (default=2)
:type window_size: int
:rtype: list(tuple(str, str))
Ú_collocationsr   )Ú	stopwordsÚenglishr’   c                 óT   >• [        U 5      S:  =(       d    U R                  5       T;   $ )Né   )r#   r"   )r7   Úignored_wordss    €r&   r/   Ú'Text.collocation_list.<locals>.<lambda>¹  s    ø€ ¬s°1«v¸©z×/W¸Q¿W¹W»YÈ-Ñ=WÐ/Wr)   )ræ   Ú_numÚ_window_sizeÚnltk.corpusrð   rh   r   Ú
from_wordsr$   Úapply_freq_filterÚapply_word_filterr   r~   ÚnbestÚlikelihood_ratiorï   )r8   ÚnumÚwindow_sizerð   ÚfinderÚbigram_measuresrô   s         @r&   Úcollocation_listÚText.collocation_list�  s³   ø€ ð ˜tŸ}™}Ó,Ø—	‘	˜SÓ Ø×!Ñ! [Ó0àŒIØ +Ôõ .à%ŸO™O¨IÓ6ˆMÜ,×7Ò7¸¿¹À[ÓQˆFØ×$Ñ$ QÔ'Ø×$Ñ$Ô%WÔXÜ1Ó3ˆOÜ!%Ø—‘˜_×=Ñ=¸sÓCó"ˆDÔð ×!Ñ!Ð!r)   c                 óŒ   • U R                  X5       VVs/ s H  u  p4US-   U-   PM     nnn[        [        USS95        gs  snnf )a•  
Print collocations derived from the text, ignoring stopwords.

    >>> from nltk.book import text4
    >>> text4.collocations() # doctest: +NORMALIZE_WHITESPACE
    United States; fellow citizens; years ago; four years; Federal
    Government; General Government; American people; Vice President; God
    bless; Chief Justice; one another; fellow Americans; Old World;
    Almighty God; Fellow citizens; Chief Magistrate; every citizen; Indian
    tribes; public debt; foreign nations


:param num: The maximum number of collocations to print.
:type num: int
:param window_size: The number of tokens spanned by a collocation (default=2)
:type window_size: int
r`   ú; )Ú	separatorN)r  r¨   r   )r8   rþ   rÿ   Úw1Úw2Úcollocation_stringss         r&   ÚcollocationsÚText.collocationsÀ  sM   € ð( )-×(=Ñ(=¸cÔ(Oô
Ú(O™f˜bˆB�‰H�rŒMÑ(Oð 	ñ 
ô 	ŒiÐ+°tÑ<Õ=ùó
s   •A c                 ó8   • U R                   R                  U5      $ )z:
Count the number of times this word appears in the text.
)r$   Úcountr†   s     r&   r  Ú
Text.countØ  ó   € ð �{‰{× Ñ  Ó&Ð&r)   c                 ó8   • U R                   R                  U5      $ )zA
Find the index of the first occurrence of the word in the text.
)r$   r�   r†   s     r&   r�   Ú
Text.indexÞ  r  r)   c                 ó   • [         er+   )ÚNotImplementedError)r8   Úmethods     r&   ÚreadabilityÚText.readabilityä  s   € ä!Ð!r)   c                 óÒ  ^^^• SU R                   ;  a  [        U R                  S S S9U l        TR	                  5       mU R                  R
                  mTTR                  5       ;   am  [        TT   5      m[        UUU4S jTR                  5        5       5      nUR                  U5       VVs/ s H  u  pEUPM	     nnn[        [        U5      5        g[        S5        gs  snnf )a>  
Distributional similarity: find other words which appear in the
same contexts as the specified word; list most similar words first.

:param word: The word used to seed the similarity search
:type word: str
:param num: The number of words to generate (default=20)
:type num: int
:seealso: ContextIndex.similar_words()
Ú_word_context_indexc                 ó"   • U R                  5       $ r+   )Úisalphar-   s    r&   r/   ÚText.similar.<locals>.<lambda>ö  s
   € ¨a¯i©i¬kr)   c                 ó"   • U R                  5       $ r+   rà   rá   s    r&   r/   r  ö  s
   € ÈÏÉÌr)   )rC   rD   c              3   ób   >#   • U  H$  nTU     H  nUT;   d  M  UT:X  a  M  Uv •  M     M&     g 7fr+   r,   )r6   r7   r\   rj   ÚwcirP   s      €€€r&   r9   ÚText.similar.<locals>.<genexpr>ÿ  s@   øé € ð â)�AØ˜Q��AØ˜‘=ó ð *+¨d©÷ ‘áñ Ú)ùs   ƒ/š/¢/z
No matchesN)ræ   r   r$   r  r"   r@   Ú
conditionsrN   r   Úmost_commonr¨   r   )	r8   rP   rþ   rl   r7   Ú_rh   rj   r  s	    `     @@r&   ÚsimilarÚText.similarè  sÀ   ú€ ð !¨¯©Ó5ä'3Ø—‘Ñ$9Ñ?Rñ(ˆDÔ$ð �z‰z‹|ˆØ×&Ñ&×8Ñ8ˆØ�3—>‘>Ó#Ó#Ü˜3˜t™9“~ˆHÜö àŸ™Ô)óó ˆBð $&§>¡>°#Ô#6Ô7Ò#6™4˜1“QÑ#6ˆEÑ7Ü”)˜EÓ"Õ#ä�,Õùó 8s   Â1C#c                 óˆ  • SU R                   ;  a  [        U R                  S S9U l         U R                  R	                  US5      nU(       d  [        S5        gUR                  U5       VVs/ s H  u  pEUPM	     nnn[        [        S U 5       5      5        gs  snnf ! [         a  n[        U5         SnAgSnAff = f)a  
Find contexts where the specified words appear; list
most frequent common contexts first.

:param words: The words used to seed the similarity search
:type words: str
:param num: The number of words to generate (default=20)
:type num: int
:seealso: ContextIndex.common_contexts()
r  c                 ó"   • U R                  5       $ r+   rà   rá   s    r&   r/   Ú&Text.common_contexts.<locals>.<lambda>  rä   r)   rå   TzNo common contexts were foundc              3   ó6   #   • U  H  u  pUS -   U-   v •  M     g7f)r"  Nr,   )r6   r  r  s      r&   r9   Ú'Text.common_contexts.<locals>.<genexpr>!  s   é € ÐLºO±&°"  S¡¨2¦ºOùs   ‚N)	ræ   r   r$   r  rm   r¨   r!  r   rf   )r8   rh   rþ   rl   r7   r"  Úranked_contextsÚes           r&   rm   ÚText.common_contexts
  s¥   € ð !¨¯©Ó5ä'3Ø—‘Ñ!4ñ(ˆDÔ$ð		Ø×)Ñ)×9Ñ9¸%ÀÓFˆBÞÜÐ5Õ6à13·±ÀÔ1DÔ"EÒ1D©¨£1Ñ1D�Ñ"EÜ”iÑL¹OÓLÓLÕMùó #Føô ó 	Ü�!�H‰Hûð	ús/   ¬.B" ÁB" Á/BÁ=B" ÂB" Â"
CÂ,B<Â<Cc                 ó    • SSK Jn  U" X5        g)zÌ
Produce a plot showing the distribution of the words through the text.
Requires pylab to be installed.

:param words: The words to be plotted
:type words: list(str)
:seealso: nltk.draw.dispersion_plot()
r   )Údispersion_plotN)Ú	nltk.drawr.  )r8   rh   r.  s      r&   r.  ÚText.dispersion_plot&  s   € õ 	.á˜Õ$r)   c                 óT   • [        X!5      u  p4[        US9nUR                  X45        U$ )N)Úorder)r
   r	   Úfit)r8   Útokenized_sentsr[   Ú
train_dataÚpadded_sentsÚmodels         r&   Ú_train_default_ngram_lmÚText._train_default_ngram_lm3  s)   € Ü#<¸QÓ#PÑ ˆ
Ü˜!‘ˆØ�	‰	�*Ô+Øˆr)   c                 ó¤  • [        SR                  U R                  5      5       Vs/ s H  oDR                  S5      PM     snU l        [        U S5      (       d7  [        S[        R                  S9  U R                  U R                  SS9U l
        / nUS:”  d   S5       e[        U5      U:  ac  [        U R                  R                  XUS	95       H&  u  pgUS
:X  a  M  US:X  a    OUR                  U5        M(     US-  n[        U5      U:  a  Mc  U(       a  SR                  U5      S-   OSnU[        USU 5      -   n	[        U	5        U	$ s  snf )a¿  
Print random text, generated using a trigram language model.
See also `help(nltk.lm)`.

:param length: The length of text to generate (default=100)
:type length: int

:param text_seed: Generation can be conditioned on preceding context.
:type text_seed: list(str)

:param random_seed: A random seed or an instance of `random.Random`. If provided,
    makes the random sampling part of generation reproducible. (default=42)
:type random_seed: int
r`   Ú_trigram_modelzBuilding ngram index...)Úfileró   )r[   r   z!The `length` must be more than 0.)Ú	text_seedÚrandom_seedz<s>z</s>r   r²   N)r   rg   r$   rÀ   Ú_tokenized_sentsÚhasattrr¨   ÚsysÚstderrr8  r;  r#   r?   Úgenerater€   r   )
r8   Úlengthr=  r>  ÚsentÚgenerated_tokensÚidxÚtokenÚprefixÚ
output_strs
             r&   rC  ÚText.generate9  sU  € ô" )6°c·h±h¸t¿{¹{Ó6KÔ(Ló!
Ú(L �J‰J�sŽOÑ(Lñ!
ˆÔô �tÐ-×.Ñ.ÜÐ+´#·*±*Ò=Ø"&×">Ñ">Ø×%Ñ%¨ð #?ð #ˆDÔð Ðà˜‹zÐ>Ð>Ó>ˆzÜÐ"Ó# fÓ,Ü'Ø×#Ñ#×,Ñ,Ø¸[ð -ð ö‘
�ð
 ˜E“>ÙØ˜F“?ÙØ ×'Ñ'¨Ö.ñð ˜1ÑˆKô Ð"Ó# fÕ,ö /8�—‘˜)Ó$ sÒ*¸RˆØœiÐ(8¸¸&Ð(AÓBÑBˆ
ÜˆjÔØÐùò9!
s   §Ec                 ó<   • U R                  5       R                  " U6 $ )zK
See documentation for FreqDist.plot()
:seealso: nltk.prob.FreqDist.plot()
)ÚvocabÚplot)r8   Úargss     r&   rN  Ú	Text.plotg  s   € ð
 �z‰z‹|× Ò  $Ð'Ð'r)   c                 óZ   • SU R                   ;  a  [        U 5      U l        U R                  $ )z
:seealso: nltk.prob.FreqDist
Ú_vocab)ræ   r   rR  rJ   s    r&   rM  Ú
Text.vocabn  s%   € ð ˜4Ÿ=™=Ó(ä" 4›.ˆDŒKØ�{‰{Ðr)   c                 óð   • SU R                   ;  a  [        U 5      U l        U R                  R                  U5      nU Vs/ s H  nSR	                  U5      PM     nn[        [        US5      5        gs  snf )aC  
Find instances of the regular expression in the text.
The text is a list of tokens, and a regexp pattern to match
a single token must be surrounded by angle brackets.  E.g.

>>> from nltk.book import text1, text5, text9
>>> text5.findall("<.*><.*><bro>")
you rule bro; telling you bro; u twizted bro
>>> text1.findall("<a>(<.*>)<man>")
monied; nervous; dangerous; white; white; white; pious; queer; good;
mature; white; Cape; great; wise; wise; butterless; white; fiendish;
pale; furious; better; certain; complete; dismasted; younger; brave;
brave; brave; brave
>>> text9.findall("<th.*>{3,}")
thread through those; the thought that; that the thing; the thing
that; that that thing; through these than through; them that the;
through the thick; them that they; thought that the

:param regexp: A regular expression
:type regexp: str
Ú_token_searcherr`   r  N)ræ   r°   rU  r½   rg   r¨   r   rÁ   s       r&   r½   ÚText.findallw  sc   € ð.  D§M¡MÓ1Ü#0°Ó#6ˆDÔ à×#Ñ#×+Ñ+¨FÓ3ˆÙ%)Ó*¢T �—‘˜–¡TˆÐ*ÜŒi˜˜dÓ#Õ$ùò +s   Á A3z\w+|[\.\!\?]c                 óÞ  • US-
  nUS:¼  aQ  U R                   R                  X   5      (       d/  US-  nUS:¼  a$  U R                   R                  X   5      (       d  M/  US:w  a  X   OSnUS-   nU[        U5      :  aZ  U R                   R                  X   5      (       d8  US-  nU[        U5      :  a$  U R                   R                  X   5      (       d  M8  U[        U5      :w  a  X   OSnXE4$ )z¹
One left & one right token, both case-normalized.  Skip over
non-sentence-final punctuation.  Used by the ``ContextIndex``
that is created for ``similar()`` and ``common_contexts()``.
r   r   r    r!   )Ú_CONTEXT_REÚmatchr#   )r8   r$   r%   Újr   r   s         r&   Ú_contextÚText._context›  sÝ   € ð �‰EˆØ�1‹f˜T×-Ñ-×3Ñ3°F±I×>Ñ>Ø�‰FˆAð �1‹f˜T×-Ñ-×3Ñ3°F±I×>Ó>à ›FˆvŠy¨	ˆð �‰EˆØ”#�f“+‹o d×&6Ñ&6×&<Ñ&<¸V¹Y×&GÑ&GØ�‰FˆAð ”#�f“+‹o d×&6Ñ&6×&<Ñ&<¸V¹Y×&GÓ&Gà¤# f£+Ó-�’	°7ˆàˆ}Ðr)   c                 ó    • SU R                   -  $ ©Nz
<Text: %s>©rÔ   rJ   s    r&   Ú__str__ÚText.__str__³  ó   € Ø˜dŸi™iÑ'Ð'r)   c                 ó    • SU R                   -  $ r^  r_  rJ   s    r&   rŠ   ÚText.__repr__¶  rb  r)   )rï   rÞ   rö   rU  r?  r;  rR  r÷   r  rÔ   r$   r+   )éO   r®   )rp   r’   ro   )ró   )éd   Né*   )rq   rr   rs   rt   ru   rÓ   rF   rØ   rÛ   rè   rž   r  r
  r  r�   r  r#  rm   r.  r8  rC  rN  rM  r½   r»   ÚcompilerX  r[  r`  rŠ   rw   r,   r)   r&   rÇ   rÇ   9  sŽ   † ñð. €LôIò.ò ôMô*Mô(!"ôF>ò0'ò'ò"ô  ôDò8%ôô,ò\(òò%ðD —*’*˜_Ó-€Kòò0(õ(r)   rÇ   c                   ó0   • \ rS rSrSrS rS rS rS rSr	g)	ÚTextCollectioni»  a  A collection of texts, which can be loaded with list of texts, or
with a corpus consisting of one or more texts, and which supports
counting, concordancing, collocation discovery, etc.  Initialize a
TextCollection as follows:

>>> import nltk.corpus
>>> from nltk.text import TextCollection
>>> from nltk.book import text1, text2, text3
>>> gutenberg = TextCollection(nltk.corpus.gutenberg)
>>> mytexts = TextCollection([text1, text2, text3])

Iterating over a TextCollection produces all the tokens of all the
texts in order.
c                 óä   • [        US5      (       a.  UR                  5        Vs/ s H  o!R                  U5      PM     nnXl        [        R                  U [        U5      5        0 U l        g s  snf )Nrh   )r@  Úfileidsrh   Ú_textsrÇ   rF   r   Ú
_idf_cache)r8   ÚsourceÚfs      r&   rF   ÚTextCollection.__init__Ë  sX   € Ü�6˜7×#Ñ#Ø/5¯~©~Ô/?Ó@Ò/?¨!—l‘l 1–oÑ/?ˆFÐ@àŒÜ�‰�dÔ-¨fÓ5Ô6Øˆ�ùò	 As   ¤A-c                 ó<   • UR                  U5      [        U5      -  $ )z"The frequency of the term in text.)r  r#   ©r8   ÚtermÚtexts      r&   ÚtfÚTextCollection.tfÓ  s   € à�z‰z˜$Ó¤# d£)Ñ+Ð+r)   c                 ó^  • U R                   R                  U5      nUc‰  [        U R                   Vs/ s H  o1U;   d  M
  SPM     sn5      n[        U R                  5      S:X  a  [	        S5      eU(       a!  [        [        U R                  5      U-  5      OSnX R                   U'   U$ s  snf )z”The number of texts in the corpus divided by the
number of texts that the term appears in.
If a term does not appear in the corpus, 0.0 is returned.Tr   z+IDF undefined for empty document collectiong        )rn  rZ   r#   rm  rf   r   )r8   rt  Úidfru  Úmatchess        r&   ry  ÚTextCollection.idf×  sŽ   € ð
 �o‰o×!Ñ! $Ó'ˆØ‰;Ü¨D¯KªKÓHªK DÀ4¹<Ÿ4©KÑHÓIˆGÜ�4—;‘;Ó 1Ó$Ü Ð!NÓOÐOÞ5<”#”c˜$Ÿ+™+Ó&¨Ñ0Ô1À#ˆCØ$'�O‰O˜DÑ!Øˆ
ùò Is
   ²	B*¿B*c                 óH   • U R                  X5      U R                  U5      -  $ r+   )rv  ry  rs  s      r&   Útf_idfÚTextCollection.tf_idfå  s   € Ø�w‰w�tÓ" T§X¡X¨d£^Ñ3Ð3r)   )rn  rm  N)
rq   rr   rs   rt   ru   rF   rv  ry  r}  rw   r,   r)   r&   rj  rj  »  s   † ñòò,òõ4r)   rj  c                  ót  • SSK Jn   [        U R                  SS95      n[	        U5        [	        5         [	        S5        UR                  S5        [	        5         [	        S5        UR                  S5        [	        5         [	        S5        UR                  5         [	        5         [	        S5        UR                  / S	Q5        [	        5         [	        S
5        UR                  S5        [	        5         [	        S5        [	        SUS   5        [	        SUSS 5        [	        SUR                  5       S   5        g )Nr   )ÚbrownÚnews)Ú
categorieszConcordance:zDistributionally similar words:zCollocations:zDispersion plot:)r�  ÚreportÚsaidÚ	announcedzVocabulary plot:é2   z	Indexing:ztext[3]:ró   z
text[3:5]:é   ztext.vocab()['news']:)rø   r€  rÇ   rh   r¨   rè   r#  r
  r.  rN  rM  )r€  ru  s     r&   Údemorˆ  é  sè   € Ý!ä�—‘ v�Ð.Ó/€DÜ	ˆ$„KÜ	„GÜ	ˆ.ÔØ×Ñ�VÔÜ	„GÜ	Ð
+Ô,Ø‡L�L�ÔÜ	„GÜ	ˆ/ÔØ×ÑÔÜ	„Gô 
Ð
ÔØ×ÑÒ@ÔAÜ	„GÜ	Ð
ÔØ‡I�Iˆb„MÜ	„GÜ	ˆ+ÔÜ	ˆ*�d˜1‘gÔÜ	ˆ,˜˜Q˜q˜	Ô"Ü	Ð
! 4§:¡:£<°Ñ#7Õ8r)   Ú__main__)r   ry   r°   rÇ   rj  )(ru   r»   rA  rŽ   Úcollectionsr   r   r   Ú	functoolsr   Úmathr   Únltk.collocationsr   Únltk.lmr	   Únltk.lm.preprocessingr
   Únltk.metricsr   r   Únltk.probabilityr   r>   r   Únltk.tokenizer   Ú	nltk.utilr   r   r   r   r   ry   r°   rÇ   rj  rˆ  rq   Ú__all__r,   r)   r&   Ú<module>r•     s¦   ðñó 
Û 
Û ß 8Ñ 8Ý Ý å 5Ý Ý ;ß 7Ý 7Ý %Ý 'ß >Ñ >áØÚMó€÷Xñ X÷v|-ñ |-÷~5ñ 5÷p~(ñ ~(ôD+4�Tô +4ò\9ð< ˆzÓÙ„Fò�r)   