from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB from sklearn.model_selection import train_test_split
# Sample dataset (text and corresponding labels) texts = [ "I love this movie", "The acting was brilliant", "I hate this movie", "The plot was boring", "What a great film", "I dislike this film" ]
# Text vectorization using Bag of Words vectorizer = CountVectorizer() X = vectorizer.fit_transform(texts)
# Train-test split (in this case, it's a very small dataset, but typically you'd split larger datasets) X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=42)
Comments
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
# Sample dataset (text and corresponding labels)
texts = [
"I love this movie",
"The acting was brilliant",
"I hate this movie",
"The plot was boring",
"What a great film",
"I dislike this film"
]
labels = [1, 1, 0, 0, 1, 0] # 1 = Positive, 0 = Negative
# Text vectorization using Bag of Words
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
# Train-test split (in this case, it's a very small dataset, but typically you'd split larger datasets)
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=42)
# Naive Bayes Classifier
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
# Test the classifier on a new sentence
new_text = ["i love this movie, the acting was brilliant"]
# Transform the new text using the same vectorizer
new_text_vectorized = vectorizer.transform(new_text)
# Predict the label (0 = Negative, 1 = Positive)
prediction = classifier.predict(new_text_vectorized)
# Output the prediction
if prediction == 1:
print("The sentiment is Positive!")
else:
print("The sentiment is Negative!")