§
    'ê[fÙ)  ã                   ó€   — d Z ddlmZ ddlmZ ddlmZmZmZm	Z	  G d„ de¦  «        Z
d„ Zedk    r e¦   «          d	S d	S )
aê  
A classifier based on the Naive Bayes algorithm.  In order to find the
probability for a label, this algorithm first uses the Bayes rule to
express P(label|features) in terms of P(label) and P(features|label):

|                       P(label) * P(features|label)
|  P(label|features) = ------------------------------
|                              P(features)

The algorithm then makes the 'naive' assumption that all features are
independent, given the label:

|                       P(label) * P(f1|label) * ... * P(fn|label)
|  P(label|features) = --------------------------------------------
|                                         P(features)

Rather than computing P(features) explicitly, the algorithm just
calculates the numerator for each label, and normalizes them so they
sum to one:

|                       P(label) * P(f1|label) * ... * P(fn|label)
|  P(label|features) = --------------------------------------------
|                        SUM[l]( P(l) * P(f1|l) * ... * P(fn|l) )
é    )Údefaultdict)ÚClassifierI)ÚDictionaryProbDistÚELEProbDistÚFreqDistÚsum_logsc                   óT   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zdd„Zdd	„Z	e
efd
„¦   «         ZdS )ÚNaiveBayesClassifiera  
    A Naive Bayes classifier.  Naive Bayes classifiers are
    paramaterized by two probability distributions:

      - P(label) gives the probability that an input will receive each
        label, given no information about the input's features.

      - P(fname=fval|label) gives the probability that a given feature
        (fname) will receive a given value (fval), given that the
        label (label).

    If the classifier encounters an input with a feature that has
    never been seen with any label, then rather than assigning a
    probability of 0 to all labels, it will ignore that feature.

    The feature value 'None' is reserved for unseen feature values;
    you generally should not use 'None' as a feature value for one of
    your own features.
    c                 ón   — || _         || _        t          |                     ¦   «         ¦  «        | _        dS )a=  
        :param label_probdist: P(label), the probability distribution
            over labels.  It is expressed as a ``ProbDistI`` whose
            samples are labels.  I.e., P(label) =
            ``label_probdist.prob(label)``.

        :param feature_probdist: P(fname=fval|label), the probability
            distribution for feature values, given labels.  It is
            expressed as a dictionary whose keys are ``(label, fname)``
            pairs and whose values are ``ProbDistI`` objects over feature
            values.  I.e., P(fname=fval|label) =
            ``feature_probdist[label,fname].prob(fval)``.  If a given
            ``(label,fname)`` is not a key in ``feature_probdist``, then
            it is assumed that the corresponding P(fname=fval|label)
            is 0 for all values of ``fval``.
        N)Ú_label_probdistÚ_feature_probdistÚlistÚsamplesÚ_labels)ÚselfÚlabel_probdistÚfeature_probdists      úL/var/www/piapp/venv/lib/python3.11/site-packages/nltk/classify/naivebayes.pyÚ__init__zNaiveBayesClassifier.__init__@   s3   € ð"  .ˆÔØ!1ˆÔÝ˜N×2Ò2Ñ4Ô4Ñ5Ô5ˆŒˆˆó    c                 ó   — | j         S ©N)r   )r   s    r   ÚlabelszNaiveBayesClassifier.labelsU   s
   € ØŒ|Ðr   c                 óP   — |                       |¦  «                             ¦   «         S r   )Úprob_classifyÚmax)r   Ú
featuresets     r   ÚclassifyzNaiveBayesClassifier.classifyX   s"   € Ø×!Ò! *Ñ-Ô-×1Ò1Ñ3Ô3Ð3r   c                 ó  — |                      ¦   «         }t          |                     ¦   «         ¦  «        D ]}| j        D ]}||f| j        v r nŒ||= Œi }| j        D ]}| j                             |¦  «        ||<   Œ | j        D ]w}|                     ¦   «         D ]`\  }}||f| j        v r3| j        ||f         }||xx         |                     |¦  «        z  cc<   ŒC||xx         t          g ¦  «        z  cc<   ŒaŒxt          |dd¬¦  «        S )NT)Ú	normalizeÚlog)
Úcopyr   Úkeysr   r   r   ÚlogprobÚitemsr   r   )r   r   ÚfnameÚlabelr$   ÚfvalÚfeature_probss          r   r   z"NaiveBayesClassifier.prob_classify[   sY  € ð  —_’_Ñ&Ô&ˆ
Ý˜*Ÿ/š/Ñ+Ô+Ñ,Ô,ð 	&ð 	&ˆEØœð &ð &�Ø˜5�> TÔ%;Ð;Ð;Ø�Eð <ð ˜uÐ%øð ˆØ”\ð 	Að 	AˆEØ!Ô1×9Ò9¸%Ñ@Ô@ˆG�E‰NˆNð ”\ð 		3ð 		3ˆEØ!+×!1Ò!1Ñ!3Ô!3ð 3ð 3‘�˜Ø˜5�> TÔ%;Ð;Ð;Ø$(Ô$:¸5À%¸<Ô$H�MØ˜E�N�N”N m×&;Ò&;¸DÑ&AÔ&AÑA�N�N‘N�Nð
 ˜E�N�N”N¥h¨r¡l¤lÑ2�N�N‘N�Nð3õ " '°T¸tÐDÑDÔDÐDr   é
   c                 óJ  ‡‡‡‡	— | j         Št          d¦  «         |                      |¦  «        D ]ò\  ŠŠˆˆˆfd„Š	t          ˆˆˆfd„| j        D ¦   «         ˆ	fd„d¬¦  «        }t          |¦  «        dk    rŒH|d         }|d	         }‰|‰f                              ‰¦  «        dk    rd
}n>d‰|‰f                              ‰¦  «        ‰|‰f                              ‰¦  «        z  z  }t          ‰d›d‰d›dd|z  d d…         d›dd|z  d d…         d›d|›d�
¦  «         Œód S )NzMost Informative Featuresc                 ó>   •— ‰| ‰f                               ‰¦  «        S r   )Úprob)ÚlÚcpdistr&   r(   s    €€€r   Ú	labelprobzFNaiveBayesClassifier.show_most_informative_features.<locals>.labelprobƒ   s    ø€ Ø˜a ˜hÔ'×,Ò,¨TÑ2Ô2Ð2r   c              3   óX   •K  — | ]$}‰‰|‰f                               ¦   «         v ¯ |V — Œ%d S r   )r   )Ú.0r.   r/   r&   r(   s     €€€r   ú	<genexpr>zFNaiveBayesClassifier.show_most_informative_features.<locals>.<genexpr>‡   sB   øè è € ÐOÐO�q¨D°F¸1¸e¸8Ô4D×4LÒ4LÑ4NÔ4NÐ,NÐ,N�Ð,NÐ,NÐ,NÐ,NÐOÐOr   c                 ó    •—  ‰| ¦  «         | fS r   © )Úelementr0   s    €r   ú<lambda>zENaiveBayesClassifier.show_most_informative_features.<locals>.<lambda>ˆ   s   ø€  i i°Ñ&8Ô&8Ð%8¸'Ð$B€ r   T)ÚkeyÚreverseé   r   éÿÿÿÿÚINFz%8.1fz>24z = Ú14Ú z%sé   z>6z : Ú6z : 1.0)r   ÚprintÚmost_informative_featuresÚsortedr   Úlenr-   )
r   Únr   Úl0Úl1Úratior/   r&   r(   r0   s
         @@@@r   Úshow_most_informative_featuresz3NaiveBayesClassifier.show_most_informative_features|   sž  øøøø€ àÔ'ˆÝÐ)Ñ*Ô*Ð*à!×;Ò;¸AÑ>Ô>ð 	ð 	‰MˆU�Dð3ð 3ð 3ð 3ð 3ð 3ð 3õ ØOÐOÐOÐOÐOÐO˜DœLÐOÑOÔOØBÐBÐBÐBØðñ ô ˆFõ
 �6‰{Œ{˜aÒÐØØ˜”ˆBØ˜”ˆBØ�b˜%�iÔ ×%Ò% dÑ+Ô+¨qÒ0Ð0Ø��àØ˜2˜u˜9Ô%×*Ò*¨4Ñ0Ô0°6¸"¸e¸)Ô3D×3IÒ3IÈ$Ñ3OÔ3OÑOñ�õ à�5�5�5˜$˜$˜$˜$ ¨¡¨B¨Q¨B¤   °$¸±)¸R¸a¸R´°°°À%À%À%ðIñô ð ð ð)	ð 	r   éd   c                 óf  ‡	‡
— t          | d¦  «        r| j        d|…         S t          ¦   «         }t          d„ ¦  «        Š	t          d„ ¦  «        Š
| j                             ¦   «         D ] \  \  }}}|                     ¦   «         D ]ƒ}||f}|                     |¦  «         |                     |¦  «        }t          |‰	|         ¦  «        ‰	|<   t          |‰
|         ¦  «        ‰
|<   ‰
|         dk    r|                     |¦  «         Œ„Œ¡t          |ˆ	ˆ
fd„¬¦  «        | _        | j        d|…         S )a—  
        Return a list of the 'most informative' features used by this
        classifier.  For the purpose of this function, the
        informativeness of a feature ``(fname,fval)`` is equal to the
        highest value of P(fname=fval|label), for any label, divided by
        the lowest value of P(fname=fval|label), for any label:

        |  max[ P(fname=fval|label1) / P(fname=fval|label2) ]
        Ú_most_informative_featuresNc                  ó   — dS )Ng        r5   r5   r   r   r7   z@NaiveBayesClassifier.most_informative_features.<locals>.<lambda>«   ó   € ¨#€ r   c                  ó   — dS )Ng      ð?r5   r5   r   r   r7   z@NaiveBayesClassifier.most_informative_features.<locals>.<lambda>¬   rN   r   r   c                 ó”   •— ‰|          ‰|          z  | d         | d         dv t          | d         ¦  «                             ¦   «         fS )Nr   r:   )NFT)ÚstrÚlower)Úfeature_ÚmaxprobÚminprobs    €€r   r7   z@NaiveBayesClassifier.most_informative_features.<locals>.<lambda>¼   sK   ø€ Ø˜HÔ%¨°Ô(9Ñ9Ø˜Q”KØ˜Q”KÐ#6Ð6Ý˜ œÑ$Ô$×*Ò*Ñ,Ô,ð	&€ r   )r8   )ÚhasattrrL   Úsetr   r   r%   r   Úaddr-   r   ÚminÚdiscardrC   )r   rE   Úfeaturesr'   r&   Úprobdistr(   ÚfeatureÚprT   rU   s            @@r   rB   z.NaiveBayesClassifier.most_informative_featuresš   si  øø€ õ �4Ð5Ñ6Ô6ð 	ØÔ2°2°A°2Ô6Ð6õ ‘u”uˆHõ " + +Ñ.Ô.ˆGÝ! + +Ñ.Ô.ˆGà,0Ô,B×,HÒ,HÑ,JÔ,Jð 2ð 2Ñ(‘�˜ Ø$×,Ò,Ñ.Ô.ð 2ð 2�DØ$ d˜m�GØ—L’L Ñ)Ô)Ð)Ø Ÿš dÑ+Ô+�AÝ'*¨1¨g°gÔ.>Ñ'?Ô'?�G˜GÑ$Ý'*¨1¨g°gÔ.>Ñ'?Ô'?�G˜GÑ$Ø˜wÔ'¨1Ò,Ð,Ø ×(Ò(¨Ñ1Ô1Ð1øð2õ /5Øðð ð ð ð ð/ñ /ô /ˆDÔ+ð Ô.¨r°¨rÔ2Ð2r   c                 ó  — t          ¦   «         }t          t           ¦  «        }t          t          ¦  «        }t          ¦   «         }|D ]w\  }}||xx         dz  cc<   |                     ¦   «         D ]M\  }	}
|||	f         |
xx         dz  cc<   ||	                              |
¦  «         |                     |	¦  «         ŒNŒx|D ]j}||         }|D ]]}	|||	f                              ¦   «         }||z
  dk    r6|||	f         dxx         ||z
  z  cc<   ||	                              d¦  «         Œ^Œk ||¦  «        }i }|                     ¦   «         D ]/\  \  }}	} ||t          ||	         ¦  «        ¬¦  «        }||||	f<   Œ0 | ||¦  «        S )z‹
        :param labeled_featuresets: A list of classified featuresets,
            i.e., a list of tuples ``(featureset, label)``.
        r:   r   N)Úbins)r   r   rW   r%   rX   ÚNrD   )ÚclsÚlabeled_featuresetsÚ	estimatorÚlabel_freqdistÚfeature_freqdistÚfeature_valuesÚfnamesr   r'   r&   r(   Únum_samplesÚcountr   r   Úfreqdistr\   s                    r   ÚtrainzNaiveBayesClassifier.trainÅ   sü  € õ "™œˆÝ&¥xÑ0Ô0ÐÝ$¥SÑ)Ô)ˆÝ‘”ˆð "5ð 	"ð 	"ÑˆJ˜Ø˜5Ð!Ð!Ô! QÑ&Ð!Ð!Ñ!Ø)×/Ò/Ñ1Ô1ð "ð "‘��tà  ¨ Ô.¨tÐ4Ð4Ô4¸Ñ9Ð4Ð4Ñ4à˜uÔ%×)Ò)¨$Ñ/Ô/Ð/à—
’
˜5Ñ!Ô!Ð!Ð!ð"ð $ð 	4ð 	4ˆEØ(¨Ô/ˆKØð 4ð 4�Ø(¨°¨Ô6×8Ò8Ñ:Ô:�ð  Ñ&¨Ò*Ð*Ø$ U¨E \Ô2°4Ð8Ð8Ô8¸KÈ%Ñ<OÑOÐ8Ð8Ñ8Ø" 5Ô)×-Ò-¨dÑ3Ô3Ð3øð4ð #˜ >Ñ2Ô2ˆð ÐØ*:×*@Ò*@Ñ*BÔ*Bð 	6ð 	6Ñ&‰^ˆe�U˜XØ �y µ°NÀ5Ô4IÑ0JÔ0JÐKÑKÔKˆHØ-5Ð˜U E˜\Ñ*Ð*àˆs�>Ð#3Ñ4Ô4Ð4r   N)r*   )rJ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   rI   rB   Úclassmethodr   rl   r5   r   r   r
   r
   +   sª   € € € € € ðð ð(6ð 6ð 6ð*ð ð ð4ð 4ð 4ðEð Eð EðBð ð ð ð<)3ð )3ð )3ð )3ðV Ø2=ð .5ð .5ð .5ñ „[ð.5ð .5ð .5r   r
   c                  ód   — ddl m}   | t          j        ¦  «        }|                     ¦   «          d S )Nr   )Ú
names_demo)Únltk.classify.utilrs   r
   rl   rI   )rs   Ú
classifiers     r   Údemorv   ü   s?   € Ø-Ð-Ð-Ð-Ð-Ð-à�Õ0Ô6Ñ7Ô7€JØ×-Ò-Ñ/Ô/Ð/Ð/Ð/r   Ú__main__N)rp   Úcollectionsr   Únltk.classify.apir   Únltk.probabilityr   r   r   r   r
   rv   rm   r5   r   r   ú<module>r{      s¿   ððð ð2 $Ð #Ð #Ð #Ð #Ð #à )Ð )Ð )Ð )Ð )Ð )Ø PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PðI5ð I5ð I5ð I5ð I5˜;ñ I5ô I5ð I5ðb0ð 0ð 0ð ˆzÒÐØ€D�F„F€F€F€Fð Ðr   