Initial upload: TermDiagram v0.1.0
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-01-29 22:28:06 +00:00
parent 70f6e8d96c
commit 301e24a843

View File

@@ -0,0 +1,55 @@
from typing import List, Optional
from rapidfuzz import fuzz
from ..models import Module, ClassSymbol, FunctionSymbol, SymbolType
class SearchResult:
def __init__(self, name: str, symbol_type: str, path: str, line_number: int, score: float):
self.name = name
self.symbol_type = symbol_type
self.path = path
self.line_number = line_number
self.score = score
class FuzzySearcher:
def __init__(self):
self.symbols: List = []
def build_index(self, symbols: List):
self.symbols = symbols
def search(self, query: str, limit: int = 10) -> List[SearchResult]:
results = []
for symbol in self.symbols:
score = fuzz.ratio(query.lower(), symbol.name.lower())
if score > 50:
symbol_type = self._get_symbol_type(symbol)
path = getattr(symbol, "path", "")
line_number = getattr(symbol, "line_number", 0)
results.append(
SearchResult(
name=symbol.name,
symbol_type=symbol_type,
path=path,
line_number=line_number,
score=score,
)
)
results.sort(key=lambda x: x.score, reverse=True)
return results[:limit]
def _get_symbol_type(self, symbol) -> str:
if isinstance(symbol, Module):
return "module"
elif isinstance(symbol, ClassSymbol):
return "class"
elif isinstance(symbol, FunctionSymbol):
return "function"
else:
return "unknown"
def get_symbol_count(self) -> int:
return len(self.symbols)