Skip to content

About

A high-performance, standalone Word Cloud generation library written entirely in pure Mojo. Designed from the ground up to take full advantage of Mojo's systems programming capabilities: strong static typing, ownership and value semantics, compile-time generics, spatial grid acceleration, and zero Python runtime dependencies.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Mojo Word Cloud

A high-performance, standalone Word Cloud generation library written entirely in pure Mojo.

Designed from the ground up to take full advantage of Mojo's systems programming capabilities: strong static typing, ownership and value semantics, compile-time generics, spatial grid acceleration, and zero Python runtime dependencies.


Features

  • Mojo: Zero Python imports, zero Python interoperability (Python.import_module), and zero C/Python runtime dependencies.
  • High-Throughput Tokenization:
    • Punctuation stripping, whitespace normalization, and contraction preservation.
    • Unicode-aware codepoint processing.
    • Configurable case sensitivity and word length boundaries (min_word_length, max_word_length).
  • Comprehensive Stopwords:
    • Native standard English stopwords set built using std.collections.Set.
    • Full support for custom stopwords (extending or replacing default collections).
  • Word Frequency Analysis:
    • Frequency counting, case normalization, minimum frequency thresholds, and rank-based sorting.
    • Direct support for precomputed frequency dictionaries (Dict[String, Int]).
  • Elliptical Archimedean Spiral Placement:
    • Aspect-ratio-aware placement engine that fills rectangular canvases naturally.
    • Configurable word rotation preferences and padding.
    • Typographic character-metric estimation with balanced power scaling (sqrt, log, linear).
  • Fast 2D Spatial Grid Collision Index:
    • $O(1)$ local neighbor lookups instead of $O(N)$ brute-force comparisons, achieving over 10 million collision queries per second.
  • Customizable Color System:
    • Predefined palettes (classic, viridis, cool, warm, pastel, monochrome).
    • Hex parsing and RGB linear interpolation.
    • User-defined custom color lists.
  • Compliant SVG Exporter:
    • Mojo SVG 1.1 generation with automatic XML entity escaping (&, <, >, ", ').
    • Native file export without external rasterizers.

Performance Benchmarks

Measured on Mojo 1.2.0 (Ubuntu-22.04 / x86_64):

Stage Throughput / Latency
Tokenizer 1,277,250 tokens/sec (~5,600 tokens in 4.38 ms)
Spatial Grid Index 4,756,084 queries/sec (1,000 queries in 0.21 ms)
Full HD Layout Placement 14.78 ms (74/75 words placed, 98.7% success rate)
SVG Serialization 0.093 ms (~6.0 KB SVG)

Project Structure

word-cloud/
├── pixi.toml                         # Project tasks and Mojo dependencies (pinned to ==1.2.0)
├── pixi.lock                         # Pinned dependency lockfile
├── LICENSE                           # MIT License
├── README.md                         # Documentation and API guide
├── CHANGELOG.md                      # Release notes & version history
├── COMPATIBILITY.md                  # Mojo versioning, API stability & upgrade guide
├── CONTRIBUTING.md                   # Development workflow
│
├── src/
│   └── wordcloud/
│       ├── __init__.mojo             # Public API exports
│       ├── version.mojo              # LibraryInfo and SemVer metadata
│       ├── types.mojo                # Core types (Rect, WordFrequency, PlacedWord)
│       ├── config.mojo               # WordCloudConfig struct and validation
│       ├── tokenizer.mojo            # Pure Mojo text tokenizer
│       ├── stopwords.mojo            # Built-in English stopwords and manager
│       ├── frequency.mojo            # Frequency counter and rank sorter
│       ├── metrics.mojo              # Typographic glyph metrics and font sizing
│       ├── collision.mojo            # SpatialGridIndex and AABB collision
│       ├── color.mojo                # Color, hex parser, and Palette
│       ├── layout.mojo               # Archimedean spiral placement engine
│       ├── wordcloud.mojo            # High-level WordCloud and WordCloudResult
│       └── exporters/
│           ├── __init__.mojo         # Exporter exports
│           └── svg.mojo              # Compliant SVG 1.1 exporter
│
├── tests/
│   ├── test_version.mojo             # 2 unit tests for LibraryInfo and version queries
│   ├── test_config.mojo              # 8 unit tests for config and types
│   ├── test_tokenizer.mojo           # 7 unit tests for tokenization
│   ├── test_stopwords.mojo           # 4 unit tests for stopwords
│   ├── test_frequency.mojo           # 4 unit tests for frequency analysis
│   ├── test_collision.mojo           # 5 unit tests for collision detection
│   ├── test_layout.mojo              # 5 unit tests for layout, determinism & high density
│   ├── test_color.mojo               # 4 unit tests for colors and palettes
│   ├── test_svg.mojo                 # 3 unit tests for SVG export and escaping
│   └── test_all.mojo                 # 3 end-to-end integration tests
│
├── examples/
│   ├── basic.mojo                    # End-to-end text word cloud
│   ├── custom_colors.mojo            # Custom neon palette on dark canvas
│   ├── custom_stopwords.mojo         # Custom domain stopwords
│   └── from_frequencies.mojo         # Generation from precomputed Dict
│
└── benchmarks/
    └── benchmark_generation.mojo     # Throughput and latency benchmarks

Quick Start

1. Requirements

  • Pixi package manager (curl -fsSL https://pixi.sh/install.sh | bash)
  • Mojo >= 1.2.0 (managed automatically by Pixi via pixi.toml)

2. Basic Usage

from wordcloud import WordCloud

def main() raises:
    # 1. Initialize WordCloud with custom settings
    var cloud = WordCloud(
        width=1000,
        height=700,
        background_color="#ffffff",
        min_font_size=12.0,
        max_font_size=80.0,
        padding=4.0,
        random_seed=42,
    )

    # 2. Input raw text
    var text = """
        Mojo combines the developer friendliness of Python with the speed of C.
        Mojo delivers high performance, strong static typing, ownership semantics,
        and hardware acceleration for systems programming.
    """

    # 3. Generate layout
    var result = cloud.generate(text)
    print("Placed words:", result.word_count())

    # 4. Save rendered SVG
    result.save_svg("wordcloud.svg")

3. Generation from Precomputed Frequencies

from wordcloud import WordCloud, Palette
from std.collections import Dict

def main() raises:
    var cloud = WordCloud(
        width=1100,
        height=700,
        background_color="#18181b",
        font_family="monospace",
    )
    cloud.set_palette(Palette.viridis())

    var freqs = Dict[String, Int]()
    freqs["Mojo"] = 150
    freqs["Rust"] = 100
    freqs["Python"] = 80
    freqs["C++"] = 75
    freqs["Go"] = 60

    var result = cloud.generate_from_frequencies(freqs)
    result.save_svg("languages.svg")

Running Tasks

All tasks are defined in pixi.toml:

# Run all 42 unit and integration tests
pixi run test

# Run performance benchmarks
pixi run benchmark

# Run examples
pixi run example-basic
pixi run example-colors
pixi run example-stopwords
pixi run example-frequencies

License

This project is licensed under the MIT License.

About

A high-performance, standalone Word Cloud generation library written entirely in pure Mojo. Designed from the ground up to take full advantage of Mojo's systems programming capabilities: strong static typing, ownership and value semantics, compile-time generics, spatial grid acceleration, and zero Python runtime dependencies.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages