Documentation
¶
Overview ¶
Package gocc is a Go implementation of OpenCC (Open Chinese Convert), converting between Traditional Chinese, Simplified Chinese and regional variants (Taiwan, Hong Kong, Japanese Shinjitai, seal script).
It follows the OpenCC 1.4 conversion model: normalization, optional maximum-match segmentation, and a chain of dictionary conversions whose groups match with "short_circuit" or "union" policy. The OpenCC configs and dictionaries are embedded (build with -tags ne to leave the dictionaries out and use LoadDict); dictionaries are stored in cedar double-array tries and shared between converters.
Example ¶
package main
import (
"fmt"
gocc "github.com/go-ego/gocc"
)
func main() {
s2t, err := gocc.New("s2t")
if err != nil {
panic(err)
}
fmt.Println(s2t.Convert("自然语言处理是人工智能领域中的一个重要方向。"))
tw := gocc.MustNew("s2twp")
fmt.Println(tw.Convert("鼠标和内存"))
}
Output: 自然語言處理是人工智能領域中的一個重要方向。 滑鼠和記憶體
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Conversions ¶
func Conversions() []string
Conversions lists the built-in configurations, such as "s2t" and "tw2sp".
Types ¶
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
Converter converts text with one OpenCC configuration. It is safe for concurrent use.
func New ¶
New returns the converter of a built-in configuration such as "s2t" (Simplified to Traditional) or "t2s"; see Conversions. A ".json" suffix is accepted.
func NewFromConfig ¶
NewFromConfig builds a converter from an OpenCC JSON config whose dictionary files are read from dicts. Text dictionaries are used for every dictionary type: "X.ocd2" and "X.ocd" are read as "X.txt". The LoadDict directories are searched after dicts.
func NewFromFile ¶
NewFromFile builds a converter from an OpenCC JSON config file, like OpenCC's SimpleConverter. As in OpenCC, dictionary files are searched in the config's directory, then the LoadDict directories, then among the built-in dictionaries.
func (*Converter) AppendConvert ¶
AppendConvert appends the conversion of s to dst and returns the result. dst must not overlap s.
func (*Converter) JP ¶
JP reports whether the converter outputs Japanese Kanji, as "t2jp" (Shinjitai) and "jp2t" (Kyūjitai) do.
func (*Converter) Name ¶
Name returns the configuration name, e.g. "Simplified Chinese to Traditional Chinese (OpenCC Standard)".
func (*Converter) Simplified ¶
Simplified reports whether the converter outputs Simplified Chinese, as "t2s" and "tw2sp" do. Like Traditional and JP, it reads the target from the config name, "... to Simplified Chinese ...".
func (*Converter) Traditional ¶
Traditional reports whether the converter outputs Traditional Chinese, as "s2t", "s2twp" and "seal2t" do.
type Option ¶
type Option func(*options)
Option configures a Converter.
func LoadDict ¶
LoadDict searches dir for dictionary files before the built-in dictionaries, like OpenCC's --path or gse's LoadDict. A file found in dir replaces the built-in one of the same name. Later paths are searched after earlier ones.
func WithTofuRisk ¶
func WithTofuRisk() Option
WithTofuRisk includes dictionaries marked "may_output_tofu": rare characters that common fonts may not render (OpenCC's --include-tofu-risk-dictionaries).
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gocc
command
Command gocc converts Chinese text between Traditional, Simplified and regional variants, like the opencc command.
|
Command gocc converts Chinese text between Traditional, Simplified and regional variants, like the opencc command. |
|
internal
|
|
|
cmd/gendict
command
Command gendict refreshes the embedded OpenCC data: it optionally syncs configs and source dictionaries from an OpenCC checkout, then writes the generated dictionaries into data/dictionary.
|
Command gendict refreshes the embedded OpenCC data: it optionally syncs configs and source dictionaries from an OpenCC checkout, then writes the generated dictionaries into data/dictionary. |
|
dictfile
Package dictfile reads and writes OpenCC text dictionaries.
|
Package dictfile reads and writes OpenCC text dictionaries. |
|
dictgen
Package dictgen derives the generated OpenCC dictionaries from their sources, mirroring the scripts in OpenCC's data/scripts directory.
|
Package dictgen derives the generated OpenCC dictionaries from their sources, mirroring the scripts in OpenCC's data/scripts directory. |