Exemple #1
0
    def run(self):
        args = self.get_arguments()
        try:
            model_file = args[0]
            datasets = args[1:]
        except IndexError:
            raise InvalidParameters

        model = SentimentModel()
        model = self.train_over_dumps(model, datasets)
        model.save(model_file)

        return False
Exemple #2
0
    def run(self):
        args = self.get_arguments()
        model_file = args[0]
        filenames = args[1:]

        if not os.path.exists(model_file):
            print("ERROR: model file '%s' doesn't exist" % model_file)
            return True

        model = SentimentModel.load(model_file)
        evaluator = ModelEvaluator(model, filenames)

        gold, test = evaluator.get_gold_comparison()

        print("Confusion matrix:")
        cm = nltk.ConfusionMatrix(gold, test)
        print(cm.pp(sort_by_count=True, show_percents=True))

        summary = evaluator.evaluate()
        precision_sum, recall_sum, fmeasure_sum = 0, 0, 0

        print("                TP     TN     FP     FN  precision  recall  f-measure")
        rowstr = "%(label)s %(tp)6s %(tn)6s %(fp)6s %(fn)6s  %(precision)2.5f   %(recall)2.5f   %(fmeasure)2.5f"
        for sentiment, label in TEXT_LABELS.items():
            print(rowstr % {
                "label": '%s:' % label.rjust(10),
                "tp": summary[sentiment]["true-positives"],
                "fp": summary[sentiment]["false-positives"],
                "tn": summary[sentiment]["true-negatives"],
                "fn": summary[sentiment]["false-negatives"],
                "precision": summary[sentiment]["precision"],
                "recall": summary[sentiment]["recall"],
                "fmeasure": summary[sentiment]["f-measure"],
            })

            precision_sum += summary[sentiment]["precision"]
            recall_sum += summary[sentiment]["recall"]
            fmeasure_sum += summary[sentiment]["f-measure"]

        precision_avg = precision_sum / len(TEXT_LABELS)
        recall_avg = recall_sum / len(TEXT_LABELS)
        fmeasure_avg = fmeasure_sum / len(TEXT_LABELS)

        print(rowstr % {
            "label": "average:".rjust(11),
            "tp": "", "fp": "", "tn": "", "fn": "",
            "precision": precision_avg,
            "recall": recall_avg,
            "fmeasure": fmeasure_avg,
        })

        return False
Exemple #3
0
    def run(self):
        args = self.get_arguments()
        model_file = args[0]
        text = ' '.join(args[1:])

        if not os.path.exists(model_file):
            print("ERROR: model file '%s' doesn't exist" % model_file)
            return True

        dummytweet = DummyTweet(text)
        model = SentimentModel.load(model_file)
        label = TEXT_LABELS[model.predict(dummytweet)]

        print("Prediction:")
        print(" %10s: %s" % (label, text))

        return False
Exemple #4
0
    def run(self):
        args = self.get_arguments()
        model_file = args[0]
        text = ' '.join(args[1:])

        if not os.path.exists(model_file):
            print("ERROR: model file '%s' doesn't exist" % model_file)
            return True

        dummytweet = DummyTweet(text)
        model = SentimentModel.load(model_file)
        label = TEXT_LABELS[model.predict(dummytweet)]

        print("Prediction:")
        print(" %10s: %s" % (label, text))

        return False
Exemple #5
0
    def run(self):
        args = self.get_arguments()
        model_file = args[0]
        filenames = args[1:]

        if not os.path.exists(model_file):
            print("ERROR: model file '%s' doesn't exist" % model_file)
            return True

        model = SentimentModel.load(model_file)
        reader = DataReader(filenames)

        print("Prediction:")
        for item in reader.get_tweets():
            sentiment, tweet = item
            label = TEXT_LABELS[model.predict(tweet)]
            print(" %10s: %s" % (label, tweet.text))

        return False
Exemple #6
0
    def run(self):
        args = self.get_arguments()
        model_file = args[0]
        filenames = args[1:]

        if not os.path.exists(model_file):
            print("ERROR: model file '%s' doesn't exist" % model_file)
            return True

        model = SentimentModel.load(model_file)
        reader = DataReader(filenames)

        print("Prediction:")
        for item in reader.get_tweets():
            sentiment, tweet = item
            label = TEXT_LABELS[model.predict(tweet)]
            print(" %10s: %s" % (label, tweet.text))

        return False
Exemple #7
0
    def run(self):
        arguments = self.get_arguments()
        try:
            model_file = arguments[0]
            language = arguments[1]
        except (IndexError, ValueError):
            raise InvalidParameters

        search_terms = arguments[2:]
        if not search_terms:
            raise InvalidParameters

        self.model = SentimentModel.load(model_file)
        s = stream.SearchListener(auth.auth(),
                                  search_terms,
                                  self.cb_search,
                                  cb_args=(language, ))

        try:
            s.run()
        except KeyboardInterrupt:
            s.close()

        return False
Exemple #8
0
    def run(self):
        arguments = self.get_arguments()
        try:
            model_file = arguments[0]
            language = arguments[1]
        except (IndexError, ValueError):
            raise InvalidParameters

        search_terms = arguments[2:]
        if not search_terms:
            raise InvalidParameters

        self.model = SentimentModel.load(model_file)
        s = stream.SearchListener(auth.auth(),
                                  search_terms,
                                  self.cb_search,
                                  cb_args=(language, ))

        try:
            s.run()
        except KeyboardInterrupt:
            s.close()

        return False