Skip to content

Commit

Permalink
modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
alainrk committed Aug 7, 2021
1 parent 8ca58fb commit a185bb1
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ class QuickMatch {
}, [])
}

run (src, candidates) {
if (!this.candidatesValidator(candidates)) throw new Error('Candidates has not a valid format!')
candidates = this.normalizeCandidates(candidates)

console.log(`\nAlgorithm: ${this.options.algorithm} - [${src}]`)

let max = -Infinity
let min = Infinity
applyAlgorithm (src, candidates) {
let maxScore = -Infinity
let minScore = Infinity
let minCandidateIdx, maxCandidateIdx

for (let i = 0; i < candidates.length; i++) {
Expand All @@ -59,26 +54,43 @@ class QuickMatch {

c.originalScore = res

if (res < min) {
min = res
if (res < minScore) {
minScore = res
minCandidateIdx = i
}
if (res > max) {
max = res
if (res > maxScore) {
maxScore = res
maxCandidateIdx = i
}
}
return {
minCandidateIdx,
maxCandidateIdx,
minScore,
maxScore,
candidates
}
}

run (src, candidates) {
if (!this.candidatesValidator(candidates)) throw new Error('Candidates has not a valid format!')
candidates = this.normalizeCandidates(candidates)

console.log(`\nAlgorithm: ${this.options.algorithm} - [${src}]`)

const { minCandidateIdx, maxCandidateIdx, minScore, maxScore } = this.applyAlgorithm(src, candidates)

console.log(JSON.stringify(candidates, ' ', 2))
console.log(min, max, minCandidateIdx, maxCandidateIdx)
console.log(minScore, maxScore, minCandidateIdx, maxCandidateIdx)
}
}

// const qmd = new QuickMatch({ algorithm: 'dice' })
// qmd.run('I want a pizza', ['Free hot dog here', 'Pizza for sale', 'Rent your cola'])

const qm = new QuickMatch({
algorithm: 'levenshtein',
algorithm: 'dice',
// algorithm: 'levenshtein',
enableStemming: true,
stemming: { language: 'en', minPreStemmingLength: 4, minPostStemmingLength: 4 },
limits: { minLengthCandidate: 3, maxCandidateWords: 5 },
Expand Down

0 comments on commit a185bb1

Please sign in to comment.