Catalyst NLP Library

repository·master·Indexed 21 days ago

https://github.com/curiosity-ai/catalyst

A high-performance, pure-C# Natural Language Processing (NLP) library designed for speed and cross-platform compatibility. Inspired by spaCy, it provides features including tokenization via FastTokenizer, Named Entity Recognition (NER) using Spotter, PatternSpotter, and AveragePerceptronEntityRecognizer, part-of-speech tagging with AveragePerceptronTagger, and embedding training with FastText. It supports native multi-threading, lazy evaluation for batch processing, and modular language support via NuGet packages.

Tokens
6.3K
Snippets
28
Records
29
Agent score
72%

What's inside Catalyst

  1. Use ImmutableDocument for memory efficiency

    master

    The ImmutableDocument class provides an immutable, memory-efficient representation of a document. Use this when you need to ensure document data remains unchanged after processing. You can convert between mutable and immutable states using ToImmutable() and ToMutable().

    // Convert to ImmutableDocument
    ImmutableDocument immutableDoc = doc.ToImmutable();
    
    // Convert back to mutable Document
    Document mutableDoc = immutableDoc.ToMutable();
  2. Use Neuralyzers to correct model mistakes

    master

    A Neuralyzer is a special component that can be added to a pipeline via nlp.UseNeuralyzer(neuralyzer). It allows you to define patterns to correct errors made by other models, such as adding or forgetting entities based on specific token patterns.

    var neuralyzer = new Neuralyzer(Language.English, 0, "fixes");
    
    // Teach the neuralyzer to add an 'Organization' entity when it sees 'Amazon'
    neuralyzer.TeachAddPattern("Organization", "Amazon", mp => mp.Add(new PatternUnit(P.Single().WithToken("Amazon"))));
    
    nlp.UseNeuralyzer(neuralyzer);
  3. Core Concepts of Catalyst

    master

    Catalyst is built around five primary abstractions that define how text is represented and processed:

    1. Document: The primary container for text. It holds the original raw text and all metadata generated during NLP processing (such as tokens, spans, and entities).
    2. Span: A segment of a Document, typically representing a sentence.
    3. Token: The smallest unit of text (e.g., a word or punctuation mark) found within a Span.
    4. Pipeline: A sequence of processing steps (models) applied to a Document. A pipeline typically orchestrates tasks like tokenization, sentence detection, and POS tagging.
    5. Language: An enum used to specify the language of a document or a model, ensuring the correct processing logic is applied.
    // Example of creating a Document
    var doc = new Document("The quick brown fox jumps over the lazy dog", Language.English);
    
    // Example of using a Pipeline to process a Document
    var nlp = await Pipeline.ForAsync(Language.English);
    nlp.ProcessSingle(doc);
  4. Quickstart: Process text with Catalyst

    master

    To use Catalyst, install the Catalyst NuGet package and the specific language package (e.g., Catalyst.Models.English). You must register the language before creating a pipeline. Catalyst uses a Storage mechanism to lazy-load models from disk or an online repository.

    1. Register the language.
    2. Configure Storage.Current (e.g., using DiskStorage).
    3. Create a pipeline using Pipeline.ForAsync(Language).
    4. Create a Document and process it using nlp.ProcessSingle(doc) or nlp.Process(docs) for batches.
    Catalyst.Models.English.Register(); // Register the language
    
    Storage.Current = new DiskStorage("catalyst-models");
    var nlp = await Pipeline.ForAsync(Language.English);
    var doc = new Document("The quick brown fox jumps over the lazy dog", Language.English);
    
    nlp.ProcessSingle(doc);
    Console.WriteLine(doc.ToJson());
  5. Quickstart: Process text with English support

    master

    Follow these steps to set up a complete NLP pipeline for English text:

    1. Install Catalyst.Models.English via NuGet.
    2. Register the language models using Catalyst.Models.English.Register().
    3. Configure Storage.Current to a DiskStorage instance.
    4. Initialize a Pipeline using Pipeline.ForAsync(Language.English).
    5. Create a Document and process it using nlp.ProcessSingle(doc).
    using Catalyst;
    using Catalyst.Models;
    using Mosaik.Core;
    
    // 1. Register the English language models
    Catalyst.Models.English.Register();
    
    // 2. Configure storage for lazy-loading models
    Storage.Current = new DiskStorage("catalyst-models");
    
    // 3. Create a pipeline for English
    var nlp = await Pipeline.ForAsync(Language.English);
    
    // 4. Create and process a document
    var doc = new Document("Hello, world!", Language.English);
    nlp.ProcessSingle(doc);
    
    // 5. Access the results
    Console.WriteLine(doc.ToJson());
  6. Create a Pipeline for a specific language

    master

    The Pipeline class is the central orchestrator in Catalyst. To create a pipeline, you must first install the language-specific NuGet package and register its models.

    Use Pipeline.ForAsync(Language) to create a default pipeline, which typically includes a tokenizer, a sentence detector, and a POS tagger.

    # Example: Adding English language support
    dotnet add package Catalyst.Models.English
    // Register the language models
    Catalyst.Models.English.Register();
    
    // Create the default pipeline
    var nlp = await Pipeline.ForAsync(Language.English);
  7. Create a Document in Catalyst

    master

    The Document class is the primary data structure in Catalyst. It represents the text being processed and serves as a container for all linguistic annotations generated by the NLP pipeline. You can instantiate a new document by providing the raw text and its corresponding Language.

    using Catalyst;
    using Mosaik.Core;
    
    var doc = new Document("The quick brown fox jumps over the lazy dog", Language.English);
  8. Iterate through captured entities in a document

    master

    After processing a document with an NER model, you can access entities by iterating through the document spans or using LINQ to flatten the entity collection.

    // Using foreach
    foreach (var span in doc)
    {
        foreach (var entity in span.GetEntities())
        {
            Console.WriteLine($"Entity: {entity.Value} [{entity.EntityType.Type}]");
        }
    }
    
    // Using LINQ
    var entities = doc.SelectMany(span => span.GetEntities());
    foreach(var entity in entities)
    {
        Console.WriteLine($"Entity: {entity.Value} [{entity.EntityType.Type}]");
    }
  9. Customize a Pipeline by adding or removing processes

    master

    Pipelines are flexible and can be modified by adding or removing components that implement IProcess.

    • Adding Processes: Use nlp.Add(process) to add a model. Catalyst automatically maintains a logical execution order: Normalizers $\rightarrow$ Tokenizers $\rightarrow$ Sentence Detectors $\rightarrow$ Taggers $\rightarrow$ Others (e.g., Entity Recognizers).
    • Removing Processes: Use nlp.RemoveAll(predicate) to remove specific models based on a condition.
    // Adding an Entity Recognizer
    var nlp = await Pipeline.ForAsync(Language.English);
    nlp.Add(await AveragePerceptronEntityRecognizer.FromStoreAsync(Language.English, Version.Latest, "WikiNER"));
    
    // Removing all taggers from the pipeline
    nlp.RemoveAll(p => p is ITagger);
  10. Create a high-performance Tokenizer-only Pipeline

    master

    If you only require tokenization (and optionally sentence detection), use Pipeline.TokenizerForAsync. This is faster than the default pipeline because it skips the part-of-speech (POS) tagging step. You can also use Pipeline.ForAsync and explicitly set tagger: false to achieve a similar result.

    // Option 1: Using the specialized tokenizer method
    var nlp = await Pipeline.TokenizerForAsync(Language.English, sentenceDetector: true);
    
    // Option 2: Disabling the tagger in the default pipeline
    var nlp = await Pipeline.ForAsync(Language.English, tagger: false);
  11. Store and load Pipelines using binary packing

    master

    You can persist a configured pipeline and all its associated models into a single binary file using PackTo and reload it using Pipeline.LoadFromPackedAsync.

    // Store the pipeline to a file
    using(var f = File.OpenWrite("my-pipeline.bin"))
    {
        nlp.PackTo(f);
    }
    
    // Load the pipeline from a file
    using(var f = File.OpenRead("my-pipeline.bin"))
    {
        var nlp2 = await Pipeline.LoadFromPackedAsync(f);
    }
  12. Install Catalyst and Language Models

    master

    To use Catalyst, install the core Catalyst NuGet package. Because Catalyst is modular, language-specific data and models are provided in separate NuGet packages. You must install the package for the language you intend to use and register it in your code before processing.

    # Install the core package (assumed)
    dotnet add package Catalyst
    
    # Install English language support
    dotnet add package Catalyst.Models.English