Class FuzzyScorer

java.lang.Object
org.aesh.readline.fuzzy.FuzzyScorer

public final class FuzzyScorer extends Object
High-level API for fuzzy filtering and ranking of text entries.

Use this class to filter a list of history entries (or any text entries) against a fuzzy pattern. Results are sorted by score (descending), with ties broken by recency (most recent first).

The scorer supports:

  • Case-insensitive matching (default)
  • Deduplication of identical entries (most recent kept)
  • Lazy narrowing: when a character is appended to the query, only the previous result set is re-filtered instead of the full list

Not thread-safe — use one instance per search session.

See Also:
  • Constructor Details

    • FuzzyScorer

      public FuzzyScorer(FuzzyScheme scheme, boolean caseSensitive)
      Create a scorer with the given scheme and case sensitivity.
      Parameters:
      scheme - the scoring scheme
      caseSensitive - whether matching is case-sensitive
    • FuzzyScorer

      public FuzzyScorer(FuzzyScheme scheme)
      Create a case-insensitive scorer with the given scheme.
      Parameters:
      scheme - the scoring scheme
  • Method Details

    • score

      public FuzzyResult score(int[] text, int[] pattern, boolean withPos)
      Score a single text entry against a pattern.
      Parameters:
      text - the text as code points
      pattern - the search pattern as code points
      withPos - whether to compute match positions
      Returns:
      the match result
    • scoreAll

      public List<FuzzyScorer.ScoredEntry> scoreAll(List<int[]> entries, int[] pattern, boolean withPos)
      Filter and rank a list of entries against a pattern.

      Entries are deduplicated (most recent occurrence kept), scored against the pattern, filtered to matches only, and sorted by score descending (ties broken by recency).

      Parameters:
      entries - the entries to search (most recent last, as from History.getAll())
      pattern - the search pattern as code points
      withPos - whether to compute match positions for each entry
      Returns:
      sorted list of scored entries (best match first)
    • scoreAll

      public List<FuzzyScorer.ScoredEntry> scoreAll(List<int[]> entries, List<Long> timestamps, int[] pattern, boolean withPos)
      Filter and rank a list of entries against a pattern, with timestamps.
      Parameters:
      entries - the entries to search (most recent last, as from History.getAll())
      timestamps - parallel list of timestamps (epoch millis), or null
      pattern - the search pattern as code points
      withPos - whether to compute match positions for each entry
      Returns:
      sorted list of scored entries (best match first)
    • narrow

      public List<FuzzyScorer.ScoredEntry> narrow(List<FuzzyScorer.ScoredEntry> previousResults, int[] pattern, boolean withPos)
      Narrow a previous result set by scoring against a longer pattern.

      This is an optimization for interactive typing: when the user appends a character to the query, we only need to re-score the entries that already matched the shorter pattern.

      Parameters:
      previousResults - the results from the previous (shorter) pattern
      pattern - the new (longer) pattern as code points
      withPos - whether to compute match positions
      Returns:
      narrowed and re-sorted results
    • deduplicate

      public static List<int[]> deduplicate(List<int[]> entries)
      Deduplicate entries, keeping only the most recent occurrence. Input order: oldest first (index 0) to most recent (last index). Output order: most recent first (for display).
      Parameters:
      entries - the entries to deduplicate (oldest first)
      Returns:
      deduplicated list with most recent entries first