-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
71 lines (61 loc) · 2.65 KB
/
demo.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
import pandas as pd
import numpy as np
import re
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.preprocessing import LabelEncoder
from collections import defaultdict
from nltk.corpus import wordnet as wn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import model_selection, naive_bayes, svm
from sklearn.metrics import accuracy_score
np.random.seed(2012)
Corpus = pd.read_csv(r"C:\Users\ravialas\Desktop\corpus.csv", encoding='latin-1')
Corpus = Corpus[['LABEL', 'TEXT']]
Corpus_filter = dict(Corpus.groupby('LABEL').TEXT.count() > 4)
Corpus['Bool'] = Corpus['LABEL'].map(Corpus_filter)
Corpus = Corpus[Corpus['Bool'] == True]
Corpus['TEXT'] = [entry.lower() for entry in Corpus['TEXT']]
Corpus['TEXT'] = [re.sub(r'[^\w\s]', '', w) for w in Corpus['TEXT']] # if w is w.isalnum()]
Corpus['TEXT'] = [word_tokenize(entry) for entry in Corpus['TEXT']]
# Remove Stop words, and Word Stemming.
tag_map = defaultdict(lambda: wn.NOUN)
tag_map['J'] = wn.ADJ
tag_map['V'] = wn.VERB
tag_map['R'] = wn.ADV
for index, entry in enumerate(Corpus['TEXT']):
Final_words = []
word_Lemmatized = WordNetLemmatizer()
for word, tag in pos_tag(entry):
if word not in stopwords.words('english'):
word_Final = word_Lemmatized.lemmatize(word, tag_map[tag[0]])
Final_words.append(word_Final)
Corpus.loc[index, 'text_final'] = str(Final_words)
Train_X, Test_X, Train_Y, Test_Y = model_selection.train_test_split(Corpus['text_final'], Corpus['LABEL'],
test_size=0.3)
Encoder = LabelEncoder()
Train_Y = Encoder.fit_transform(Train_Y)
Test_Y = Encoder.fit_transform(Test_Y)
Tfidf_vect = TfidfVectorizer(max_features=3000)
Tfidf_vect.fit(Corpus['text_final'])
Train_X_Tfidf = Tfidf_vect.transform(Train_X)
Test_X_Tfidf = Tfidf_vect.transform(Test_X)
print(Tfidf_vect.vocabulary_)
print(Train_X_Tfidf)
# Classifier - Algorithm - SVM
# fit the training dataset on the classifier
SVM = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto')
SVM.fit(Train_X_Tfidf, Train_Y)
# predict the labels on validation dataset
predictions_SVM = SVM.predict(Test_X_Tfidf)
# A
print("SVM Accuracy Score -> ", accuracy_score(predictions_SVM, Test_Y) * 100)
# fit the training dataset on the NB classifier
Naive = naive_bayes.MultinomialNB()
Naive.fit(Train_X_Tfidf, Train_Y)
# predict the labels on validation dataset
predictions_NB = Naive.predict(Test_X_Tfidf)
# Use accuracy_score function to get the accuracy
print("Naive Bayes Accuracy Score -> ", accuracy_score(predictions_NB, Test_Y) * 100)