-
Notifications
You must be signed in to change notification settings - Fork 1
/
similar-context-plugin.js
51 lines (44 loc) · 1.75 KB
/
similar-context-plugin.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
const stringSimilarity = require('string-similarity')
const keywordExtractor = require('keyword-extractor')
const utils = require('../utils.js')
/**
* Param 'extractKeywords' by default false:
* Returns a comparing score of two strings based on Dice's Coefficient using the stringSimilarity module.
*
* If param 'extractKeywords' is true: first uses the keyword-extractor to get the keywords, then compares the keywords with stringSimilarity to get a context score
*
* @param sString1 - first string for comparison
* @param sString2 - second string for comparison
* @param params - object with attribute:
* 'extractKeywords' as parameter for extracting the keywords (true - extract keyword before calculating similarity score, false - whithout extraction)
*/
function similarityPlugin (sString1, sString2, params) {
let extractKeywords = (params && params['extractKeywords']) || false
if ((typeof sString1 === 'undefined') || (typeof sString2 === 'undefined')) {
throw new Error(`Two Strings as input are needed.`)
}
utils.isValidString(sString1)
utils.isValidString(sString2)
if (typeof extractKeywords !== 'boolean') {
throw new Error(`Boolean expected, found ${extractKeywords}`)
}
if (extractKeywords) {
sString1 = getKeywords(sString1)
sString2 = getKeywords(sString2)
}
return stringSimilarity.compareTwoStrings(sString1, sString2)
}
/**
* Uses the keyword-extractor module to get the keywords from the description and then joins the array to a string
*
* @param description - as string
*/
function getKeywords (description) {
return keywordExtractor.extract(description, {
language: 'english',
remove_digits: true,
return_changed_case: true,
remove_duplicates: false
}).join(' ')
}
module.exports = similarityPlugin