ropencc 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +27 -0
- data/README.md +19 -3
- data/lib/ropencc.rb +89 -35
- data/lib/ropencc/version.rb +2 -2
- data/test/test_ropencc.rb +26 -0
- metadata +21 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: adaf5620e6399a717239d1a33c387d27d04697f3
|
4
|
+
data.tar.gz: e979d2ae3b6965605c2b3e7a1d8c15275bb2e715
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54a7d7787c83f7978a186d16a90460c0d0af8f086c403d5461d23c6d04a5301d3521c5fa75ad2975b6154bde4da827bfe1bfc679cc2501ae3726c1186929710d
|
7
|
+
data.tar.gz: 658768813f7916feff7e102596bf3805b49504cc39af61cc3cbcf7a76bd9c9597936ae4073d93b132579520ab4f2c6bfe778ef1c937867490c865679181c114a
|
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2013, Psi
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
- Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
- Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
- Neither the name of Thomas J Bradley nor the names of its contributors may
|
15
|
+
be used to endorse or promote products derived from this software without
|
16
|
+
specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
22
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
25
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -21,12 +21,14 @@ Install
|
|
21
21
|
如果你正在使用Ubuntu 10.10 (Maverick) 以上的版本,opencc已經被加入到了官方源中,使用
|
22
22
|
|
23
23
|
sudo apt-get install opencc
|
24
|
+
sudo apt-get install libopencc-dev
|
24
25
|
|
25
26
|
如果你更願意體驗最新的版本,請使用ppa:
|
26
27
|
|
27
28
|
sudo add-apt-repository ppa:byvoid-kcp/ppa
|
28
29
|
sudo apt-get update
|
29
30
|
sudo apt-get install opencc
|
31
|
+
sudo apt-get install libopencc-dev
|
30
32
|
|
31
33
|
|
32
34
|
Mac OS X
|
@@ -44,17 +46,31 @@ Install
|
|
44
46
|
Usage
|
45
47
|
-----
|
46
48
|
|
47
|
-
简转繁
|
49
|
+
简转繁 (Simple Interface)
|
48
50
|
|
49
51
|
Ropencc.conv('simp_to_trad', str)
|
50
52
|
|
51
|
-
繁转简
|
53
|
+
繁转简 (Simple Interface)
|
52
54
|
|
53
55
|
Ropencc.conv('trad_to_simp', str)
|
54
56
|
|
57
|
+
简转繁 (File-Class like Interface)
|
58
|
+
|
59
|
+
Ropencc.open 'zhs2zhtw_vp.ini' do |cc|
|
60
|
+
rs = cc.convert '新年快乐''
|
61
|
+
assert_equal '新年快樂', rs
|
62
|
+
end
|
63
|
+
|
64
|
+
繁转简 (File-Class like Interface)
|
65
|
+
|
66
|
+
Ropencc.open 'zhtw2zhcn_s.ini' do |cc|
|
67
|
+
rs = cc.convert '新年快樂'
|
68
|
+
assert_equal '新年快乐', rs
|
69
|
+
end
|
70
|
+
|
55
71
|
|
56
72
|
附:libopencc 作者简介
|
57
73
|
--------------------
|
58
74
|
BYVoid,清華大學計算機系二零一零級本科生,清華大學學生網絡管理委員會副會長,現於微軟亞洲研究院系統組實習。
|
59
75
|
|
60
|
-
出生於1991年末,年十九歲,河南安陽人。熱愛計算機科學、語言學、漢語音韻學。能說有入聲的老派安陽話,會用簡繁體流暢地閱讀書寫,能夠辨析漢字簡繁正異用法。認爲「正體字」不等於「臺灣正體字」,支持傳統正體字回歸主流,主張廢除簡體字, 希望陸、港、澳、臺、日、韓、越早日實現漢字統一。
|
76
|
+
出生於1991年末,年十九歲,河南安陽人。熱愛計算機科學、語言學、漢語音韻學。能說有入聲的老派安陽話,會用簡繁體流暢地閱讀書寫,能夠辨析漢字簡繁正異用法。認爲「正體字」不等於「臺灣正體字」,支持傳統正體字回歸主流,主張廢除簡體字, 希望陸、港、澳、臺、日、韓、越早日實現漢字統一。
|
data/lib/ropencc.rb
CHANGED
@@ -1,39 +1,93 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
|
2
3
|
require 'ffi'
|
3
|
-
|
4
|
-
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
4
|
+
|
5
|
+
class Ropencc
|
6
|
+
module LibOpenCC
|
7
|
+
extend FFI::Library
|
8
|
+
ffi_lib 'opencc'
|
9
|
+
attach_function :opencc_open, [:string], :pointer
|
10
|
+
attach_function :opencc_close, [:pointer], :int
|
11
|
+
attach_function :opencc_convert_utf8, [:pointer, :string, :int], :pointer
|
12
|
+
attach_function :opencc_convert, [:pointer, :string, :int], :int
|
13
|
+
attach_function :opencc_perror, [:string], :void
|
14
|
+
attach_function :opencc_dict_load, [:pointer, :string, :int], :int
|
15
|
+
end
|
16
|
+
|
17
|
+
module LibC
|
18
|
+
extend FFI::Library
|
19
|
+
ffi_lib FFI::Library::LIBC
|
20
|
+
attach_function :free, [:pointer], :void
|
21
|
+
end # module LibC
|
22
|
+
|
23
|
+
attr_accessor :descriptor
|
24
|
+
|
25
|
+
CONFIGS = ['zhs2zhtw_p.ini', 'zhs2zhtw_v.ini', 'zhs2zhtw_vp.ini', 'zht2zhtw_p.ini',
|
26
|
+
'zht2zhtw_v.ini', 'zht2zhtw_vp.ini', 'zhtw2zhs.ini', 'zhtw2zht.ini',
|
27
|
+
'zhtw2zhcn_s.ini', 'zhtw2zhcn_t.ini', 'zhs2zht.ini', 'zht2zhs.ini']
|
28
|
+
|
29
|
+
DICTS = {
|
30
|
+
:simp_to_trad => 'zhs2zhtw_vp.ini',
|
31
|
+
:trad_to_simp => 'zhtw2zhcn_s.ini'
|
32
|
+
}
|
33
|
+
|
34
|
+
def initialize(config_file)
|
35
|
+
@descriptor = LibOpenCC.opencc_open config_file
|
36
|
+
@is_open = true
|
37
|
+
end
|
38
|
+
|
39
|
+
private :initialize
|
40
|
+
|
41
|
+
def self.open(*args)
|
42
|
+
cc = new(*args)
|
43
|
+
|
44
|
+
return cc unless block_given?
|
45
|
+
|
46
|
+
begin
|
47
|
+
yield cc
|
48
|
+
ensure
|
49
|
+
begin
|
50
|
+
cc.close unless cc.closed?
|
51
|
+
rescue StandardError
|
52
|
+
# nothing, just swallow them
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.conv(dicttype, str)
|
58
|
+
raise 'unknown simple conversion type' if DICTS.has_key?(dicttype)
|
59
|
+
od = LibOpenCC.opencc_open DICTS[dicttype.to_sym]
|
60
|
+
ptr = LibOpenCC.opencc_convert_utf8(od, str, str.bytesize)
|
61
|
+
out_str = ptr.read_string
|
62
|
+
LibC.free ptr
|
63
|
+
LibOpenCC.opencc_close od
|
64
|
+
out_str.force_encoding("UTF-8") if out_str.respond_to?(:force_encoding)
|
65
|
+
out_str
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
def convert(str)
|
70
|
+
if @is_open == false
|
71
|
+
raise IOError, 'not opened for conversion'
|
72
|
+
end
|
73
|
+
|
74
|
+
ptr = LibOpenCC.opencc_convert_utf8(@descriptor, str, str.bytesize)
|
75
|
+
out_str = ptr.read_string
|
76
|
+
LibC.free ptr
|
77
|
+
out_str.force_encoding("UTF-8") if out_str.respond_to?(:force_encoding)
|
78
|
+
out_str
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def closed?
|
83
|
+
@is_open == false
|
84
|
+
end
|
85
|
+
|
86
|
+
def close
|
87
|
+
LibOpenCC.opencc_close @descriptor
|
88
|
+
@descriptor = nil
|
89
|
+
@is_open = false
|
90
|
+
|
91
|
+
return nil
|
37
92
|
end
|
38
|
-
end
|
39
93
|
end
|
data/lib/ropencc/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
class Ropencc
|
2
|
+
VERSION = '0.0.5'
|
3
3
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
#$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '../lib')
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require '../lib/ropencc'
|
7
|
+
|
8
|
+
class RopenccTest < Test::Unit::TestCase
|
9
|
+
def test_ropencc_open
|
10
|
+
cc = Ropencc.open 'zhtw2zhcn_s.ini'
|
11
|
+
assert_not_nil cc, 'Ropencc.open should return a handle'
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ropencc_convert
|
15
|
+
Ropencc.open 'zhtw2zhcn_s.ini' do |cc|
|
16
|
+
rs = cc.convert '新年快樂'
|
17
|
+
|
18
|
+
assert_equal '新年快乐', rs
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_ropencc_simple_conv
|
23
|
+
rs = Ropencc.conv 'trad_to_simp', '新年快樂'
|
24
|
+
assert_equal '新年快乐', rs
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,64 +1,68 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ropencc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Psi
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: ffi
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
|
26
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description: " a project for conversion between Traditional and Simplified Chinese,
|
28
|
+
wrapper in ruby. "
|
27
29
|
email:
|
28
30
|
- psi.xie@gmail.com
|
29
31
|
executables: []
|
30
32
|
extensions: []
|
31
33
|
extra_rdoc_files: []
|
32
34
|
files:
|
33
|
-
- .gitignore
|
35
|
+
- ".gitignore"
|
34
36
|
- Gemfile
|
37
|
+
- LICENSE
|
35
38
|
- README.md
|
36
39
|
- Rakefile
|
37
40
|
- lib/ropencc.rb
|
38
41
|
- lib/ropencc/version.rb
|
39
42
|
- ropencc.gemspec
|
43
|
+
- test/test_ropencc.rb
|
40
44
|
homepage: https://github.com/Psli/ropencc
|
41
45
|
licenses: []
|
46
|
+
metadata: {}
|
42
47
|
post_install_message:
|
43
48
|
rdoc_options: []
|
44
49
|
require_paths:
|
45
50
|
- lib
|
46
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
52
|
requirements:
|
49
|
-
- -
|
53
|
+
- - ">="
|
50
54
|
- !ruby/object:Gem::Version
|
51
55
|
version: '0'
|
52
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
57
|
requirements:
|
55
|
-
- -
|
58
|
+
- - ">="
|
56
59
|
- !ruby/object:Gem::Version
|
57
60
|
version: '0'
|
58
61
|
requirements: []
|
59
62
|
rubyforge_project:
|
60
|
-
rubygems_version:
|
63
|
+
rubygems_version: 2.2.2
|
61
64
|
signing_key:
|
62
|
-
specification_version:
|
65
|
+
specification_version: 4
|
63
66
|
summary: a project for conversion between Traditional and Simplified Chinese.
|
64
|
-
test_files:
|
67
|
+
test_files:
|
68
|
+
- test/test_ropencc.rb
|