UNPKG

@leolee9086/string-metrics-dice

Version:
69 lines (60 loc) 2.11 kB
/** * @typedef {Object} DiceCoefficientOptions * @property {number} [nGramSize=2] - n-gram大小,默认为2(bigram) */ /** * @typedef {Object} DiceCoefficientResult * @property {number} coefficient - Dice系数,范围[0,1] * @property {number} distance - Dice距离,范围[0,1] * @property {string} algorithm - 使用的算法名称 * @property {number} executionTime - 执行时间(毫秒) */ /** * @typedef {Object} AlgorithmMetadata * @property {string} name - 算法名称 * @property {string} version - 算法版本 * @property {Object} complexity - 复杂度信息 * @property {string} complexity.time - 时间复杂度 * @property {string} complexity.space - 空间复杂度 * @property {string[]} characteristics - 算法特征 * @property {string[]} bestFor - 适用场景 * @property {string} author - 作者 */ /** * @typedef {Object} StringDistanceAlgorithm * @property {string} name - 算法名称 * @property {string} version - 算法版本 * @property {function(string, string, DiceCoefficientOptions): number} compute - 计算函数 * @property {AlgorithmMetadata} metadata - 算法元数据 */ /** * 默认的Dice系数计算选项 * @type {DiceCoefficientOptions} */ export const DEFAULT_OPTIONS = { nGramSize: 2 }; /** * 验证Dice系数计算选项 * @param {DiceCoefficientOptions} options - 选项对象 * @returns {DiceCoefficientOptions} 验证后的选项 */ export function validateOptions(options = {}) { const validated = { ...DEFAULT_OPTIONS, ...options }; if (typeof validated.nGramSize !== 'number' || validated.nGramSize < 1) { throw new TypeError('nGramSize must be a positive number'); } return validated; } /** * 预处理字符串 * @param {string} str - 输入字符串 * @param {DiceCoefficientOptions} options - 选项 * @returns {string} 预处理后的字符串 */ export function preprocessString(str, options) { if (typeof str !== 'string') { throw new TypeError('Input must be a string'); } return str; }