-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoemSentimentDetector.py
64 lines (51 loc) · 2.28 KB
/
PoemSentimentDetector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score
from sklearn.preprocessing import LabelBinarizer
import os
import pandas as pd
class PoemSentimentDetector:
def __init__(self, elegy_themes, ode_themes):
# Initialize Tfidf and SVC
self.vectorizer = TfidfVectorizer(stop_words='english')
self.model = SVC(probability=True)
self.elegy_themes = elegy_themes
self.ode_themes = ode_themes
def train(self, poem_data, labels):
X = self.vectorizer.fit_transform(poem_data)
# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=42, stratify=labels)
self.model.fit(X_train, y_train)
# Predict on the test set and evaluate performance
preds = self.model.predict(X_test)
lb = LabelBinarizer()
y_test_bin = lb.fit_transform(y_test)
pred_bin = lb.transform(preds)
# Print evaluation metrics
print('Accuracy: ', accuracy_score(y_test, preds))
print('F1 Score: ', f1_score(y_test, preds, pos_label="elegy"))
print('ROC AUC Score: ', roc_auc_score(y_test_bin, pred_bin))
def predict(self, poem_content):
# Transform poem into TF-IDF features
X = self.vectorizer.transform([poem_content])
# Predict Sentiment
sentiment = self.model.predict(X)[0]
if sentiment in self.elegy_themes:
return "Elegy"
elif sentiment in self.ode_themes:
return "Ode"
else:
return "Neither"
def load_data(self, folder_path):
# Load poem data and corresponding labels
data = []
labels = []
for filename in os.listdir(folder_path):
if filename.endswith(".txt"):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r', encoding="utf-8", errors="ignore") as file:
content = file.read()
data.append(content)
labels.append("elegy" if "elegy" in filename.lower() else "ode")
return data, labels