Text 0.7.0 cosine - CyrilB1531/lodestar GitHub Wiki

Lodestar.Text 0.7.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

Home › Set similarity

Cosine

Shared q-grams over the geometric mean of the two bag sizes: |A∩B| / √(|A|·|B|). Also the Ochiai coefficient.

public static class Cosine

Example — the same pair the other four score, on this scale.

using Lodestar.Text.Similarity;

double related = Cosine.Similarity("apple", "pineapple");  // => 0.7453…

Remarks — this is the cosine of the angle between the two gram-count vectors, which is where the name comes from; on multisets of q-grams it reduces to the expression above. It is a compromise between its neighbours, and that is the reason to reach for it: √(|A|·|B|) sits between SorensenDice's arithmetic mean and Overlap's min, so a size gap costs something without costing everything.

It has nothing to do with the cosine similarity of two embedding vectors, which compares learned representations rather than counted grams; that one lives in Lodestar.Embeddings.

Applies to — net10.0, netstandard2.0.

See also — Overlap, SorensenDice, the set-similarity index.

Members

Member What it does
Cosine.Similarity Shared grams over the geometric mean of both bag sizes.