This program trains an SVM to tell positive and negative movie reviews apart using simple example sentences. It then predicts new sentences and shows how accurate it is.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Sample text data
train_texts = [
'I love this movie',
'This film was terrible',
'Amazing story and great acting',
'Worst movie ever',
'I enjoyed the film a lot'
]
train_labels = [1, 0, 1, 0, 1] # 1=positive, 0=negative
test_texts = [
'I hate this movie',
'What a fantastic film'
]
def main():
# Convert text to numbers
vectorizer = TfidfVectorizer(stop_words='english')
X_train = vectorizer.fit_transform(train_texts)
X_test = vectorizer.transform(test_texts)
# Create and train SVM model
model = SVC(kernel='linear')
model.fit(X_train, train_labels)
# Predict test data
predictions = model.predict(X_test)
# Show predictions
print('Predictions:', predictions.tolist())
# For demonstration, assume true labels for test
true_labels = [0, 1]
accuracy = accuracy_score(true_labels, predictions)
print(f'Accuracy: {accuracy:.2f}')
if __name__ == '__main__':
main()