Saltar al contenido
uencoder

Calcular cosine similarity between texts

Cosine similarity measures el angle entre texto vectors, indicating semantic similarity. Usado en search, clustering, y recommendation.

  1. 1

    Cosine similarity = dot(A, B) / (|A| × |B|), where A and B are term frequency (or TF-IDF) vectors. It ranges from 0 (no shared terms) to 1 (identical direction). Unlike Euclidean distance, it is unaffected by document length — two documents with the same word proportions but different lengths have similarity 1. Convert texts to vectors (TF or TF-IDF), then compute cosine.

  2. 2
    Contador de palabras →

    Convertir each text a 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
    Frecuencia de caracteres →

    For character-level similarity (useful for code, DNA, non-text), use character frequency vectors instead of word vectors. This captures finer-grained patterns and works for text without word boundaries (CJK languages).

Preguntas frecuentes

When to use cosine similarity?
For text comparison where document length should not matter: search relevance (query vs document), document clustering, duplicate detection, and recommendation systems. It is the standard similarity metric for text vectors. For tasks where length matters (plagiarism detection), use other metrics.
Cosine vs Jaccard vs Euclidean?
Cosine: angle between vectors (length-invariant) — best for text. Jaccard: intersection/union of sets — best for binary presence/absence. Euclidean: straight-line distance — sensitive to length. For text, cosine is standard; for sets, Jaccard; for spatial data, Euclidean.

Flujos que resuelven esta tarea

Herramientas relacionadas