forked from alainrk/quick-match
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.js
78 lines (70 loc) · 2.27 KB
/
result.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
'use strict'
class Result {
constructor (algorithm, text, candidates) {
this.algorithm = algorithm
this.minScore = Infinity
this.maxScore = -Infinity
this.maxIntersections = -Infinity
this.candidates = candidates
this.text = text
this.stemmedText = []
this.minCandidateIdx = null
this.maxCandidateIdx = null
this.maxIntersectionsCandidateIdx = null
this.bestCandidateIdx = null
this.bestCandidate = null
this.numberMatch = undefined
this.numberMatchType = undefined
}
setCandidateScore (candidateIdx, score) {
this.candidates[candidateIdx].score = score
if (score < this.minScore) {
this.minScore = score
this.minCandidateIdx = candidateIdx
}
if (score > this.maxScore) {
this.maxScore = score
this.maxCandidateIdx = candidateIdx
}
return this
}
setCandidateStemIntersections (candidateIdx, intersections) {
this.candidates[candidateIdx].intersections = intersections
if (intersections.length > this.maxIntersections) {
this.maxIntersections = intersections.length
this.maxIntersectionsCandidateIdx = candidateIdx
}
return this
}
setStemmedText (arr) {
this.stemmedText = arr
return this
}
setStemmedCandidate (candidateIdx, arr) {
this.candidates[candidateIdx].stemmed = arr
return this
}
setNumberMatch (type, idx) {
if (!['digit', 'cardinal', 'ordinal'].includes(type)) throw new Error(`Type ${type} is not a valid number match result`)
this.numberMatch = true
this.numberMatchType = type
this.bestCandidateIdx = idx
return this
}
build () {
if (!this.candidates || this.candidates.length === 0) throw new Error('Cannot build solution, no candidates in result')
if (!this.numberMatch) {
if (this.algorithm === 'dice') {
this.bestCandidateIdx = this.maxCandidateIdx
} else if (this.algorithm === 'levenshtein') {
this.bestCandidateIdx = this.minCandidateIdx
} else {
throw new Error('Not supported algorithm')
}
}
if (!this.bestCandidateIdx && this.bestCandidateIdx !== 0) throw new Error('Cannot build solution, no best candidate in result')
this.bestCandidate = this.candidates[this.bestCandidateIdx]
return this
}
}
module.exports = { Result }