示例#1
0
class Sentiment:
    def __init__(self):
        self.classifier = Bayes()
        self.seg = Seg()
        self.seg.load('seg.pickle')

    def save(self, fname):
        self.classifier.save(fname)

    def load(self, fname):
        self.classifier = self.classifier.load(fname)

    def handle(self, doc):
        words = self.seg.seg(doc)
        words = self.filter_stop(words)

        return words

    def train(self, neg_docs, pos_docs):
        datas = []
        for doc in neg_docs:
            datas.append([self.handle(doc), 'neg'])
        for doc in pos_docs:
            datas.append([self.handle(doc), 'pos'])

        self.classifier.train(datas)

    def classify(self, doc):
        ret, prob = self.classifier.classify(self.handle(doc))
        if ret == 'pos':
            return prob
        else:
            return 1 - prob

    @staticmethod
    def filter_stop(words):
        return list(filter(lambda x: x not in stop_words, words))
示例#2
0
print_menu()
classifier = None

while (True):
    command = input("Enter command:")
    command = command.lower()
    #Train clause
    if command.startswith('t'):
        classifier = pp.main()
    #Load training clause
    elif command.startswith('l'):
        print("Loading: ", end='')
        if classifier is None:
            classifier = Bayes(trained=True)
        else:
            classifier.load()
    #Save training clause
    elif command.startswith('s'):
        print("Saving: ", end='')
        if classifier is not None:
            classifier.save()
        else:
            print("Nothing to save")
    #Classify clause
    elif command.startswith('c'):
        if classifier is None:
            print("Load training first")
        else:
            path = command.split(" ")
            if (len(path) < 2):
                print("Please enter filepath")
示例#3
0
 def load_model(self, model_path):
     self.model = Bayes.load(model_path)