Skip to content

Go wrapper for OpenCC (Open Chinese Convert) using WebAssembly, enabling Chinese text conversion between Simplified and Traditional Chinese.

Notifications You must be signed in to change notification settings

bestnite/go-opencc

Repository files navigation

Go OpenCC

Go wrapper for OpenCC (Open Chinese Convert) using WebAssembly, enabling Chinese text conversion between Simplified and Traditional Chinese.

Features

  • Convert between Simplified and Traditional Chinese
  • Support for various regional variants (Taiwan, Hong Kong)
  • WebAssembly-based implementation for cross-platform compatibility
  • High performance with wazero runtime
  • Thread-safe operations
  • Memory efficient

Installation

go get github.com/bestnite/go-opencc

Prerequisites

To build the WebAssembly module, you need:

  1. WASI SDK: Download from wasi-sdk releases
  2. CMake: Version 3.5 or higher
  3. wasm-opt (optional): For optimization, install from binaryen

Building

  1. Clone the repository with submodules:
git clone --recursive https://github.com/bestnite/go-opencc.git
cd go-opencc
  1. Build the WebAssembly module:
./build.sh
  1. Download Go dependencies:
go mod tidy

Usage

Simple Conversion Functions

package main

import (
    "fmt"
    "log"

    "github.com/bestnite/go-opencc"
)

func main() {
    // Convert Simplified to Traditional Chinese
    result, err := opencc.ConvertS2T("简体字")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result) // Output: 簡體字

    // Convert Traditional to Simplified Chinese
    result, err = opencc.ConvertT2S("繁體字")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result) // Output: 繁体字
}

Using Converter Instance

For better performance when doing multiple conversions, use a converter instance:

package main

import (
    "fmt"
    "log"

    "github.com/bestnite/go-opencc"
)

func main() {
    // Create a converter instance
    converter, err := opencc.NewConverter("s2t.json")
    if err != nil {
        log.Fatal(err)
    }
    defer converter.Close()

    // Convert multiple texts
    texts := []string{"简体字", "测试", "转换"}
    for _, text := range texts {
        result, err := converter.Convert(text)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%s -> %s\n", text, result)
    }
}

API Reference

Functions

ConvertS2T(input string) (string, error)

Converts Simplified Chinese to Traditional Chinese.

ConvertT2S(input string) (string, error)

Converts Traditional Chinese to Simplified Chinese.

NewConverter(configFile string) (*Converter, error)

Creates a new converter instance with the specified configuration file.

Types

type Converter struct

Represents an OpenCC converter instance.

Methods:

  • Convert(input string) (string, error) - Converts text using the converter
  • Close() error - Closes the converter and releases resources

Errors

  • ErrInvalidConverter - Returned when converter creation fails
  • ErrConversionFailed - Returned when text conversion fails

Testing

Run tests:

go test

Run benchmarks:

go test -bench=.

Performance

This implementation uses WebAssembly with the wazero runtime, providing:

  • Fast startup times
  • Low memory overhead
  • Thread-safe operations
  • Cross-platform compatibility

License

This project is licensed under the Apache License 2.0 - see the OpenCC project for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run tests and ensure they pass
  6. Submit a pull request

Acknowledgments

  • OpenCC - The original OpenCC library
  • wazero - WebAssembly runtime for Go

About

Go wrapper for OpenCC (Open Chinese Convert) using WebAssembly, enabling Chinese text conversion between Simplified and Traditional Chinese.

Topics

Resources

Stars

Watchers

Forks