This repository was archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttribute.py
214 lines (173 loc) · 5.71 KB
/
Attribute.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
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
'''
Created on March 11, 2015
Last edit on April 14, 2015
@author: Brenden, Justin
'''
class Attribute():
def __init__(self, name = "Attribute", attrWeight = 1,
words = None, weights = None, sentiments = None):
self.name = name
self.attrWeight = attrWeight
self.words = words
self.weights = weights
self.sentiments = sentiments
'''Just checks to see that all values are equal.'''
def __eq__(self, other):
if self.name != other.name:
return False
if self.attrWeight != other.attrWeight:
return False
if self.words != other.words:
return False
if self.weights != other.weights:
return False
if self.sentiments != other.sentiments:
return False
return True
'''
name: A string by which this attribute will be referred.
Special case: The name "Attribute" will indicate that the name was not set in the GUI.
In this case, set the name to the first highest-value word.
'''
def set_name(self, name):
#We don't really want to change the attribute name, here.
if name != "Attribute":
self.name = name
#This is to give each attribute a name of its own that isn't the default "Attribute"
if (name == "Attribute" or name == "") and self.words is not None and self.weights is not None:
newName = self.generate_name(self.words, self.weights)
#If no words are set, then we don't have anything to change it to.
if newName != "":
self.name = newName
def set_attr_weight(self, weight):
if weight == "High":
self.attrWeight = 3
elif weight == "Medium":
self.attrWeight = 2
else:
self.attrWeight = 1
def set_words(self, words):
self.words = words
'''Expects a list of strings.'''
def set_weights(self, weights):
if len(weights) > 0 and not isinstance(weights[0], basestring):
raise ValueError("set_weights: This function expects strings.")
newWeights = []
for weight in weights:
if weight == "High":
newWeights.append(3)
elif weight == "Medium":
newWeights.append(2)
else:
newWeights.append(1)
self.weights = newWeights
'''Expects a list of numbers.'''
def set_weights_nums(self, weights):
if len(weights) > 0 and not isinstance(weights[0], int):
raise ValueError("set_weights_nums: This function expects numbers.")
self.weights = weights
'''Expects a list of strings.'''
def set_sentiments(self, sentiments):
if len(sentiments) > 0 and not isinstance(sentiments[0], basestring):
raise ValueError("set_sentiments: This function expects strings.")
newSentiments = []
for sentiment in sentiments:
if sentiment == "Neutral" or sentiment == "Sentiment": #The latter happens when it goes unset.
newSentiments.append(0)
elif sentiment == "Positive":
newSentiments.append(1)
else:
newSentiments.append(-1)
self.sentiments = newSentiments
'''Expects a list of numbers.'''
def set_sentiments_nums(self, sentiments):
if len(sentiments) > 0 and not isinstance(sentiments[0], int):
raise ValueError("set_weights_nums: This function expects numbers.")
self.sentiments = sentiments
'''Returns the word in an attribute from a given index (int)'''
def get_word(self, index):
if not isinstance(index, int) or index >= len(self.words):
raise ValueError("get_word: Index out of bounds.")
return self.words[index]
'''
index: int, index of word in internal array
returns: word "High, "Medium", or "Low"
'''
def get_weight(self, index):
if index >= len(self.words):
raise ValueError("get_word: Index out of bounds.")
if self.weights[index] == 3:
return "High"
elif self.weights[index] == 2:
return "Medium"
else:
return "Low"
def get_weight_num(self, index):
if index >= len(self.words):
raise ValueError("get_word: Index out of bounds.")
return self.weights[index]
'''
index: int, index of word in internal array
returns: word "Positive", "Neutral", or "Negative"
'''
def get_sentiment(self, index):
if index >= len(self.words):
raise ValueError("get_word: Index out of bounds.")
if self.sentiments[index] == 1:
return "Positive"
elif self.sentiments[index] == 0:
return "Neutral"
else:
return "Negative"
def get_sentiment_num(self, index):
if index >= len(self.words):
raise ValueError("get_word: Index out of bounds.")
return self.sentiments[index]
'''Returns # of words in attribute'''
def get_size(self):
if self.words is None:
return 0
else:
return len(self.words)
'''
The following functions are for bulk processing in the SearchPacket class.
'''
'''Gets the attribute name.'''
def get_name(self):
return self.name
'''Gets the attribute weight.'''
def get_attr_weight_num(self):
return self.attrWeight
def get_attr_weight(self):
if self.attrWeight == 3:
return "High"
elif self.attrWeight == 2:
return "Medium"
else:
return "Low"
'''Gets the entire list of attribute words.'''
def get_words(self):
return self.words
'''Gets the entire list of attribute weights.'''
def get_weights(self):
return self.weights
'''Gets the entire list of attribute expected sentiments.'''
def get_sentiments(self):
return self.sentiments
def get_max_score(self):
if self.words is None or len(self.words) == 0:
raise ValueError("get_word: No words to calculate score.")
return sum(self.weights)
'''Generates a name based on the first highest weighted word.'''
def generate_name(self, words, weights):
if words is None or weights is None:
raise ValueError("generate_name: words and weights must not be none.")
if len(words) != len(weights):
raise ValueError("generate_name: words and weights must be the same size.")
max = 0
name = ""
for word, weight in zip(words, weights):
if weight > max and word != "":
max = weight
name = word
return name