o
    rZŽh@  ã                   @   sL   d Z ddlmZ ddlmZ ddlmZmZmZ G dd„ deƒZ	dd„ Z
d	S )
a  
A variant of the Naive Bayes Classifier that performs binary classification with
partially-labeled training sets. In other words, assume we want to build a classifier
that assigns each example to one of two complementary classes (e.g., male names and
female names).
If we have a training set with labeled examples for both classes, we can use a
standard Naive Bayes Classifier. However, consider the case when we only have labeled
examples for one of the classes, and other, unlabeled, examples.
Then, assuming a prior distribution on the two labels, we can use the unlabeled set
to estimate the frequencies of the various features.

Let the two possible labels be 1 and 0, and let's say we only have examples labeled 1
and unlabeled examples. We are also given an estimate of P(1).

We compute P(feature|1) exactly as in the standard case.

To compute P(feature|0), we first estimate P(feature) from the unlabeled set (we are
assuming that the unlabeled examples are drawn according to the given prior distribution)
and then express the conditional probability as:

|                  P(feature) - P(feature|1) * P(1)
|  P(feature|0) = ----------------------------------
|                               P(0)

Example:

    >>> from nltk.classify import PositiveNaiveBayesClassifier

Some sentences about sports:

    >>> sports_sentences = [ 'The team dominated the game',
    ...                      'They lost the ball',
    ...                      'The game was intense',
    ...                      'The goalkeeper catched the ball',
    ...                      'The other team controlled the ball' ]

Mixed topics, including sports:

    >>> various_sentences = [ 'The President did not comment',
    ...                       'I lost the keys',
    ...                       'The team won the game',
    ...                       'Sara has two kids',
    ...                       'The ball went off the court',
    ...                       'They had the ball for the whole game',
    ...                       'The show is over' ]

The features of a sentence are simply the words it contains:

    >>> def features(sentence):
    ...     words = sentence.lower().split()
    ...     return dict(('contains(%s)' % w, True) for w in words)

We use the sports sentences as positive examples, the mixed ones ad unlabeled examples:

    >>> positive_featuresets = map(features, sports_sentences)
    >>> unlabeled_featuresets = map(features, various_sentences)
    >>> classifier = PositiveNaiveBayesClassifier.train(positive_featuresets,
    ...                                                 unlabeled_featuresets)

Is the following sentence about sports?

    >>> classifier.classify(features('The cat is on the table'))
    False

What about this one?

    >>> classifier.classify(features('My team lost the game'))
    True
é    )Údefaultdict)ÚNaiveBayesClassifier)ÚDictionaryProbDistÚELEProbDistÚFreqDistc                   @   s   e Zd Zedefdd„ƒZdS )ÚPositiveNaiveBayesClassifierg      à?c                 C   s  t tƒ}t tƒ}t tƒ}tƒ }d}| D ]%}	|	 ¡ D ]\}
}||
 |  d7  < ||
  |¡ | |
¡ q|d7 }qd}|D ]%}	|	 ¡ D ]\}
}||
 |  d7  < ||
  |¡ | |
¡ qC|d7 }q=|D ]}
||
  ¡ }||
 d  || 7  < ||
  d¡ qe|D ]}
||
  ¡ }||
 d  || 7  < ||
  d¡ qƒd| }t||dœƒ}i }| ¡ D ]\}
}||t||
 ƒd}||d|
f< q°| ¡ D ]:\}
}||t||
 ƒd}i }||
 D ]}| |¡||d|
f  |¡  | }t	|dƒ||< qÝt|dd	|d
|
f< qÉt
||ƒS )an  
        :param positive_featuresets: An iterable of featuresets that are known as positive
            examples (i.e., their label is ``True``).

        :param unlabeled_featuresets: An iterable of featuresets whose label is unknown.

        :param positive_prob_prior: A prior estimate of the probability of the label
            ``True`` (default 0.5).
        r   é   Ng      ð?)TF)ZbinsTg        )Ú	normalizeF)r   r   ÚsetÚitemsÚaddÚNr   ÚlenÚprobÚmaxr   )Zpositive_featuresetsZunlabeled_featuresetsZpositive_prob_priorZ	estimatorZpositive_feature_freqdistZunlabeled_feature_freqdistZfeature_valuesÚfnamesZnum_positive_examplesZ
featuresetÚfnameZfvalZnum_unlabeled_examplesÚcountZnegative_prob_priorZlabel_probdistZfeature_probdistZfreqdistZprobdistZglobal_probdistZnegative_feature_probsr   © r   úO/var/www/auris/lib/python3.10/site-packages/nltk/classify/positivenaivebayes.pyÚtrainY   s`   

ÿÿýÿ
z"PositiveNaiveBayesClassifier.trainN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r   r   r   r   X   s
    ür   c                  C   s"   ddl m}  | tjƒ}| ¡  d S )Nr   )Úpartial_names_demo)Znltk.classify.utilr   r   r   Zshow_most_informative_features)r   Z
classifierr   r   r   Údemo°   s   
r   N)Ú__doc__Úcollectionsr   Znltk.classify.naivebayesr   Znltk.probabilityr   r   r   r   r   r   r   r   r   Ú<module>   s   FX