B
    >?[-                 @   s*  d dl mZmZ d dlZd dlZd dlmZmZ d dlZd dl	Z	d dl
mZmZ ejd  dkZerdd Zd dlZejZejZd dlmZ ejZd d	lmZ nd
d Zyd dlmZ W n  ek
r   d dlmZ Y nX eZd dlmZmZ ed ZeddZG dd deZe Zd dlZd dl Z d dlZG dd dZ!d dl"Z#d dlZ$d dlm%Z% G dd de&ZddddgZ'dd e'D Z(dd Z)dd Z*d d! Z+yd d"l,m,Z- W nB ek
r   yd d"l.m,Z- W n ek
r   e+Z-Y nX Y nX d#d$ Z/d%d& Z0d'd( Z1d)d* Z2d+d, Z3G d-d. d.ej4Z4dS )/    )absolute_importprint_functionN)update_wrapperwraps)string_types	text_type   c             C   s   | j jS )N)__self__	__class__)meth r   *lib/python3.7/site-packages/nltk/compat.pyget_im_class   s    r   )timezone)TemporaryDirectoryc             C   s   | j S )N)Zim_class)r   r   r   r   r   (   s    )StringIO)tzinfo	timedelta   )Zhoursc               @   s(   e Zd Zd Zdd Zdd Zdd ZdS )UTCc             C   s   t S )N)ZERO)selfdtr   r   r   	utcoffset:   s    zUTC.utcoffsetc             C   s   dS )Nr   r   )r   r   r   r   r   tzname=   s    z
UTC.tznamec             C   s   t S )N)r   )r   r   r   r   r   dst@   s    zUTC.dstN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   7   s   r   c               @   s2   e Zd ZdZejddfddZdd Zdd	 Zd
S )UnicodeWriterz
        A CSV writer which will write rows to CSV file "f",
        which is encoded in the given encoding.
        see https://docs.python.org/2/library/csv.html
        zutf-8replacec             K   sD   t  | _tj| jfd|i|| _|| _t|}||d| _d S )Ndialect)errors)		cStringIOr   queuecsvwriterstreamcodecsgetincrementalencoderencoder)r   fr"   encodingr#   kwdsZencoder_clsr   r   r   __init__P   s
    

zUnicodeWriter.__init__c             C   s   t |tr|dS |S d S )Nzutf-8)
isinstancer   encode)r   datar   r   r   r1   Z   s    

zUnicodeWriter.encodec                sX    j  fdd|D   j }|d} j|d} j|  j	d d S )Nc                s   g | ]}  |qS r   )r1   ).0s)r   r   r   
<listcomp>a   s    z*UnicodeWriter.writerow.<locals>.<listcomp>zutf-8r!   r   )
r'   writerowr%   getvaluedecoder+   r1   r(   writetruncate)r   rowr2   r   )r   r   r6   `   s    

zUnicodeWriter.writerowN)	r   r   r   r   r&   Zexcelr/   r1   r6   r   r   r   r   r    I   s   	r    )mkdtempc               @   s   e Zd ZdZdddZdd Zd	d
 ZdddZdd Zdd Z	e
ejZe
ejjZe
ejjZe
ejjZe
ejZe
ejZejZdd ZdS )r   a  Create and return a temporary directory.  This has the same
        behavior as mkdtemp but can be used as a context manager.  For
        example:

            with TemporaryDirectory() as tmpdir:
                ...

        Upon exiting the context, the directory and everything contained
        in it are removed.

        http://stackoverflow.com/questions/19296146/tempfile-temporarydirectory-context-manager-in-python-2-7
         tmpNc             C   s   d| _ d | _t|||| _d S )NF)_closednamer<   )r   suffixprefixdirr   r   r   r/   ~   s    zTemporaryDirectory.__init__c             C   s   d | jj| jS )Nz	<{} {!r}>)formatr
   r   r@   )r   r   r   r   __repr__   s    zTemporaryDirectory.__repr__c             C   s   | j S )N)r@   )r   r   r   r   	__enter__   s    zTemporaryDirectory.__enter__Fc          
   C   s   | j r| jsy| | j  W nJ ttfk
rf } z(dt|kr@ td|| tj	d d S d }~X Y nX d| _|r| 
d| t d S )NNonez"ERROR: {!r} while cleaning up {!r})fileTzImplicitly cleaning up {!r})r@   r?   _rmtree	TypeErrorAttributeErrorstrprintrD   sysstderr_warnWarning)r   rP   exr   r   r   cleanup   s    

zTemporaryDirectory.cleanupc             C   s   |    d S )N)rS   )r   excvaluetbr   r   r   __exit__   s    zTemporaryDirectory.__exit__c             C   s   | j dd d S )NT)rP   )rS   )r   r   r   r   __del__   s    zTemporaryDirectory.__del__c          	   C   s   x|  |D ]x}| ||}y| |o2| | }W n tk
rN   d}Y nX |r`| | qy| | W q tk
r   Y qX qW y| | W n tk
r   Y nX d S )NF)_listdir
_path_join_isdir_islinkOSErrorrI   _remove_rmdir)r   pathr@   fullnameisdirr   r   r   rI      s     

zTemporaryDirectory._rmtree)r=   r>   N)F)r   r   r   r   r/   rE   rF   rS   rW   rX   staticmethod_oslistdirrY   r`   joinrZ   rb   r[   islinkr\   remover^   rmdirr_   	_warningswarnrP   rI   r   r   r   r   r   p   s   




r   )ZchunkersZmaxent_ne_chunker)helpZtagsets)ZtaggersZmaxent_treebank_pos_tagger)Z
tokenizersZpunktc             C   s   g | ]}t jj| qS r   )osr`   rf   )r3   	path_listr   r   r   r5      s    r5   c             C   sz   t rvxptD ]h}|t| kr
dt| kr
| |t| }| ||d  dkrT|d7 }| d | d | |d   } P q
W | S )Nz/PY3   z.zip)PY3_PY3_DATA_UPDATESrL   indexlen)r`   itemposr   r   r   add_py3_data   s    
rv   c                s    fdd}t  |S )Nc                 s*   | d t | d f| dd   }  | |S )Nr   r      )rv   )argskwargs)	init_funcr   r   
_decorator   s     zpy3_data.<locals>._decorator)r   )rz   r{   r   )rz   r   py3_data   s    r|   c                s:   t | tr| d} tj d fddtd| D S )Nasciir=   c             3   s   | ]} |d kr|V  qdS )ZMnNr   )r3   c)categoryr   r   	<genexpr>   s    z!remove_accents.<locals>.<genexpr>ZNFKD)r0   bytesr8   unicodedatar   rf   Z	normalize)textr   )r   r   remove_accents   s
    

r   )	unidecodec             C   s^   t | tstdt| js8| j| _ts8tt| j| _t| j	sZ| j	| _
tsZt| j
| _	| S )a  
    This decorator defines __unicode__ method and fixes
    __repr__ and __str__ methods under Python 2.

    To support Python 2 and 3 with a single code base,
    define __str__ and __repr__ methods returning unicode
    text and apply this decorator to the class.

    Original __repr__ and __str__ would be available
    as unicode_repr and __unicode__ (under both Python 2
    and Python 3).
    z1This decorator doesn't work for old-style classes)
issubclassobject
ValueError
_was_fixed__str__Z__unicode__rp   _7bit_transliteratedrE   unicode_repr)klassr   r   r   python_2_unicode_compatible  s    
	

r   c             C   s@   t rt| S t| dr|  S t| tr8t| dd S t| S )a0  
    For classes that was fixed with @python_2_unicode_compatible
    ``unicode_repr`` returns ``obj.unicode_repr()``; for unicode strings
    the result is returned without "u" letter (to make output the
    same under Python 2.x and Python 3.x); for other variables
    it is the same as ``repr``.
    r   r   N)rp   reprhasattrr   r0   r   )objr   r   r   r   *  s    

r   c                s8    fdd}t | ddg t dr. j|_d|_|S )Nc                s   t  | S )N)transliterate)r   )methodr   r   wrapper@  s    z _transliterated.<locals>.wrapperr   r   _nltk_compat_7bitT)r   r   r   _nltk_compat_transliterated)r   r   r   )r   r   r   ?  s    
r   c                s8    fdd}t | ddg t dr. j|_d|_|S )Nc                s    |  ddS )Nr}   backslashreplace)r1   )r   )r   r   r   r   L  s    z_7bit.<locals>.wrapperr   r   r   T)r   r   r   r   )r   r   r   )r   r   r   K  s    
r   c             C   s   t | ddpt | ddS )Nr   Fr   )getattr)r   r   r   r   r   X  s    r   c                   s"   e Zd ZdZd fdd	Z  ZS )Fractiona  
    This is a simplified backwards compatible version of fractions.Fraction
    from Python >=3.5. It adds the `_normalize` parameter such that it does
    not normalize the denominator to the Greatest Common Divisor (gcd) when
    the numerator is 0.

    This is most probably only used by the nltk.translate.bleu_score.py where
    numerator and denominator of the different ngram precisions are mutable.
    But the idea of "mutable" fraction might not be applicable to other usages,
    See http://stackoverflow.com/questions/34561265

    This objects should be deprecated once NLTK stops supporting Python < 3.5
    See https://github.com/nltk/nltk/issues/1330
    r   NTc                s8   t t| | ||} |s4t|tkr4|r4|| _|| _| S )N)superr   __new__typeintZ
_numeratorZ_denominator)cls	numeratordenominatorZ
_normalize)r
   r   r   r   n  s
    zFraction.__new__)r   NT)r   r   r   r   r   __classcell__r   r   )r
   r   r   ^  s   r   )5Z
__future__r   r   rm   rN   	functoolsr   r   Z	fractionsr   Zsixr   r   version_inforp   r   ior   BytesIOZdatetimer   Zutcr   Ztempfiler   r$   ImportErrorr   r   r   ZHOURr&   r)   r    warningsrj   rd   r<   r   ZDATA_UPDATESrq   rv   r|   r   r   r   Ztext_unidecoder   r   r   r   r   r   r   r   r   r   <module>	   sn   
#Z	%