forked from shishirkulkarni/happy-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordsPolarity.py
executable file
·69 lines (60 loc) · 2.25 KB
/
WordsPolarity.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 8 21:30:44 2018
@author: gayatrigadre
Prerequisite: Install nltk python library
Execution: python WordsPolarity.py <inputFile>
"""
from nltk import tokenize
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.corpus import stopwords
import os
import sys
def filter_stopwords(words_list):
''' Pre-processing step to remove unwanted words'''
for word in words_list:
if word in stopwords.words('english'):
words_list.remove(word)
def get_scores(line,outputFile):
''' Use nltk library function to get scores per word'''
words_list = tokenize.word_tokenize(line)
filter_stopwords(words_list)
sentimentAnalyzer = SentimentIntensityAnalyzer()
for word in words_list:
print(word)
score = sentimentAnalyzer.polarity_scores(word)
for k in sorted(score):
# Omit words with 0.0 as score
if score[k] != 0.0 and k=='compound':
#print(word,'{0}: {1}, '.format(k, ss[k]))
outputFile.write("\n"+word+"\t"'{0}'.format(score[k]))
def getOutputFileName(inputFile):
''' Get new output file name'''
fname = inputFile.split('.')
fileName = fname[0]+"Output.txt"
return fileName
def computePolarity(inputFileName):
''' Function to computer polarity of words and write to file '''
with open(inputFileName,"r", encoding='ISO-8859-1') as file:
try:
if file.mode == 'r':
outputFileName = getOutputFileName(inputFileName)
outputFile = open(outputFileName,"w+")
for line in file.readlines():
get_scores(line,outputFile)
outputFile.close()
print("Word score generated in",outputFileName,"file")
except IOError:
print("File cannot be opened:",inputFileName)
# Read from a word count file
if __name__ == "__main__":
if len(sys.argv)!=2:
print("Input file not specified")
else:
inputFileName = sys.argv[1]
print("Input File:",inputFileName)
if os.path.exists(inputFileName):
computePolarity(inputFileName)
else:
print("File not found:",inputFileName)