-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·172 lines (133 loc) · 7.84 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from preprocessing.preprocesstweets import *
from embedding.sswe_extractor import *
from models import Dataset
from classification.Classifier import Classifier
from classification.NeuralNets import *
from classification.Evaluator import Evaluator
from feature_extracting import SennaFeatureExtractor
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale
from tqdm import tqdm
import numpy as np
def main():
""" Sentiment Specific Embedding for twitter classification """
embeddings_size = 50 # Embedding size for SSWE model
vocab_file = "Embedding/features/semeval_vocabs_200.txt" # path to the vocabulary file
vector_file = "Embedding/features/semeval_vectors_200.txt" # path to the vector file
stopwordsfile = "preprocess/stopwords.txt"
""" Sentiment-Specific Word Embedding (SSWE) """
if True:
# Load dataset
data_train = 'dataset/training1600000.csv' # training data set file path
pre_data_train = 'dataset/preprocessed_dataset1600000.csv' # file to save dataset after cleaning
if True:
print("\n **** Dataset cleaning ****")
tweets_prepocess(data_train, pre_data_train, stopwordsfile)
if True:
print("\n **** SSWE model Trainig ****")
train_model = None # path to the file contains the trained model if it is already exist
save_model = "Embedding/models/SSWE_model_1600000_200" # path to the file where model will be saved
sswe = create_sswe_model(pre_data_train, vocab_file, vector_file, train_model,
save_model, embeddings_size)
sswe_trainer(sswe)
""" Embedding visualisation and Similarity computing """
if True:
visualiser = Visualiser(sizeOfEmbedding=embeddings_size,
VocabsFname=vocab_file,
VectorsFname=vector_file,
WVFilename="Visualisation/data/w2vformat.txt",
visualizerHTMLfilename="Visualisation/data/embedding.html")
visualiser.visualize()
""" Twitter Sentiment Classification """
if True:
# Data pre-processing
print("\n **** Training data cleaning ****")
pre_processing_train = "dataset/preprocessed_semeval_traindataset.csv"
# tweets_prepocess(train_set, pre_processing_train, stopwordsfile)
print("\n **** Test data cleaning ****")
pre_processing_test = "dataset/preprocessed_semeval_testdataset.csv"
# tweets_prepocess(test_set, pre_processing_test, stopwordsfile)
# LOAD TRAIN SET
dataset_train = Dataset.DatasetReview()
dataset_train.load_review_from_csv(pre_processing_train)
# LOAD TEST SET
dataset_test = Dataset.DatasetReview()
dataset_test.load_review_from_csv(pre_processing_test)
################################### Neural Nets classifier ###########################
# Extract Features
tweet2v = get_sswe_features(vocab_file, vector_file)
# Extract samples and labels
x_train, y_train = split_data(dataset_train)
x_test, y_test = split_data(dataset_train)
tfidf = build_tfidf(x_train)
train_vecs_sswe = np.concatenate(
[buildWordVector(z.split(), embeddings_size,
tweet2v, tfidf) for z in tqdm(map(lambda x: x, x_train))])
train_vecs_sswe = scale(train_vecs_sswe)
test_vecs_sswe = np.concatenate(
[buildWordVector(z.split(), embeddings_size,
tweet2v, tfidf) for z in tqdm(map(lambda x: x, x_test))])
test_vecs_sswe = scale(test_vecs_sswe)
# neural network model
neuralnets = NeuralNets(input_size=embeddings_size, x_train=train_vecs_sswe, y_train=y_train,
epochs=450, batch_size=32, x_test=test_vecs_sswe, y_test=y_test)
neuralnets.train_neural_nets()
##########################################################################################
########
######## Classical classifiers with sklearn
########
##########################################################################################
print("\n**** CROSS VALIDATION EVALUATION (CORPUS: SemEval) ****\n")
fe_sswe = SennaFeatureExtractor(infile=vector_file, vocabfile=vocab_file, dimen=embeddings_size)
feature_extractors = [fe_sswe]
ev = Evaluator()
################################# SVM ###################################################
print ("\n**** CROSS VALIDATION EVALUATION (CORPUS: SemEval) ****\n")
model = Classifier(models="svm")
kfold = KFold(n_splits=10)
ev.eval_with_cross_validation(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
ev.create_evaluation_result(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
print ("\n**** TEST SET EVALUATION (CORPUS: SemEval) ****\n")
ev.eval_with_test_set(model, feature_extractors=feature_extractors,
training_set=dataset_train,
test_set=dataset_test)
################################### Naive bayes ##########################################
print ("\n**** CROSS VALIDATION EVALUATION (CORPUS: SemEval) ****\n")
model = Classifier(models="multinomial")
kfold = KFold(n_splits=10)
ev.eval_with_cross_validation(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
ev.create_evaluation_result(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
print ("\n**** TEST SET EVALUATION (CORPUS: DATASET) ****\n")
ev.eval_with_test_set(model, feature_extractors=feature_extractors,
training_set=dataset_train,
test_set=dataset_test)
######################################### RandomForestClassifier #######################
print ("\n**** CROSS VALIDATION EVALUATION (CORPUS: SemEval) ****\n")
model = Classifier(models="rfc")
kfold = KFold(n_splits=10)
ev.eval_with_cross_validation(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
ev.create_evaluation_result(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
print ("\n**** TEST SET EVALUATION (CORPUS: SemEval) ****\n")
ev.eval_with_test_set(model, feature_extractors=feature_extractors,
training_set=dataset_train,
test_set=dataset_test)
######################################### MLPClassifier #######################
print ("\n**** CROSS VALIDATION EVALUATION (CORPUS: SemEval) ****\n")
model = Classifier(models="nn")
kfold = KFold(n_splits=10)
ev.eval_with_cross_validation(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
ev.create_evaluation_result(model, feature_extractors=feature_extractors,
training_set=dataset_train, num_fold=10, cv=kfold)
print ("\n**** TEST SET EVALUATION (CORPUS: SemEval) ****\n")
ev.eval_with_test_set(model, feature_extractors=feature_extractors,
training_set=dataset_train,
test_set=dataset_test)
if __name__ == '__main__':
main()