Overview
Thebun_nltk library provides full WebAssembly support, allowing you to run NLP operations directly in the browser with near-native performance. The WASM module is self-contained and requires no external dependencies.
Installation
First, install the package:.wasm files. Most modern bundlers like Vite, Webpack 5+, and Parcel support this out of the box.
Basic Browser Example
Initialization Patterns
From URL (Recommended for Production)
From Local Path (Node.js or Bun)
Singleton Pattern
For applications that need a single shared instance:Memory Management
Memory Pool Reuse
TheWasmNltk class uses an internal memory pool to avoid repeated allocations. Memory blocks are automatically reused across operations:
Memory Blocks
The following memory blocks are managed internally:offsets/lengths- Token offset arraysnorm_offsets/norm_lengths- Normalized token arrayssent_offsets/sent_lengths- Sentence offset arraysmetrics- Metric computation outputperceptron_*- POS tagging arrayslm_*- Language model arrayschunk_*- Chunking arrayscyk_*- Parser arraysnb_*- Naive Bayes arrays
Manual Cleanup
When you’re done with the WASM instance, calldispose() to free all allocated memory:
Best Practices
- Reuse instances: Create one instance and reuse it across operations
- Dispose when done: Call
dispose()when the instance is no longer needed - Avoid repeated initialization: Initialize once and cache the instance
- Input size limits: The input buffer has a fixed capacity (check with errors)
Error Handling
The WASM module tracks error codes internally:Input Buffer Limits
The WASM module uses a pre-allocated input buffer:Complete React Example
Browser Compatibility
- Chrome/Edge: Full support (v57+)
- Firefox: Full support (v52+)
- Safari: Full support (v11+)
- Node.js: v12+ (with
--experimental-wasm-modulesflag in older versions) - Bun: Full native support
Next Steps
- Runtime API Reference - Detailed API documentation
- Tokenization Guide - Learn about tokenization methods
- POS Tagging - Part-of-speech tagging guide
- Text Classification - Classification algorithms