ó
    ´OÂfÉ(  ã                   óp   • S r SSKJr  SSKJr  SSKJrJrJrJ	r	   " S S\5      r
S r\S:X  a  \" 5         g	g	)
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                   óZ   • \ rS rSrSrS rS rS rS rSS jr	SS jr
\\4S	 j5       rS
rg)ÚNaiveBayesClassifieré+   aÌ  
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                 óX   • Xl         X l        [        UR                  5       5      U l        g)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      Ú:/usr/lib/python3/dist-packages/nltk/classify/naivebayes.pyÚ__init__ÚNaiveBayesClassifier.__init__@   s%   € ð"  .ÔØ!1ÔÜ˜N×2Ñ2Ó4Ó5ˆ�ó    c                 ó   • U R                   $ ©N)r   )r   s    r   ÚlabelsÚNaiveBayesClassifier.labelsU   s   € Ø�|‰|Ðr   c                 ó@   • U R                  U5      R                  5       $ r   )Úprob_classifyÚmax)r   Ú
featuresets     r   ÚclassifyÚNaiveBayesClassifier.classifyX   s   € Ø×!Ñ! *Ó-×1Ñ1Ó3Ð3r   c                 ó,  • UR                  5       n[        UR                  5       5       H,  nU R                   H  nX24U R                  ;   d  M    M(     X	 M.     0 nU R                   H   nU R
                  R                  U5      XC'   M"     U R                   Ho  nUR                  5        HX  u  p%X24U R                  ;   a-  U R                  X24   nXC==   UR                  U5      -  ss'   MC  XC==   [        / 5      -  ss'   MZ     Mq     [        USSS9$ )NT)Ú	normalizeÚlog)
Úcopyr   Úkeysr   r   r   ÚlogprobÚitemsr   r   )r   r    ÚfnameÚlabelr(   ÚfvalÚfeature_probss          r   r   Ú"NaiveBayesClassifier.prob_classify[   sý   € ð  —_‘_Ó&ˆ
Ü˜*Ÿ/™/Ó+Ö,ˆEØŸœ�Ø�> T×%;Ñ%;Õ;Úñ &ð
 Ò%ñ -ð ˆØ—\”\ˆEØ!×1Ñ1×9Ñ9¸%Ó@ˆG‹Nñ "ð —\”\ˆEØ)×/Ñ/Ö1‘�Ø�> T×%;Ñ%;Ó;Ø$(×$:Ñ$:¸5¸<Ñ$H�MØ“N m×&;Ñ&;¸DÓ&AÑA•Nð
 “N¤h¨r£lÑ2•Nó  2ñ "ô " '°T¸tÑDÐDr   c                 óø  ^^^^	• U R                   m[        S5        U R                  U5       HÊ  u  mmUUU4S jm	[        UUU4S jU R                   5       U	4S jSS9n[        U5      S:X  a  MD  US   nUS	   nTUT4   R                  T5      S:X  a  S
nO0STUT4   R                  T5      TUT4   R                  T5      -  -  n[        T<S ST<S SSU-  S S <S SSU-  S S <S SU< S3
5        MÌ     g )NzMost Informative Featuresc                 ó0   >• TU T4   R                  T5      $ r   )Úprob)ÚlÚcpdistr*   r,   s    €€€r   Ú	labelprobÚFNaiveBayesClassifier.show_most_informative_features.<locals>.labelprobƒ   s   ø€ Ø˜a ˜hÑ'×,Ñ,¨TÓ2Ð2r   c              3   ó^   >#   • U  H"  nTTUT4   R                  5       ;   d  M  Uv •  M$     g 7fr   )r   )Ú.0r2   r3   r*   r,   s     €€€r   Ú	<genexpr>ÚFNaiveBayesClassifier.show_most_informative_features.<locals>.<genexpr>‡   s,   øé € ÐOšL�q¨D°F¸1¸e¸8Ñ4D×4LÑ4LÓ4NÑ,N—‘šLùs   ƒ-¤	-c                 ó   >• T" U 5      * U 4$ r   © )Úelementr4   s    €r   Ú<lambda>ÚENaiveBayesClassifier.show_most_informative_features.<locals>.<lambda>ˆ   s   ø€ ¡i°Ó&8Ð%8¸'Ñ$Br   T)ÚkeyÚreverseé   r   éÿÿÿÿÚINFz%8.1fz>24z = Ú14Ú z%sé   z>6z : Ú6z : 1.0)r   ÚprintÚmost_informative_featuresÚsortedr   Úlenr1   )
r   Únr   Úl0Úl1Úratior3   r*   r,   r4   s
         @@@@r   Úshow_most_informative_featuresÚ3NaiveBayesClassifier.show_most_informative_features|   s   û€ à×'Ñ'ˆÜÐ)Ô*à×9Ñ9¸!Ö<‰KˆE�4÷3ô ÞO˜DŸLšLÓOÜBØñˆFô
 �6‹{˜aÓÙØ˜‘ˆBØ˜‘ˆBØ�b˜%�iÑ ×%Ñ% dÓ+¨qÓ0Ø‘àØ˜2˜u˜9Ñ%×*Ñ*¨4Ó0°6¸"¸e¸)Ñ3D×3IÑ3IÈ$Ó3OÑOñ�ô äœ$ ¨¡¨B¨Q¤°$¸±)¸R¸a´Ã%ðIöò) =r   c                 ó  ^	^
• [        U S5      (       a  U R                  SU $ [        5       n[        [        5      m	[        S 5      m
U R
                  R                  5        H„  u  u  p4nUR                  5        Hh  nXF4nUR                  U5        UR                  U5      n[        UT	U   5      T	U'   [        UT
U   5      T
U'   T
U   S:X  d  MW  UR                  U5        Mj     M†     [        UU	U
4S jS9U l        U R                  SU $ )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                  ó   • g)Ng      ð?r;   r;   r   r   r=   Ú@NaiveBayesClassifier.most_informative_features.<locals>.<lambda>¬   s   € ¨#r   r   c                 óh   >• TU    TU    -  U S   U S   S;   [        U S   5      R                  5       4$ )Nr   rA   )NFT)ÚstrÚlower)Úfeature_ÚmaxprobÚminprobs    €€r   r=   rU   ¼   sC   ø€ Ø˜HÑ%¨°Ñ(9Ñ9Ø˜Q‘KØ˜Q‘KÐ#6Ñ6Ü˜ ™Ó$×*Ñ*Ó,ñ	&r   )r?   )ÚhasattrrS   Úsetr   Úfloatr   r)   r   Úaddr1   r   ÚminÚdiscardrJ   )r   rL   Úfeaturesr+   r*   Úprobdistr,   ÚfeatureÚprZ   r[   s            @@r   rI   Ú.NaiveBayesClassifier.most_informative_featuresš   s  ù€ ô �4Ð5×6Ñ6Ø×2Ñ2°2°AÐ6Ð6ô “uˆHô "¤%Ó(ˆGÜ!¡+Ó.ˆGà,0×,BÑ,B×,HÑ,HÖ,JÑ(‘� Ø$×,Ñ,Ö.�DØ$˜m�GØ—L‘L Ô)Ø Ÿ™ dÓ+�AÜ'*¨1¨g°gÑ.>Ó'?�G˜GÑ$Ü'*¨1¨g°gÑ.>Ó'?�G˜GÑ$Ø˜wÑ'¨1Õ,Ø ×(Ñ(¨Ö1ó /ñ -Kô /5Øõñ/ˆDÔ+ð ×.Ñ.¨r°Ð2Ð2r   c                 óp  • [        5       n[        [         5      n[        [        5      n[        5       nU H_  u  pxX8==   S-  ss'   UR                  5        H:  u  pšXHU	4   U
==   S-  ss'   XY   R	                  U
5        UR	                  U	5        M<     Ma     U HT  nX8   nU HG  n	XHU	4   R                  5       nX¼-
  S:”  d  M!  XHU	4   S==   X¼-
  -  ss'   XY   R	                  S5        MI     MV     U" U5      n0 nUR                  5        H   u  u  p‰nU" U[        XY   5      S9nUXèU	4'   M"     U " XÞ5      $ )zs
:param labeled_featuresets: A list of classified featuresets,
    i.e., a list of tuples ``(featureset, label)``.
rA   r   N)Úbins)r   r   r]   r)   r_   ÚNrK   )ÚclsÚlabeled_featuresetsÚ	estimatorÚlabel_freqdistÚfeature_freqdistÚfeature_valuesÚfnamesr    r+   r*   r,   Únum_samplesÚcountr   r   Úfreqdistrc   s                    r   ÚtrainÚNaiveBayesClassifier.trainÅ   sU  € ô "›ˆÜ&¤xÓ0ÐÜ$¤SÓ)ˆÜ“ˆó "5ÑˆJØÓ! QÑ&Ó!Ø)×/Ñ/Ö1‘�à ¨ Ñ.¨tÓ4¸Ñ9Ó4àÑ%×)Ñ)¨$Ô/à—
‘
˜5Ö!ó  2ñ "5ó $ˆEØ(Ñ/ˆKÛ�Ø(°¨Ñ6×8Ñ8Ó:�ð Ñ&¨Õ*Ø$¨E \Ñ2°4Ó8¸KÑ<OÑOÓ8Ø"Ñ)×-Ñ-¨dÖ3ó  ñ $ñ # >Ó2ˆð ÐØ(8×(>Ñ(>Ö(@Ñ$‰NˆU˜HÙ  ´°NÑ4IÓ0JÑKˆHØ-5Ð E˜\Ó*ñ )Añ �>Ó4Ð4r   )r   r   r   rS   N)é
   )éd   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r!   r   rP   rI   Úclassmethodr   rt   Ú__static_attributes__r;   r   r   r
   r
   +   s?   † ñò(6ò*ò4òEôBô<)3ðV Ø2=ó .5ó ó.5r   r
   c                  ó\   • SSK Jn   U " [        R                  5      nUR	                  5         g )Nr   )Ú
names_demo)Únltk.classify.utilr€   r
   rt   rP   )r€   Ú
classifiers     r   Údemorƒ   ü   s"   € Ý-áÔ0×6Ñ6Ó7€JØ×-Ñ-Õ/r   Ú__main__N)r|   Úcollectionsr   Únltk.classify.apir   Únltk.probabilityr   r   r   r   r
   rƒ   rx   r;   r   r   Ú<module>rˆ      s@   ðñõ2 $å )ß PÓ PôI5˜;ô I5òb0ð ˆzÓÙ…Fð r   