sudachi.rs

repository·develop·Indexed 19 days ago

https://github.com/worksapplications/sudachi.rs

A high-performance Rust implementation of the Sudachi Japanese morphological analyzer. It provides tokenization, normalization, and part-of-speech tagging. The project includes a Rust-based CLI (sudachi-cli) and Python bindings via the sudachipy package. It supports multi-granular tokenization modes (A, B, and C) and multiple dictionary editions (small, core, and full), with capabilities for creating custom user dictionaries.

Tokens
21.3K
Snippets
76
Records
101
Agent score
65%

What's inside sudachi.rs

  1. Check platform compatibility for SudachiPy wheels

    develop

    SudachiPy provides pre-built Python wheels for several platforms. Use these wheels for faster installation and performance (especially on Linux) compared to building from source.

    Supported Architectures:

    • Linux: amd64 (x86_64) and aarch64 (arm64). Linux wheels are built with Profile-Guided Optimizations (PGO) for better performance and use manylinux containers for broad distribution compatibility.
    • Windows: x86_64. Note that Windows builds do not use PGO.
    • macOS: universal2 (Intel and ARM), x86_64, and arm64.

    Support Levels:

    • Linux: Fully supported; bugs are actively fixed.
    • Windows: Best-effort support; bug fixes may take time.
    • macOS: Not officially supported; builds are provided on a best-effort basis and are tested less rigorously. Contributions for macOS support are welcomed.
  2. Handling errors and panics in Sudachi.rs

    develop

    When using Sudachi.rs as a library, you should expect that any issues arising from user-provided text will be returned as Errors rather than causing a panic or crash. The library is designed to be safe against arbitrary user-inputted text.

    Important distinction:

    • User Input: Should always return an Error. Crashes caused by user input are considered bugs.
    • Invalid Dictionaries: Sudachi.rs may panic or exhibit undefined behavior if provided with an invalid binary dictionary. Binary dictionaries are treated as trusted components.
    • Incorrect Usage: The library may panic if used incorrectly by the developer (e.g., violating the API contract), but it should not panic based on specific input values.
  3. Security considerations for using custom dictionaries

    develop

    Sudachi.rs follows a specific threat model regarding security:

    1. User Text: It is safe to process any user-inputted text; crashes related to text analysis are treated as bugs.
    2. Binary Dictionaries: Maliciously crafted binary dictionaries are not considered a security threat that the library protects against. While binary dictionaries are mapped read-only (preventing memory modification), they could theoretically allow data leaking.

    Best Practice for Unsafe Dictionaries: If you must use a dictionary provided by an untrusted user, do not use a pre-compiled binary dictionary. Instead, accept the dictionary in its CSV form and compile it into a binary dictionary yourself using the Sudachi.rs compilation tools. This ensures the data is validated during the compilation process.

  4. Memory reuse caution with Morpheme.split

    develop

    The sudachipy.Morpheme.split method also supports memory reuse, but it introduces a dependency on the parent MorphemeList. When you call .split() on a Morpheme, the resulting MorphemeList contains references to the data in the parent MorphemeList.

    Warning: If you reuse the parent MorphemeList as an out parameter in a subsequent tokenize() call, the data in the original list is replaced, and any MorphemeList objects derived from it (via .split()) will be invalidated. Accessing them after the parent has been reused may raise an exception.

    ml1 = tok.tokenize("外国人参政権")
    subl1 = ml1[0].split(SplitMode.A)
    
    # This next line invalidates subl1 because ml1 is being reused
    tok.tokenize("something", out=ml1)
    
    # Accessing subl1 now is unsafe and may raise an exception
    subl1[0].surface()
  5. Configure Split Modes in SudachiPy

    develop

    SudachiPy supports multi-granular tokenization via the SplitMode enum. This allows you to control how text is segmented.

    • SplitMode.C (Default): Least granular (longest words).
    • SplitMode.B: Intermediate granularity.
    • SplitMode.A: Most granular (shortest words).

    Example: For the input "国家公務員":

    • Mode C: ['国家公務員']
    • Mode B: ['国家', '公務員']
    • Mode A: ['国家', '公務', '員']
    from sudachipy import Dictionary, SplitMode
    
    tokenizer_obj = Dictionary().create()
    
    # Mode C (Default)
    print([m.surface() for m in tokenizer_obj.tokenize("国家公務員", SplitMode.C)])
    
    # Mode B
    print([m.surface() for m in tokenizer_obj.tokenize("国家公務員", SplitMode.B)])
    
    # Mode A
    print([m.surface() for m in tokenizer_obj.tokenize("国家公務員", SplitMode.A)])
  6. Use the sudachi CLI for morphological analysis

    develop

    The sudachi command performs Japanese morphological analysis. By default, it outputs tab-separated values containing the surface form, part-of-speech (comma-separated), and normalized form.

    Common Tasks

    Split text using different modes Use the -m or --mode flag to change the splitting granularity. Modes are A (short), B (middle), or C (Named Entity, default).

    $ echo 選挙管理委員会 | sudachi --mode A

    Output only surface forms (Wakati) Use the -w or --wakati flag to output only the surface forms separated by spaces.

    $ echo "外国人参政権" | sudachi -m A -w

    Get detailed analysis Use the -a or --all flag to include dictionary form, reading, dictionary ID, and synonym group ID in the output.

    $ echo "外国人参政権" | sudachi -a

    Process a file Pass a file path as an argument to process its contents.

    $ sudachi --wakati lemon.txt
    $ echo "高輪ゲートウェイ駅" | sudachi
    高輪ゲートウェイ駅  名詞,固有名詞,一般,*,*,*    高輪ゲートウェイ駅
    EOS
  7. Download Sudachi Dictionaries

    develop

    Sudachi requires a dictionary to function. You can either download a zip file (small, core, or full) from SudachiDict, extract it, and place the system_*.dic file in a known location (the default config expects resources/system.dic), or use the provided script to automate the process.

    To download the latest core dictionary:

    ./fetch_dictionary.sh

    To download a specific version and type (e.g., small):

    ./fetch_dictionary.sh 20241021 small
  8. Install SudachiPy and a Dictionary

    develop

    To use SudachiPy, you must install both the library and a dictionary package. The sudachidict_core edition is the default.

    # Install the library
    pip install sudachipy
    
    # Install the default core dictionary
    pip install sudachidict_core
    pip install sudachipy sudachidict_core