Component Overview
Tokenizador consists of four main components, each with specific responsibilities:TokenAnalyzer
Application orchestrator that coordinates all other components and manages the application lifecycle
TokenizationService
Tokenization engine that integrates with tiktoken and provides fallback tokenization
UIController
UI manager that handles all DOM manipulation, user input, and visual updates
StatisticsCalculator
Computation engine that calculates metrics, costs, and context utilization
TokenAnalyzer
The TokenAnalyzer class is the main application orchestrator. It creates and coordinates all other components, handling the overall application lifecycle and user workflows.Responsibilities
- Initialize all services and controllers
- Coordinate communication between components
- Handle user actions (text change, model change, clear)
- Manage error handling and warnings
- Provide export functionality
Component Diagram
Key Methods
constructor()
constructor()
Creates instances of all three core components and calls
init().init()
init()
Initializes the application by setting up event handlers and waiting for tiktoken to load.
performRealTimeAnalysis()
performRealTimeAnalysis()
The core analysis workflow that tokenizes text, calculates statistics, and updates the UI.
compareModels()
compareModels()
Compares tokenization across multiple models.
exportResults()
exportResults()
Exports analysis results in JSON, CSV, or TXT format.
Usage Example
TokenizationService
The TokenizationService handles all tokenization logic, including integration with the tiktoken library and providing fallback tokenization when tiktoken is unavailable.Responsibilities
- Initialize and manage the tiktoken encoder
- Tokenize text using the appropriate encoding
- Create token objects with type classification
- Provide fallback tokenization when tiktoken fails
- Apply model-specific token ratios
Component Diagram
Key Methods
initializeTokenizer()
initializeTokenizer()
Asynchronously loads and initializes the tiktoken library.
tokenizeText()
tokenizeText()
The main tokenization method that processes text using tiktoken or fallback.
createTokensFromEncoding()
createTokensFromEncoding()
Creates visual token objects from tiktoken encoding with type classification.Token Types:
palabra- Regular word tokenespacio_en_blanco- Whitespacenumber- Numeric tokenpunctuation- Punctuation markpalabra_con_espacio- Word with leading spacesubword- Subword/partial word token
fallbackTokenization()
fallbackTokenization()
Provides approximate tokenization when tiktoken is unavailable.
Token Structure
Each token object returned by the service has this structure:Usage Example
UIController
The UIController manages all user interface interactions, DOM manipulation, and visual updates. It serves as the presentation layer of the application.Responsibilities
- Initialize and cache DOM element references
- Handle user input events (keyboard, mouse)
- Update statistics displays
- Render token visualizations
- Manage UI state (loading, empty, error)
- Provide visual feedback (highlighting, scrolling)
Component Diagram
Key Methods
initializeElements()
initializeElements()
Caches all DOM element references for performance.
Caching element references improves performance by avoiding repeated DOM queries.
bindEvents()
bindEvents()
Attaches event listeners to DOM elements.
updateStatistics()
updateStatistics()
Updates all statistics displays with formatted values.
updateTokenVisualization()
updateTokenVisualization()
Renders color-coded token spans in the main visualization area.
updateTokensList()
updateTokensList()
Renders the detailed token list with IDs and types.
highlightTokenInList()
highlightTokenInList()
Highlights a specific token in the list and scrolls it into view.
Event Handler Pattern
The UIController uses a callback pattern for loose coupling:Usage Example
StatisticsCalculator
The StatisticsCalculator performs all statistical computations including token counts, costs, context utilization, and efficiency metrics.Responsibilities
- Calculate comprehensive text statistics
- Estimate API costs based on token counts
- Compute context window utilization
- Generate context limit warnings
- Compare statistics across models
- Calculate efficiency metrics
Component Diagram
Key Methods
calculateStatistics()
calculateStatistics()
Computes all statistics for a given text and model.Returns:
calculateCost()
calculateCost()
Calculates the estimated API cost based on token count.
Cost calculation uses input pricing since the text being analyzed represents input to the model. Output costs are shown separately for reference.
calculateContextUtilization()
calculateContextUtilization()
Computes what percentage of the model’s context window is being used.
getContextWarning()
getContextWarning()
Returns appropriate warning messages based on context utilization.Warning Thresholds:
- 100%+: Critical - exceeds context limit
- 90-99%: Warning - approaching limit
- 75-89%: Info - high utilization
- Under 75%: No warning
compareModels()
compareModels()
Compares statistics across multiple models for the same text.
getEfficiencyMetrics()
getEfficiencyMetrics()
Calculates efficiency metrics for deeper analysis.
Statistics Object Structure
A complete statistics object contains:Usage Example
Component Interactions
Here’s a comprehensive example showing how all components work together:Summary
TokenAnalyzer
Role: Application OrchestratorKey Features:
- Coordinates all components
- Handles user workflows
- Manages application lifecycle
- Provides export functionality
TokenizationService
Role: Tokenization EngineKey Features:
- Integrates with tiktoken
- Classifies token types
- Provides fallback tokenization
- Applies model-specific ratios
UIController
Role: Presentation ManagerKey Features:
- Manages DOM interactions
- Handles user input
- Updates visualizations
- Provides visual feedback
StatisticsCalculator
Role: Computation EngineKey Features:
- Calculates comprehensive stats
- Estimates costs
- Compares models
- Generates warnings
Next Steps
Architecture Overview
Return to the architecture overview
API Reference
Explore the complete API documentation
How to Use
Learn how to use the application
Contributing
Contribute to Tokenizador