
    g                     D   d Z ddlmZ ddlmZ 	 ddlmZ ddlmZ dgZ
 G d de      Zedk(  rgdd	lmZ dd
lmZ ddlmZmZ  ed        e e ed            j*                  e        ed        e e ed            j*                  e       yy# e	$ r Y w xY w)a  
scikit-learn (https://scikit-learn.org) is a machine learning library for
Python. It supports many classification algorithms, including SVMs,
Naive Bayes, logistic regression (MaxEnt) and decision trees.

This package implements a wrapper around scikit-learn classifiers. To use this
wrapper, construct a scikit-learn estimator object, then use that to construct
a SklearnClassifier. E.g., to wrap a linear SVM with default settings:

>>> from sklearn.svm import LinearSVC
>>> from nltk.classify.scikitlearn import SklearnClassifier
>>> classif = SklearnClassifier(LinearSVC())

A scikit-learn classifier may include preprocessing steps when it's wrapped
in a Pipeline object. The following constructs and wraps a Naive Bayes text
classifier with tf-idf weighting and chi-square feature selection to get the
best 1000 features:

>>> from sklearn.feature_extraction.text import TfidfTransformer
>>> from sklearn.feature_selection import SelectKBest, chi2
>>> from sklearn.naive_bayes import MultinomialNB
>>> from sklearn.pipeline import Pipeline
>>> pipeline = Pipeline([('tfidf', TfidfTransformer()),
...                      ('chi2', SelectKBest(chi2, k=1000)),
...                      ('nb', MultinomialNB())])
>>> classif = SklearnClassifier(pipeline)
    )ClassifierI)DictionaryProbDist)DictVectorizer)LabelEncoderSklearnClassifierc                   @    e Zd ZdZedfdZd Zd Zd Zd Z	d Z
d	 Zy
)r   z%Wrapper for scikit-learn classifiers.Tc                 T    || _         t               | _        t        ||      | _        y)a  
        :param estimator: scikit-learn classifier object.

        :param dtype: data type used when building feature array.
            scikit-learn estimators work exclusively on numeric data. The
            default value should be fine for almost all situations.

        :param sparse: Whether to use sparse matrices internally.
            The estimator must support these; not all scikit-learn classifiers
            do (see their respective documentation and look for "sparse
            matrix"). The default value is True, since most NLP problems
            involve sparse feature sets. Setting this to False may take a
            great amount of memory.
        :type sparse: boolean.
        )dtypesparseN)_clfr   _encoderr   _vectorizer)self	estimatorr
   r   s       N/var/www/openai/venv/lib/python3.12/site-packages/nltk/classify/scikitlearn.py__init__zSklearnClassifier.__init__1   s#      	$)fE    c                      d| j                   z  S )Nz<SklearnClassifier(%r)>)r   r   s    r   __repr__zSklearnClassifier.__repr__E   s    (49944r   c                     | j                   j                  |      }| j                  j                  }| j                  j                  |      D cg c]  }||   	 c}S c c}w )a  Classify a batch of samples.

        :param featuresets: An iterable over featuresets, each a dict mapping
            strings to either numbers, booleans or strings.
        :return: The predicted class label for each input sample.
        :rtype: list
        )r   	transformr   classes_r   predict)r   featuresetsXclassesis        r   classify_manyzSklearnClassifier.classify_manyH   sV     &&{3--(($(II$5$5a$89$8q
$8999s   Ac                     | j                   j                  |      }| j                  j                  |      }|D cg c]  }| j	                  |       c}S c c}w )zCompute per-class probabilities for a batch of samples.

        :param featuresets: An iterable over featuresets, each a dict mapping
            strings to either numbers, booleans or strings.
        :rtype: list of ``ProbDistI``
        )r   r   r   predict_proba_make_probdist)r   r   r   y_proba_listy_probas        r   prob_classify_manyz$SklearnClassifier.prob_classify_manyT   sQ     &&{3yy..q1<HIL##G,LIIIs   Ac                 @    t        | j                  j                        S )zHThe class labels used by this classifier.

        :rtype: list
        )listr   r   r   s    r   labelszSklearnClassifier.labels_   s    
 DMM**++r   c                     t        t        |       \  }}| j                  j                  |      }| j                  j                  |      }| j
                  j                  ||       | S )z
        Train (fit) the scikit-learn estimator.

        :param labeled_featuresets: A list of ``(featureset, label)``
            where each ``featureset`` is a dict mapping strings to either
            numbers, booleans or strings.
        )r'   zipr   fit_transformr   r   fit)r   labeled_featuresetsr   ys       r   trainzSklearnClassifier.trainf   sY     C,-.1**1-MM''*		ar   c                     | j                   j                  }t        t        |      D ci c]  \  }}||   | c}}      S c c}}w )N)r   r   r   	enumerate)r   r$   r   r   ps        r   r"   z SklearnClassifier._make_probdistv   sA    --((!Yw=O"P=OTQ71:q==O"PQQ"Ps   A
N)__name__
__module____qualname____doc__floatr   r   r   r%   r(   r/   r"    r   r   r   r   .   s0    /(-d F(5
:	J, Rr   __main__)LogisticRegression)BernoulliNB)
names_demonames_demo_featureszscikit-learn Naive Bayes:F)binarize)featuresz#

scikit-learn logistic regression:i  )CN)r6   nltk.classify.apir   nltk.probabilityr   sklearn.feature_extractionr   sklearn.preprocessingr   ImportError__all__r   r3   sklearn.linear_modelr:   sklearn.naive_bayesr;   nltk.classify.utilr<   r=   printr/   r8   r   r   <module>rK      s   8 * /	92 
JR JRZ z7/B 

%&+u56<<$ 

12,t45;;$# g  		s   B BB