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

View all

  • [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)

graph LR classifyByTfSoft["classifyByTfSoft"] makeField["makeField"] classifyField["classifyField"] detect["detect"] resetModelMock["resetModelMock"] makeField -->|uses| classifyByTfSoft classifyField -->|calls| classifyByTfSoft detect -->|calls| classifyByTfSoft resetModelMock -->|uses| classifyByTfSoft style classifyByTfSoft fill:#dbeafe,stroke:#2563eb,stroke-width:2px click classifyByTfSoft "0bd31cb0fc7321e5.html" click makeField "ae72134e0e8cd3e2.html" click classifyField "aa03a8b1140f5f42.html" click detect "e524d9f6725c7557.html" click resetModelMock "4ef72c19f1c89871.html"
SourceType
makeField uses
classifyField calls
detect calls
resetModelMock uses