You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.1 KiB
69 lines
2.1 KiB
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.FeatureMap = exports.segmentString = exports.wordToTwoLetterFeatures = exports.wordToSingleLetterFeatures = exports.mergeFeatures = exports.wordToFeatures = exports.compareResults = void 0; |
|
/** |
|
* Comparison function to return the best (highest score) results first. |
|
* @param a Result A |
|
* @param b Result B |
|
*/ |
|
function compareResults(a, b) { |
|
return b.score - a.score || a.word.localeCompare(b.word); |
|
} |
|
exports.compareResults = compareResults; |
|
function wordToFeatures(word) { |
|
const map = new FeatureMap(); |
|
mergeFeatures(map, wordToSingleLetterFeatures(word)); |
|
mergeFeatures(map, wordToTwoLetterFeatures(word)); |
|
return map; |
|
} |
|
exports.wordToFeatures = wordToFeatures; |
|
function mergeFeatures(map, features) { |
|
map.append(features); |
|
} |
|
exports.mergeFeatures = mergeFeatures; |
|
function wordToSingleLetterFeatures(word) { |
|
return word.split('').map((a) => [a, 1]); |
|
} |
|
exports.wordToSingleLetterFeatures = wordToSingleLetterFeatures; |
|
function wordToTwoLetterFeatures(word) { |
|
return segmentString(word, 2).map((s) => [s, 1]); |
|
} |
|
exports.wordToTwoLetterFeatures = wordToTwoLetterFeatures; |
|
function segmentString(s, segLen) { |
|
const count = Math.max(0, s.length - segLen + 1); |
|
const result = new Array(count); |
|
for (let i = 0; i < count; ++i) { |
|
result[i] = s.substr(i, segLen); |
|
} |
|
return result; |
|
} |
|
exports.segmentString = segmentString; |
|
class FeatureMap extends Map { |
|
constructor() { |
|
super(); |
|
this._count = 0; |
|
} |
|
get count() { |
|
return this._count; |
|
} |
|
append(features) { |
|
features.forEach(([k, v]) => { |
|
this.set(k, (this.get(k) || 0) + v); |
|
this._count += v; |
|
}); |
|
return this; |
|
} |
|
correlationScore(m) { |
|
const score = this.intersectionScore(m); |
|
return score / (this._count + m._count - score); |
|
} |
|
intersectionScore(m) { |
|
let score = 0; |
|
for (const [k, v] of this) { |
|
score += Math.min(v, m.get(k) || 0); |
|
} |
|
return score; |
|
} |
|
} |
|
exports.FeatureMap = FeatureMap; |
|
//# sourceMappingURL=helpers.js.map
|