class Recognizer():
    def __init__(self):
        self.trainer = None

    def train(self, dataFileName):
        self.trainer = Trainer(dataFileName)
        self.trainer.trainAll()
        self.trainer.dump()

    def load(self):
        trainer = Trainer()
        trainer.load()
        self.trainer = trainer

    def classify(self, X, label1, label2):
        '''
        输入向量X, 在label1, label2间预测它的类属性
        '''
        positiveLabel = min(label1, label2)
        negativeLabel = max(label1, label2)
        svm = self.trainer.getSvmInstance(positiveLabel, negativeLabel)
        y = svm.predict(X)
        if y == 1:
            return positiveLabel
        elif y == -1:
            return negativeLabel
        else:
            raise

    def predict(self, X):
        count_dict = {} #{label : times}
        for i in range(10):
            for j in range(i, 10, 1):
                if i == j:
                    continue
                label = self.classify(X, i, j)
                if count_dict.has_key(label):
                    count_dict[label] += 1
                else:
                    count_dict[label] = 1

        maxTime = -1
        maxLabel = -1
        for label in count_dict:
            time = count_dict[label]
            if time > maxTime:
                maxTime = time
                maxLabel = label
        return maxLabel
 def test_dump(self):
     trainer = Trainer(os.path.join('..', 'data', 'sample'))
     trainer.trainAll()
     trainer.dump()