a
     ˜ëj‹R  ã                   @   sj   d Z ddlZddlZddlZddlmZ ddlZddlm	Z	 G dd„ dƒZ
G dd„ dƒZG d	d
„ d
ƒZdS )a  
If you use the VADER sentiment analysis tools, please cite:

Hutto, C.J. & Gilbert, E.E. (2014). VADER: A Parsimonious Rule-based Model for
Sentiment Analysis of Social Media Text. Eighth International Conference on
Weblogs and Social Media (ICWSM-14). Ann Arbor, MI, June 2014.
é    N)Úproduct)Úpairwisec                C   @   s  e Zd ZdZdZdZdZdZh d£ZeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedœBZ	ddd	d
ddd
dœZ
e de ej¡› d�¡Zg d¢Zdd„ Zddd„Zddd„Zdd„ ZdS )ÚVaderConstantsz8
    A class to keep the Vader lists and constants.
    g�—nƒÀÒ?g�—nƒÀÒ¿gÛù~j¼tç?g®Gáz®ç¿>;   ZcannotZisntzhasn'tZdespiteZdoesntZneedntZwontzoughtn'tZcantzdaren'tZseldomZdontzweren'tzain'tzneedn'tZshantZshouldntZdidntZnorzdidn'tzcouldn'tzwon'tzhadn'tZwasntzisn'tzuh-uhzwouldn'tZnowherez	shouldn'tZmightntÚnoneZnothingZuhuhZrarelyzmustn'tZneitherZoughtntzdon'tzdoesn'tZhasntzshan'tZdarentzaren'tZmustntÚnotzhaven'tÚneverzmightn'tZnopeZarentZwouldntZaintZhaventZwerentZhadntzwasn'tZcouldntZwithoutzcan't)BZ
absolutelyZ	amazinglyZawfullyZ
completelyZconsiderablyZ	decidedlyZdeeplyZeffingZ
enormouslyZentirelyZ
especiallyZexceptionallyZ	extremelyZ
fabulouslyZflippingZflippinZfrickingZfrickinZfriggingZfrigginZfullyZfuckingZgreatlyZhellaZhighlyZhugelyZ
incrediblyZ	intenselyZmajorlyZmoreZmostZparticularlyZpurelyZquiteZreallyZ
remarkablyÚsoZsubstantiallyZ
thoroughlyZtotallyZtremendouslyZuberZunbelievablyZ	unusuallyZutterlyÚveryZalmostZbarelyZhardlyzjust enoughzkind ofZkindaZkindofzkind-ofZlessÚlittleZ
marginallyZoccasionallyZpartlyZscarcelyZslightlyZsomewhatzsort ofZsortaZsortofzsort-ofé   ç      ø?éþÿÿÿé   g      ø¿)zthe shitzthe bombzbad assz
yeah rightzcut the mustardzkiss of deathzhand to mouthú[ú])Ú.ú!ú?ú,ú;ú:ú-ú'ú"z!!z!!!z??z???z?!?z!?!z?!?!z!?!?c                 C   s   d S ©N© )Úselfr   r   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/sentiment/vader.pyÚ__init__Ù   s    zVaderConstants.__init__Tc                    sn   | j ‰ t‡ fdd„|D ƒƒr dS |r:tdd„ |D ƒƒr:dS t|ƒD ]&\}}| ¡ dkrB| ¡ dkrB dS qBdS )z<
        Determine if input contains negation words
        c                 3   s   | ]}|  ¡ ˆ v V  qd S r   ©Úlower©Ú.0Úword©Z	neg_wordsr   r   Ú	<genexpr>á   ó    z)VaderConstants.negated.<locals>.<genexpr>Tc                 s   s   | ]}d |  ¡ v V  qdS )zn'tNr   r!   r   r   r   r%   ä   r&   ÚleastÚatF)ÚNEGATEÚanyr   r    )r   Zinput_wordsZ
include_ntÚfirstÚsecondr   r$   r   ÚnegatedÜ   s    zVaderConstants.negatedé   c                 C   s   |t  || | ¡ }|S )z|
        Normalize the score to be between -1 and 1 using an alpha that
        approximates the max expected value
        )ÚmathÚsqrt)r   ZscoreÚalphaZ
norm_scorer   r   r   Ú	normalizeë   s    zVaderConstants.normalizec                 C   s^   d}|  ¡ }|| jv rZ| j| }|dk r0|d9 }| ¡ rZ|rZ|dkrP|| j7 }n
|| j8 }|S )zh
        Check if the preceding words increase, decrease, or negate/nullify the
        valence
        ç        r   éÿÿÿÿ)r    ÚBOOSTER_DICTÚisupperÚC_INCR)r   r#   ÚvalenceÚis_cap_diffZscalarZ
word_lowerr   r   r   Úscalar_inc_decó   s    


zVaderConstants.scalar_inc_decN)T)r.   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZB_INCRÚB_DECRr7   ÚN_SCALARr)   r5   ÚSPECIAL_CASE_IDIOMSÚreÚcompileÚescapeÚstringÚpunctuationÚREGEX_REMOVE_PUNCTUATIONÚ	PUNC_LISTr   r-   r2   r:   r   r   r   r   r   !   s®   B¾Gù

r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú	SentiTextzL
    Identify sentiment-relevant string-level properties of input text.
    c                 C   sF   t |tƒst| d¡ƒ}|| _|| _|| _|  ¡ | _|  | j¡| _	d S )Nzutf-8)
Ú
isinstanceÚstrÚencodeÚtextrH   rG   Ú_words_and_emoticonsÚwords_and_emoticonsÚallcap_differentialr9   )r   rM   Z	punc_listZregex_remove_punctuationr   r   r   r     s    

zSentiText.__init__c                 C   sd   | j  d| j¡}| ¡ }dd„ |D ƒ}dd„ t| j|ƒD ƒ}dd„ t|| jƒD ƒ}|}| |¡ |S )zt
        Returns mapping of form:
        {
            'cat,': 'cat',
            ',cat': 'cat',
        }
        Ú c                 S   s   h | ]}t |ƒd kr|’qS ©é   ©Úlen)r"   Úwr   r   r   Ú	<setcomp>#  r&   z-SentiText._words_plus_punc.<locals>.<setcomp>c                 S   s   i | ]}d   |¡|d “qS )rQ   rS   ©Újoin©r"   Úpr   r   r   Ú
<dictcomp>%  r&   z.SentiText._words_plus_punc.<locals>.<dictcomp>c                 S   s   i | ]}d   |¡|d “qS )rQ   r   rX   rZ   r   r   r   r\   &  r&   )rG   ÚsubrM   Úsplitr   rH   Úupdate)r   Zno_punc_textZ
words_onlyZpunc_beforeZ
punc_afterÚwords_punc_dictr   r   r   Ú_words_plus_punc  s    
zSentiText._words_plus_puncc                 C   sJ   | j  ¡ }|  ¡ }dd„ |D ƒ}t|ƒD ]\}}||v r(|| ||< q(|S )z¬
        Removes leading and trailing puncutation
        Leaves contractions and most emoticons
            Does not preserve punc-plus-letter emoticons (e.g. :D)
        c                 S   s   g | ]}t |ƒd kr|‘qS rR   rT   )r"   Úwer   r   r   Ú
<listcomp>3  r&   z2SentiText._words_and_emoticons.<locals>.<listcomp>)rM   r^   ra   Ú	enumerate)r   Zwesr`   Úirb   r   r   r   rN   +  s    
zSentiText._words_and_emoticonsc                 C   sR   d}d}|D ]}|  ¡ r|d7 }qt|ƒ| }d|  k rFt|ƒk rNn nd}|S )zÅ
        Check whether just some words in the input are ALL CAPS

        :param list words: The words to inspect
        :returns: `True` if some but not all items in `words` are ALL CAPS
        Fr   rS   T)r6   rU   )r   ÚwordsZis_differentZallcap_wordsr#   Zcap_differentialr   r   r   rP   9  s    
zSentiText.allcap_differentialN)r;   r<   r=   r>   r   ra   rN   rP   r   r   r   r   rI     s
   rI   c                   @   sz   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSentimentIntensityAnalyzerz8
    Give a sentiment intensity score to sentences.
    ú;sentiment/vader_lexicon.zip/vader_lexicon/vader_lexicon.txtc                 C   s$   t j |¡| _|  ¡ | _tƒ | _d S r   )ÚnltkÚdataÚloadÚlexicon_fileÚmake_lex_dictÚlexiconr   Ú	constants)r   rl   r   r   r   r   P  s    
z#SentimentIntensityAnalyzer.__init__c                 C   s@   i }| j  d¡D ]*}| ¡  d¡dd… \}}t|ƒ||< q|S )z6
        Convert lexicon file to a dictionary
        Ú
ú	r   r   )rl   r^   ÚstripÚfloat)r   Zlex_dictÚliner#   Zmeasurer   r   r   rm   X  s
    z(SentimentIntensityAnalyzer.make_lex_dictc                 C   s¬   t || jj| jjƒ}g }|j}|D ]p}d}| |¡}|t|ƒd k rd| ¡ dkrd||d   ¡ dkst| ¡ | jjv r€| 	|¡ q"|  
|||||¡}q"|  ||¡}|  ||¡S )aö  
        Return a float for sentiment strength based on the input text.
        Positive values are positive valence, negative value are negative
        valence.

        :note: Hashtags are not taken into consideration (e.g. #BAD is neutral). If you
            are interested in processing the text in the hashtags too, then we recommend
            preprocessing your data to remove the #, after which the hashtag text may be
            matched as if it was a normal word in the sentence.
        r   rS   ÚkindZof)rI   ro   rH   rG   rO   ÚindexrU   r    r5   ÚappendÚsentiment_valenceÚ
_but_checkÚscore_valence)r   rM   Ú	sentitextÚ
sentimentsrO   Úitemr8   re   r   r   r   Úpolarity_scoresb  s*    ÿ
ÿ
þýü
z*SentimentIntensityAnalyzer.polarity_scoresc                 C   s   |j }|j}| ¡ }|| jv �r| j| }| ¡ rX|rX|dkrL|| jj7 }n|| jj8 }tddƒD ] }	||	krb|||	d    ¡ | jvrb| j |||	d   ||¡}
|	dkr¼|
dkr¼|
d }
|	dkrÔ|
dkrÔ|
d }
||
 }|  	|||	|¡}|	dkrb|  
|||¡}qb|  |||¡}| |¡ |S )Nr   r   rS   gffffffî?r   gÍÌÌÌÌÌì?)r9   rO   r    rn   r6   ro   r7   Úranger:   Ú_never_checkÚ_idioms_checkÚ_least_checkrw   )r   r8   r{   r}   re   r|   r9   rO   Zitem_lowercaseÚstart_iÚsr   r   r   rx   „  s@    
ÿÿþÿÿ	
z,SentimentIntensityAnalyzer.sentiment_valencec                 C   sª   |dkrh||d    ¡ | jvrh||d    ¡ dkrh||d    ¡ dkr¦||d    ¡ dkr¦|| jj }n>|dkr¦||d    ¡ | jvr¦||d    ¡ dkr¦|| jj }|S )NrS   r'   r   r(   r	   r   )r    rn   ro   r@   )r   r8   rO   re   r   r   r   r‚   ¶  s&    ÿþýÿþÿþýz'SentimentIntensityAnalyzer._least_checkc                 C   sr   dd„ |D ƒ}dht |ƒ@ }|rn| tt|ƒƒ¡}t|ƒD ]2\}}||k rX|d ||< q:||kr:|d ||< q:|S )Nc                 S   s   g | ]}|  ¡ ‘qS r   r   )r"   Zw_er   r   r   rc   Ë  r&   z9SentimentIntensityAnalyzer._but_check.<locals>.<listcomp>Úbutg      à?r   )Úsetrv   ÚnextÚiterrd   )r   rO   r|   r…   ZbiZsidxZ	sentimentr   r   r   ry   Ê  s    z%SentimentIntensityAnalyzer._but_checkc                 C   s”  ||d  › d|| › �}d  ||d  ||d  || ¡}||d  › d||d  › �}d  ||d  ||d  ||d  ¡}d  ||d  ||d  ¡}|||||g}	|	D ] }
|
| jjv r®| jj|
 } qÐq®t|ƒd |k�r|| › d||d  › �}|| jjv �r| jj| }t|ƒd |d k�rhd  || ||d  ||d  ¡}|| jjv �rh| jj| }|| jjv �s„|| jjv �r�|| jj }|S )NrS   ú z{} {} {}r   r   z{} {})Úformatro   rA   rU   r5   r?   )r   r8   rO   re   ZonezeroZ
twoonezeroZtwooneZthreetwooneZthreetwoÚ	sequencesÚseqZzerooneZ
zeroonetwor   r   r   r�   Ö  sL    

ý


ýÿ

ý
ÿ
þz(SentimentIntensityAnalyzer._idioms_checkc                 C   s"  |dkr*| j  ||d  g¡r*|| j j }|dkr’||d  dkrl||d  dksb||d  dkrl|d }n&| j  |||d   g¡r’|| j j }|dk�r||d  dkrÌ||d  dksì||d  dksì||d  dksì||d  dkrö|d	 }n(| j  |||d   g¡�r|| j j }|S )
Nr   rS   r   r   r   Úthisr   r   g      ô?)ro   r-   r@   )r   r8   rO   rƒ   re   r   r   r   r€     s6    ÿþ

ÿýüùø
z'SentimentIntensityAnalyzer._never_checkc                 C   s    |   |¡}|  |¡}|| }|S r   )Ú_amplify_epÚ_amplify_qm)r   Úsum_srM   Úep_amplifierÚqm_amplifierÚpunct_emph_amplifierr   r   r   Ú_punctuation_emphasis$  s    

z0SentimentIntensityAnalyzer._punctuation_emphasisc                 C   s"   |  d¡}|dkrd}|d }|S )Nr   é   gã¥›Ä °Ò?©Úcount)r   rM   Zep_countr‘   r   r   r   rŽ   +  s
    
z&SentimentIntensityAnalyzer._amplify_epc                 C   s0   |  d¡}d}|dkr,|dkr(|d }nd}|S )Nr   r   rS   r   g
×£p=
Ç?g¸…ëQ¸î?r–   )r   rM   Zqm_countr’   r   r   r   r�   5  s    

z&SentimentIntensityAnalyzer._amplify_qmc                 C   s`   d}d}d}|D ]D}|dkr,|t |ƒd 7 }|dk rD|t |ƒd 7 }|dkr|d7 }q|||fS )Nr3   r   rS   )rs   )r   r|   Úpos_sumÚneg_sumÚ	neu_countZsentiment_scorer   r   r   Ú_sift_sentiment_scoresB  s    
ÿ
ÿ
z1SentimentIntensityAnalyzer._sift_sentiment_scoresc                 C   s   |rÆt t|ƒƒ}|  ||¡}|dkr.||7 }n|dk r>||8 }| j |¡}|  |¡\}}}|t |¡krr||7 }n|t |¡k rˆ||8 }|t |¡ | }	t ||	 ¡}
t ||	 ¡}t ||	 ¡}nd}d}
d}d}t|dƒt|dƒt|
dƒt|dƒdœ}|S )Nr   r3   r   r•   )ÚnegÚneuÚposÚcompound)	rs   Úsumr”   ro   r2   r›   r/   ÚfabsÚround)r   r|   rM   r�   r“   rŸ   r˜   r™   rš   Útotalrž   rœ   r�   Zsentiment_dictr   r   r   rz   T  s6    

üz(SentimentIntensityAnalyzer.score_valenceN)rh   )r;   r<   r=   r>   r   rm   r~   rx   r‚   ry   r�   r€   r”   rŽ   r�   r›   rz   r   r   r   r   rg   K  s    þ

"21
rg   )r>   r/   rB   rE   Ú	itertoolsr   Z	nltk.datari   Z	nltk.utilr   r   rI   rg   r   r   r   r   Ú<module>   s    gD