ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - tyrchen/chinese_translation: An elixir module to translate simplified Chinese to traditional Chinese, and vice versa, based on wikipedia data
An elixir module to translate simplified Chinese to traditional Chinese, and vice versa, based on wikipedia data - tyrchen/chinese_translation
Visit Site

GitHub - tyrchen/chinese_translation: An elixir module to translate simplified Chinese to traditional Chinese, and vice versa, based on wikipedia data

GitHub - tyrchen/chinese_translation: An elixir module to translate simplified Chinese to traditional Chinese, and vice versa, based on wikipedia data

ChineseTranslation

This module provides three core functionalities related with chinese translation:

  1. Translate tranditional chinese to simplified chinese, or vise versa. It is based on wikipedia's latest translation data.
  2. Translate Chinese words to pinyin. It is based on the data collected by janx/ruby-pinyin.
  3. Slugify Chinese words (or pinyin).

This module is highly encouraged by Elixir unicode module, to read the translation meta data from file, and generate the desired functions for pattern matching.

Installation

First, add ChineseTranslation to your mix.exs dependencies:

def deps do
  [{:chinese_translation, "~> 0.2.0"}]
end

and run $ mix deps.get to get the dependencies.

Then you could compile the module by mix compile. Note that it will compile over 133, 000 functions by default (compile all the 2-char phrases and 1-char chanracters). The compilation time is around 30 minutes. So be patient! You can set environment variable MAX_WORD_LEN to tune the compilation:

$ MAX_WORD_LEN=1 mix compile   # this will compile around 40, 000 functions

If later you found the translation files has changed, you can run the following mix task to download the latest translation files:

$ mix chinese_translation

The downloaded file will be put into deps/chinese_translation/data and the whole module will be recompiled.

Usage

ChineseTranslation is very easy to use, as follows:

Translation

iex> ChineseTranslation.translate("我是中国人", :s2t)
"我是中國人"

iex> ChineseTranslation.translate("我是中國人")
"我是中国人"

Pinyin (note the polyphone)

iex> ChineseTranslation.pinyin("长工长大以后")
"cháng gōng zhǎng dà yǐ hòu"

iex> ChineseTranslation.pinyin("長工長大以後", :trad)
"cháng gōng zhǎng dà yǐ hòu"

Slugify (also the polyphone)

For slugify you could choose to use

iex> ChineseTranslation.slugify("长工长大以后")
"chang-gong-zhang-da-yi-hou"

iex> ChineseTranslation.slugify("长工长大以后", [:tone])
"chang2-gong1-zhang3-da4-yi3-hou4"

iex> ChineseTranslation.slugify("長工長大以後", [:trad, :tone])
"chang2-gong1-zhang3-da4-yi3-hou4"

iex> ChineseTranslation.slugify(" *& 我是46 848 中 ----- 国人")
"wo-shi-zhong-guo-ren"

You can explore more examples in the test.

Performance

If you installed benchfella. You could test the performance of this module in your system. Below is a general benchmark result.

$ mix bench
Settings:
  duration:      1.0 s
  mem stats:     false
  sys mem stats: false

[20:49:22] 1/8: ChineseTranslationBench.translate a character t->s
[20:49:25] 2/8: ChineseTranslationBench.translate 158-character chinese to pinyin
[20:49:28] 3/8: ChineseTranslationBench.translate a 158-character sentence s->t
[20:49:30] 4/8: ChineseTranslationBench.slugify pinyin with tone
[20:49:32] 5/8: ChineseTranslationBench.slugify pinyin
[20:49:36] 6/8: ChineseTranslationBench.translate a character s->t
[20:49:39] 7/8: ChineseTranslationBench.translate a 158-character sentence t->s
[20:49:41] 8/8: ChineseTranslationBench.slugify a short sentence
Finished in 23.25 seconds

ChineseTranslationBench.translate a character t->s:                  10000000   0.28 µs/op
ChineseTranslationBench.translate a character s->t:                  10000000   0.29 µs/op
ChineseTranslationBench.translate a 158-character sentence t->s:       100000   16.78 µs/op
ChineseTranslationBench.translate a 158-character sentence s->t:       100000   16.87 µs/op
ChineseTranslationBench.slugify pinyin with tone:                       50000   32.59 µs/op
ChineseTranslationBench.translate 158-character chinese to pinyin:      50000   45.07 µs/op
ChineseTranslationBench.slugify pinyin:                                 50000   66.96 µs/op
ChineseTranslationBench.slugify a short sentence:                       50000   69.64 µs/op

License

Copyright © 2015-2016 Tyr Chen <[email protected]>

This work is free. You can redistribute it and/or modify it under the
terms of the MIT License. See the LICENSE file for more details.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory