ó
    ´OÂf‹?  ã                   ó|   • S 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\5      r " S	 S
\5      rg)a	  

Penn Treebank Tokenizer

The Treebank tokenizer uses regular expressions to tokenize text as in Penn Treebank.
This implementation is a port of the tokenizer sed script written by Robert McIntyre
and available at http://www.cis.upenn.edu/~treebank/tokenizer.sed.
é    N)ÚIteratorÚListÚTuple)Ú
TokenizerI)ÚMacIntyreContractions)Úalign_tokensc            
       óR  • \ rS rSrSr\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4/r\R                  " S	5      S
4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4/r\R                  " S5      S4r	\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S 5      S!4/r
\R                  " S"5      S#4r\R                  " S$5      S%4\R                  " S&5      S%4\R                  " S'5      S(4\R                  " S)5      S(4/r\" 5       r\" \" \R                  \R"                  5      5      r\" \" \R                  \R$                  5      5      r S2S*\S+\S,\S-\\   4S. jjrS*\S-\\\\4      4S/ jrS0rg1)3ÚTreebankWordTokenizeré   aÅ  
The Treebank tokenizer uses regular expressions to tokenize text as in Penn Treebank.

This tokenizer performs the following steps:

- split standard contractions, e.g. ``don't`` -> ``do n't`` and ``they'll`` -> ``they 'll``
- treat most punctuation characters as separate tokens
- split off commas and single quotes, when followed by whitespace
- separate periods that appear at the end of line

>>> from nltk.tokenize import TreebankWordTokenizer
>>> s = '''Good muffins cost $3.88\nin New York.  Please buy me\ntwo of them.\nThanks.'''
>>> TreebankWordTokenizer().tokenize(s)
['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York.', 'Please', 'buy', 'me', 'two', 'of', 'them.', 'Thanks', '.']
>>> s = "They'll save and invest more."
>>> TreebankWordTokenizer().tokenize(s)
['They', "'ll", 'save', 'and', 'invest', 'more', '.']
>>> s = "hi, my name can't hello,"
>>> TreebankWordTokenizer().tokenize(s)
['hi', ',', 'my', 'name', 'ca', "n't", 'hello', ',']
z^\"ú``z(``)z \1 z([ \(\[{<])(\"|\'{2})z\1 `` z([:,])([^\d])z \1 \2z([:,])$z\.\.\.z ... z[;@#$%&]z \g<0> z([^\.])(\.)([\]\)}>"\']*)\s*$z\1 \2\3 z[?!]z([^'])' z\1 ' z[\]\[\(\)\{\}\<\>]z\(ú-LRB-z\)ú-RRB-z\[ú-LSB-z\]ú-RSB-z\{ú-LCB-z\}ú-RCB-ú--ú -- ú''z '' Ú"z([^' ])('[sS]|'[mM]|'[dD]|') z\1 \2 z)([^' ])('ll|'LL|'re|'RE|'ve|'VE|n't|N'T) ÚtextÚconvert_parenthesesÚ
return_strÚreturnc                 ó¾  • USLa  [         R                  " S[        SS9  U R                   H  u  pEUR	                  XQ5      nM     U R
                   H  u  pEUR	                  XQ5      nM     U R                  u  pEUR	                  XQ5      nU(       a&  U R                   H  u  pEUR	                  XQ5      nM     U R                  u  pEUR	                  XQ5      nSU-   S-   nU R                   H  u  pEUR	                  XQ5      nM     U R                   H  nUR	                  SU5      nM     U R                   H  nUR	                  SU5      nM     UR                  5       $ )aU  Return a tokenized copy of `text`.

>>> from nltk.tokenize import TreebankWordTokenizer
>>> s = '''Good muffins cost $3.88 (roughly 3,36 euros)\nin New York.  Please buy me\ntwo of them.\nThanks.'''
>>> TreebankWordTokenizer().tokenize(s) # doctest: +NORMALIZE_WHITESPACE
['Good', 'muffins', 'cost', '$', '3.88', '(', 'roughly', '3,36',
'euros', ')', 'in', 'New', 'York.', 'Please', 'buy', 'me', 'two',
'of', 'them.', 'Thanks', '.']
>>> TreebankWordTokenizer().tokenize(s, convert_parentheses=True) # doctest: +NORMALIZE_WHITESPACE
['Good', 'muffins', 'cost', '$', '3.88', '-LRB-', 'roughly', '3,36',
'euros', '-RRB-', 'in', 'New', 'York.', 'Please', 'buy', 'me', 'two',
'of', 'them.', 'Thanks', '.']

:param text: A string with a sentence or sentences.
:type text: str
:param convert_parentheses: if True, replace parentheses to PTB symbols,
    e.g. `(` to `-LRB-`. Defaults to False.
:type convert_parentheses: bool, optional
:param return_str: If True, return tokens as space-separated string,
    defaults to False.
:type return_str: bool, optional
:return: List of tokens from `text`.
:rtype: List[str]
FzHParameter 'return_str' has been deprecated and should no longer be used.é   )ÚcategoryÚ
stacklevelÚ z \1 \2 )ÚwarningsÚwarnÚDeprecationWarningÚSTARTING_QUOTESÚsubÚPUNCTUATIONÚPARENS_BRACKETSÚCONVERT_PARENTHESESÚDOUBLE_DASHESÚENDING_QUOTESÚCONTRACTIONS2ÚCONTRACTIONS3Úsplit)Úselfr   r   r   ÚregexpÚsubstitutions         Ú8/usr/lib/python3/dist-packages/nltk/tokenize/treebank.pyÚtokenizeÚTreebankWordTokenizer.tokenizee   sK  € ð6 ˜UÒ"Ü�MŠMð"ä+Øò	ð %)×$8Ô$8Ñ ˆFØ—:‘:˜lÓ1ŠDñ %9ð %)×$4Ô$4Ñ ˆFØ—:‘:˜lÓ1ŠDñ %5ð  $×3Ñ3ÑˆØ�z‰z˜,Ó-ˆæØ(,×(@Ô(@Ñ$�Ø—z‘z ,Ó5’ñ )Að  $×1Ñ1ÑˆØ�z‰z˜,Ó-ˆð �T‰z˜CÑˆà$(×$6Ô$6Ñ ˆFØ—:‘:˜lÓ1ŠDñ %7ð ×(Ô(ˆFØ—:‘:˜j¨$Ó/ŠDñ )à×(Ô(ˆFØ—:‘:˜j¨$Ó/ŠDñ )ð �z‰z‹|Ðó    c              #   óD  #   • U R                  U5      nSU;   d  SU;   a^  [        R                  " SU5       Vs/ s H  o3R                  5       PM     nnU Vs/ s H  nUS;   a  UR	                  S5      OUPM     nnOUn[        Xa5       Sh  v•N   gs  snf s  snf  N7f)añ  
Returns the spans of the tokens in ``text``.
Uses the post-hoc nltk.tokens.align_tokens to return the offset spans.

    >>> from nltk.tokenize import TreebankWordTokenizer
    >>> s = '''Good muffins cost $3.88\nin New (York).  Please (buy) me\ntwo of them.\n(Thanks).'''
    >>> expected = [(0, 4), (5, 12), (13, 17), (18, 19), (19, 23),
    ... (24, 26), (27, 30), (31, 32), (32, 36), (36, 37), (37, 38),
    ... (40, 46), (47, 48), (48, 51), (51, 52), (53, 55), (56, 59),
    ... (60, 62), (63, 68), (69, 70), (70, 76), (76, 77), (77, 78)]
    >>> list(TreebankWordTokenizer().span_tokenize(s)) == expected
    True
    >>> expected = ['Good', 'muffins', 'cost', '$', '3.88', 'in',
    ... 'New', '(', 'York', ')', '.', 'Please', '(', 'buy', ')',
    ... 'me', 'two', 'of', 'them.', '(', 'Thanks', ')', '.']
    >>> [s[start:end] for start, end in TreebankWordTokenizer().span_tokenize(s)] == expected
    True

:param text: A string with a sentence or sentences.
:type text: str
:yield: Tuple[int, int]
r   r   z
``|'{2}|\")r   r   r   r   N)r1   ÚreÚfinditerÚgroupÚpopr   )r-   r   Ú
raw_tokensÚmÚmatchedÚtokÚtokenss          r0   Úspan_tokenizeÚ#TreebankWordTokenizer.span_tokenize¬   s§   é € ð. —]‘] 4Ó(ˆ
ð �4‹K˜T T›\ä*,¯+ª+°mÀTÔ*JÓKÒ*J Q—w‘w–yÑ*JˆGÐKñ
 &óâ%�Cð #&Ð):Ó":�—‘˜A”ÀÒCÙ%ð ð ˆFð
  ˆFä Ó-×-Ñ-ùò Lùòñ 	.ùs(   ‚7B ¹BÁB Á#BÁ;B ÂBÂB © N)FF)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r5   Úcompiler#   r%   r&   r'   r(   r)   r   Ú_contractionsÚlistÚmapr*   r+   ÚstrÚboolr   r1   r   r   Úintr>   Ú__static_attributes__r@   r3   r0   r
   r
      sK  † ñð0 
�Š�FÓ	˜UÐ#Ø	�Š�GÓ	˜gÐ&Ø	�ŠÐ,Ó	-¨yÐ9ð€Oð 
�ŠÐ$Ó	% yÐ1Ø	�Š�JÓ	 Ð)Ø	�Š�IÓ	 Ð)Ø	�Š�KÓ	  *Ð-à�JŠJÐ7Ó8Øð	
ð 
�Š�GÓ	˜jÐ)Ø	�Š�KÓ	  (Ð+ð€Kð —z’zÐ"7Ó8¸*ÐE€Oð 
�Š�EÓ	˜GÐ$Ø	�Š�EÓ	˜GÐ$Ø	�Š�EÓ	˜GÐ$Ø	�Š�EÓ	˜GÐ$Ø	�Š�EÓ	˜GÐ$Ø	�Š�EÓ	˜GÐ$ðÐð —Z’Z Ó&¨Ð0€Mð 
�Š�EÓ	˜FÐ#Ø	�Š�DÓ	˜6Ð"Ø	�ŠÐ4Ó	5°yÐAØ	�ŠÐ@Ó	AÀ9ÐMð	€Mñ *Ó+€MÙ™˜RŸZ™Z¨×)DÑ)DÓEÓF€MÙ™˜RŸZ™Z¨×)DÑ)DÓEÓF€Mð PUñEØðEØ.2ðEØHLðEà	ˆc‰õEðN). #ð ).¨(°5¸¸c¸±?Ñ*C÷ ).r3   r
   c                   ób  • \ rS rSrSr\" 5       r\R                   V VVs/ s H)  n[        R                  " UR                  SS5      5      PM+     snnn r\R                   V VVs/ s H)  n[        R                  " UR                  SS5      5      PM+     snnn r\R                  " S5      S4\R                  " S5      S4\R                  " S5      S	4\R                  " S
5      S	4\R                  " S5      S4/r\R                  " S5      S4r\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4\R                  " S5      S4/r\R                  " S5      S4\R                  " S5      S4\R                  " S5      S	4/r\R                  " S5      S 4\R                  " S!5      S4\R                  " S"5      S#4\R                  " S$5      S4\R                  " S%5      S4\R                  " S&5      S'4\R                  " S(5      S)4/r\R                  " S*5      S+4\R                  " S,5      S)4\R                  " S-5      S4/rS5S.\\   S/\S0\4S1 jjrS5S.\\   S/\S0\4S2 jjrS3rg4s  snnn f s  snnn f )6ÚTreebankWordDetokenizeréØ   aË
  
The Treebank detokenizer uses the reverse regex operations corresponding to
the Treebank tokenizer's regexes.

Note:

- There're additional assumption mades when undoing the padding of ``[;@#$%&]``
  punctuation symbols that isn't presupposed in the TreebankTokenizer.
- There're additional regexes added in reversing the parentheses tokenization,
   such as the ``r'([\]\)\}\>])\s([:;,.])'``, which removes the additional right
   padding added to the closing parentheses precedding ``[:;,.]``.
- It's not possible to return the original whitespaces as they were because
  there wasn't explicit records of where `'\n'`, `'\t'` or `'\s'` were removed at
  the text.split() operation.

>>> from nltk.tokenize.treebank import TreebankWordTokenizer, TreebankWordDetokenizer
>>> s = '''Good muffins cost $3.88\nin New York.  Please buy me\ntwo of them.\nThanks.'''
>>> d = TreebankWordDetokenizer()
>>> t = TreebankWordTokenizer()
>>> toks = t.tokenize(s)
>>> d.detokenize(toks)
'Good muffins cost $3.88 in New York. Please buy me two of them. Thanks.'

The MXPOST parentheses substitution can be undone using the ``convert_parentheses``
parameter:

>>> s = '''Good muffins cost $3.88\nin New (York).  Please (buy) me\ntwo of them.\n(Thanks).'''
>>> expected_tokens = ['Good', 'muffins', 'cost', '$', '3.88', 'in',
... 'New', '-LRB-', 'York', '-RRB-', '.', 'Please', '-LRB-', 'buy',
... '-RRB-', 'me', 'two', 'of', 'them.', '-LRB-', 'Thanks', '-RRB-', '.']
>>> expected_tokens == t.tokenize(s, convert_parentheses=True)
True
>>> expected_detoken = 'Good muffins cost $3.88 in New (York). Please (buy) me two of them. (Thanks).'
>>> expected_detoken == d.detokenize(t.tokenize(s, convert_parentheses=True), convert_parentheses=True)
True

During tokenization it's safe to add more spaces but during detokenization,
simply undoing the padding doesn't really help.

- During tokenization, left and right pad is added to ``[!?]``, when
  detokenizing, only left shift the ``[!?]`` is needed.
  Thus ``(re.compile(r'\s([?!])'), r'\g<1>')``.

- During tokenization ``[:,]`` are left and right padded but when detokenizing,
  only left shift is necessary and we keep right pad after comma/colon
  if the string after is a non-digit.
  Thus ``(re.compile(r'\s([:,])\s([^\d])'), r'\1 \2')``.

>>> from nltk.tokenize.treebank import TreebankWordDetokenizer
>>> toks = ['hello', ',', 'i', 'ca', "n't", 'feel', 'my', 'feet', '!', 'Help', '!', '!']
>>> twd = TreebankWordDetokenizer()
>>> twd.detokenize(toks)
"hello, i can't feel my feet! Help!!"

>>> toks = ['hello', ',', 'i', "can't", 'feel', ';', 'my', 'feet', '!',
... 'Help', '!', '!', 'He', 'said', ':', 'Help', ',', 'help', '?', '!']
>>> twd.detokenize(toks)
"hello, i can't feel; my feet! Help!! He said: Help, help?!"
z(?#X)z\sz+([^' ])\s('ll|'LL|'re|'RE|'ve|'VE|n't|N'T) z\1\2 z([^' ])\s('[sS]|'[mM]|'[dD]|') z(\S)\s(\'\')ú\1\2z(\'\')\s([.,:)\]>};%])r   r   r   r   r   Ú(r   Ú)r   Ú[r   Ú]r   Ú{r   Ú}z([\[\(\{\<])\sz\g<1>z\s([\]\)\}\>])z([\]\)\}\>])\s([:;,.])z([^'])\s'\sz\1' z\s([?!])z([^\.])\s(\.)([\]\)}>"\']*)\s*$z\1\2\3z([#$])\sz\s([;%])z
\s\.\.\.\sz...z\s([:,])z\1z([ (\[{<])\s``z\1``z(``)\sr   r=   r   r   c                 óÐ  • SR                  U5      nSU-   S-   nU R                   H  nUR                  SU5      nM     U R                   H  nUR                  SU5      nM     U R                   H  u  pEUR                  XS5      nM     UR                  5       nU R                  u  pEUR                  XS5      nU(       a&  U R                   H  u  pEUR                  XS5      nM     U R                   H  u  pEUR                  XS5      nM     U R                   H  u  pEUR                  XS5      nM     U R                   H  u  pEUR                  XS5      nM     UR                  5       $ )a]  
Treebank detokenizer, created by undoing the regexes from
the TreebankWordTokenizer.tokenize.

:param tokens: A list of strings, i.e. tokenized text.
:type tokens: List[str]
:param convert_parentheses: if True, replace PTB symbols with parentheses,
    e.g. `-LRB-` to `(`. Defaults to False.
:type convert_parentheses: bool, optional
:return: str
r   rQ   )Újoinr+   r$   r*   r)   Ústripr(   r'   r&   r%   r#   )r-   r=   r   r   r.   r/   s         r0   r1   Ú TreebankWordDetokenizer.tokenize[  sN  € ð �x‰x˜Óˆð �T‰z˜CÑˆð ×(Ô(ˆFØ—:‘:˜g tÓ,ŠDñ )à×(Ô(ˆFØ—:‘:˜g tÓ,ŠDñ )ð %)×$6Ô$6Ñ ˆFØ—:‘:˜lÓ1ŠDñ %7ð �z‰z‹|ˆð  $×1Ñ1ÑˆØ�z‰z˜,Ó-ˆæØ(,×(@Ô(@Ñ$�Ø—z‘z ,Ó5’ñ )Að %)×$8Ô$8Ñ ˆFØ—:‘:˜lÓ1ŠDñ %9ð %)×$4Ô$4Ñ ˆFØ—:‘:˜lÓ1ŠDñ %5ð %)×$8Ô$8Ñ ˆFØ—:‘:˜lÓ1ŠDñ %9ð �z‰z‹|Ðr3   c                 ó$   • U R                  X5      $ )z&Duck-typing the abstract *tokenize()*.)r1   )r-   r=   r   s      r0   Ú
detokenizeÚ"TreebankWordDetokenizer.detokenize�  s   € à�}‰}˜VÓ9Ð9r3   r@   N)F)rA   rB   rC   rD   rE   r   rG   r*   r5   rF   Úreplacer+   r)   r(   r'   r&   r%   r#   r   rJ   rK   r1   r]   rM   )Ú.0Úpatternr5   s   000r0   rO   rO   Ø   sÂ  † ñ:ñx *Ó+€Mð %×2Ò2õâ2ˆGô 	�
Š
�7—?‘? 7¨EÓ2Ö3Ù2ó€Mð %×2Ò2õâ2ˆGô 	�
Š
�7—?‘? 7¨EÓ2Ö3Ù2ó€Mð 
�ŠÐBÓ	CÀXÐNØ	�ŠÐ6Ó	7¸ÐBØ	�Š�OÓ	$ gÐ.à�JŠJÐ0Ó1Øð	
ð 
�Š�EÓ	˜CÐ ð	€Mð —Z’Z Ó(¨%Ð0€Mð 
�Š�GÓ	˜cÐ"Ø	�Š�GÓ	˜cÐ"Ø	�Š�GÓ	˜cÐ"Ø	�Š�GÓ	˜cÐ"Ø	�Š�GÓ	˜cÐ"Ø	�Š�GÓ	˜cÐ"ðÐð 
�ŠÐ%Ó	&¨Ð1Ø	�ŠÐ%Ó	&¨Ð1Ø	�ŠÐ-Ó	.°Ð8ð€Oð 
�Š�NÓ	# WÐ-Ø	�Š�KÓ	  (Ð+à	�ŠÐ6Ó	7¸ÐCð
 
�Š�KÓ	  (Ð+Ø	�Š�KÓ	  (Ð+à	�Š�MÓ	" FÐ+ð �JŠJ�{Ó#Øð	
ð€Kð, 
�ŠÐ%Ó	&¨Ð0Ø	�Š�IÓ	 Ð&Ø	�Š�EÓ	˜DÐ!ð€Oñ3˜t C™yð 3¸tð 3ÐPSõ 3ñj:  c¡ð :Àð :ÐRU÷ :ð :ùôuùôs    0J#Á%0J*rO   )rE   r5   r    Útypingr   r   r   Únltk.tokenize.apir   Únltk.tokenize.destructiver   Únltk.tokenize.utilr   r
   rO   r@   r3   r0   Ú<module>rf      s>   ðñó 
Û ß (Ñ (å (Ý ;Ý +ôx.˜Jô x.ôvz:˜jõ z:r3   