-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaspectAnalysis.py
More file actions
102 lines (83 loc) · 3.6 KB
/
Copy pathaspectAnalysis.py
File metadata and controls
102 lines (83 loc) · 3.6 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
import pandas as pd
import numpy as np
import nltk
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
import stanfordnlp
import stanza
# Make sure you have downloaded the StanfordNLP English model and other essential tools using,
# stanfordnlp.download('en')
# nltk.download('stopwords')
# nltk.download('punkt')
# nltk.download('averaged_perceptron_tagger')
def aspect_sentiment_analysis(txt, stop_words, nlp):
txt = txt.lower() # LowerCasing the given Text
sentList = nltk.sent_tokenize(txt) # Splitting the text into sentences
fcluster = []
totalfeatureList = []
finalcluster = []
dic = {}
for line in sentList:
newtaggedList = []
txt_list = nltk.word_tokenize(line) # Splitting up into words
taggedList = nltk.pos_tag(txt_list) # Doing Part-of-Speech Tagging to each word
newwordList = []
flag = 0
for i in range(0,len(taggedList)-1):
if(taggedList[i][1]=="NN" and taggedList[i+1][1]=="NN"): # If two consecutive words are Nouns then they are joined together
newwordList.append(taggedList[i][0]+taggedList[i+1][0])
flag=1
else:
if(flag==1):
flag=0
continue
newwordList.append(taggedList[i][0])
if(i==len(taggedList)-2):
newwordList.append(taggedList[i+1][0])
finaltxt = ' '.join(word for word in newwordList)
new_txt_list = nltk.word_tokenize(finaltxt)
wordsList = [w for w in new_txt_list if not w in stop_words]
taggedList = nltk.pos_tag(wordsList)
import stanza
stanza.download('en') # download English model
nlp = stanza.Pipeline('en') # initialize English neural pipeline
doc = nlp(finaltxt) # Object of Stanford NLP Pipeleine
# Getting the dependency relations betwwen the words
dep_node = []
for dep_edge in doc.sentences[0].dependencies:
dep_node.append([dep_edge[2].text, dep_edge[0].id, dep_edge[1]])
for i in range(0, len(dep_node)):
if (int(dep_node[i][1]) != 0):
dep_node[i][1] = newwordList[(int(dep_node[i][1]) - 1)]
print(dep_node)
# Coverted it into appropriate format
featureList = []
categories = []
for i in taggedList:
if(i[1]=='JJ' or i[1]=='NN' or i[1]=='JJR' or i[1]=='NNS' or i[1]=='RB'):
featureList.append(list(i)) # For features for each sentence
totalfeatureList.append(list(i)) # Stores the features of all the sentences in the text
categories.append(i[0])
for i in featureList:
filist = []
for j in dep_node:
if((j[0]==i[0] or j[1]==i[0]) and (j[2] in ["nsubj", "acl:relcl", "obj", "dobj", "agent", "advmod", "amod", "neg", "prep_of", "acomp", "xcomp", "compound"])):
if(j[0]==i[0]):
filist.append(j[1])
else:
filist.append(j[0])
fcluster.append([i[0], filist])
for i in totalfeatureList:
dic[i[0]] = i[1]
for i in fcluster:
if(dic[i[0]]=="NN"):
finalcluster.append(i)
return(finalcluster)
'''
nlp = stanfordnlp.Pipeline()
stop_words = set(stopwords.words('english'))
txt = "The Sound Quality is great but the battery life is very bad."
print(aspect_sentiment_analysis(txt, stop_words, nlp))
'''