opencc-rb 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 36111587c88ebb5bad5627397aec98d5a5100863ba55c6da2decf5c6740e0008
4
+ data.tar.gz: 58d1302032b5612973a48d13df89ca86bdfefb619e6b70261fa587ac137c5680
5
+ SHA512:
6
+ metadata.gz: a589ef598d17e5506844398be9c91e0b67053cbcfa5bad8731a926a0c016f849ca5ebac405921c98ed08b76d29704b4033f37394e477e3c3f79104d8c429826e
7
+ data.tar.gz: 89dc71b414e3515009836a0ee5f3940289cb794a610a6ecca0ed3a5b4580a94e122d0db71c63d2f60f83e46d69b22520cc203eb54370ba05479792b3a958a89a
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+ .DS_Store
15
+ .keep
@@ -0,0 +1,11 @@
1
+ #!/bin/bash
2
+
3
+ if [ $TRAVIS_OS_NAME = 'osx' ]; then
4
+ brew install opencc
5
+ else
6
+ git clone https://github.com/BYVoid/OpenCC
7
+ cd OpenCC
8
+ git checkout ver.1.1.2
9
+ make PREFIX=/usr/local
10
+ sudo make install
11
+ fi
data/.travis.yml ADDED
@@ -0,0 +1,26 @@
1
+ ---
2
+ language: ruby
3
+ os:
4
+ - linux
5
+ - osx
6
+ dist: focal
7
+ compiler:
8
+ - clang
9
+ - gcc
10
+ cache: bundler
11
+ rvm:
12
+ - 2.7.0
13
+ - 2.6.5
14
+ - 2.5.5
15
+ addons:
16
+ apt:
17
+ update: true
18
+ packages:
19
+ - cmake
20
+ - doxygen
21
+ before_install:
22
+ - gem install bundler -v 2.1.4
23
+ install:
24
+ - ./.travis/install.sh
25
+ - bundle install
26
+ script: bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in opencc.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "rake-compiler"
8
+ gem "minitest", "~> 5.14", ">= 5.14.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ opencc-rb (1.0.5)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.4)
10
+ rake (13.0.3)
11
+ rake-compiler (1.1.1)
12
+ rake
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ minitest (~> 5.14, >= 5.14.3)
19
+ opencc-rb!
20
+ rake (~> 13.0)
21
+ rake-compiler
22
+
23
+ BUNDLED WITH
24
+ 2.2.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 zsj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # OpenCC [![Build Status](https://travis-ci.org/songjiz/opencc-rb.svg?branch=master)](https://travis-ci.org/songjiz/opencc-rb)
2
+
3
+ ## Description
4
+
5
+ This gem is for conversions between Traditional Chinese, Simplified Chinese and Japanese Kanji (Shinjitai). It's builded on [OpenCC](https://github.com/BYVoid/OpenCC).
6
+
7
+ ## Installation
8
+
9
+ You have to install OpenCC firstly.
10
+
11
+ MacOS:
12
+
13
+ ```shell
14
+ brew install opencc
15
+ ```
16
+
17
+ Debian/Ubuntu:
18
+
19
+ ```shell
20
+ sudo apt install libopencc-dev
21
+ ```
22
+ Archlinux:
23
+
24
+ ```shell
25
+ sudo pacman -Sy opencc
26
+ ```
27
+
28
+ Fedora:
29
+
30
+ ```shell
31
+ sudo yum install opencc-devel
32
+ ```
33
+
34
+ Or install from source code:
35
+
36
+ ```shell
37
+ cd /usr/src
38
+ git clone https://github.com/BYVoid/OpenCC
39
+ cd OpenCC
40
+ git checkout ver.1.1.1
41
+ make PREFIX=/usr/local
42
+ sudo make install
43
+ ```
44
+
45
+ Then add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'opencc-rb'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ ```shell
54
+ bundle install
55
+ ```
56
+
57
+ Or install it yourself as:
58
+
59
+ ```shell
60
+ gem install opencc-rb
61
+ ```
62
+
63
+ ## Usage
64
+
65
+ ```ruby
66
+ require "opencc"
67
+
68
+ # The converter will automatically be closed when the block terminates.
69
+ OpenCC.with(:s2t) do |ct|
70
+ ct.convert("汉字") # => "漢字"
71
+ ct.convert("曾经有一份真诚的爱情放在我面前,我没有珍惜,等我失去的时候我才后悔莫及。人事间最痛苦的事莫过于此。如果上天能够给我一个再来一次得机会,我会对那个女孩子说三个字,我爱你。如果非要在这份爱上加个期限,我希望是,一万年。") # => "曾經有一份真誠的愛情放在我面前,我沒有珍惜,等我失去的時候我才後悔莫及。人事間最痛苦的事莫過於此。如果上天能夠給我一個再來一次得機會,我會對那個女孩子說三個字,我愛你。如果非要在這份愛上加個期限,我希望是,一萬年。"
72
+ end
73
+
74
+ OpenCC.with(:s2t) do
75
+ convert("汉字") # => "漢字"
76
+ convert("曾经有一份真诚的爱情放在我面前,我没有珍惜,等我失去的时候我才后悔莫及。人事间最痛苦的事莫过于此。如果上天能够给我一个再来一次得机会,我会对那个女孩子说三个字,我爱你。如果非要在这份爱上加个期限,我希望是,一万年。") # => "曾經有一份真誠的愛情放在我面前,我沒有珍惜,等我失去的時候我才後悔莫及。人事間最痛苦的事莫過於此。如果上天能夠給我一個再來一次得機會,我會對那個女孩子說三個字,我愛你。如果非要在這份愛上加個期限,我希望是,一萬年。"
77
+ end
78
+
79
+ # Or
80
+ OpenCC.s2t("汉字") # => "漢字"
81
+ OpenCC.t2s("漢字") # => "汉字"
82
+ OpenCC.s2tw('着装污染虚伪发泄棱柱群众里面') # => "著裝汙染虛偽發洩稜柱群眾裡面"
83
+ OpenCC.tw2s("著裝汙染虛偽發洩稜柱群眾裡面") # => "着装污染虚伪发泄棱柱群众里面"
84
+ OpenCC.s2hk("虚伪叹息") # => "虛偽嘆息"
85
+ OpenCC.hk2s("虛偽嘆息") # => "虚伪叹息"
86
+ OpenCC.s2twp("海内存知己") # => "海內存知己"
87
+ OpenCC.tw2sp("海內存知己") # => "海内存知己"
88
+ OpenCC.hk2t("想到自己一緊張就口吃,我就沒胃口吃飯") # => "想到自己一緊張就口吃,我就沒胃口喫飯"
89
+ OpenCC.t2hk("想到自己一緊張就口吃,我就沒胃口喫飯") # => "想到自己一緊張就口吃,我就沒胃口吃飯"
90
+ OpenCC.t2jp("藝術 缺航 飲料罐") # => "芸術 欠航 飲料缶"
91
+ OpenCC.jp2t("芸術 欠航 飲料缶") # => "藝術 缺航 飲料罐"
92
+ OpenCC.tw2t("想到自己一緊張就口吃,我就沒胃口吃飯") # => "想到自己一緊張就口吃,我就沒胃口喫飯"
93
+
94
+ # Or
95
+ converter = OpenCC[:s2t]
96
+ converter = OpenCC.new(:s2t)
97
+ converter = OpenCC::Converter.new(:s2t)
98
+ converter.convert("汉字") # => '漢字'
99
+ converter.close # => true
100
+ converter.closed? # => true
101
+ converter.close # => false
102
+ converter.convert("汉字") # => nil
103
+ ```
104
+
105
+ ## Development
106
+
107
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
108
+
109
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/songjiz/opencc-rb.
114
+
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task :build => :compile
13
+
14
+ Rake::ExtensionTask.new("opencc") do |ext|
15
+ ext.lib_dir = "lib/opencc"
16
+ end
17
+
18
+ task :default => [:clobber, :compile, :test]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opencc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ require "mkmf"
2
+
3
+ def missing!(lib)
4
+ if RUBY_PLATFORM =~ /mingw|mswin/
5
+ abort "-----\n#{lib} is missing. Check your installation of OpenCC, and try again.\n-----"
6
+ elsif RUBY_PLATFORM =~ /darwin/
7
+ abort "-----\n#{lib} is missing. You may need to 'brew install opencc', and try again.\n-----"
8
+ else
9
+ abort "-----\n#{lib} is missing. You may need to 'sudo apt-get install libopencc-dev', 'sudo pacman -Sy opencc' or 'sudo yum install opencc-devel' and try again.\n-----"
10
+ end
11
+ end
12
+
13
+ missing!('opencc') if !have_library('opencc')
14
+ missing!('opencc.h') if !have_header('opencc.h')
15
+
16
+ have_func 'opencc_open'
17
+ have_func 'opencc_convert_utf8'
18
+ have_func 'opencc_convert_utf8_free'
19
+ have_func 'opencc_close'
20
+
21
+ create_makefile 'opencc/opencc'
@@ -0,0 +1,49 @@
1
+ #include "opencc.h"
2
+
3
+ VALUE rb_mOpenCC;
4
+ VALUE rb_mOpenCC_Mixin;
5
+
6
+ static VALUE rb_opencc_open(VALUE self, VALUE rb_cfg) {
7
+ const char *cfg = OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD;
8
+ opencc_t ptr;
9
+
10
+ if (TYPE(rb_cfg) == T_STRING && RSTRING_LEN(rb_cfg) > 0) {
11
+ cfg = RSTRING_PTR(rb_cfg);
12
+ }
13
+
14
+ ptr = opencc_open(cfg);
15
+
16
+ // On error the return value will be (opencc_t) -1.
17
+ if (ptr == (opencc_t) - 1) {
18
+ return Qnil;
19
+ } else {
20
+ return LONG2FIX((long)ptr);
21
+ }
22
+ }
23
+
24
+ static VALUE rb_opencc_convert(VALUE self, VALUE rb_opencc, VALUE rb_str) {
25
+ opencc_t ptr = (opencc_t) FIX2LONG(rb_opencc);
26
+ char * buff = opencc_convert_utf8(ptr, RSTRING_PTR(rb_str), RSTRING_LEN(rb_str));
27
+ VALUE converted = rb_utf8_str_new_cstr(buff);
28
+ opencc_convert_utf8_free(buff);
29
+ return converted;
30
+ }
31
+
32
+ static VALUE rb_opencc_close(VALUE self, VALUE rb_opencc) {
33
+ opencc_t ptr = (opencc_t) FIX2LONG(rb_opencc);
34
+
35
+ if (opencc_close(ptr) == 0) {
36
+ return Qtrue;
37
+ } else {
38
+ return Qfalse;
39
+ }
40
+ }
41
+
42
+ void Init_opencc(void) {
43
+ rb_mOpenCC = rb_define_module("OpenCC");
44
+ rb_mOpenCC_Mixin = rb_define_module_under(rb_mOpenCC, "Mixin");
45
+
46
+ rb_define_private_method(rb_mOpenCC_Mixin, "opencc_open", rb_opencc_open, 1);
47
+ rb_define_private_method(rb_mOpenCC_Mixin, "opencc_close", rb_opencc_close, 1);
48
+ rb_define_private_method(rb_mOpenCC_Mixin, "opencc_convert", rb_opencc_convert, 2);
49
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef RUBY_OPENCC_H
2
+ #define RUBY_OPENCC_H 1
3
+
4
+ #include <ruby.h>
5
+ #include <opencc/opencc.h>
6
+
7
+ static VALUE rb_opencc_open(VALUE self, VALUE rb_cfg);
8
+ static VALUE rb_opencc_convert(VALUE self, VALUE rb_occid, VALUE rb_str);
9
+ static VALUE rb_opencc_close(VALUE self, VALUE rb_occid);
10
+
11
+ #endif /* RUBY_OPENCC_H */
@@ -0,0 +1,70 @@
1
+ module OpenCC
2
+ class Converter
3
+ include OpenCC::Mixin
4
+
5
+ class << self
6
+ def with(config, &block)
7
+ converter = new(config)
8
+ if block.arity == 0
9
+ converter.instance_eval(&block)
10
+ else
11
+ yield converter
12
+ end
13
+ ensure
14
+ converter.close
15
+ end
16
+ end
17
+
18
+ attr_reader :config
19
+
20
+ # *<tt>:config</tt> - The config file name without .json suffix, default "s2t"
21
+ def initialize(config = nil)
22
+ @config = (config || OpenCC::DEFAULT_CONFIG).to_s
23
+
24
+ if !OpenCC::CONFIGS.include?(@config)
25
+ raise ArgumentError, "Unsupported configuration name #{@config.inspect}, expected one of #{OpenCC::CONFIGS.join(', ')}"
26
+ end
27
+
28
+ @mutex = Mutex.new
29
+ @closed = false
30
+ end
31
+
32
+ def convert(input)
33
+ synchronize do
34
+ return if closed?
35
+
36
+ @__opencc__ ||= opencc_open(config_file_name)
37
+
38
+ if @__opencc__.nil?
39
+ raise RuntimeError, "OpenCC failed to load (#{config_file_name})"
40
+ end
41
+
42
+ opencc_convert(@__opencc__, input.force_encoding(Encoding::UTF_8))
43
+ end
44
+ end
45
+
46
+ def close
47
+ synchronize do
48
+ return false if closed?
49
+ return false if @__opencc__.nil?
50
+ return false if !opencc_close(@__opencc__)
51
+
52
+ @__opencc__ = nil
53
+ @closed = true
54
+ end
55
+ end
56
+
57
+ def closed?
58
+ @closed
59
+ end
60
+
61
+ private
62
+ def synchronize(&block)
63
+ @mutex.synchronize(&block)
64
+ end
65
+
66
+ def config_file_name
67
+ "#{@config}.json"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module OpenCC
2
+ VERSION = "1.0.5"
3
+ end
data/lib/opencc.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "opencc/version"
2
+ require "opencc/opencc"
3
+ require "opencc/converter"
4
+
5
+ module OpenCC
6
+ CONFIGS = %w[ s2t t2s s2tw tw2s s2hk hk2s s2twp tw2sp hk2t t2hk t2jp jp2t tw2t ]
7
+ DEFAULT_CONFIG = "s2t"
8
+
9
+ class << self
10
+ def with(config, &block)
11
+ Converter.with(config, &block)
12
+ end
13
+
14
+ def [](config)
15
+ config && new(config)
16
+ end
17
+
18
+ def new(config = nil)
19
+ Converter.new(config)
20
+ end
21
+
22
+ CONFIGS.each do |config|
23
+ define_method :"#{config}" do |input|
24
+ with(config) do |converter|
25
+ converter.convert(input)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
data/opencc.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ require_relative "lib/opencc/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "opencc-rb"
5
+ spec.version = OpenCC::VERSION
6
+ spec.authors = ["zsj"]
7
+ spec.email = ["lekyzsj@gmail.com"]
8
+
9
+ spec.summary = %q{This gem is for conversions between Traditional Chinese, Simplified Chinese and Japanese Kanji (Shinjitai).}
10
+ spec.description = %q{This gem is for conversions between Traditional Chinese, Simplified Chinese and Japanese Kanji (Shinjitai). It supports character-level and phrase-level conversion, character variant conversion and regional idioms among Mainland China, Taiwan and Hong Kong.}
11
+ spec.homepage = "https://github.com/songjiz/opencc-rb"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+ spec.extensions = ["ext/opencc/extconf.rb"]
25
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opencc-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - zsj
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem is for conversions between Traditional Chinese, Simplified Chinese
14
+ and Japanese Kanji (Shinjitai). It supports character-level and phrase-level conversion,
15
+ character variant conversion and regional idioms among Mainland China, Taiwan and
16
+ Hong Kong.
17
+ email:
18
+ - lekyzsj@gmail.com
19
+ executables: []
20
+ extensions:
21
+ - ext/opencc/extconf.rb
22
+ extra_rdoc_files: []
23
+ files:
24
+ - ".gitignore"
25
+ - ".travis.yml"
26
+ - ".travis/install.sh"
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - ext/opencc/extconf.rb
35
+ - ext/opencc/opencc.c
36
+ - ext/opencc/opencc.h
37
+ - lib/opencc.rb
38
+ - lib/opencc/converter.rb
39
+ - lib/opencc/version.rb
40
+ - opencc.gemspec
41
+ homepage: https://github.com/songjiz/opencc-rb
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/songjiz/opencc-rb
46
+ source_code_uri: https://github.com/songjiz/opencc-rb
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.2.15
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: This gem is for conversions between Traditional Chinese, Simplified Chinese
66
+ and Japanese Kanji (Shinjitai).
67
+ test_files: []