<s> from __future__ import unicode_literals <EOL> import os <EOL> import codecs <EOL> from . . import normal <EOL> from . . import seg <EOL> from . . classification . bayes import Bayes <EOL> data_path = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> '<STR_LIT>' ) <EOL> class Sentiment ( object ) : <EOL> def __init__ ( self ) : <EOL> self . classifier = Bayes ( ) <EOL> def save ( self , fname , iszip = True ) : <EOL> self . classifier . save ( fname , iszip ) <EOL> def load ( self , fname = data_path , iszip = True ) : <EOL> self . classifier . load ( fname , iszip ) <EOL> def handle ( self , doc ) : <EOL> words = seg . seg ( doc ) <EOL> words = normal . filter_stop ( words ) <EOL> return words <EOL> def train ( self , neg_docs , pos_docs ) : <EOL> data = [ ] <EOL> for sent in neg_docs : <EOL> data . append ( [ self . handle ( sent ) , '<STR_LIT>' ] ) <EOL> for sent in pos_docs : <EOL> data . append ( [ self . handle ( sent ) , '<STR_LIT>' ] ) <EOL> self . classifier . train ( data ) <EOL> def classify ( self , sent ) : <EOL> ret , prob = self . classifier . classify ( self . handle ( sent ) ) <EOL> if ret == '<STR_LIT>' : <EOL> return prob <EOL> return <NUM_LIT:1> - prob <EOL> classifier = Sentiment ( ) <EOL> classifier . load ( ) <EOL> def train ( neg_file , pos_file ) : <EOL> neg_docs = codecs . open ( neg_file , '<STR_LIT:r>' , '<STR_LIT:utf-8>' ) . readlines ( ) <EOL> pos_docs = codecs . open ( pos_file , '<STR_LIT:r>' , '<STR_LIT:utf-8>' ) . readlines ( ) <EOL> global classifier <EOL> classifier = Sentiment ( ) <EOL> classifier . train ( neg_docs , pos_docs ) <EOL> def save ( fname , iszip = True ) : <EOL> classifier . save ( fname , iszip ) <EOL> def load ( fname , iszip = True ) : <EOL> classifier . load ( fname , iszip ) <EOL> def classify ( sent ) : <EOL> return classifier . classify ( sent ) </s>
