mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-28 12:35:02 +02:00
@@ -601,6 +601,38 @@ export function cosinesim (A: number[], B: number[]): number {
|
||||
return similarity
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Sørensen–Dice coefficient
|
||||
*/
|
||||
export function calcSørensenDiceCoefficient (a: string, b: string): number {
|
||||
const first = a.replace(/\s+/g, '')
|
||||
const second = b.replace(/\s+/g, '')
|
||||
|
||||
if (first === second) return 1 // identical or empty
|
||||
if (first.length < 2 || second.length < 2) return 0 // if either is a 0-letter or 1-letter string
|
||||
|
||||
const firstBigrams = new Map<string, number>()
|
||||
for (let i = 0; i < first.length - 1; i++) {
|
||||
const bigram = first.substring(i, i + 2)
|
||||
const count = (firstBigrams.get(bigram) ?? 0) + 1
|
||||
|
||||
firstBigrams.set(bigram, count)
|
||||
}
|
||||
|
||||
let intersectionSize = 0
|
||||
for (let i = 0; i < second.length - 1; i++) {
|
||||
const bigram = second.substring(i, i + 2)
|
||||
const count = firstBigrams.get(bigram) ?? 0
|
||||
|
||||
if (count > 0) {
|
||||
firstBigrams.set(bigram, count - 1)
|
||||
intersectionSize++
|
||||
}
|
||||
}
|
||||
|
||||
return (2.0 * intersectionSize) / (first.length + second.length - 2)
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user