THULAC (THU Lexical Analyzer for Chinese)

repository·master·Indexed 24 days ago

https://github.com/thunlp/thulac-python

A high-efficiency Chinese lexical analysis toolkit developed by Tsinghua University. It provides high-accuracy word segmentation and Part-of-Speech (POS) tagging via a Python API and command-line interface. Features include support for user dictionaries, Traditional to Simplified Chinese conversion, and a high-performance fast interface using libthulac.so.

Tokens
1.1K
Snippets
3
Records
8
Agent score
35%

What's inside THULAC

  1. Install THULAC from GitHub

    master

    You can also install THULAC by downloading it from GitHub. Note that if you use this method, you must manually download the model files and place them in the thulac directory for the library to function.

    1. Download the files from GitHub.
    2. Place the thulac folder in your working directory.
    3. Download models from thulac.thunlp.org and place them inside the thulac directory.
  2. Configure the thulac() constructor

    master

    The thulac() constructor allows you to customize the analyzer's behavior using the following parameters:

    ParameterDefaultDescription
    user_dictNonePath to a UTF8 encoded user dictionary (one word per line). Words in this dictionary are tagged with uw.
    model_pathmodels/Directory where the model files are located.
    T2SFalseWhether to convert Traditional Chinese to Simplified Chinese.
    seg_onlyFalseIf True, only performs word segmentation and skips POS tagging.
    filtFalseIf True, uses a filter to remove meaningless words (e.g., "可以").
    deli'_'The separator between a word and its POS tag.
    rm_spaceFalseWhether to remove spaces from the original text before segmentation.
  3. Use the THULAC Python API for word segmentation

    master

    You can initialize the THULAC analyzer and perform word segmentation (and POS tagging) on strings or files.

    Basic Usage (Segmentation and POS Tagging)

    By default, THULAC performs both word segmentation and Part-of-Speech (POS) tagging.

    import thulac
    
    thu1 = thulac.thulac()  # Initialize in default mode
    text = thu1.cut("我爱北京天安门", text=True)  # Segment a sentence
    print(text)

    Segmentation Only

    To perform only word segmentation without POS tagging, set seg_only=True during initialization.

    thu1 = thulac.thulac(seg_only=True)
    # Segment a file and save to another
    thu1.cut_f("input.txt", "output.txt")
    import thulac
    
    thu1 = thulac.thulac()  # 默认模式
    text = thu1.cut("我爱北京天安门", text=True)  #进行一句话分词
    print(text)
  4. Use the THULAC fast interface

    master

    For high-performance requirements, a .so version is available. After downloading and placing libthulac.so in the same directory as your models folder, you can use the fast_ prefixed versions of the standard functions. These functions accept the same parameters as the standard API.

    • cut $\rightarrow$ fast_cut
    • cut_f $\rightarrow$ fast_cut_f
  5. Use the cut() and cut_f() methods

    master

    cut(text, text=False)

    Segments a single string.

    • text (bool): If True, returns the original text along with the segments. If False (default), returns a 2D array of [[word, tag], ...] pairs. In seg_only mode, the tag will be an empty string.

    cut_f(input_file, output_file)

    Segments the content of an input file and writes the results to an output file.

  6. Understand THULAC POS tags

    master

    THULAC uses the following Part-of-Speech (POS) tags:

    TagMeaning
    nNoun
    npPerson name
    nsPlace name
    niOrganization name
    nzOther proper noun
    mNumber
    qQuantifier
    mqQuantity word
    tTime word
    fDirectional word
    sLocation word
    vVerb
    aAdjective
    dAdverb
    hPre-component
    kPost-component
    iIdiom
    jAbbreviation
    rPronoun
    cConjunction
    pPreposition
    uParticle
    yModal particle
    eExclamation
    oOnomatopoeia
    gMorpheme
    wPunctuation
    xOther
  7. Run THULAC via Command Line Interface

    master

    If you installed THULAC via pip, you can run it directly from the terminal.

    Standard Mode (Segmentation + POS Tagging)

    Read from input.txt and output results to output.txt:

    python -m thulac input.txt output.txt

    Segmentation Only Mode

    Add the seg_only flag to skip POS tagging:

    python -m thulac input.txt output.txt seg_only
    python -m thulac input.txt output.txt
    # or for segmentation only:
    python -m thulac input.txt output.txt seg_only