ConnectOnionConnectOnion
DocsTUIFuzzy Matching

Fuzzy Matching

Fuzzy matching utilities for autocomplete

Quick Start

main.py
1from connectonion.tui import fuzzy_match, highlight_match 2 3# Check if query matches text 4matched, score, positions = fuzzy_match("gp", "gpt-4") 5# matched: True 6# score: 16 (higher = better) 7# positions: [0, 1] (matched char indices)

fuzzy_match

Match query against text with scoring:

main.py
1from connectonion.tui import fuzzy_match 2 3matched, score, positions = fuzzy_match(query, text)

Scoring

BonusDescription
+1Per matched character
+10Consecutive character bonus
+5Word boundary bonus (after /_-. )

Examples

main.py
1fuzzy_match("gp", "gpt-4") # (True, 16, [0, 1]) 2fuzzy_match("g4", "gpt-4") # (True, 7, [0, 4]) 3fuzzy_match("xyz", "gpt-4") # (False, 0, []) 4fuzzy_match("", "anything") # (True, 0, [])

highlight_match

Highlight matched characters in Rich Text:

main.py
1from connectonion.tui import highlight_match 2from rich.console import Console 3 4console = Console() 5 6# Get match positions 7matched, score, positions = fuzzy_match("gp", "gpt-4") 8 9# Create highlighted text 10text = highlight_match("gpt-4", positions) 11console.print(text) # "gp" highlighted in magenta

API

main.py
1fuzzy_match(query: str, text: str) -> tuple[bool, int, list[int]] 2# Returns: (matched, score, positions) 3 4highlight_match(text: str, positions: list[int]) -> Text 5# Returns: Rich Text with matched chars highlighted