<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from collections import defaultdict <EOL> from nltk . classify . util import apply_features , accuracy as eval_accuracy <EOL> from nltk . collocations import BigramCollocationFinder <EOL> from nltk . metrics import ( BigramAssocMeasures , precision as eval_precision , <EOL> recall as eval_recall , f_measure as eval_f_measure ) <EOL> from nltk . probability import FreqDist <EOL> from nltk . sentiment . util import save_file , timer <EOL> class SentimentAnalyzer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , classifier = None ) : <EOL> self . feat_extractors = defaultdict ( list ) <EOL> self . classifier = classifier <EOL> def all_words ( self , documents , labeled = None ) : <EOL> """<STR_LIT>""" <EOL> all_words = [ ] <EOL> if labeled is None : <EOL> labeled = documents and isinstance ( documents [ <NUM_LIT:0> ] , tuple ) <EOL> if labeled == True : <EOL> for words , sentiment in documents : <EOL> all_words . extend ( words ) <EOL> elif labeled == False : <EOL> for words in documents : <EOL> all_words . extend ( words ) <EOL> return all_words <EOL> def apply_features ( self , documents , labeled = None ) : <EOL> """<STR_LIT>""" <EOL> return apply_features ( self . extract_features , documents , labeled ) <EOL> def unigram_word_feats ( self , words , top_n = None , min_freq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> unigram_feats_freqs = FreqDist ( word for word in words ) <EOL> return [ w for w , f in unigram_feats_freqs . most_common ( top_n ) <EOL> if unigram_feats_freqs [ w ] > min_freq ] <EOL> def bigram_collocation_feats ( self , documents , top_n = None , min_freq = <NUM_LIT:3> , <EOL> assoc_measure = BigramAssocMeasures . pmi ) : <EOL> """<STR_LIT>""" <EOL> finder = BigramCollocationFinder . from_documents ( documents ) <EOL> finder . apply_freq_filter ( min_freq ) <EOL> return finder . nbest ( assoc_measure , top_n ) <EOL> def classify ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> instance_feats = self . apply_features ( [ instance ] , labeled = False ) <EOL> return self . classifier . classify ( instance_feats [ <NUM_LIT:0> ] ) <EOL> def add_feat_extractor ( self , function , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . feat_extractors [ function ] . append ( kwargs ) <EOL> def extract_features ( self , document ) : <EOL> """<STR_LIT>""" <EOL> all_features = { } <EOL> for extractor in self . feat_extractors : <EOL> for param_set in self . feat_extractors [ extractor ] : <EOL> feats = extractor ( document , ** param_set ) <EOL> all_features . update ( feats ) <EOL> return all_features <EOL> def train ( self , trainer , training_set , save_classifier = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> self . classifier = trainer ( training_set , ** kwargs ) <EOL> if save_classifier : <EOL> save_file ( self . classifier , save_classifier ) <EOL> return self . classifier <EOL> def evaluate ( self , test_set , classifier = None , accuracy = True , f_measure = True , <EOL> precision = True , recall = True , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> if classifier is None : <EOL> classifier = self . classifier <EOL> print ( "<STR_LIT>" . format ( type ( classifier ) . __name__ ) ) <EOL> metrics_results = { } <EOL> if accuracy == True : <EOL> accuracy_score = eval_accuracy ( classifier , test_set ) <EOL> metrics_results [ '<STR_LIT>' ] = accuracy_score <EOL> gold_results = defaultdict ( set ) <EOL> test_results = defaultdict ( set ) <EOL> labels = set ( ) <EOL> for i , ( feats , label ) in enumerate ( test_set ) : <EOL> labels . add ( label ) <EOL> gold_results [ label ] . add ( i ) <EOL> observed = classifier . classify ( feats ) <EOL> test_results [ observed ] . add ( i ) <EOL> for label in labels : <EOL> if precision == True : <EOL> precision_score = eval_precision ( gold_results [ label ] , <EOL> test_results [ label ] ) <EOL> metrics_results [ '<STR_LIT>' . format ( label ) ] = precision_score <EOL> if recall == True : <EOL> recall_score = eval_recall ( gold_results [ label ] , <EOL> test_results [ label ] ) <EOL> metrics_results [ '<STR_LIT>' . format ( label ) ] = recall_score <EOL> if f_measure == True : <EOL> f_measure_score = eval_f_measure ( gold_results [ label ] , <EOL> test_results [ label ] ) <EOL> metrics_results [ '<STR_LIT>' . format ( label ) ] = f_measure_score <EOL> if verbose == True : <EOL> for result in sorted ( metrics_results ) : <EOL> print ( '<STR_LIT>' . format ( result , metrics_results [ result ] ) ) <EOL> return metrics_results </s>
