Table of Contents

Class NamedEntityRecognition

Namespace
LMKit.TextAnalysis
Assembly
LM-Kit.NET.dll

A class for performing Named Entity Recognition (NER) on input content a Language Model (LM). This class identifies and extracts all occurrences of specified entity types (built-in or custom) from the provided content, returning each entity verbatim along with its type and optional positional information.

public class NamedEntityRecognition
Inheritance
NamedEntityRecognition
Inherited Members

Examples

Example: Basic named entity recognition

using LMKit.Model;
using LMKit.TextAnalysis;
using System;

// Load the language model
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");

// Create the NER engine with default entity types
NamedEntityRecognition ner = new NamedEntityRecognition(model);

// Extract entities from text
string text = "Apple Inc. announced that CEO Tim Cook will visit Paris on January 15, 2025.";
var entities = ner.Recognize(text);

foreach (var entity in entities)
{
    Console.WriteLine($"[{entity.Type}] {entity.Value}");
}
// Output:
// [Organization] Apple Inc.
// [Person] Tim Cook
// [Location] Paris
// [Date] January 15, 2025

Example: Custom entity definitions

using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;

LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");

// Define custom entity types
var definitions = new List<EntityDefinition>
{
    new EntityDefinition(NamedEntityType.Person),
    new EntityDefinition(NamedEntityType.Organization),
    new EntityDefinition(NamedEntityType.Custom, "ProductCode", "Product codes like SKU-12345 or ITEM-ABC")
};

NamedEntityRecognition ner = new NamedEntityRecognition(model, definitions);

string invoice = "Contact John Smith at Acme Corp regarding SKU-78901 and ITEM-XYZ.";
var entities = ner.Recognize(invoice);

foreach (var entity in entities)
{
    Console.WriteLine($"[{entity.Type}] {entity.Value}");
}

Remarks

Use this class to extract entities such as persons, organizations, locations, dates, etc., by leveraging an underlying LMKit LM model. You can customize which entity types to extract by setting the EntityDefinitions property.

Built-in Entity Types

  • Person - Names of individuals
  • Organization - Companies, institutions, agencies
  • Location - Places, addresses, geographic regions
  • Date - Calendar dates and time references
  • Money - Currency amounts and financial values
  • Percent - Percentage values
  • Product - Product names and brands
  • Event - Named events (conferences, holidays)
  • Custom - User-defined entity types

Key Features

  • Extract from text, images, and documents
  • Configurable entity types via EntityDefinitions
  • Custom entity type support
  • Optional OCR for image-based extraction
  • Confidence scoring

Constructors

NamedEntityRecognition(LM, bool)

Initializes a new instance of NamedEntityRecognition with a default set of entity definitions (all built-in NamedEntityRecognition.NamedEntityType values except Custom and, by default, Other).

NamedEntityRecognition(LM, List<EntityDefinition>)

Initializes a new instance of NamedEntityRecognition with a custom list of entity definitions.

Properties

Confidence

Gets the confidence score of the last recognition operation.

EntityDefinitions

Gets or sets the list of entity types (built‐in and/or custom) that this recognizer will extract. Must contain at least one NamedEntityRecognition.EntityDefinition; otherwise, an ArgumentNullException is thrown.

Guidance

Gets or sets semantic guidance for the recognition process.

MaxContextLength

Gets or sets the maximum context length (in tokens) used during named-entity recognition.

Model

The underlying LM instance used for performing extraction.

OcrEngine

Gets or sets an optional OcrEngine used to perform traditional OCR on raster content during named‐entity recognition.

PreferredInferenceModality

Gets or sets the preferred modality for inference. This determines whether text, image, or both modalities are used when processing input. Defaults to Multimodal.

Methods

Recognize(Attachment, CancellationToken)

Synchronously recognizes named entities in the given image attachment.

Recognize(string, CancellationToken)

Synchronously recognizes named entities in the given content.

RecognizeAsync(Attachment, CancellationToken)

Asynchronously recognizes named entities in the given image attachment.

RecognizeAsync(string, CancellationToken)

Asynchronously recognizes named entities in the given content.