File size: 8,793 Bytes
60ded96 |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
import pandas as pd
import numpy as np
def has_vectors(doc):
return np.all([token.has_vector for token in doc])
def extract_doc_aspects(doc):
prod_pronouns = ['it','this','they','these']
rule1_pairs = []
rule2_pairs = []
rule3_pairs = []
rule4_pairs = []
rule5_pairs = []
rule6_pairs = []
rule7_pairs = []
for token in doc:
if token.text == 'product':
continue
## FIRST RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
## RULE = M is child of A with a relationship of amod
A = "999999"
M = "999999"
if token.dep_ == "amod" and not token.is_stop:
M = token.text
A = token.head.text
# add adverbial modifier of adjective (e.g. 'most comfortable headphones')
M_children = token.children
for child_m in M_children:
if(child_m.dep_ == "advmod"):
M_hash = child_m.text
M = M_hash + " " + M
break
# negation in adjective, the "no" keyword is a 'det' of the noun (e.g. no interesting characters)
A_children = token.head.children
for child_a in A_children:
if(child_a.dep_ == "det" and child_a.text == 'no'):
neg_prefix = 'not'
M = neg_prefix + " " + M
break
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict1 = {"noun" : A, "adj" : M, "rule" : 1}
rule1_pairs.append(dict1)
# # SECOND RULE OF DEPENDANCY PARSE -
# # M - Sentiment modifier || A - Aspect
# Direct Object - A is a child of something with relationship of nsubj, while
# M is a child of the same something with relationship of dobj
# Assumption - A verb will have only one NSUBJ and DOBJ
children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False
for child in children :
if(child.dep_ == "nsubj" and not child.is_stop):
A = child.text
if((child.dep_ == "dobj" and child.pos_ == "ADJ") and not child.is_stop):
M = child.text
if(child.dep_ == "neg"):
neg_prefix = child.text
add_neg_pfx = True
if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict2 = {"noun" : A, "adj" : M, "rule" : 2}
rule2_pairs.append(dict2)
## THIRD RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
## Adjectival Complement - A is a child of something with relationship of nsubj, while
## M is a child of the same something with relationship of acomp
## Assumption - A verb will have only one NSUBJ and DOBJ
## "The sound of the speakers would be better. The sound of the speakers could be better" - handled using AUX dependency
children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False
for child in children :
if(child.dep_ == "nsubj" and not child.is_stop):
A = child.text
if(child.dep_ == "acomp" and not child.is_stop):
M = child.text
# example - 'this could have been better' -> (this, not better)
if(child.dep_ == "aux" and child.tag_ == "MD"):
neg_prefix = "not"
add_neg_pfx = True
if(child.dep_ == "neg"):
neg_prefix = child.text
add_neg_pfx = True
if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict3 = {"noun" : A, "adj" : M, "rule" : 3}
rule3_pairs.append(dict3)
## FOURTH RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
#Adverbial modifier to a passive verb - A is a child of something with relationship of nsubjpass, while
# M is a child of the same something with relationship of advmod
#Assumption - A verb will have only one NSUBJ and DOBJ
children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False
for child in children :
if((child.dep_ == "nsubjpass" or child.dep_ == "nsubj") and not child.is_stop):
A = child.text
if(child.dep_ == "advmod" and not child.is_stop):
M = child.text
M_children = child.children
for child_m in M_children:
if(child_m.dep_ == "advmod"):
M_hash = child_m.text
M = M_hash + " " + child.text
break
if(child.dep_ == "neg"):
neg_prefix = child.text
add_neg_pfx = True
if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict4 = {"noun" : A, "adj" : M, "rule" : 4}
rule4_pairs.append(dict4)
## FIFTH RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
#Complement of a copular verb - A is a child of M with relationship of nsubj, while
# M has a child with relationship of cop
#Assumption - A verb will have only one NSUBJ and DOBJ
children = token.children
A = "999999"
buf_var = "999999"
for child in children :
if(child.dep_ == "nsubj" and not child.is_stop):
A = child.text
if(child.dep_ == "cop" and not child.is_stop):
buf_var = child.text
if(A != "999999" and buf_var != "999999"):
if A in prod_pronouns :
A = "product"
dict5 = {"noun" : A, "adj" : token.text, "rule" : 5}
rule5_pairs.append(dict5)
## SIXTH RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
## Example - "It ok", "ok" is INTJ (interjections like bravo, great etc)
children = token.children
A = "999999"
M = "999999"
if(token.pos_ == "INTJ" and not token.is_stop):
for child in children :
if(child.dep_ == "nsubj" and not child.is_stop):
A = child.text
M = token.text
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict6 = {"noun" : A, "adj" : M, "rule" : 6}
rule6_pairs.append(dict6)
## SEVENTH RULE OF DEPENDANCY PARSE -
## M - Sentiment modifier || A - Aspect
## ATTR - link between a verb like 'be/seem/appear' and its complement
## Example: 'this is garbage' -> (this, garbage)
children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False
for child in children :
if(child.dep_ == "nsubj" and not child.is_stop):
A = child.text
if((child.dep_ == "attr") and not child.is_stop):
M = child.text
if(child.dep_ == "neg"):
neg_prefix = child.text
add_neg_pfx = True
if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M
if(A != "999999" and M != "999999"):
if A in prod_pronouns :
A = "product"
dict7 = {"noun" : A, "adj" : M, "rule" : 7}
rule7_pairs.append(dict7)
aspects = []
aspects = rule1_pairs + rule2_pairs + rule3_pairs +rule4_pairs +rule5_pairs + rule6_pairs + rule7_pairs
return aspects
def extract_aspects(nlp, reviews):
aspects = []
data = ([
(x[1], x[0]) for x in reviews['text_cleaned'].reset_index().to_numpy()
])
for doc, review_id in nlp.pipe(data, as_tuples=True):
doc_aspects = extract_doc_aspects(doc)
doc_aspects = [
[review_id, aspect['noun'], aspect['adj'], aspect['rule']]
for aspect in doc_aspects if not aspect['noun'].lower().startswith('product')
]
# filter aspects with out of vocubalary nouns
doc_aspects = [
doc_aspect for doc_aspect in doc_aspects
if has_vectors(nlp(doc_aspect[1]))
]
aspects.extend(doc_aspects)
aspects = pd.DataFrame(aspects, columns=['review_id', 'aspect', 'opinion', 'rule'])
return aspects
|