opencc-python Documentation

repository·master·Indexed 20 days ago

https://github.com/yichen0831/opencc-python

A pure Python implementation of OpenCC (Open Chinese Convert) for dictionary-based conversion between Simplified and Traditional Chinese variants, including Hong Kong and Taiwan standards. Provides an OpenCC Python class for programmatic conversion, a command-line interface (CLI), and helper scripts for merging and reversing dictionary files.

Tokens
1K
Snippets
5
Records
7
Agent score
19%

What's inside opencc-python

  1. Reverse dictionary keys and values using reverse.py

    master

    The reverse.py script is used to reverse the keys and values within dictionary files. This requires Python 3.

    Usage Pattern: python3 reverse.py <input_file1> <input_file2> ... <output_file1> <output_file2> ...

    Example: To reverse the contents of TWVariants.txt, TWPhrases.txt, and HKVariants.txt into new files with the Rev suffix:

    python3 reverse.py 'TWVariants.txt' 'TWPhrases.txt' 'HKVariants.txt' 'TWVariantsRev.txt' 'TWPhrasesRev.txt' 'HKVariantsRev.txt'
  2. Merge multiple dictionary files using merge.py

    master

    The merge.py script allows you to merge multiple dictionary files into a single consolidated file. This requires Python 3.

    Usage Pattern: python3 merge.py <input_file1> <input_file2> ... <output_file>

    Example: To merge TWPhrasesIT.txt, TWPhrasesName.txt, and TWPhrasesOther.txt into a single file named TWPhrases.txt:

    python3 merge.py 'TWPhrasesIT.txt' 'TWPhrasesName.txt' 'TWPhrasesOther.txt' 'TWPhrases.txt'
  3. Use the OpenCC class in Python

    master

    To use OpenCC in your code, import the OpenCC class. Initialize it with a conversion configuration string (e.g., 's2t' for Simplified to Traditional). You can then use the .convert() method to transform text. You can also change the conversion type dynamically using .set_conversion(config).

    from opencc import OpenCC
    
    # Initialize for Simplified Chinese to Traditional Chinese
    cc = OpenCC('s2t')
    
    # Optionally change conversion type later
    # cc.set_conversion('s2tw')
    
    to_convert = '开放中文转换'
    converted = cc.convert(to_convert)
  4. Reference OpenCC CLI arguments

    master

    The following arguments are available for the python -m opencc command:

    • -h, --help: Show this help message and exit.
    • -i <file>, --input <file>: Read original text from <file>. Defaults to STDIN.
    • -o <file>, --output <file>: Write converted text to <file>. Defaults to STDOUT.
    • -c <conversion>, --config <conversion>: The conversion configuration (e.g., s2t).
    • --in-enc <encoding>: Encoding for input. Defaults to UTF-8.
    • --out-enc <encoding>: Encoding for output. Defaults to UTF-8.
  5. Available OpenCC conversion configurations

    master

    When initializing OpenCC(config) or using the CLI -c flag, use one of the following conversion strings:

    ConfigDescription
    hk2sTraditional Chinese (Hong Kong standard) to Simplified Chinese
    s2hkSimplified Chinese to Traditional Chinese (Hong Kong standard)
    s2tSimplified Chinese to Traditional Chinese
    s2twSimplified Chinese to Traditional Chinese (Taiwan standard)
    s2twpSimplified Chinese to Traditional Chinese (Taiwan standard, with phrases)
    t2hkTraditional Chinese to Traditional Chinese (Hong Kong standard)
    t2sTraditional Chinese to Simplified Chinese
    t2twTraditional Chinese to Traditional Chinese (Taiwan standard)
    tw2sTraditional Chinese (Taiwan standard) to Simplified Chinese
    tw2spTraditional Chinese (Taiwan standard) to Simplified Chinese (with phrases)
  6. Use the OpenCC CLI

    master

    You can run OpenCC from the command line using python -m opencc. This is useful for converting entire files.

    # Example: Convert a UTF-8 encoded file from Simplified to Traditional
    python -m opencc -c s2t -i my_simplified_input_file.txt -o my_traditional_output_file.txt