Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 2.21 KB

File metadata and controls

83 lines (60 loc) · 2.21 KB

Unified NLP Service

Index Terms: NLP, wink-nlp, singleton, tokenization, analysis

1. Overview

1.1 Introduction (Beginner-Friendly)

The Unified NLP Service is like the bot's language center—it understands human text. Previously, the bot had multiple copies of this "brain" running (wasting memory). Now there's one shared instance that saves 700MB+ of memory.

It handles:

  • Tokenization: Breaking text into words
  • Entity recognition: Finding names, places, things
  • Sentiment analysis: Understanding emotion
  • Intent detection: What does the user want?

1.2 Technical Summary

Singleton NLP service using wink-nlp with LRU caching for performance.

Location: src/nlp/unifiedNLPService.ts

2. Architecture

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#3d5a80', 'primaryTextColor': '#e0e0e0', 'lineColor': '#6c757d', 'secondaryColor': '#293241', 'background': '#1a1a2e'}}}%%
flowchart TB
    subgraph Service["Unified NLP Service (Singleton)"]
        NLP[wink-nlp Instance]
        EC[Entity Cache<br/>1000 entries]
        SC[Sentiment Cache<br/>1000 entries]
        AC[Analysis Cache<br/>500 entries]
    end

    M[Message] --> Service
    Service --> E[Entities]
    Service --> S[Sentiment]
    Service --> I[Intent]
    Service --> T[Topics]
Loading

3. Key Features

3.1 Singleton Pattern

class UnifiedNLPService {
  private static instance: UnifiedNLPService;
  private nlp: WinkNLP;

  private constructor() {
    this.nlp = winkNLP(model);
  }

  static getInstance(): UnifiedNLPService {
    if (!UnifiedNLPService.instance) {
      UnifiedNLPService.instance = new UnifiedNLPService();
    }
    return UnifiedNLPService.instance;
  }
}

3.2 Caching Strategy

Cache Size TTL Purpose
Entity 1000 15min NER results
Sentiment 1000 15min Emotion scores
Analysis 500 10min Full analysis

3.3 Memory Savings

Before After Savings
12 instances × 60MB 1 instance × 60MB 700MB+

See Also