a
     ˜ëjÖ.  ã                   @   st   d Z ddlZddlT ddlT e d¡Ze d¡Ze d¡Ze d¡ZG dd	„ d	ƒZ	G d
d„ dƒZ
G dd„ deƒZdS )a	  
CorpusReader for reviews corpora (syntax based on Customer Review Corpus).

Customer Review Corpus information
==================================

Annotated by: Minqing Hu and Bing Liu, 2004.
    Department of Computer Science
    University of Illinois at Chicago

Contact: Bing Liu, liub@cs.uic.edu
        https://www.cs.uic.edu/~liub

Distributed with permission.

The "product_reviews_1" and "product_reviews_2" datasets respectively contain
annotated customer reviews of 5 and 9 products from amazon.com.

Related papers:

- Minqing Hu and Bing Liu. "Mining and summarizing customer reviews".
    Proceedings of the ACM SIGKDD International Conference on Knowledge
    Discovery & Data Mining (KDD-04), 2004.

- Minqing Hu and Bing Liu. "Mining Opinion Features in Customer Reviews".
    Proceedings of Nineteeth National Conference on Artificial Intelligence
    (AAAI-2004), 2004.

- Xiaowen Ding, Bing Liu and Philip S. Yu. "A Holistic Lexicon-Based Appraoch to
    Opinion Mining." Proceedings of First ACM International Conference on Web
    Search and Data Mining (WSDM-2008), Feb 11-12, 2008, Stanford University,
    Stanford, California, USA.

Symbols used in the annotated reviews:

    :[t]: the title of the review: Each [t] tag starts a review.
    :xxxx[+|-n]: xxxx is a product feature.
    :[+n]: Positive opinion, n is the opinion strength: 3 strongest, and 1 weakest.
           Note that the strength is quite subjective.
           You may want ignore it, but only considering + and -
    :[-n]: Negative opinion
    :##:   start of each sentence. Each line is a sentence.
    :[u]:  feature not appeared in the sentence.
    :[p]:  feature not appeared in the sentence. Pronoun resolution is needed.
    :[s]:  suggestion or recommendation.
    :[cc]: comparison with a competing product from a different brand.
    :[cs]: comparison with a competing product from the same brand.

Note: Some of the files (e.g. "ipod.txt", "Canon PowerShot SD500.txt") do not
    provide separation between different reviews. This is due to the fact that
    the dataset was specifically designed for aspect/feature-based sentiment
    analysis, for which sentence-level annotation is sufficient. For document-
    level classification and analysis, this peculiarity should be taken into
    consideration.
é    N)Ú*z^\[t\](.*)$z%((?:(?:\w+\s)+)?\w+)\[((?:\+|\-)\d)\]z\[(?!t)(p|u|s|cc|cs)\]z##(.*)$c                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚReviewz>
    A Review is the main block of a ReviewsCorpusReader.
    Nc                 C   s    || _ |du rg | _n|| _dS )zŒ
        :param title: the title of the review.
        :param review_lines: the list of the ReviewLines that belong to the Review.
        N)ÚtitleÚreview_lines)Úselfr   r   © r   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/reviews.pyÚ__init__R   s    zReview.__init__c                 C   s   t |tƒsJ ‚| j |¡ dS )z‡
        Add a line (ReviewLine) to the review.

        :param review_line: a ReviewLine instance that belongs to the Review.
        N)Ú
isinstanceÚ
ReviewLiner   Úappend)r   Úreview_liner   r   r   Úadd_line]   s    zReview.add_linec                 C   s    g }| j D ]}| |j¡ q
|S )a  
        Return a list of features in the review. Each feature is a tuple made of
        the specific item feature and the opinion strength about that feature.

        :return: all features of the review as a list of tuples (feat, score).
        :rtype: list(tuple)
        )r   ÚextendÚfeatures)r   r   r   r   r   r   r   f   s    
zReview.featuresc                 C   s   dd„ | j D ƒS )z¡
        Return all tokenized sentences in the review.

        :return: all sentences of the review as lists of tokens.
        :rtype: list(list(str))
        c                 S   s   g | ]
}|j ‘qS r   )Úsent)Ú.0r   r   r   r   Ú
<listcomp>z   ó    z Review.sents.<locals>.<listcomp>)r   ©r   r   r   r   Úsentss   s    zReview.sentsc                 C   s   d  | j| j¡S )Nz#Review(title="{}", review_lines={}))Úformatr   r   r   r   r   r   Ú__repr__|   s    ÿzReview.__repr__)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r   r   M   s   
		r   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )r   z—
    A ReviewLine represents a sentence of the review, together with (optional)
    annotations of its features and notes about the reviewed item.
    Nc                 C   s6   || _ |d u rg | _n|| _|d u r,g | _n|| _d S )N©r   r   Únotes)r   r   r   r   r   r   r   r	   ˆ   s    zReviewLine.__init__c                 C   s   d  | j| j| j¡S )Nz*ReviewLine(features={}, notes={}, sent={}))r   r   r   r   r   r   r   r   r   ”   s    ÿzReviewLine.__repr__)NN)r   r   r   r   r	   r   r   r   r   r   r   ‚   s   
r   c                   @   sl   e Zd ZdZeZeƒ dfdd„Zddd„Zddd	„Z	dd
d„Z
ddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚReviewsCorpusReadera²  
    Reader for the Customer Review Data dataset by Hu, Liu (2004).
    Note: we are not applying any sentence tokenization at the moment, just word
    tokenization.

        >>> from nltk.corpus import product_reviews_1
        >>> camera_reviews = product_reviews_1.reviews('Canon_G3.txt')
        >>> review = camera_reviews[0]
        >>> review.sents()[0] # doctest: +NORMALIZE_WHITESPACE
        ['i', 'recently', 'purchased', 'the', 'canon', 'powershot', 'g3', 'and', 'am',
        'extremely', 'satisfied', 'with', 'the', 'purchase', '.']
        >>> review.features() # doctest: +NORMALIZE_WHITESPACE
        [('canon powershot g3', '+3'), ('use', '+2'), ('picture', '+2'),
        ('picture quality', '+1'), ('picture quality', '+1'), ('camera', '+2'),
        ('use', '+2'), ('feature', '+1'), ('picture quality', '+3'), ('use', '+1'),
        ('option', '+1')]

    We can also reach the same information directly from the stream:

        >>> product_reviews_1.features('Canon_G3.txt')
        [('canon powershot g3', '+3'), ('use', '+2'), ...]

    We can compute stats for specific product features:

        >>> n_reviews = len([(feat,score) for (feat,score) in product_reviews_1.features('Canon_G3.txt') if feat=='picture'])
        >>> tot = sum([int(score) for (feat,score) in product_reviews_1.features('Canon_G3.txt') if feat=='picture'])
        >>> mean = tot / n_reviews
        >>> print(n_reviews, tot, mean)
        15 24 1.6
    Úutf8c                 C   s    t  | |||¡ || _d| _dS )ad  
        :param root: The root directory for the corpus.
        :param fileids: a list or regexp specifying the fileids in the corpus.
        :param word_tokenizer: a tokenizer for breaking sentences or paragraphs
            into words. Default: `WordPunctTokenizer`
        :param encoding: the encoding that should be used to read the corpus.
        z
README.txtN)ÚCorpusReaderr	   Ú_word_tokenizerZ_readme)r   ÚrootÚfileidsZword_tokenizerÚencodingr   r   r   r	   ¼   s    zReviewsCorpusReader.__init__Nc                    s>   |du rˆ j }nt|tƒr |g}t‡ fdd„ˆ  |d¡D ƒƒS )au  
        Return a list of features. Each feature is a tuple made of the specific
        item feature and the opinion strength about that feature.

        :param fileids: a list or regexp specifying the ids of the files whose
            features have to be returned.
        :return: all features for the item(s) in the given file(s).
        :rtype: list(tuple)
        Nc                    s"   g | ]\}}ˆ j |ˆ j|d �‘qS ©)r%   )Ú
CorpusViewÚ_read_features©r   ÚfileidÚencr   r   r   r   Ú   s   ÿz0ReviewsCorpusReader.features.<locals>.<listcomp>T)Ú_fileidsr
   ÚstrÚconcatÚabspaths©r   r$   r   r   r   r   Ë   s    



þÿzReviewsCorpusReader.featuresc                    s,   |du rˆ j }t‡ fdd„ˆ  |d¡D ƒƒS )aS  
        Return all the reviews as a list of Review objects. If `fileids` is
        specified, return all the reviews from each of the specified files.

        :param fileids: a list or regexp specifying the ids of the files whose
            reviews have to be returned.
        :return: the given file(s) as a list of reviews.
        Nc                    s"   g | ]\}}ˆ j |ˆ j|d �‘qS r&   )r'   Ú_read_review_blockr)   r   r   r   r   ì   s   ÿz/ReviewsCorpusReader.reviews.<locals>.<listcomp>T)r,   r.   r/   r0   r   r   r   Úreviewsà   s    	

þÿzReviewsCorpusReader.reviewsc                    s    t ‡ fdd„ˆ  |dd¡D ƒƒS )aY  
        Return all sentences in the corpus or in the specified files.

        :param fileids: a list or regexp specifying the ids of the files whose
            sentences have to be returned.
        :return: the given file(s) as a list of sentences, each encoded as a
            list of word strings.
        :rtype: list(list(str))
        c                    s$   g | ]\}}}ˆ j |ˆ j|d �‘qS r&   )r'   Ú_read_sent_block©r   Úpathr+   r*   r   r   r   r   ý   s   ÿz-ReviewsCorpusReader.sents.<locals>.<listcomp>T©r.   r/   r0   r   r   r   r   ò   s
    

þÿzReviewsCorpusReader.sentsc                    s    t ‡ fdd„ˆ  |dd¡D ƒƒS )aK  
        Return all words and punctuation symbols in the corpus or in the specified
        files.

        :param fileids: a list or regexp specifying the ids of the files whose
            words have to be returned.
        :return: the given file(s) as a list of words and punctuation symbols.
        :rtype: list(str)
        c                    s$   g | ]\}}}ˆ j |ˆ j|d �‘qS r&   )r'   Ú_read_word_blockr4   r   r   r   r     s   ÿz-ReviewsCorpusReader.words.<locals>.<listcomp>Tr6   r0   r   r   r   Úwords  s
    

þÿzReviewsCorpusReader.wordsc                 C   s<   g }t dƒD ]*}| ¡ }|s$|  S | t t|¡¡ q|S )Né   )ÚrangeÚreadliner   ÚreÚfindallÚFEATURES)r   Ústreamr   ÚiÚliner   r   r   r(     s    z"ReviewsCorpusReader._read_featuresc           
      C   sÄ   |  ¡ }|sg S t t|¡}|r t| d¡ ¡ d�}q8q | ¡ }|  ¡ }|sR|gS t t|¡rn| |¡ |gS t 	t
|¡}t 	t|¡}t 	t|¡}|r¦| j |d ¡}t|||d�}	| |	¡ q8d S )Né   )r   r   r   )r;   r<   ÚmatchÚTITLEr   ÚgroupÚstripÚtellÚseekr=   r>   ÚNOTESÚSENTr"   Útokenizer   r   )
r   r?   rA   Ztitle_matchÚreviewÚoldposZfeatsr   r   r   r   r   r   r1     s.    ÿ
z&ReviewsCorpusReader._read_review_blockc                 C   s0   g }|   |¡D ]}| dd„ | ¡ D ƒ¡ q|S )Nc                 S   s   g | ]}|‘qS r   r   )r   r   r   r   r   r   A  r   z8ReviewsCorpusReader._read_sent_block.<locals>.<listcomp>)r1   r   r   )r   r?   r   rL   r   r   r   r3   >  s    z$ReviewsCorpusReader._read_sent_blockc                 C   sD   g }t dƒD ]2}| ¡ }t t|¡}|r| | j |d ¡¡ q|S )Nr9   r   )r:   r;   r<   r=   rJ   r   r"   rK   )r   r?   r8   r@   rA   r   r   r   r   r7   D  s    z$ReviewsCorpusReader._read_word_block)N)N)N)N)r   r   r   r   ZStreamBackedCorpusViewr'   ZWordPunctTokenizerr	   r   r2   r   r8   r(   r1   r3   r7   r   r   r   r   r   š   s   ÿ




	!r   )r   r<   Znltk.corpus.reader.apiZnltk.tokenizeÚcompilerD   r>   rI   rJ   r   r   r!   r   r   r   r   r   Ú<module>   s   8
ÿ

5