-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.js
80 lines (69 loc) · 1.62 KB
/
analyze.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
var CL = require('./searchcraigslist');
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
CL.getLinks('boston','roo').then(function(res){
res.forEach(function(obj){
var newtext = obj.text.replace(/\n/g,' ').replaceAll("&"," ").replaceAll('<br>'," ").replaceAll(''',"'")
obj.text = newtext;
obj.wordcount = countWords(obj.text)
})
//console.log(res);
var commonwords = {};
res.forEach(function(obj){
var wordcount = obj.wordcount;
for(var word in wordcount){
if(word in commonwords){
commonwords[word] = commonwords[word] + wordcount[word];
} else {
commonwords[word] = wordcount[word];
}
}
})
res.forEach(function(obj){
var wordcount = obj.wordcount;
for(var word in wordcount){
obj['wordcount'][word] = wordcount[word]/commonwords[word];
}
})
res.forEach(function(obj){
var common = [];
for(var word in obj.wordcount){
common.push([word,obj.wordcount[word]]);
}
common.sort(function(a,b){
if(a[1]>b[1]){
return 1;
}
if(a[1]<b[1]){
return -1
}
return 0;
})
obj['common'] = JSON.stringify(common);
obj['sig'] = obj['common'].slice(obj['common'].length-20,obj['common'].length)
})
console.log(res);
})
function countWords(string){
var count = {};
var arr = string.split(' ');
arr.forEach(function(word){
if (word in count){
count[word] = count[word] + 1;
} else {
count[word] = 1;
}
})
return count;
}
function objFilter(obj,cb){
var results = {};
for (var key in obj){
if (cb(obj[key],key)){
results[key] = obj[key]
}
}
return results;
}