-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagger.js
More file actions
211 lines (162 loc) · 4.41 KB
/
Copy pathtagger.js
File metadata and controls
211 lines (162 loc) · 4.41 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
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
// grabs tag from image
var Clarifai = require('./clarifai_node.js');
var MongoClient = require('mongodb').MongoClient;
var syllable = require('syllable');
var assert = require('assert');
var Q = require('q');
var url = 'mongodb://localhost:27017/dogeku';
module.exports = function(imgUrl, fileName) {
var dfd = Q.defer();
// scan image
runClarifai(imgUrl, fileName)
.then(function(tags) {
// get good tags
tags = max34(tags);
// db matches
substituteStrings(tags)
.then(function(replacers) {
// console.log('REPLACERS', replacers)
tags = subSub(replacers, tags);
dfd.resolve(getTerms(tags));
}, function(err) {
dfd.reject(err);
});
}, function(err) {
dfd.reject(err);
});
return dfd.promise;
}
function runClarifai(imgUrl, fileName) {
var dfd = Q.defer();
Clarifai.tagURL(imgUrl, fileName, function(err, res) {
// console.log('request handler!');
if(err) {
console.log(' there was an error!');
if( typeof err.status_code === "string" && err.status_code === "TIMEOUT") {
console.log("TAG request timed out");
}
else if( typeof err.status_code === "string" && err.status_code === "ALL_ERROR") {
console.log("TAG request received ALL_ERROR. Contact Clarifai support if it continues.");
}
else if( typeof err.status_code === "string" && err.status_code === "TOKEN_FAILURE") {
console.log("TAG request received TOKEN_FAILURE. Contact Clarifai support if it continues.");
}
else if( typeof err.status_code === "string" && err.status_code === "ERROR_THROTTLED") {
console.log("Clarifai host is throttling this application.");
}
else {
console.log("TAG request encountered an unexpected error: ");
console.log(err);
}
dfd.reject(err);
return;
}
// console.log('else');
// if some images were successfully tagged and some encountered errors,
// the status_code PARTIAL_ERROR is returned. In this case, we inspect the
// status_code entry in each element of res["results"] to evaluate the individual
// successes and errors. if res.status_code === "OK" then all images were
// successfully tagged.
// This is where it's successful, or partially successful
if( typeof res.status_code === "string" &&
( res.status_code === "OK" || res.status_code === "PARTIAL_ERROR" )) {
var tags = [];
res.results.forEach(function(result) {
if(result.status_code === "OK" ) {
tags = result.result.tag.classes;
} else {
console.log(
'docid=' + res.result.docid,
'local_id=' + res.result.local_id,
'status_code=' + res.result.status_code,
'error = ' + res.result.result.error
);
}
});
dfd.resolve(tags);
}
});
return dfd.promise;
}
function max34(tags) {
var tagList = [];
tags.every(function(tag) {
if (syllable(tag) <= 3 && tag !== 'no person') {
tagList.push(tag);
if(tagList.length === 4) {
return false;
}
return true;
}
return true;
});
return tagList;
}
//grab array of objects from replace.js ---> replacers[]
//use replacers[0].toReplace for function
function substituteStrings(tags) {
console.log('sub');
var dfd = Q.defer();
MongoClient.connect(url, function(err, db) {
console.log('connected');
if(err) {
dfd.reject(err);
return;
}
var replacers = [];
var cursor = db.collection('replacers').find();
cursor.each(function(err, doc) {
if(err) {
console.log('err');
dfd.reject(err);
return false;
}
if(doc) {
// console.log('doc');
replacers.push(doc);
} else {
// console.log('else');
dfd.resolve(replacers);
}
});
});
return dfd.promise;
}
function subSub(replacers, tags) {
var replacedTags = [];
var count = 0;
tags.forEach(function(tag) {
// should only run 4 times
for (var j = 0; j < replacers.length; j++) {
// run through all replacement options
// check sameness
if (tag.includes(replacers[j].toReplace)) {
var newstr = tag.replace(replacers[j].regExp, replacers[j].replaceWith);
replacedTags.push(newstr);
return false;
}
count++;
if (count == replacers.length){
count = 0;
replacedTags.push(tag);
}
}
});
return replacedTags;
}
// term and syll
function getTerms(tags) {
var syllableList = [];
var terms = [];
tags.forEach(function(tag) {
syllableList.push(syllable(tag));
});
tags.forEach(function(tag, i) {
terms.push({
term: tag,
syll: syllableList[i]
})
});
console.log(terms);
return terms;
}