jieba: Chinese Text Segmentation for Python

repository·master·Indexed 13 days ago

https://github.com/fxsjy/jieba

A highly efficient Chinese text segmentation (tokenization) library for Python. It supports precise, full, and search engine segmentation modes, as well as a deep learning-based Paddle mode. Key features include custom dictionary management, keyword extraction using TF-IDF and TextRank, POS tagging via the ictclas standard, and parallel processing for large datasets.

Tokens
3.1K
Snippets
15
Records
16
Agent score
49%

What's inside jieba

  1. Enable Paddle mode for deep learning segmentation

    master

    To use the paddle mode (which uses the PaddlePaddle deep learning framework for segmentation and POS tagging), you must first install paddlepaddle-tiny and then call jieba.enable_paddle().

    Requirements:

    • jieba version 0.40 or higher.
    • paddlepaddle-tiny==1.6.1.
    pip install paddlepaddle-tiny==1.6.1
    import jieba
    jieba.enable_paddle()
    # Now you can use use_paddle=True in jieba.cut()
  2. Fix incorrect segmentation by increasing word frequency

    master

    If a specific phrase (e.g., '台中') is being incorrectly split into individual characters (e.g., '台 中'), it is because the probability of the phrase occurring is lower than the product of its parts ($P( ext{phrase}) < P( ext{part1}) imes P( ext{part2})$).

    You can force the segmenter to treat the phrase as a single word by increasing its frequency using jieba.add_word() or jieba.suggest_freq() with the True flag.

    # Method 1: Add the word to the dictionary
    jieba.add_word('台中')
    
    # Method 2: Suggest a higher frequency for the phrase
    jieba.suggest_freq('台中', True)
  3. Install jieba

    master

    You can install jieba using several methods:

    Automatic installation (Recommended):

    pip install jieba

    Semi-automatic installation: Download the package from PyPI, extract it, and run:

    python setup.py install

    Manual installation: Place the jieba directory directly into your current working directory or your Python site-packages directory.

    pip install jieba
  4. Fix incorrect segmentation by decreasing word frequency

    master

    If a phrase is being incorrectly joined together (e.g., '今天天气' instead of '今天 天气'), you can force the segmenter to split it by decreasing its frequency or removing it.

    Use jieba.suggest_freq() with the True flag to suggest a split, or use jieba.del_word() to remove the phrase from the dictionary entirely.

    # Method 1: Suggest splitting the phrase into specific parts
    jieba.suggest_freq(('今天', '天气'), True)
    
    # Method 2: Delete the phrase from the dictionary
    jieba.del_word('今天天气')
  5. Perform Part-of-Speech (POS) tagging

    master

    Use jieba.posseg to segment text and identify the Part-of-Speech for each word. The tagging follows the ictclas standard.

    API:

    • jieba.posseg.cut(string, ...): Returns an iterator of (word, flag) tuples.
    • Supports use_paddle=True for deep learning-based tagging.

    Common Tags:

    • n: Noun, nr: Person, ns: Place, nt: Organization, v: Verb, a: Adjective, r: Pronoun, etc.
    • Paddle mode also includes uppercase tags like PER (Person), LOC (Location), and ORG (Organization).
    import jieba.posseg as pseg
    
    words = pseg.cut("我爱北京天安门")
    for word, flag in words:
        print(f"{word} {flag}")
    # Output example: 我 r, 爱 v, 北京 ns, 天安门 ns
  6. Enable parallel segmentation

    master

    For large datasets, you can speed up segmentation by using multiple processes. This is based on Python's multiprocessing module and is not supported on Windows.

    API:

    • jieba.enable_parallel(processes): Enables parallel mode with the specified number of processes.
    • jieba.disable_parallel(): Disables parallel mode.
    import jieba
    
    jieba.enable_parallel(4)  # Use 4 processes
    # ... perform segmentation ...
    jieba.disable_parallel()
  7. Add and manage a custom dictionary

    master

    You can improve segmentation accuracy by providing your own dictionary.

    Loading a User Dictionary

    Use jieba.load_userdict(file_name) to load a file. The file must be UTF-8 encoded. Each line should follow this format: word [frequency] [tag] (space-separated). Frequency and tag are optional.

    Example format:

    创新办 3 i
    云计算 5
    台中

    Dynamic Dictionary Modification

    • jieba.add_word(word, freq=None, tag=None): Add a word dynamically.
    • jieba.del_word(word): Remove a word.
    • jieba.suggest_freq(segment, tune=True): Adjust the frequency of a word to force or prevent it from being segmented in a specific way.
    import jieba
    
    # Load a file
    jieba.load_userdict("my_dict.txt")
    
    # Dynamic updates
    jieba.add_word("new_word")
    jieba.suggest_freq("台中", True)
  8. Get word positions with tokenize()

    master

    The jieba.tokenize() method returns the start and end indices of each word in the original string.

    API:

    • jieba.tokenize(string, mode='default')
    • mode='default': Standard segmentation.
    • mode='search': Search engine mode segmentation.

    Returns an iterable of tuples: (word, start_index, end_index).

    import jieba
    
    # Default mode
    result = jieba.tokenize(u'永和服装饰品有限公司')
    for word, start, end in result:
        print(f"word {word}\t\t start: {start} \t\t end:{end}")
  9. Add and modify a custom dictionary

    master

    You can improve accuracy by providing your own dictionary or modifying the existing one dynamically.

    Load a dictionary file

    Use jieba.load_userdict(file_name) where file_name is a path or a file-like object. The format should be: word [frequency] [POS tag] (one per line, space-separated).

    Example format:

    创新办 3 i
    云计算 5
    台中

    Dynamic modification

    • jieba.add_word(word, freq=None, tag=None): Add a word to the dictionary.
    • jieba.del_word(word): Remove a word.
    • jieba.suggest_freq(segment, tune=True): Adjust the frequency of a single word to force or prevent specific segmentation.
    import jieba
    
    # Load a file
    jieba.load_userdict("my_dict.txt")
    
    # Add a word
    jieba.add_word("云计算", freq=5, tag="n")
    
    # Adjust frequency to fix segmentation
    jieba.suggest_freq(('中', '将'), True)
    # Or for a single word
    jieba.suggest_freq('台中', True)
  10. Perform Chinese text segmentation

    master

    Jieba provides several modes for word segmentation:

    1. Precise Mode (Default): Attempts to cut the sentence into the most accurate segments. Best for text analysis.
    2. Full Mode: Scans all possible words in the sentence. Very fast but cannot resolve ambiguity.
    3. Search Engine Mode: Based on precise mode but further segments long words to improve recall. Ideal for building search engine inverted indices.
    4. Paddle Mode: Uses a deep learning model (Bi-GRU) via PaddlePaddle.

    API Methods:

    • jieba.cut(string, cut_all=False, HMM=True, use_paddle=False): Returns an iterable generator of words.
    • jieba.cut_for_search(string, HMM=True): Returns an iterable generator optimized for search engines.
    • jieba.lcut(string, ...): Returns a list of words.
    • jieba.lcut_for_search(string, ...): Returns a list of words optimized for search engines.
    import jieba
    
    # Precise Mode (Default)
    seg_list = jieba.cut("我来到北京清华大学")
    print("/".join(seg_list))
    
    # Full Mode
    seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
    print("/".join(seg_list))
    
    # Search Engine Mode
    seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所")
    print("/".join(seg_list))
    
    # Paddle Mode (requires jieba.enable_paddle())
    seg_list = jieba.cut("我来到北京清华大学", use_paddle=True)
    print("/".join(seg_list))
  11. Extract keywords using TF-IDF or TextRank

    master

    Jieba provides two algorithms for keyword extraction via the jieba.analyse module.

    TF-IDF Algorithm

    jieba.analyse.extract_tags(sentence, topK=20, withWeight=False, allowPOS=())

    • topK: Number of keywords to return (default 20).
    • withWeight: If True, returns weights along with keywords.
    • allowPOS: Filter keywords by specific Part-of-Speech tags.

    Customization:

    • jieba.analyse.set_idf_path(file_name): Set a custom IDF frequency corpus.
    • jieba.analyse.set_stop_words(file_name): Set a custom stop words corpus.

    TextRank Algorithm

    jieba.analyse.textrank(sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))

    • Uses a graph-based approach (PageRank) on word co-occurrence within a sliding window.
    • Note: allowPOS has default filters for specific POS tags.
    import jieba.analyse
    
    text = "这是一个用于测试关键词提取的句子"
    
    # TF-IDF
    keywords = jieba.analyse.extract_tags(text, topK=5)
    
    # TextRank
    keywords_tr = jieba.analyse.textrank(text, topK=5)