fugashi Documentation

repository·main·Indexed 19 days ago

https://github.com/polm/fugashi

A high-performance Cython wrapper for MeCab, a Japanese morphological analyzer. It provides a pythonic interface for Japanese tokenization, optimized for UniDic dictionaries via the Tagger class, and supports arbitrary dictionaries through GenericTagger and create_feature_wrapper. Includes a command-line tool and an experimental build_dictionary utility for user dictionaries.

Tokens
1.2K
Snippets
7
Records
8
Agent score
68%

What's inside fugashi

  1. Install fugashi and dictionaries

    main

    fugashi is a Cython wrapper for MeCab. It provides wheels for Linux, OSX (Intel), and Win64. You must install a dictionary to use it.

    For a lightweight setup, install unidic-lite. For full Unidic support (recommended for serious processing), you must install the unidic extra and run the download command.

    Note: If you are on a platform without wheels (e.g., Alpine Linux, PowerPC, or Windows 32-bit), you must install MeCab manually from source first.

    # Install with the lightweight dictionary
    pip install 'fugashi[unidic-lite]'
    
    # Install with the full UniDic (requires extra download step)
    pip install 'fugashi[unidic]'
    python -m unidic download
  2. Basic usage with Tagger and UniDic

    main

    Use the Tagger class for standard Japanese morphological analysis with UniDic. When using UniDic, the word.feature attribute is returned as a named tuple, allowing you to access properties like lemma and pos directly.

    from fugashi import Tagger
    
    tagger = Tagger('-Owakati')
    text = "麩菓子は、麩を主材料とした日本の菓子。"
    tagger.parse(text)
    # => '麩 菓子 は 、 麩 を 主材 料 と し た 日本 の 菓子 。'
    
    for word in tagger(text):
        print(word, word.feature.lemma, word.pos, sep='\t')
        # "feature" is the Unidic feature data as a named tuple
  3. Use GenericTagger with arbitrary dictionaries

    main

    If you are using a dictionary other than UniDic, use GenericTagger. In this mode, features are not named tuples but must be accessed via integer field numbers.

    from fugashi import GenericTagger
    
    tagger = GenericTagger()
    text = 'something'
    
    # parse can be used as normal
    tagger.parse(text)
    
    # features from the dictionary can be accessed by field numbers
    for word in tagger(text):
        print(word.surface, word.feature[0])
  4. Create a custom feature wrapper for GenericTagger

    main

    To use non-UniDic dictionaries while still benefiting from named tuple access, use create_feature_wrapper. This allows you to map specific feature field names to the dictionary's output.

    from fugashi import GenericTagger, create_feature_wrapper
    
    # Define the wrapper with the expected field names
    CustomFeatures = create_feature_wrapper('CustomFeatures', 'alpha beta gamma')
    tagger = GenericTagger(wrapper=CustomFeatures)
    
    text = 'example text'
    for word in tagger.parseToNodeList(text):
        print(word.surface, word.feature.alpha)
  5. Access fugashi public API via the top-level package

    main

    The fugashi package exports its primary API directly at the top level. You can import the tokenizer, dictionary, and related classes directly from fugashi instead of the internal .fugashi module.

    import fugashi
    
    # Accessing exported symbols directly
    tokenizer = fugashi.Tokenizer()
  6. Build a user dictionary with build_dictionary

    main

    Fugashi provides an experimental wrapper for MeCab's user dictionary building command via build_dictionary. By default, this utility uses utf8 for both input (-f) and output (-t) encoding.

    from fugashi import build_dictionary
    
    # The command string follows the pattern: -f <input_enc> -t <output_enc> <args>
    build_dictionary("-f utf8 -t utf8 my_user_dict.dic")
  7. Use fugashi from the command line

    main

    Fugashi can be used as a command-line tool similar to the MeCab binary. It treats each line of stdin as a single sentence. You can pass tagger arguments directly via the command line. The tool attempts to use GenericTagger first, falling back to Tagger if necessary.

    # Example: piping text into fugashi
    echo "麩菓子は、麩を主原料とした日本の菓子。" | fugashi
  8. Get fugashi dictionary information

    main
    You can inspect the configuration and metadata of the dictionary currently being used by the tagger. This includes the version, size, charset, and filename. This is useful for verifying which dictionary (e.g., UniDic) is active.