import dataclasses
from collections.abc import Iterable
from typing import Self

NGrams = dict[int, list[str]]


@dataclasses.dataclass(order=True)
class SearchMatch:
    distance: float
    ngram: str

    @classmethod
    def combine_matches(cls, matches: Iterable[Self]) -> Self:
        ngrams, distances = zip(*((match.ngram, match.distance) for match in matches))
        return cls(
            ngram=', '.join(ngrams),
            distance=sum(distances)/len(distances)
        )


@dataclasses.dataclass
class QueryResult:
    id: int
    distance: float
    ngram: str
