-
Notifications
You must be signed in to change notification settings - Fork 1
/
svm_experiment.py
84 lines (58 loc) · 2.32 KB
/
svm_experiment.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from preprocessing.reader import EvalitaDatasetReader
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from utils.fileprovider import FileProvider
import argparse
import string
import re
import logging
logging.getLogger().setLevel(logging.INFO)
def process_text(texts, exclude=set(string.punctuation)):
res = []
for text in texts:
text = re.sub(r'http\S+', '', text)
text = text.lower()
res.append(''.join(ch for ch in text if ch not in exclude))
return res
parser = argparse.ArgumentParser(description='Train the emoji task')
parser.add_argument('--workdir', required=False, help='Work path', default='data')
parser.add_argument('--max-dict', type=int, default=100000, help='Maximum dictionary size')
args = parser.parse_args()
files = FileProvider(args.workdir)
raw_train, raw_test = EvalitaDatasetReader(files.evalita).split()
texts_train = []
texts_test = []
labels_train = []
labels_test = []
for elem in raw_train.X:
texts_train.append(elem[0])
for elem in raw_test.X:
texts_test.append(elem[0])
for elem in raw_train.Y:
labels_train.append(elem)
for elem in raw_test.Y:
labels_test.append(elem)
del raw_train
texts_train = process_text(texts_train)
texts_test = process_text(texts_test)
vectorizer = TfidfVectorizer()
logging.info("Starting vectorization")
vectorizer.fit(texts_train)
tfidf_matrix_train = vectorizer.transform(texts_train)
tfidf_matrix_test = vectorizer.transform(texts_test)
del texts_train
del texts_test
logging.info("Ended vectorization. Starting svm fit")
clf = SVC(verbose=True)
clf.fit(tfidf_matrix_train, labels_train)
logging.info("Ended svm fit. Starting prediction")
prediction = clf.predict(tfidf_matrix_test)
logging.info("Ended prediction. Starting dump of scores")
scores_file = open('scores_file.txt', 'w')
scores_file.write('Accuracy: ' + str(accuracy_score(prediction, labels_test)) + '\n')
scores_file.write('Precision: ' + str(precision_score(prediction, labels_test, average='macro')) + '\n')
scores_file.write('Recall: ' + str(recall_score(prediction, labels_test, average='macro')) + '\n')
scores_file.write('F1-score: ' + str(f1_score(prediction, labels_test, average='macro')) + '\n')
scores_file.close()
logging.info("Done")