fuzzy

Fuzzy string matching, Levenshtein distance, similarity scoring and ranked search.

v1.0.0Text & stringsbundledimport "fuzzy";
Overview Installation Documentation Examples

Overview

Zero-dependency fuzzy string matching library for HolyPHP. Provides Levenshtein edit distance, normalized similarity scoring, subsequence matching, and ranked candidate search.

bundled — works offline zero dependencies levenshtein search

Quick start

import "fuzzy";

$dist = fuzzy_levenshtein("kitten", "sitting");       // 3
$sim  = fuzzy_similarity("kitten", "sitting");        // ~0.5714
$ok   = fuzzy_match("hphp", "HolyPHP");               // true

$candidates = ["apple", "application", "banana", "apricot", "pineapple"];
$results = fuzzy_search("app", $candidates, 3);

foreach ($results as $r) {
    println($r["value"] . " (score: " . $r["score"] . ")");
}

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install fuzzy

This adds the library to hphp.yaml, installs it into .hphp/packages/fuzzy/ and pins the exact version in hphp.lock:

require:
  fuzzy: "^1.0"

Use it

import "fuzzy";

fuzzy is bundled with the compiler, so import "fuzzy"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install fuzzy          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install fuzzy
Packagefuzzy
Version1.0.0 (follows the compiler)
Entry filefuzzy.hphp
Sourcepackages/lib/fuzzy ↗

Documentation

API Reference

fuzzy_levenshtein(string $a, string $b, bool $caseSensitive = true): int

Computes the Levenshtein edit distance (minimum insertions, deletions, and substitutions) between $a and $b.

fuzzy_similarity(string $a, string $b, bool $caseSensitive = true): float

Computes a normalized similarity score in the range [0.0, 1.0] derived from Levenshtein distance: 1.0 - (distance / max(len($a), len($b))). Two empty strings return 1.0.

fuzzy_match(string $pattern, string $text, bool $caseSensitive = false): bool

Checks whether $pattern is a subsequence of $text (every character of $pattern appears in $text in order, not necessarily contiguously). An empty pattern always matches.

fuzzy_score(string $query, string $candidate, bool $caseSensitive = false): float

Computes a relevance score in [0.0, 1.0] for ranking a candidate against a query. Exact matches score 1.0, prefix matches score >= 0.85, substring matches score >= 0.70, subsequence matches score >= 0.50, and other strings fall back to scaled Levenshtein similarity.

fuzzy_search(string $query, array $candidates, int $limit = 10, float $minScore = 0.0, bool $caseSensitive = false): array

Ranks $candidates against $query and returns up to $limit matches with score >= $minScore, sorted descending by score (ties broken by shorter length, then original index).

Error Handling

fuzzy_search validates its arguments and throws Exception with clear, actionable messages:

Examples

01Quick start

Bundled with the compiler — import "fuzzy"; works offline in any file, or add it to a project with hphp install fuzzy.

import "fuzzy";

$dist = fuzzy_levenshtein("kitten", "sitting");       // 3
$sim  = fuzzy_similarity("kitten", "sitting");        // ~0.5714
$ok   = fuzzy_match("hphp", "HolyPHP");               // true

$candidates = ["apple", "application", "banana", "apricot", "pineapple"];
$results = fuzzy_search("app", $candidates, 3);

foreach ($results as $r) {
    println($r["value"] . " (score: " . $r["score"] . ")");
}
Try it
hphp install fuzzy
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_fuzzy.hphp.