-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollector.py
More file actions
140 lines (126 loc) · 5.42 KB
/
Copy pathCollector.py
File metadata and controls
140 lines (126 loc) · 5.42 KB
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
import pandas as pd
from transformers import pipeline
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from collections import defaultdict
from nltk.corpus import wordnet as wn
import pickle
# of the sentence.
def vader_scores(sentence):
"""
Args:
sentence (str): sentence we will weight
"""
# Create a SentimentIntensityAnalyzer object.
sid_obj = SentimentIntensityAnalyzer()
# polarity_scores method of SentimentIntensityAnalyzer
# object gives a sentiment dictionary.
# which contains pos, neg, neu, and compound scores.
sentiment_dict = sid_obj.polarity_scores(sentence)
#only returns the compound value for ease of use
compound_value = sentiment_dict['compound']
return compound_value
def tb_scores(sentence):
"""
Args:
sentence (str): sentence we will weight
"""
#By default uses the Pattern Library
sentencetb = TextBlob(sentence)
compound_value = sentencetb.sentiment.polarity
return compound_value
def siebert_scores(sentence):
"""
Args:
sentence (str): sentence we will weight
"""
if(len(sentence)>500):
return 1
sentiment_analysis = pipeline("sentiment-analysis",model="siebert/sentiment-roberta-large-english", device= "cuda")
rating=sentiment_analysis(sentence)
if (rating[0]['label']=="POSITIVE"):
return 1
else:
return -1
def svm_scores(sentence):
text = {'text': [sentence]}
text = pd.DataFrame(text)
loaded_sv = pickle.load(open(r'C:\Users\Seeratul\Documents\GitHub\BachelorThesis\code\fSVM_model.sav', 'rb'))
return vectroremapper(loaded_sv.predict(preprocesser(text)))
def nb_scores(sentence):
text= {'text': [sentence]}
text = pd.DataFrame(text)
loaded_nb = pickle.load(open(r'C:\Users\Seeratul\Documents\GitHub\BachelorThesis\code\fNaive_model.sav', 'rb'))
return vectroremapper(loaded_nb.predict(preprocesser(text)))
def sentimentmapper(df, scoring= "Vader"):
"""
Args:
sentence (df): pandas dataframe to be analyzed
scoring: Vader,Siebert,TextBlob,FelixsSVM,FelixsNB
"""
# Mapps the sentiments expects a dataframe containing indexes, titles, year, and excerpt adds a score
df = df.reset_index()
#creates new indexes incase indexes are not 1-n
if (scoring == "Vader"):
dfs = df['title'].apply(vader_scores)
elif (scoring == "Siebert"):
dfs = df['title'].apply(siebert_scores)
elif (scoring == "TextBlob"):
dfs = df['title'].apply(tb_scores)
elif (scoring == "FelixsSVM"):
dfs = df['title'].apply(svm_scores)
elif (scoring == "FelixsNB"):
dfs = df['title'].apply(nb_scores)
else:
raise ValueError('You specified a non legal scoring method. (Vader,Siebert,TextBlob,FelixsSVM,FelixsNB)')
#creates new dataframe only conatining sentiment scores
dfs = dfs.rename('scores')
#renames the scores as they previously inherited the name title
L = pd.concat([df,dfs], axis= 1)
#concatonates both dataframes turning them into a list
dfn = pd.DataFrame(L, columns=["index", "year", "title", "scores"])
#turns the list back into a dataframe
return dfn
def preprocesser(Corpus):
"""
Corpus: A dataframe containing a "text" segment
to be handed over to my NB or SVM model.
"""
loadedvect = pickle.load(open(r'C:\Users\Seeratul\Documents\GitHub\BachelorThesis\code\vectorizer.sav', 'rb'))
Corpusnew = Corpus
Corpusnew['text'].dropna(inplace=True)
# Step - 1 : Change all the text to lower case. This is required as python interprets 'dog' and 'DOG' differently
Corpusnew['text'] = [entry.lower() for entry in Corpusnew['text']]
# Step - 2 : Tokenization : In this each entry in the corpus will be broken into set of words
Corpusnew['text']= [word_tokenize(entry) for entry in Corpusnew['text']]
# Step - 3 : Remove Stop words, Non-Numeric and perfom Word Stemming/Lemmenting.# WordNetLemmatizer requires Pos tags to understand if the word is noun or verb or adjective etc. By default it is set to Noun
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(Corpusnew['text']):
# Declaring Empty List to store the words that follow the rules for this step
Final_words = []
# Initializing WordNetLemmatizer()
word_Lemmatized = WordNetLemmatizer()
# pos_tag function below will provide the 'tag' i.e if the word is Noun(N) or Verb(V) or something else.
for word, tag in pos_tag(entry):
# Below condition is to check for Stop words and consider only alphabets
if word not in stopwords.words('english') and word.isalpha():
word_Final = word_Lemmatized.lemmatize(word,tag_map[tag[0]])
Final_words.append(word_Final)
# The final processed set of words for each iteration will be stored in 'text_final'
Corpusnew.loc[index,'text_final'] = str(Final_words)
return loadedvect.transform(Corpusnew["text_final"])
def vectroremapper(int):
#necassery due to the weird neutral positive neg encoding of the encoder
if int == 0:
return -1
elif int ==1:
return 0
else:
return 1