跳到主要内容
uencoder

计算 cosine similarity between texts

Cosine 相似度 measures 角度之间文本 vectors, indicating semantic 相似度. Used 在搜索, 聚类, 和 recommendation.

  1. 1

    Cosine 相似度 = dot(, B) / (|| × |B|), where 和 B 是项 频率 (或 TF-IDF) vectors. 它 ranges 从 0 (否 shared terms) 到 1 (identical 方向). Unlike Euclidean 距离, 它是 unaffected 通过 document 长度 — two documents 具有相同字 proportions 但不同 lengths 有相似度 1. Convert texts 到 vectors (TF 或 TF-IDF), 然后计算 cosine.

  2. 2
    字数统计器 →

    将 each text 转换为 a word frequency vector. The vocabulary is the union of all words in both texts. Each text becomes a vector of word counts. Use TF-IDF weights instead of raw counts for better results — TF-IDF downweights common words

  3. 3
    字符频率统计器 →

    为字符-级别相似度 (useful 为代码, DNA, non-文本), use 字符频率 vectors 而非字 vectors. 此 captures finer-grained patterns 和工作为 文本无 字 boundaries (CJK languages).

常见问题

当到 use cosine 相似度?
为文本 comparison where document 长度应该不 matter: 搜索 relevance (查询 vs document), document 聚类, 重复检测, 和 recommendation systems. 它是 标准相似度指标为 文本 vectors. 为任务 where 长度 matters (plagiarism 检测), use 其他 metrics.
Cosine vs Jaccard vs Euclidean?
Cosine: 角度之间 vectors (长度-不变量) — best 为文本. Jaccard: 交集/联合的 sets — best 为二进制 presence/absence. Euclidean: straight-行距离 — sensitive 到长度. 为文本, cosine 是标准; 为 sets, Jaccard; 为 spatial 数据, Euclidean.

解决此任务的工作流

相关工具