-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (153 loc) · 5.68 KB
/
index.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
const fs = require('fs');
const _ = require('lodash');
const stopword = require('stopword');
const threshold = 0.4;
if(process.argv.length < 4) {
console.log('Usage: node index.js *filename* *keyword*');
process.exit(1);
}
const readFile = async function(fileName) {
return await new Promise(function(resolve, reject) {
fs.readFile(fileName, 'utf8', function(err, data) {
if(err) {
reject(err);
}
resolve(data);
});
});
};
const splitUserStories = async function(uS) {
return await new Promise(function(resolve) {
if(uS.includes('\n\n\n')) {
resolve(uS.split('\n\n\n'));
} else if(uS.includes('\r\n\r\n\r\n')) {
resolve(uS.split('\r\n\r\n\r\n'));
} else{
console.log('String was unable to be separated!');
process.exit(1);
}
});
};
const getKeywords = async function(keyword) {
return await new Promise(function(resolve) {
if(keyword === 'metadata') {
resolve(['metadata', 'rights', 'permissions', 'batch', 'technical', 'track', 'bitstream', 'screen', 'integrity', 'terms', 'EAD']);
} else if(keyword === 'workgroup') {
resolve(['workgroup', 'workgroups', 'delete', 'batch', 'organize', 'manage ', 'bitstream', 'integrity', 'deposits', 'submit']);
}
resolve([keyword]);
});
};
const selectImportantUserStories = async function(userStoriesSplit, keywords) {
return await new Promise(function(resolve) {
let importantUserStories = [];
_.each(userStoriesSplit, function(userStory) {
let isImportant = false;
_.each(keywords, function(keyword) {
isImportant = userStory.includes(keyword);
if(isImportant) {
return false;
}
return true;
});
if(isImportant) {
importantUserStories.push(userStory);
}
});
resolve(importantUserStories);
});
};
const keyUserStories = async function(uSs) {
return await new Promise(function(resolve) {
let userStories = [];
_.each(uSs, function(uS) {
const indexOfColon = uS.indexOf(':');
let key = '';
if(indexOfColon === 3) {
key = uS.charAt(indexOfColon - 1);
} else if(indexOfColon === 4) {
key = uS.charAt(indexOfColon - 2) + uS.charAt(indexOfColon - 1);
}
let string = uS.substring(indexOfColon + 2);
userStories.push({
id: key,
string: string
});
});
resolve(userStories);
});
};
const formatUserStories = async function(uSs) {
return await new Promise(function(resolve) {
_.each(uSs, function(uS) {
uS.string = uS.string.replace(/:/g, '');
if(uS.string.includes('\n\n')) {
uS.string = uS.string.replace(/\n/g, ' ');
} else if(uS.string.includes('\r\n\r\n')) {
uS.string = uS.string.replace(/\r\n/g, ' ');
}
uS.string = uS.string.replace(' ', ' ');
uS.string = uS.string.toLowerCase();
uS.string = uS.string.split(' ');
uS.string = stopword.removeStopwords(uS.string);
uS.string = uS.string.filter(function(item, index, array) {
return array.indexOf(item) === index;
});
});
resolve(uSs);
});
};
const compareUserStories = async function(uSs) {
let similarUserStories = [];
_.each(uSs, function(uS1) {
_.each(uSs, function(uS2) {
if(uS1.id === uS2.id) {
return;
}
const differenceArray = _.xor(uS1.string, uS2.string);
const totalLength = uS1.string.length + uS2.string.length;
const differenceLength = differenceArray.length;
const overlapLength = totalLength - differenceLength;
const overlapPercentage = overlapLength / totalLength;
if(overlapPercentage >= threshold) {
// console.log(uS1.id + ',' + uS2.id);
// console.log(uS1.id + ',' + uS2.id + ' (' + overlapPercentage + ')');
similarUserStories.push({
id1: uS1.id,
id2: uS2.id
});
}
});
});
return similarUserStories;
};
const removeDuplicates = async function(uSs) {
let userStories = uSs;
let finalUserStories = [];
_.each(userStories, function(userStory) {
if(_.some(userStories, { id1: userStory.id2, id2: userStory.id1})) {
const index = _.findIndex(finalUserStories, { id1: userStory.id2, id2: userStory.id1});
if(index === -1) {
finalUserStories.push(userStory);
}
}
});
return finalUserStories;
};
const printSimilarUserStories = async function(uSs) {
_.each(uSs, function(uS) {
console.log(uS.id1 + ',' + uS.id2);
});
};
const execute = async function() {
const userStoriesText = await readFile(process.argv[2]);
const userStoriesSplit = await splitUserStories(userStoriesText);
const keywords = await getKeywords(process.argv[3]);
const importantUserStories = await selectImportantUserStories(userStoriesSplit, keywords);
const keyedUserStories = await keyUserStories(importantUserStories);
const formattedUserStories = await formatUserStories(keyedUserStories);
const similarUserStories = await compareUserStories(formattedUserStories);
const trimmedUserStories = await removeDuplicates(similarUserStories);
printSimilarUserStories(trimmedUserStories);
};
execute();