Ejemplo n.º 1
0
class Classifier(object):
    def __init__(self):
        self.classifier = NaiveBayes()

    def learn_from_tweets(self, user_ids, category):
        """
        Train the classifier by tweets.
        user_ids : A list of twitter ids which their tweets are included
        in the category.
        category : The category of the tweets.
        """
        tweets = get_tweets(user_ids)
        categories = [category] * len(tweets)
        self.classifier.fit(tweets, categories)
        print("Training...")

    def predict_user_input(self):
        """Read user input until 'exit' is entered"""
        sentence = input("input =>")
        while(sentence != 'exit'):
            category = self.classifier.predict_(sentence)
            print("{}\n".format(category))
            sentence = input("input =>")

    def save(self, filename):
        """Save the model."""
        self.classifier.dump_json(filename)

    def load(self, filename):
        """Load the model from a file."""
        self.classifier.load_json(filename)
Ejemplo n.º 2
0
class Classifier(object):
    def __init__(self):
        self.classifier = NaiveBayes()

    def learn_from_tweets(self, user_ids, category):
        """
        Train the classifier by tweets.
        user_ids : A list of twitter ids which their tweets are included
        in the category.
        category : The category of the tweets.
        """
        tweets = get_tweets(user_ids)
        categories = [category] * len(tweets)
        self.classifier.fit(tweets, categories)
        print("Training...")

    def predict_user_input(self):
        """Read user input until 'exit' is entered"""
        sentence = input("input =>")
        while (sentence != 'exit'):
            category = self.classifier.predict_(sentence)
            print("{}\n".format(category))
            sentence = input("input =>")

    def save(self, filename):
        """Save the model."""
        self.classifier.dump_json(filename)

    def load(self, filename):
        """Load the model from a file."""
        self.classifier.load_json(filename)