Skip to content

Text cosine

github-actions[bot] edited this page Aug 26, 2026 · 24 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Text is 0.4.0 — read its documentation.

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 alsoOverlap, SorensenDice, the set-similarity index.

Members

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

Lodestar

Project

Clone this wiki locally