-
Notifications
You must be signed in to change notification settings - Fork 0
/
textmatcher.js
250 lines (208 loc) · 7.89 KB
/
textmatcher.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const keyword_extractor = require("keyword-extractor");
const rake = require('rake-js');
var openai = require("./openaifunctionality.js");
module.exports = function(data, isRake, useOpenAI) {
if(useOpenAI){
return openai("Can you rephrase the following sentence without negations and can you extract the functional and non-functional requirements of the text and write them in two arrays, one array for functional requirements and one for non-functional requirements \n"+
+ "the individual requirements should be separated by commas. The format for the response should be json: "+ data.input).then((resolve) => {
console.log("resolve");
console.log(resolve);
return matching(data, isRake, resolve.choices[0].message.content, useOpenAI);
});
}else{
return matching(data, isRake, data.input, useOpenAI);
}
}
function matching(data, isRake, resultvalue, isRephrased){
var infos = data.algodata;
console.log("The infos");
console.log(infos)
var datainput = resultvalue.toLowerCase();
console.log("input:");
console.log(data.input);
if(isRephrased){
console.log("rephrasedInput:");
console.log(datainput);
}
var allkeywords = [];
var results = [];
infos.forEach(algorithminfo => {
if(isRake){
const intentkeywords2 = rake.default(algorithminfo.data.intent, { language: 'english' });
const problemkeywords2 = rake.default(algorithminfo.data.problem, { language: 'english' });
const solutionkeywords2 = rake.default(algorithminfo.data.solution, { language: 'english' });
let intentkeywords = [];
let problemkeywords = [];
let solutionkeywords = [];
intentkeywords2.forEach(words => {
intentkeywords = intentkeywords.concat(words.split(' '));
});
problemkeywords2.forEach(words => {
problemkeywords = intentkeywords.concat(words.split(' '));
});
solutionkeywords2.forEach(words => {
solutionkeywords = intentkeywords.concat(words.split(' '));
});
//quadruple value
var applicationareaskeywords = [];
var problemtypeskeywords = [];
if (algorithminfo.data.applicationAreas && algorithminfo.data.applicationAreas.length > 0) {
algorithminfo.data.applicationAreas.forEach(word => {
applicationareaskeywords.push(word.label);
applicationareaskeywords.push(word.label);
applicationareaskeywords.push(word.label);
applicationareaskeywords.push(word.label);
});
}
allkeywords.push({name: algorithminfo.name,
keywords: intentkeywords.concat(intentkeywords).concat(problemkeywords, solutionkeywords)
.concat(applicationareaskeywords, problemtypeskeywords)
});
}else{
console.log("Bbbbb")
console.log(algorithminfo.data)
const intentkeywords =[];
const problemkeywords =[];
const solutionkeywords = [];
/**
const intentkeywords = keyword_extractor.extract(algorithminfo.data.intent,{
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false
});
const problemkeywords = keyword_extractor.extract(algorithminfo.data.problem,{
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false
});
const solutionkeywords = keyword_extractor.extract(algorithminfo.datasolution,{
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false
});
*/
//quadruple value
var applicationareaskeywords = [];
var problemtypeskeywords = [];
console.log("appli")
console.log(algorithminfo.data.applicationAreas);
if (algorithminfo.data.applicationAreas && algorithminfo.data.applicationAreas.length > 0) {
algorithminfo.data.applicationAreas.forEach(word => {
console.log("applicationwwhw")
console.log(word)
applicationareaskeywords.push(word.split(" ")[0].toLowerCase());
});
}
allkeywords.push({name: algorithminfo.name,
keywords: intentkeywords.concat(intentkeywords).concat(problemkeywords, solutionkeywords)
.concat(applicationareaskeywords, problemtypeskeywords)});
}
console.log("key words");
console.log(allkeywords)
});
allkeywords.forEach(algo => {
const occurrences = algo.keywords.reduce(function (acc, curr) {
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc}, {});
results.push({name: algo.name, occurrences: occurrences});
});
var extraction_result_input = [];
var similarities = [];
if(isRake){
extraction_result_input2 = rake.default(datainput, { language: 'english' });
extraction_result_input2.forEach(words => {
extraction_result_input = extraction_result_input.concat(words.split(' '));
});
console.log("input keywords");
console.log(extraction_result_input);
console.log(results)
}else{
extraction_result_input =
keyword_extractor.extract(datainput,{
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false
});
console.log("input keywords");
console.log(extraction_result_input);
console.log(results)
}
// cosine similarity with keywords
var sim = [];
results.forEach(alg => {
sim.push({name: alg.name, cosineSimilarity: textCosineSimilarity(alg.name, alg.occurrences, extraction_result_input)});
});
sim.sort((a, b) => b.cosineSimilarity - a.cosineSimilarity);
console.log("result");
console.log(sim);
if(isRephrased){
return {result: sim, rephrasedInput: datainput}
}else{
return {result: sim}
}
}
//--------------------------------------------------------------------------------------------
// https://sumn2u.medium.com/string-similarity-comparision-in-js-with-examples-4bae35f13968
//--------------------------------------------------------------------------------------------
function termFreqMap(array) {
var termFreq = {};
array.forEach(function(w) {
termFreq[w] = (termFreq[w] || 0) + 1;
});
return termFreq;
}
function addKeysToDict(map, dict) {
for (var key in map) {
let k = key.toLowerCase()
dict[k] = true;
}
}
function termFreqMapToVector(map, dict) {
var termFreqVector = [];
for (var term in dict) {
console.log(term)
termFreqVector.push(map[term] || 0);
}
console.log("vector A")
console.log(termFreqVector)
return termFreqVector;
}
function vecDotProduct(vecA, vecB) {
var product = 0;
console.log(vecDotProduct)
console.log(vecA)
console.log(vecB)
for (var i = 0; i < vecA.length; i++) {
product += vecA[i] * vecB[i];
}
return product;
}
function vecMagnitude(vec) {
var sum = 0;
for (var i = 0; i < vec.length; i++) {
//console.log(vec[i])
sum += vec[i] * vec[i];
}
return Math.sqrt(sum);
}
function cosineSimilarity(vecA, vecB) {
console.log("cosine similarity");
console.log(vecDotProduct(vecA, vecB));
console.log(vecMagnitude(vecA));
console.log((vecMagnitude(vecB)))
return vecDotProduct(vecA, vecB) / (vecMagnitude(vecA) * vecMagnitude(vecB));
}
function textCosineSimilarity(name, occurences, input) {
console.log("compute cs");
console.log(name);
var termFreqB = termFreqMap(input);
var dict = {};
addKeysToDict(occurences, dict);
addKeysToDict(termFreqB, dict);
var termFreqVecA = termFreqMapToVector(occurences, dict);
var termFreqVecB = termFreqMapToVector(termFreqB, dict);
return cosineSimilarity(termFreqVecA, termFreqVecB);
}