|
| 1 | +import { errorSignature } from './tools/loop-context/dist/context-manager.js'; |
| 2 | + |
| 3 | +function getTrigrams(str) { |
| 4 | + const trigrams = new Set(); |
| 5 | + const padded = ` ${str.toLowerCase()} `; |
| 6 | + for (let i = 0; i < padded.length - 2; i++) { |
| 7 | + trigrams.add(padded.substring(i, i + 3)); |
| 8 | + } |
| 9 | + return trigrams; |
| 10 | +} |
| 11 | + |
| 12 | +function calculateSimilarity(a, b) { |
| 13 | + if (a === b) return 1.0; |
| 14 | + const setA = getTrigrams(a); |
| 15 | + const setB = getTrigrams(b); |
| 16 | + if (setA.size === 0 && setB.size === 0) return 1.0; |
| 17 | + if (setA.size === 0 || setB.size === 0) return 0.0; |
| 18 | + |
| 19 | + let intersection = 0; |
| 20 | + for (const item of setA) { |
| 21 | + if (setB.has(item)) intersection++; |
| 22 | + } |
| 23 | + const union = setA.size + setB.size - intersection; |
| 24 | + return intersection / union; |
| 25 | +} |
| 26 | + |
| 27 | +const e1 = errorSignature('Error: timeout on port 8080'); |
| 28 | +const e2 = errorSignature('Error: timed out waiting for 8080'); |
| 29 | +const e3 = errorSignature('Error: connection timeout on port 8080'); |
| 30 | +console.log('e1:', e1); |
| 31 | +console.log('e2:', e2); |
| 32 | +console.log('e3:', e3); |
| 33 | +console.log('e1 vs e2:', calculateSimilarity(e1, e2)); |
| 34 | +console.log('e1 vs e3:', calculateSimilarity(e1, e3)); |
| 35 | +console.log('e2 vs e3:', calculateSimilarity(e2, e3)); |
0 commit comments