opencc4j

repository·master·Indexed 20 days ago

https://github.com/houbb/opencc4j

A Java library for converting Chinese characters between Simplified and Traditional forms. It supports regional variations for Taiwan, Hong Kong, and Japanese Shinjitai, and provides word-level conversion through customizable segmentation strategies and data maps. Key utilities include ZhConverterUtil, ZhTwConverterUtil, ZhHkConverterUtil, and ZhJpConverterUtil for character conversion and identification.

Tokens
2.9K
Snippets
10
Records
15
Agent score
19%

What's inside opencc4j

  1. Overview of Opencc4j Converter Utilities

    master

    Opencc4j provides several utility classes for different Chinese character conversion needs. All utility classes follow a consistent API design for easy memorization.

    Core utility classes:

    • ZhConverterUtil: Basic Simplified/Traditional Chinese conversion.
    • ZhTwConverterUtil: Conversion between Mainland Chinese and Taiwan regions.
    • ZhHkConverterUtil: Conversion between Mainland Chinese and Hong Kong regions.
    • ZhJpConverterUtil: Conversion between Mainland Chinese and Japanese Shinjitai (new character forms).
  2. Install Opencc4j via Maven

    master

    To use Opencc4j in your Java project, add the following dependency to your pom.xml file:

    <dependency>
        <groupId>com.github.houbb</groupId>
        <artifactId>opencc4j</artifactId>
        <version>1.14.0</version>
    </dependency>
  3. Extend conversion data with IDataMap

    master

    To add custom conversion rules (like specific regional phrases or character mappings), you can extend the existing data maps. The recommended way is to extend AbstractDataMapExtra, which allows you to provide additional phrase and character mappings on top of a base IDataMap.

    Key methods to override in AbstractDataMapExtra:

    • tsPhraseExtra(): Additional Traditional $\rightarrow$ Simplified phrases.
    • tsCharExtra(): Additional Traditional $\rightarrow$ Simplified characters.
    • stPhraseExtra(): Additional Simplified $\rightarrow$ Traditional phrases.
    • stCharExtra(): Additional Simplified $\rightarrow$ Traditional characters.

    After creating your custom data map, register it using ZhConvertBootstrap.dataMap(dataMap) and use a compatible segmenter like DataMapFastForwardSegment.

    // 1. Define the extra data map
    final IDataMap dataMap = new MyFooDataMapExtra();
    // 2. Specify the segmentation strategy using the new data map
    final Segment segment = new DataMapFastForwardSegment(dataMap);
    
    // 3. Initialize the bootstrap
    ZhConvertBootstrap bs = ZhConvertBootstrap.newInstance()
            .dataMap(dataMap)
            .segment(segment)
            .init();
    
    // 4. Use it
    String result = bs.toTraditional("人生自是有情痴");
  4. Use a custom Segment with ZhConvertBootstrap

    master

    Starting from version 1.1.0, you can use the ZhConvertBootstrap class to integrate your custom Segment implementation. ZhConvertBootstrap supports a fluent API, making it easy to chain segmentation and conversion tasks.

    // Assuming MySegment is your custom implementation
    final String result = ZhConvertBootstrap.newInstance()
        .segment(new MySegment())
        .toTraditional(original);
  5. Configure segmentation and data maps

    master

    Opencc4j uses a bootstrap pattern to configure segmentation (tokenization) and data maps. This is useful if you want to change how words are split or use specific regional data.

    Default Configuration:

    ZhConvertBootstrap.newInstance()
                    .segment(Segments.defaults())
                    .dataMap(DataMaps.defaults())
                    .init();

    Taiwan Configuration:

    ZhConvertBootstrap.newInstance()
                    .segment(Segments.twFastForward())
                    .dataMap(DataMaps.taiwan())
                    .init();
    ZhConvertBootstrap.newInstance()
                    .segment(Segments.defaults())
                    .dataMap(DataMaps.defaults()).init();
  6. Convert Chinese characters using ZhConverterUtil

    master

    Use ZhConverterUtil for standard Simplified and Traditional Chinese conversions.

    To Simplified:

    String original = "生命不息,奮鬥不止";
    String result = ZhConverterUtil.toSimple(original);
    // result: "生命不息,奋斗不止"

    To Traditional:

    String original = "生命不息,奋斗不止";
    String result = ZhConverterUtil.toTraditional(original);
    // result: "生命不息,奮鬥不止"
    String original = "生命不息,奮鬥不止";
    String result = ZhConverterUtil.toSimple(original);
    Assert.assertEquals("生命不息,奋斗不止", result);
  7. Get all possible Simplified/Traditional forms for a single character

    master

    When a single character has multiple possible mappings, these methods return them as a list.

    To Traditional list:

    // Returns [幹, 乾, 干]
    List<String> result = ZhConverterUtil.toTraditional('干');

    To Simplified list:

    // Returns [测]
    List<String> result = ZhConverterUtil.toSimple('測');
    Assert.assertEquals("[幹, 乾, 干]", ZhConverterUtil.toTraditional('干').toString());
  8. Convert between Mainland Chinese and Taiwan/Hong Kong/Japan

    master

    Use specialized utilities for regional conversions:

    Taiwan (ZhTwConverterUtil):

    ZhTwConverterUtil.toTraditional("使用互联网"); // "使用網際網路"
    ZhTwConverterUtil.toSimple("使用網際網路"); // "使用互联网"

    Hong Kong (ZhHkConverterUtil):

    ZhHkConverterUtil.toTraditional("千家万户"); // "千家萬户"
    ZhHkConverterUtil.toSimple("千家萬户"); // "千家万户"

    Japan (ZhJpConverterUtil): Converts via the path: Simplified $\rightarrow$ Standard Traditional $\rightarrow$ Japanese Shinjitai.

    ZhJpConverterUtil.toTraditional("我在日本学习音乐"); // "我在日本学習音楽"
    ZhJpConverterUtil.toSimple("我在日本学習音楽"); // "我在日本学习音乐"
  9. Extract lists of Simplified or Traditional words from a string

    master

    You can retrieve a list of words/characters identified as Simplified or Traditional within a string. This uses the configured segmentation (tokenization) strategy.

    Get Simplified list:

    final String original = "生命不息奋斗不止";
    final List<String> resultList = ZhConverterUtil.simpleList(original);
    // result: [生, 命, 不, 息, 奋斗, 不, 止]

    Get Traditional list:

    final String original = "生命不息奮鬥不止";
    final List<String> resultList = ZhConverterUtil.traditionalList(original);
    // result: [生, 命, 不, 息, 奮, 鬥, 不, 止]
    final String original = "生命不息奋斗不止";
    final List<String> resultList = ZhConverterUtil.simpleList(original);
    Assert.assertEquals("[生, 命, 不, 息, 奋斗, 不, 止]", resultList.toString());
  10. Implement a custom segmentation strategy

    master

    You can implement your own segmentation logic by implementing the Segment interface. This allows you to integrate custom tokenizers into the Opencc4j conversion pipeline.

    public interface Segment {
        /**
         * Segment the original text
         * @param original The original string
         * @return A list of segmented strings
         */
        List<String> seg(final String original);
    }
    public interface Segment {
        List<String> seg(final String original);
    }
  11. Identify Simplified, Traditional, or Chinese characters

    master

    The ZhConverterUtil class provides methods to check the character type of strings or individual characters.

    Check if Simplified:

    • isSimple(String): Returns true if the entire string is Simplified.
    • isSimple(char): Returns true if the character is Simplified.
    • containsSimple(String): Returns true if the string contains any Simplified characters.

    Check if Traditional:

    • isTraditional(String): Returns true if the entire string is Traditional.
    • isTraditional(char): Returns true if the character is Traditional.
    • containsTraditional(String): Returns true if the string contains any Traditional characters.

    Check if Chinese:

    • isChinese(String): Returns true if the entire string is Chinese.
    • isChinese(char): Returns true if the character is Chinese.
    • containsChinese(char): Returns true if the string contains Chinese characters.
  12. Implement a custom Segment interface

    master

    If the default segmentation methods do not suit your specific business logic, you can implement the Segment interface to provide your own segmentation implementation. This allows you to control how text is split before conversion.

    import com.github.houbb.opencc4j.support.segment.Segment;
    import java.util.List;
    
    /**
     * Implement this interface to provide custom segmentation logic.
     */
    public class MySegment implements Segment {
    
        @Override
        public List<String> seg(final String original) {
            // Your custom segmentation logic here
            return // ... list of segmented strings;
        }
    }