# worksapplications/sudachi architecture

> Architecture diagram of the GitHub repository worksapplications/sudachi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/worksapplications/sudachi
- Repository: https://github.com/worksapplications/sudachi

## Overview

##### Project type and purpose
- Sudachi is a library-and-CLI tool for Japanese morphological analysis (segmentation, POS tagging, normalization).
- It provides both a Java API (for embedding in applications) and a command-line interface (CLI).
- It uses a plugin architecture to let users extend normalization, unknown-word handling, connection cost, path rewriting, and formatting.

##### File structure
- Top-level build: Gradle scripts (`build.gradle`, `settings.gradle`, `gradlew`), GitHub workflows (CI/CD), and `LICENSE`/`README`.
- `src/main/java`: core Java packages under `com.worksap.nlp.sudachi`, with subpackages for dictionary access, dictionary building, sentence detection, plugins, and the CLI entry point (`SudachiCommandLine`).
- `src/main/resources`: configuration files (`sudachi.json`, `char.def`, `rewrite.def`, `unk.def`) and logging properties.
- `src/jmh`: JMH benchmarks for core components (dictionary lookup, builder performance).
- `benchmark/`: shell scripts and Java driver for real-world corpus benchmarking.
- `src/test`: unit and integration tests covering core, dictionary build, plugin behavior, and CLI functionality.

##### README for additional insights
- The core workflow: input text → normalization → segmentation (mode A/B/C) → POS tagging → output formatting.
- Plugin types and extension points:
– InputTextPlugin (normalization)
– OovProviderPlugin (unknown-word generation)
– ConnectionCostPlugin (override connection costs)
– PathRewritePlugin (alter best path)
– MorphemeFormatterPlugin (alter output)
- Dictionaries: binary system dictionaries (small, core, full) loaded via memory-mapped files (`MMap`) and double-array lexicon structures.

##### Essential architectural elements
- Plugin loading and extension points clearly.
- Do not overload with every class—group related classes (e.g., all dictionary-build classes under “Dictionary Builder”).
- Resource/configuration dependencies (JSON, .def files) are visible.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% I/O Layer
    subgraph "I/O Layer"
        CLI["CLI Launcher / Java API Facade"]:::io
        API["Java API Facade (Tokenizer entry)"]:::io
    end

    %% Plugin Manager
    subgraph "Plugin Manager" 
        PMBase["Plugin base / Manager"]:::plugin
        InputPluginIF["InputTextPlugin"]:::plugin
        OovPluginIF["OovProviderPlugin"]:::plugin
        CostPluginIF["EditConnectionCostPlugin"]:::plugin
        PathPluginIF["PathRewritePlugin"]:::plugin
        FormatPluginIF["MorphemeFormatterPlugin"]:::plugin
    end

    %% Normalization Chain
    subgraph "Input Text Normalization Chain" 
        Builder["InputTextBuilder"]:::plugin
        DefaultNorm["DefaultInputTextPlugin"]:::plugin
        TextNorm["TextNormalizer"]:::plugin
    end

    %% Core Pipeline
    subgraph "Core Pipeline" 
        Tokenizer["Tokenizer"]:::core
        Lattice["LatticeImpl"]:::core
        PathFinder["PathFinder (Viterbi)"]:::core
        MorphemeMod["Morpheme Model"]:::core
        MorphemeSeq["MorphemeList"]:::core
    end

    %% Dictionary Access
    subgraph "Dictionary Access Layer"
        DictFactory["DictionaryFactory"]:::core
        DictIF["DictionaryAccess"]:::core
        BinaryDict["BinaryDictionary"]:::core
        DALex["DoubleArrayLexicon"]:::core
        MMapComp["MMap"]:::core
    end

    %% Output Formatter
    subgraph "Output Formatter"
        SimpleFmt["SimpleMorphemeFormatter"]:::plugin
        SegFmt["WordSegmentationFormatter"]:::plugin
    end

    %% Dictionary Builder
    subgraph "Dictionary Builder Pipeline"
        CSVParser["CSVParser"]:::core
        DicBuilder["DicBuilder"]:::core
        ModelOut["ModelOutput"]:::core
    end

    %% Configuration & Resources
    subgraph "Configuration & Resources"
        ConfigJSON["sudachi.json"]:::config
        CharDef["char.def"]:::config
        RewriteDef["rewrite.def"]:::config
        UnkDef["unk.def"]:::config
    end

    %% Benchmark & Tests
    subgraph "Benchmark & Test Harness"
        JMHBench["DoubleArrayLookupBench"]:::test
        DBPerf["DictionaryBuilderPerformanceTest"]:::test
        ShellScript["benchmark_run.sh"]:::test
        MultiThreadBench["TokenizeMultiThread.java"]:::test
    end

    %% Data Flow
    CLI -->|"invoke"| PMBase
    API -->|"call"| PMBase
    PMBase -->|"load plugins"| InputPluginIF
    InputPluginIF --> Builder --> DefaultNorm --> TextNorm --> Tokenizer
    Tokenizer --> Lattice --> PathFinder --> MorphemeMod --> MorphemeSeq
    Tokenizer -->|"lookup"| DictFactory --> DictIF --> BinaryDict & DALex & MMapComp
    MorphemeSeq --> PMBase --> SimpleFmt --> SegFmt --> CLI

    %% Dictionary Builder Flow
    CSVParser --> DicBuilder --> ModelOut

    %% Configuration Dependencies
    ConfigJSON --> PMBase
    CharDef --> TextNorm
    RewriteDef --> DefaultNorm
    UnkDef --> PMBase

    %% Benchmark & Test Dependencies
    JMHBench --> DictIF
    DBPerf --> DicBuilder
    ShellScript --> MultiThreadBench

    %% Click Events
    click CLI "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/SudachiCommandLine.java"
    click API "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/JapaneseTokenizer.java"
    click DictFactory "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/DictionaryFactory.java"
    click DefaultNorm "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/DefaultInputTextPlugin.java"
    click TextNorm "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/TextNormalizer.java"
    click InputPluginIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/InputTextPlugin.java"
    click Builder "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/InputTextBuilder.java"
    click Tokenizer "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/Tokenizer.java"
    click Lattice "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/LatticeImpl.java"
    click PathPluginIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/PathRewritePlugin.java"
    click MorphemeMod "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/Morpheme.java"
    click MorphemeSeq "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/MorphemeList.java"
    click DictIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/DictionaryAccess.java"
    click BinaryDict "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/BinaryDictionary.java"
    click DALex "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/DoubleArrayLexicon.java"
    click MMapComp "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/MMap.java"
    click PMBase "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/Plugin.java"
    click OovPluginIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/OovProviderPlugin.java"
    click CostPluginIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/EditConnectionCostPlugin.java"
    click FormatPluginIF "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/MorphemeFormatterPlugin.java"
    click SimpleFmt "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/SimpleMorphemeFormatter.java"
    click SegFmt "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/WordSegmentationFormatter.java"
    click CSVParser "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/CSVParser.java"
    click DicBuilder "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/build/DicBuilder.java"
    click ModelOut "https://github.com/worksapplications/sudachi/blob/develop/src/main/java/com/worksap/nlp/sudachi/dictionary/build/ModelOutput.java"
    click ConfigJSON "https://github.com/worksapplications/sudachi/blob/develop/src/main/resources/sudachi.json"
    click CharDef "https://github.com/worksapplications/sudachi/blob/develop/src/main/resources/char.def"
    click RewriteDef "https://github.com/worksapplications/sudachi/blob/develop/src/main/resources/rewrite.def"
    click UnkDef "https://github.com/worksapplications/sudachi/blob/develop/src/main/resources/unk.def"
    click JMHBench "https://github.com/worksapplications/sudachi/blob/develop/src/jmh/java/com/worksap/nlp/sudachi/dictionary/DoubleArrayLookupBench.java"
    click DBPerf "https://github.com/worksapplications/sudachi/blob/develop/src/jmh/java/com/worksap/nlp/sudachi/dictionary/DictionaryBuilderPerformanceTest.java"
    click ShellScript "https://github.com/worksapplications/sudachi/blob/develop/benchmark/benchmark_run.sh"
    click MultiThreadBench "https://github.com/worksapplications/sudachi/blob/develop/benchmark/src/com/worksap/nlp/sudachi/benchmark/TokenizeMultiThread.java"

    %% Styles
    classDef core fill:#E0F7FA,stroke:#006064
    classDef plugin fill:#E8F5E9,stroke:#1B5E20
    classDef io fill:#FFF3E0,stroke:#E65100
    classDef config fill:#ECEFF1,stroke:#37474F
    classDef test fill:#F3E5F5,stroke:#6A1B9A
```
