forked from ssrivasan2011/sentimentAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supportingFunctions.py
106 lines (91 loc) · 2.09 KB
/
supportingFunctions.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
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import nltk
from textblob import Word
from textblob import TextBlob
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
def lowerCase(x,logger):
logger.info('case conversion to lower')
try:
return(x.lower())
except:
return(x)
def stopWords(x,logger):
logger.info('Removal of stopwords')
stopWords = stopwords.words('english')
try:
x =[i for i in x.split() if i not in stopWords]
return(" ".join(x))
except:
return(x)
def stemming(x,logger):
logger.info('Stemminization')
try:
st=PorterStemmer()
x = [st.stem(word) for word in x.split()]
return(" ".join(x))
except:
return(x)
def lemmatization(x,logger):
logger.info('Lemmatization')
try:
x = [Word(word).lemmatize() for word in x.split()]
return(" ".join(x))
except:
return(x)
def sentiments(x,logger):
logger.info('sentiment analysis')
try:
return(TextBlob(x).sentiment[0])
except:
return('Unknown')
def sentimentReviews(x,logger):
logger.info('reviews')
try:
if x > 0.5:
return('Positive')
elif x < -0.5:
return('negative')
else:
return('Unknown')
except:
return(x)
def wordCount(x,logger):
logger.info('counting the words')
try:
x = x.split(" ")
return(len(x))
except:
return(0)
def averageWord(x,logger):
logger.info('avarage of the words')
try:
words = x.split()
return(sum(len(word) for word in words)/len(words))
except:
return(0)
def specialCharacters(x,logger):
logger.info('special characters')
try:
x = [i for i in x.split(" ") if i.startswith('#')]
return(len(x))
except:
return(0)
def upperCase(x,logger):
logger.info('upper case')
try:
x = [i for i in x.split() if i.isupper()]
return(len(x))
except:
return(0)
def infoProduct(x,logger):
logger.info('product')
try:
wordToken = nltk.word_tokenize(x)
posTag = nltk.pos_tag(wordToken)
productInfo = [key for key,value in posTag if value =='NN']
return(",".join(productInfo))
except Exception as e:
logger.info('enter product Exception {}'.format(e))
return('No procuct')