webui / smart_matcher.py
Nerdur's picture
Upload 2 files
99c6072 verified
Raw
History Blame Contribute Delete
2.09 kB
"""
title: Smart Matcher
author: nerdur
version: 0.1
description: Sparuje stavke iz dvije liste i vraća ranking s confidence scorom.
"""
from pydantic import BaseModel
from typing import Optional
import difflib
class Tools:
class Valves(BaseModel):
threshold: float = 0.3
def __init__(self):
self.valves = self.Valves()
def match_items(
self,
list_a: str,
list_b: str,
__user__: Optional[dict] = None,
) -> str:
"""
Match items between two comma-separated lists and return ranked pairs with confidence scores.
:param list_a: First list of items, comma-separated
:param list_b: Second list of items, comma-separated
:return: Markdown table with matched pairs and confidence scores
"""
items_a = [x.strip() for x in list_a.split(",") if x.strip()]
items_b = [x.strip() for x in list_b.split(",") if x.strip()]
if not items_a or not items_b:
return "❌ Both lists must have at least one item."
results = []
used_b = set()
for a in items_a:
best_match = None
best_score = 0.0
for b in items_b:
if b in used_b:
continue
score = difflib.SequenceMatcher(None, a.lower(), b.lower()).ratio()
if score > best_score:
best_score = score
best_match = b
if best_match and best_score >= self.valves.threshold:
results.append((a, best_match, best_score))
used_b.add(best_match)
else:
results.append((a, "— no match —", 0.0))
results.sort(key=lambda x: x[2], reverse=True)
lines = ["| | Item A | Item B | Confidence |", "|---|---|---|---|"]
for i, (a, b, score) in enumerate(results):
star = "⭐" if i == 0 and score > 0 else ""
pct = f"{int(score * 100)}%"
lines.append(f"| {star} | **{a}** | {b} | {pct} |")
return "\n".join(lines)