-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect_duplicates.js
executable file
·179 lines (155 loc) · 6.4 KB
/
detect_duplicates.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
#!/usr/bin/env node
/**
* detect_duplicates.js
* Finds pairwise annotated questions by title string comparison.
*/
var program = require('commander'),
fs = require('fs');
program
.option('-q, --question-file <file name>', 'JSON file with questions to check')
.option('-f, --output-format [output format]', 'output format [list]', 'list');
program.on('--help', function(){
console.log(' Supported output formats:\n');
console.log(' list: list with information about authors of duplicates');
console.log(' idlist: list with plain question IDs');
console.log(' blacklist: black list for each author with IDs of duplicate by other author');
console.log(' questions: JSON dump of duplicate questions');
console.log(' map: JSON object mapping question IDs to their corresponding duplicate ID');
console.log(' merged: JSON export of duplicate questions with their annotations merged');
console.log();
});
program.parse(process.argv);
if (typeof program.questionFile == 'undefined') {
program.help();
}
var questions = JSON.parse(fs.readFileSync(program.questionFile));
var done = {};
var pairs = {};
var duplicates = [];
// some special cases that cannot be detected automatically
var specialCases = {
'52ebb2c698d0239505000029': '52f77f042059c6d71c000029',
'530cf4fe960c95ad0c000003': '52e8e96698d023950500001f'
};
questions.forEach(function (q) {
questions.forEach(function (qq) {
var lesserID = q._id < qq._id ? q._id : qq._id;
var lesserCreatorQuestion = q.creator < qq.creator ? q : qq;
var title1 = q.body.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
var title2 = qq.body.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
var isSpecialCase = (specialCases[q._id] === qq._id) || (specialCases[qq._id] === q._id);
if (((title1 === title2) || isSpecialCase) && (q._id !== qq._id) &&
!done[lesserID]) {
done[lesserID] = true;
// default duplicate info list
if (program.outputFormat === 'list') {
process.stdout.write(q.body + '\n');
process.stdout.write(q._id + ' (' + q.creator + ')\n');
process.stdout.write(qq._id + ' (' + qq.creator + ')\n');
process.stdout.write('\n');
} else if (program.outputFormat === 'idlist') {
process.stdout.write(q._id + '\n');
process.stdout.write(qq._id + '\n');
} else if (program.outputFormat === 'blacklist') {
pairs[q.creator] = pairs[q.creator] || [];
pairs[q.creator].push(qq._id);
pairs[qq.creator] = pairs[qq.creator] || [];
pairs[qq.creator].push(q._id);
} else if (program.outputFormat === 'questions') {
duplicates.push(q);
duplicates.push(qq);
} else if (program.outputFormat === 'map') {
pairs[q._id] = qq._id;
pairs[qq._id] = q._id;
} else if (program.outputFormat === 'merged') {
duplicates.push(mergeQuestions(q, qq));
} else {
program.help();
}
}
});
});
if (Object.keys(pairs).length) {
process.stdout.write(JSON.stringify(pairs, false, 4));
}
if (duplicates.length) {
process.stdout.write(JSON.stringify(duplicates, false, 4));
}
function mergeQuestions(question1, question2) {
var mergedQuestion = JSON.parse(JSON.stringify(question1));
mergedQuestion._id = question1._id + question2._id;
delete mergedQuestion.creator;
// merge concepts
Array.prototype.push.apply(mergedQuestion.concepts, question2.concepts.filter(function (c) {
return !(mergedQuestion.concepts.some(function (mc) {
if (mc.uri === c.uri) { console.error('ignoring duplicate concept: ' + c.uri); }
return (mc.uri === c.uri);
}));
}));
// merge documents
Array.prototype.push.apply(mergedQuestion.documents, question2.documents.filter(function (d) {
return !(mergedQuestion.documents.some(function (md) {
if (md.uri === d.uri) { console.error('ignoring duplicate document: ' + d.uri); }
return (md.uri === d.uri);
}));
}));
// merge statements
Array.prototype.push.apply(mergedQuestion.statements, question2.statements.filter(function (s) {
return !(mergedQuestion.statements.some(function (ms) {
if (ms.title === s.title) { console.error('ignoring duplicate statement: ' + s.title); }
return (ms.title === s.title);
}));
}));
// merge snippets
mergedQuestion.snippets.forEach(function (s1) {
question2.snippets.forEach(function (s2) {
if (s1['document'] === s2['document']) {
if (doesSnippetOverlapWithSnippet(s1, s2)) {
s1.beginIndex = Math.min(s1.beginIndex, s2.beginIndex);
s1.endIndex = Math.max(s1.endIndex, s2.endIndex);
for (var i = 0; i < question1.documents.length; i++) {
if (question1.documents[i].uri === s1['document']) {
s1.text = snippetText(question1.documents[i], s1);
break;
}
}
console.error('snippets merged: ' + s1.text);
} else {
mergedQuestion.snippets.push(s2);
}
}
});
});
return mergedQuestion;
};
function doesSnippetOverlapWithSnippet(s1, s2) {
try {
compareSnippets(s1, s2);
} catch (e) {
return true;
}
return false;
};
function compareSnippets(op1, op2) {
if (op1.endSection < op2.beginSection) {
return -1;
} else if (op1.beginSection > op2.endSection) {
return 1;
} else {
if (op1.endIndex < op2.beginIndex) {
return -1;
} else if (op1.beginIndex > op2.endIndex) {
return 1;
} else {
throw Error('Overlapping snippets!');
}
}
};
function snippetText(doc, snippet) {
if (snippet.beginSection !== snippet.endSection) {
throw Error('Snippet spanning multiple sections!');
}
var parts = snippet.beginSection.split('.'),
section = parts.length === 1 ? doc[parts[0]] : doc[parts[0]][parseInt(parts[1], 10)];
return section.substring(snippet.beginIndex, snippet.endIndex);
};