Calculer cosine similarity between texts
Cosine similarity measures le angle entre texte vectors, indicating semantic similarity. Utilisé en search, clustering, et recommendation.
- 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 Compteur de mots →
Convertir each text en 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 Fréquence de caractères →
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).
Questions fréquentes
- 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.