-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzyMatching.js
187 lines (134 loc) · 5.01 KB
/
fuzzyMatching.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
// **************************
// ****** DEPENDENCIES ******
// **************************
var _ = require('lodash');
var fuzzy = require('fuzzy');
var fs = require('fs');
var proc = require('./util/process');
var dmpmod = require('./util/diff_match_patch');
var dmp = new dmpmod.diff_match_patch();
var FuzzySet = require('fuzzyset.js');
var getBizNameWordCounts = function(uniqueBizNames, shouldLog) {
var bizNameWords = uniqueBizNames.join(' ').replace(/[^\w\s]|_/g, '').match(/\S+/g);
var bizNameWordCount = _.countBy(bizNameWords, function(x){return x;});
if (shouldLog) {
var toLog = _.map(_.pairs(bizNameWordCount), function(pair) { return { word : pair[0], score : pair[1] }; });
toLog = _.sortBy(toLog, function(result) { return result.score; });
console.log(toLog);
}
// remove outliers
delete bizNameWordCount["Restaurant"];
return bizNameWordCount;
};
var cleanTweet = function(text) {
var URL_REGEX = "http://[a-z0-9].[a-z]{2,3}/[a-zA-Z0-9]+";
var MENTION_REGEX = "@[^ ]+ ";
var RT_START = "RT ";
// remove links
text = text.replace(new RegExp(URL_REGEX, 'g'), "");
// remove @ mentions
text = text.replace(new RegExp(MENTION_REGEX, 'g'), "");
// remove RT
if (text.indexOf(RT_START) == 0) {
text = text.substring(RT_START.length);
}
return text;
};
exports.fuzzyMatch = function(uniqueBizNames, tweets) {
var bizNameWordCount = getBizNameWordCounts(uniqueBizNames);
var maxCount = _.max(_.values(bizNameWordCount));
var avgCount = _.reduce(_.values(bizNameWordCount), function(memo, num){ return memo + num; }, 0) / _.values(bizNameWordCount).length;
var tweetText = _.map(tweets, function(t) { return cleanTweet(t.text); });
var scoresByName = {};
_.each(uniqueBizNames, function(bizName) {
var scoreByIndex = {};
var nameParts = bizName.split(' ');
var requiredParts = _.filter(nameParts, function(part) {
return bizNameWordCount[part] < 20;
});
if (requiredParts.length > 0) {
var bonusParts = _.filter(nameParts, function(part) {
return bizNameWordCount[part] >= 20;
});
_.each(bonusParts, function(part) {
var wordCount = bizNameWordCount[part];
var results = fuzzy.filter(part, tweetText);
_.each(results, function(result) {
var wordScale = 0.5 * (maxCount - wordCount) / maxCount;
if (!scoreByIndex[result.index]) {
scoreByIndex[result.index] = { bonus:0, required:[] };
}
scoreByIndex[result.index].bonus += result.score * wordScale;
});
});
var REQUIRED_PART_SCORE_CUTOFF = 20;
_.each(requiredParts, function(part) {
var wordCount = bizNameWordCount[part];
var results = fuzzy.filter(part, tweetText);
_.each(results, function(result) {
if (!scoreByIndex[result.index]) {
scoreByIndex[result.index] = { bonus:0, required:[] };
}
if (result.score < REQUIRED_PART_SCORE_CUTOFF) {
scoreByIndex[result.index].required.push(0);
}
else {
var wordScale = (REQUIRED_PART_SCORE_CUTOFF - wordCount) / REQUIRED_PART_SCORE_CUTOFF;
scoreByIndex[result.index].required.push(result.score * wordScale);
}
});
});
var scoresToKeep = _.filter(_.pairs(scoreByIndex), function(pair) {
return pair[1].required.length == requiredParts.length
&& _.all(pair[1].required, function(score) { return score > 0; });
});
var tweetScores = _.map(scoresToKeep, function(pair) {
var score = pair[1].bonus + _.reduce(pair[1].required, function(memo, num){ return memo + num; }, 0);
return { tweet : tweetText[pair[0]], score : score };
});
if (tweetScores.length > 0) {
scoresByName[bizName] = tweetScores;
}
}
});
return scoresByName;
};
return;
var TWEETS_FILE = './private/tweets-20140227T030314518Z.json';
var YELP_BIZ_FILE = './private/yelp/yelp_businesses-20140323T163547057Z.json';
var tweets = JSON.parse(fs.readFileSync(TWEETS_FILE));
var bizs = JSON.parse(fs.readFileSync(YELP_BIZ_FILE));
var scoresByName = exports.fuzzyMatch(_.uniq(_.pluck(bizs, 'name')), tweets);
_.each(_.pairs(scoresByName), function(pair) {
var bizName = pair[0];
var tweetScores = pair[1];
if (tweetScores.length > 0) {
console.log("*********************");
console.log(bizName + ' (required: ' + requiredParts.join(' ') + ', bonus: ' + bonusParts.join(' ') + ')');
console.log("*********************");
console.log(tweetScores);
}
});
proc.done();
var results = fuzzy.filter('the mid-life crisis', tweetText);
results = _.filter(results, function(r) {
return r.score > 20;
});
results = _.map(results, function(r) {
return _.pick(r, 'string', 'score');
});
var results2 = _.map(tweetText, function(t) {
var score = dmp.match_main(t, "the mid-life crisis", 0);
return { string: t, score: score };
});
results2 = _.filter(results2, function(r) {
return r.score > 20;
});
console.log(results2);
var t1 = _.pluck(results, 'string');
var t2 = _.pluck(results2, 'string');
var in1Not2 = _.difference(t1, t2);
var in2Not1 = _.difference(t2, t1);
return;
console.log(in2Not1);
var set = FuzzySet();