From 93d8bf4a5b5242de9dd7acbbaaad887b936d410c Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 1 Apr 2026 11:41:21 -0400 Subject: [PATCH] finished TextBlob hw --- TextBlob/text_classifier.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/TextBlob/text_classifier.py b/TextBlob/text_classifier.py index e69de29..1407994 100644 --- a/TextBlob/text_classifier.py +++ b/TextBlob/text_classifier.py @@ -0,0 +1,31 @@ +from textblob.classifiers import NaiveBayesClassifier +from pathlib import Path + +def main(): + # Get directory where this script is located + base_dir = Path(__file__).parent + + # Build paths to data files + train_path = base_dir / "train.json" + test_path = base_dir / "test.json" + + # Load training data + with open(train_path, "r") as train_file: + classifier = NaiveBayesClassifier(train_file, format="json") + + # Load test data + with open(test_path, "r") as test_file: + accuracy = classifier.accuracy(test_file) + + print("Accuracy:", accuracy) + + print("Classifying sentences:") + print("I love this place ->", classifier.classify("I love this place")) + print("I hate this job ->", classifier.classify("I hate this job")) + + print("\nMost Informative Features:") + classifier.show_informative_features(5) + + +if __name__ == "__main__": + main() \ No newline at end of file