Options
All
  • Public
  • Public/Protected
  • All
Menu

EF Classification

Allows text strings to be classified in groups of meanings, intents, etc. Can be used as an interface for understanding a users input.

import classify, {
  setClassifier,
  TargetIntent,
  TargetMeaning,
  NoMatchersClassificationError,
  isITargetIntent,
  isITargetMeaning,
} from '@ef-carbon/classification';

// Globally set a classifier backend
setClassifier(myAwesomeClassfier);

async function demo() {
  try {
    for await (const { target, confidence } of classify('a cute kitten', [
      // An intent determines if the input is *about* something
      new TargetIntent('animals'),
      // A meaning determines if the input is *similar* to something
      new TargetMeaning('cat'),
    ])) {
      if (isITargetIntent(target) && (confidence > 0.5)) {
        console.log(`Probably talking about '${target.intent}'`);
      } else if (isITargetMeaning(target) && (confidence > 0.9)) {
        console.log(`Very likely a '${target.meaning}'`);
      }
    };
  } catch (error) {
    if (error instanceof NoMatchersClassificationError) {
      console.warn(`No matches for ${error.input}`);
    } else {
      throw error;
    }
  }
}

demo();