File size: 1,273 Bytes
71d49e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | from collections.abc import Iterable
import numpy as np
from scipy.sparse import csr_matrix
from sklearn.base import BaseEstimator, TransformerMixin
from .structural_features import FEATURE_NAMES, featurize
TOOL_WORDS = (
"search",
"look up",
"lookup",
"latest",
"current",
"today",
"news",
"weather",
"price",
"stock",
"score",
"live",
"fetch",
"browse",
"web",
"internet",
"calendar",
"schedule",
"concert",
"trending",
"recent",
"up-to-date",
)
TOOL_STRUCT_FEATURE_NAMES = list(FEATURE_NAMES) + ["has_tool", "tool_count"]
class ToolStruct(BaseEstimator, TransformerMixin):
"""19 structural features plus two tool-dependency cues."""
def fit(self, X: Iterable[str], y: np.ndarray | None = None) -> "ToolStruct":
return self
def transform(self, X: Iterable[str]) -> csr_matrix:
rows = [self._featurize(text) for text in X]
return csr_matrix(np.asarray(rows, dtype=np.float64))
@staticmethod
def _featurize(text: str) -> list[float]:
source = text if isinstance(text, str) else ""
hits = sum(source.lower().count(phrase) for phrase in TOOL_WORDS)
return featurize(source) + [float(hits > 0), float(hits)]
|