64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
"""Keyword scorer — checks for presence/absence of required keywords in output.
|
|
|
|
Configurable with required_present and required_absent keyword lists.
|
|
Score is computed as (matches / total_requirements) ratio.
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
from engine.scorers.base import BaseScorer
|
|
|
|
|
|
class KeywordScorer(BaseScorer):
|
|
"""Score outputs based on keyword presence and absence.
|
|
|
|
Args:
|
|
required_present: Keywords that must appear in the output.
|
|
required_absent: Keywords that must not appear in the output.
|
|
case_sensitive: Whether keyword matching is case-sensitive (default False).
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
required_present: list[str] | None = None,
|
|
required_absent: list[str] | None = None,
|
|
case_sensitive: bool = False,
|
|
) -> None:
|
|
self.required_present = required_present or []
|
|
self.required_absent = required_absent or []
|
|
self.case_sensitive = case_sensitive
|
|
|
|
if not self.required_present and not self.required_absent:
|
|
raise ValueError(
|
|
"At least one of required_present or required_absent must be provided."
|
|
)
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "keyword"
|
|
|
|
def score(self, input_data: Any, output: str, context: dict) -> float:
|
|
"""Score output based on keyword presence/absence.
|
|
|
|
Returns the ratio of satisfied keyword requirements to total requirements.
|
|
Each keyword in required_present scores a point if found.
|
|
Each keyword in required_absent scores a point if NOT found.
|
|
"""
|
|
total = len(self.required_present) + len(self.required_absent)
|
|
if total == 0:
|
|
return 1.0
|
|
|
|
check_output = output if self.case_sensitive else output.lower()
|
|
satisfied = 0
|
|
|
|
for keyword in self.required_present:
|
|
check_keyword = keyword if self.case_sensitive else keyword.lower()
|
|
if check_keyword in check_output:
|
|
satisfied += 1
|
|
|
|
for keyword in self.required_absent:
|
|
check_keyword = keyword if self.case_sensitive else keyword.lower()
|
|
if check_keyword not in check_output:
|
|
satisfied += 1
|
|
|
|
return satisfied / total
|