-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTermFrequencyModel.js
executable file
·217 lines (195 loc) · 6.19 KB
/
TermFrequencyModel.js
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
/*
TermFrequencyModel.js
This model processes and packages data for the term frequency view.
Initialization:
Load term frequency from 'data/global-term-freqs.json'
Data Update:
Listens to FilteredTermTopicProbabilityModel (events: )
Details:
--------
Pulls data from FilteredTermTopicProbilityModel. The model loads some parameters from
the url. On updates, the model receives a list
of terms and generates a list of item(term, frequency) (same order as the term list
received as input).
*/
var TermFrequencyModel = Backbone.Model.extend({
defaults : {
"termIndex" : null,
"totalTermFreqs": {},
"topicalFreqMatrix": [],
"colorList": [],
"selectedTopics": {}
},
url : "data/global-term-freqs.json",
initialize : function() {
this.parentModel = null;
this.stateModel = null;
// original data
this.originalMatrix = null;
this.originalTopicIndex = null;
this.originalTermIndex = null;
// mappings
this.termFreqMap = null;
// iteractions
// TODO: (later) clean up these. Definitely don't need all of these variables
this.selectedTopics = {};
this.colorList = [];
this.colorToTopic = {};
this.topicalFreqs = null;
}
});
/**
* Initialize Term Frequency Model's parent and state model
*
* @private
*/
TermFrequencyModel.prototype.initModels = function( parent, state ){
this.parentModel = parent;
this.stateModel = state;
};
/**
* Initialize all topics' selection status to null (called once by load)
*
* @private
*/
TermFrequencyModel.prototype.defaultSelection = function(){
var topicIndex = this.parentModel.get("topicIndex");
for( var i = 0; i < topicIndex.length; i++ ){
this.selectedTopics[i] = null;
}
this.set("selectedTopics", this.selectedTopics);
};
/**
* Loads matrix, termIndex, topicIndex, and term to frequency mapping from the model's "url"
* and triggers a loaded event that the next model (child model) listens to. Also, pulls
* any selected topics from state model and processes them.
* (This function is called after the filtered model loaded event is fired)
*
* @param { string } the location of datafile to load values from
* @return { void }
*/
TermFrequencyModel.prototype.load = function(){
var successHandler = function( model, response, options )
{
this.set("termIndex", this.parentModel.get("termIndex"));
this.originalMatrix = response.matrix;
this.originalTopicIndex = response.topicIndex;
this.originalTermIndex = response.termIndex;
this.termFreqMap = response.termFreqMap;
this.defaultSelection();
this.getTotalTermFreqs();
// process selected topics from the saved state
var coloredTopics = this.stateModel.get("selectedTopics");
var colorList = [];
for( var obj in coloredTopics){
claimColor( coloredTopics[obj] );
colorList.push({"topic":obj, "color":coloredTopics[obj]});
}
colorList.sort(function(a, b) {return colorNames.indexOf(a.color) - colorNames.indexOf(b.color)});
for( var i = 0; i < colorList.length; i++){
this.selectTopic({"topic": colorList[i].topic, "color": colorList[i].color} );
}
// signal completion
this.trigger("loaded:freqModel");
}.bind(this);
var errorHandler = function( model, xhr, options ) { }.bind(this);
this.fetch({
add : false,
success : successHandler,
error : errorHandler
});
};
/**
* Calls appropriate functions to update based on data change(s)
*/
TermFrequencyModel.prototype.update = function(){
this.generateTopicalMatrix( true );
this.getTotalTermFreqs();
this.set("termIndex", this.parentModel.get("termIndex"));
};
/**
* Finds total frequency for each term in termIndex
*
* @private
*/
TermFrequencyModel.prototype.getTotalTermFreqs = function(){
var frequencies = {};
var terms = this.parentModel.get("termIndex");
for( var i = 0; i < terms.length; i++){
frequencies[terms[i]] = this.termFreqMap[terms[i]];
}
this.set("totalTermFreqs", frequencies);
};
/**
* Finds frequency / topic for each term in termIndex and each topic in selectedTopics
*
* @private
*/
TermFrequencyModel.prototype.generateTopicalMatrix = function( keepQuiet ) {
var frequencies = [];
var terms = this.parentModel.get("termIndex");
for( var index = 0; index < this.colorList.length; index++){
var tempList = [];
var topic = this.colorToTopic[this.colorList[index]];
for( var i = 0; i < terms.length; i++){
var termIndex = this.originalTermIndex.indexOf(terms[i]);
tempList.push(this.originalMatrix[termIndex][topic]);
}
frequencies.push(tempList);
}
this.topicalFreqs = frequencies;
this.set("topicalFreqMatrix", frequencies, {silent: keepQuiet});
this.set("colorList", this.colorList);
this.set("selectedTopics", this.selectedTopics);
return frequencies;
};
/**
* Called by term frequency view. Returns frequency / topic for every term in termIndex
*
* @this { TermFrequencyModel }
* @param { int } target topic index
* @return { array } list of topical frequencies in termIndex ordering
*/
TermFrequencyModel.prototype.getTopicalsForTopic = function( topic ) {
var frequencies = [];
var terms = this.get("termIndex");
for( var i = 0; i < terms.length; i++){
var termIndex = this.originalTermIndex.indexOf(terms[i]);
frequencies.push(this.originalMatrix[termIndex][topic]);
}
return frequencies;
};
// interactions
/**
* Behavior when topic is selected
*
* @this { TermFrequencyModel }
* @param { object } topic: target topic index, color: associated color
* @return { void }
*/
TermFrequencyModel.prototype.selectTopic = function( obj ) {
var topic = obj.topic;
var color = obj.color;
var topicIndex = this.parentModel.get("topicIndex");
if( topic !== null){
// if color is DEFAULT, the event can be treated as a deselect
if( color === DEFAULT || color == brown) {
if(this.selectedTopics[topic] !== null){
var index = this.colorList.indexOf(this.selectedTopics[topic]);
this.colorList.splice(index,1);
this.selectedTopics[topic] = null;
delete this.colorToTopic[color];
} else {
return;
}
}
// only add if this topic wasn't added previously
else if(this.selectedTopics[topic] === null) {
this.selectedTopics[topic] = color;
this.colorList.push(color);
this.colorToTopic[color] = topic;
}
// recompute the topical matrix
this.generateTopicalMatrix( false );
}
};