§
    'ê[f!0  ã                   óò   — d Z ddlZddlT ddlT  ej        d¦  «        Z ej        d¦  «        Z ej        d¦  «        Z ej        d¦  «        Z G d„ d	¦  «        Z	 G d
„ d¦  «        Z
 G 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                   ó2   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zd„ ZdS )	ÚReviewz>
    A Review is the main block of a ReviewsCorpusReader.
    Nc                 ó8   — || _         |€	g | _        dS || _        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   s      úN/var/www/piapp/venv/lib/python3.11/site-packages/nltk/corpus/reader/reviews.pyÚ__init__zReview.__init__R   s-   € ð
 ˆŒ
ØÐØ "ˆDÔÐÐà ,ˆDÔÐÐó    c                 óh   — 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_lines     r
   Úadd_linezReview.add_line]   s7   € õ ˜+¥zÑ2Ô2Ð2Ð2Ð2ØÔ× Ò  Ñ-Ô-Ð-Ð-Ð-r   c                 óR   — g }| j         D ]}|                     |j        ¦  «         Œ|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   s      r
   r   zReview.featuresf   s8   € ð ˆØÔ,ð 	2ð 	2ˆKØ�OŠO˜KÔ0Ñ1Ô1Ð1Ð1Øˆr   c                 ó$   — 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                 ó   — g | ]	}|j         ‘Œ
S © )Úsent)Ú.0r   s     r
   ú
<listcomp>z Review.sents.<locals>.<listcomp>z   s   € ÐFÐFÐF [�Ô ÐFÐFÐFr   )r   ©r	   s    r
   ÚsentszReview.sentss   s   € ð GÐF°DÔ4EÐFÑFÔFÐFr   c                 óB   — d                      | j        | j        ¦  «        S )Nz#Review(title="{}", review_lines={}))Úformatr   r   r   s    r
   Ú__repr__zReview.__repr__|   s$   € Ø4×;Ò;ØŒJ˜Ô)ñ
ô 
ð 	
r   ©NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    r   r   r
   r   r   M   sq   € € € € € ðð ð	-ð 	-ð 	-ð 	-ð.ð .ð .ðð ð ðGð Gð Gð
ð 
ð 
ð 
ð 
r   r   c                   ó    — e Zd ZdZdd„Z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                 óZ   — || _         |€g | _        n|| _        |€	g | _        d S || _        d S ©N©r   r   Únotes)r	   r   r   r*   s       r
   r   zReviewLine.__init__ˆ   s:   € ØˆŒ	ØÐØˆDŒMˆMà$ˆDŒMàˆ=ØˆDŒJˆJˆJàˆDŒJˆJˆJr   c                 óN   — d                      | j        | j        | j        ¦  «        S )Nz*ReviewLine(features={}, notes={}, sent={}))r   r   r*   r   r   s    r
   r    zReviewLine.__repr__”   s'   € Ø;×BÒBØŒM˜4œ: t¤yñ
ô 
ð 	
r   r!   )r"   r#   r$   r%   r   r    r   r   r
   r   r   ‚   sA   € € € € € ðð ð

ð 
ð 
ð 
ð
ð 
ð 
ð 
ð 
r   r   c                   ój   — e Zd ZdZeZ e¦   «         dfd„Zdd„Zdd„Z	dd„Z
dd„Zd	„ Zd
„ Zd„ Z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                 ó\   — 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_tokenizerÚ_readme)r	   ÚrootÚfileidsÚword_tokenizerÚencodings        r
   r   zReviewsCorpusReader.__init__¼   s1   € õ 	×Ò˜d D¨'°8Ñ<Ô<Ð<Ø-ˆÔØ#ˆŒˆˆr   Nc                 ó¦   ‡ — |€‰ j         }nt          |t          ¦  «        r|g}t          ˆ f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                 óP   •— g | ]"\  }}‰                      |‰j        |¬ ¦  «        ‘Œ#S ©)r6   )Ú
CorpusViewÚ_read_features©r   ÚfileidÚencr	   s      €r
   r   z0ReviewsCorpusReader.features.<locals>.<listcomp>Ú   sB   ø€ ð ð ð á!�V˜Sð —’ ¨Ô(;Àc�ÑJÔJðð ð r   T)Ú_fileidsr   ÚstrÚconcatÚabspaths©r	   r4   s   ` r
   r   zReviewsCorpusReader.featuresË   sr   ø€ ð ˆ?Ø”mˆGˆGÝ˜¥Ñ%Ô%ð 	 Ø�iˆGÝðð ð ð à%)§]¢]°7¸DÑ%AÔ%Aðñ ô ñ
ô 
ð 	
r   c                 ót   ‡ — |€‰ j         }t          ˆ f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                 óP   •— g | ]"\  }}‰                      |‰j        |¬ ¦  «        ‘Œ#S r9   )r:   Ú_read_review_blockr<   s      €r
   r   z/ReviewsCorpusReader.reviews.<locals>.<listcomp>ì   sB   ø€ ð ð ð á!�V˜Sð —’ ¨Ô(?È#�ÑNÔNðð ð r   T)r?   rA   rB   rC   s   ` r
   ÚreviewszReviewsCorpusReader.reviewsà   sW   ø€ ð ˆ?Ø”mˆGÝðð ð ð à%)§]¢]°7¸DÑ%AÔ%Aðñ ô ñ
ô 
ð 	
r   c                 ód   ‡ — t          ˆ f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                 óR   •— g | ]#\  }}}‰                      |‰j        |¬ ¦  «        ‘Œ$S r9   )r:   Ú_read_sent_block©r   Úpathr>   r=   r	   s       €r
   r   z-ReviewsCorpusReader.sents.<locals>.<listcomp>ý   óD   ø€ ð ð ð á'�T˜3 ð —’  dÔ&;Àc�ÑJÔJðð ð r   T©rA   rB   rC   s   ` r
   r   zReviewsCorpusReader.sentsò   óM   ø€ õ ðð ð ð à+/¯=ª=¸À$ÈÑ+MÔ+Mðñ ô ñ
ô 
ð 	
r   c                 ód   ‡ — t          ˆ f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                 óR   •— g | ]#\  }}}‰                      |‰j        |¬ ¦  «        ‘Œ$S r9   )r:   Ú_read_word_blockrK   s       €r
   r   z-ReviewsCorpusReader.words.<locals>.<listcomp>  rM   r   TrN   rC   s   ` r
   ÚwordszReviewsCorpusReader.words  rO   r   c                 ó¼   — g }t          d¦  «        D ]I}|                     ¦   «         }|s|c S |                     t          j        t
          |¦  «        ¦  «         ŒJ|S )Né   )ÚrangeÚreadliner   ÚreÚfindallÚFEATURES)r	   Ústreamr   ÚiÚlines        r
   r;   z"ReviewsCorpusReader._read_features  sb   € ØˆÝ�r‘”ð 	8ð 	8ˆAØ—?’?Ñ$Ô$ˆDØð  Ø���Ø�OŠO�BœJ¥x°Ñ6Ô6Ñ7Ô7Ð7Ð7Øˆr   c                 óÊ  — 	 |                      ¦   «         }|sg S t          j        t          |¦  «        }|r6t	          |                     d¦  «                             ¦   «         ¬¦  «        }nŒk	 |                     ¦   «         }|                      ¦   «         }|s|gS t          j        t          |¦  «        r|                     |¦  «         |gS t          j	        t          |¦  «        }t          j	        t          |¦  «        }t          j	        t          |¦  «        }|r | j                             |d         ¦  «        }t          |||¬¦  «        }	|                     |	¦  «         Œ÷)NTé   )r   r   r)   )rW   rX   ÚmatchÚTITLEr   ÚgroupÚstripÚtellÚseekrY   rZ   ÚNOTESÚSENTr1   Útokenizer   r   )
r	   r[   r]   Útitle_matchÚreviewÚoldposÚfeatsr*   r   r   s
             r
   rF   z&ReviewsCorpusReader._read_review_block  sV  € ð		Ø—?’?Ñ$Ô$ˆDØð Ø�	Ýœ(¥5¨$Ñ/Ô/ˆKØð ÝØ%×+Ò+¨AÑ.Ô.×4Ò4Ñ6Ô6ðñ ô �ð ð		ð	)Ø—[’[‘]”]ˆFØ—?’?Ñ$Ô$ˆDàð  Ø�x�õ Œx�˜tÑ$Ô$ð  Ø—’˜FÑ#Ô#Ð#Ø�x�å”J�x¨Ñ.Ô.ˆEÝ”J�u dÑ+Ô+ˆEÝ”:�d DÑ)Ô)ˆDØð >ØÔ+×4Ò4°T¸!´WÑ=Ô=�Ý$¨$¸ÀeÐLÑLÔLˆKØ�OŠO˜KÑ(Ô(Ð(ð%	)r   c                 óœ   — g }|                       |¦  «        D ]3}|                     d„ |                     ¦   «         D ¦   «         ¦  «         Œ4|S )Nc                 ó   — g | ]}|‘ŒS r   r   )r   r   s     r
   r   z8ReviewsCorpusReader._read_sent_block.<locals>.<listcomp>A  s   € Ð:Ð:Ð: 4˜$Ð:Ð:Ð:r   )rF   r   r   )r	   r[   r   rj   s       r
   rJ   z$ReviewsCorpusReader._read_sent_block>  sU   € ØˆØ×-Ò-¨fÑ5Ô5ð 	<ð 	<ˆFØ�LŠLÐ:Ð:¨6¯<ª<©>¬>Ð:Ñ:Ô:Ñ;Ô;Ð;Ð;Øˆr   c                 óô   — g }t          d¦  «        D ]e}|                     ¦   «         }t          j        t          |¦  «        }|r3|                     | j                             |d         ¦  «        ¦  «         Œf|S )NrU   r   )rV   rW   rX   rY   rg   r   r1   rh   )r	   r[   rS   r\   r]   r   s         r
   rR   z$ReviewsCorpusReader._read_word_blockD  st   € ØˆÝ�r‘”ð 	Eð 	EˆAØ—?’?Ñ$Ô$ˆDÝ”:�d DÑ)Ô)ˆDØð EØ—’˜TÔ1×:Ò:¸4À¼7ÑCÔCÑDÔDÐDøØˆr   r(   )r"   r#   r$   r%   ÚStreamBackedCorpusViewr:   ÚWordPunctTokenizerr   r   rG   r   rS   r;   rF   rJ   rR   r   r   r
   r-   r-   š   sÖ   € € € € € ðð ð> (€Jð -?Ð,>Ñ,@Ô,@È6ð$ð $ð $ð $ð
ð 
ð 
ð 
ð*
ð 
ð 
ð 
ð$
ð 
ð 
ð 
ð"
ð 
ð 
ð 
ð"ð ð ð)ð )ð )ðBð ð ðð ð ð ð r   r-   )r%   rX   Únltk.corpus.reader.apiÚnltk.tokenizeÚcompilera   rZ   rf   rg   r   r   r0   r-   r   r   r
   ú<module>ru      s  ðð6ð 6ðp 
€	€	€	à $Ð $Ð $Ð $Ø Ð Ð Ð àˆŒ
�>Ñ"Ô"€Øˆ2Œ:Ø,ñô €ð 	ˆŒ
Ð,Ñ-Ô-€Ø€r„z�*ÑÔ€ð2
ð 2
ð 2
ð 2
ð 2
ñ 2
ô 2
ð 2
ðj
ð 
ð 
ð 
ð 
ñ 
ô 
ð 
ð0qð qð qð qð q˜,ñ qô qð qð qð qr   