classifyByTfSoft function exported ✓ 100.0%
Last updated: 2026-03-02T11:04:51.015Z
Metrics
LOC: 70
Complexity: 14
Params: 3
Coverage: 100.0% (32/32 lines, 22x executed)
Signature
classifyByTfSoft(
input: string | StructuredSignals,
context?: StructuredSignalContext,
): : { type: FieldType; score: number } | null
Summary
Classify via: 1. Learned vectors (Chrome AI + user corrections) — higher threshold 2. TF.js pre-trained model softmax — TF_THRESHOLD Returns null if signals are empty, the model is not loaded, or the best score is below the threshold.
Architecture violations
- [warning] max-cyclomatic-complexity: 'classifyByTfSoft' has cyclomatic complexity 14 (max 10)
Source Code
export function classifyByTfSoft(
input: string | StructuredSignals,
context?: StructuredSignalContext,
): { type: FieldType; score: number } | null {
const featureText =
typeof input === "string"
? buildFeatureText(fromFlatSignals(input), context)
: buildFeatureText(input, context);
if (!featureText.trim()) return null;
if (!_pretrained || !_tfModule) {
log.warn(TF_MESSAGES.classify.notLoaded(featureText));
return null;
}
const inputVec = vectorize(featureText, _pretrained.vocab);
if (!inputVec.some((v) => v > 0)) return null;
// Step 1: Learned vectors (user corrections + Chrome AI)
if (_learnedVectors.length > 0) {
let bestLearnedScore = -1;
let bestLearnedType: FieldType | null = null;
for (const entry of _learnedVectors) {
const sim = dotProduct(inputVec, entry.vector);
if (sim > bestLearnedScore) {
bestLearnedScore = sim;
bestLearnedType = entry.type;
}
}
if (bestLearnedScore >= thresholds.learned && bestLearnedType) {
log.debug(
TF_MESSAGES.classify.learnedMatch(
bestLearnedType,
bestLearnedScore.toFixed(3),
thresholds.learned,
featureText,
),
);
return { type: bestLearnedType, score: bestLearnedScore };
}
}
// Step 2: TF.js pre-trained model
const { bestIdx, bestScore } = _tfModule.tidy(() => {
const input = _tfModule!.tensor2d([Array.from(inputVec)]);
const probs = (_pretrained!.model.predict(input) as Tensor).dataSync();
let idx = 0;
let score = -1;
for (let i = 0; i < probs.length; i++) {
if (probs[i] > score) {
score = probs[i];
idx = i;
}
}
return { bestIdx: idx, bestScore: score };
});
if (bestScore < thresholds.model) {
log.warn(
TF_MESSAGES.classify.lowScore(
bestScore.toFixed(3),
thresholds.model,
featureText,
_pretrained.labels[bestIdx],
),
);
return null;
}
return { type: _pretrained.labels[bestIdx], score: bestScore };
}
No outgoing dependencies.
Impact (Incoming)
| Source | Type |
|---|---|
| makeField | uses |
| classifyField | calls |
| detect | calls |
| resetModelMock | uses |