opencc-rb 1.0.5 → 1.0.6
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 +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +1 -4
- data/ext/opencc/opencc.c +78 -26
- data/ext/opencc/opencc.h +0 -4
- data/lib/opencc.rb +15 -12
- data/lib/opencc/version.rb +1 -1
- metadata +3 -4
- data/lib/opencc/converter.rb +0 -70
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d68ddb8428b8039e56ea9784495cf60534822013694fdda4c1e94815169fb614
|
|
4
|
+
data.tar.gz: d3bb445106db7e37f3347f263bc2bb083ee4eaa11f8336dcb5ca36263ba8a3e5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b04c2067f6dad9761b3d27113e1dae9760b085734208735a5983046c0fa1d52a98179eb285eaa09b0ff8730bc41b5539223af021840878fada02b73cd2aa7500
|
|
7
|
+
data.tar.gz: 219a38feecfc811789f86e35390c3b2fe394d9d03b1ae114b17458e7a36d38674b2c34582c8037347ebf55440ede174cb0c25deaedc49b7091892a3c1dff00ac
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -92,14 +92,11 @@ OpenCC.jp2t("芸術 欠航 飲料缶") # => "藝術 缺航 飲料罐"
|
|
|
92
92
|
OpenCC.tw2t("想到自己一緊張就口吃,我就沒胃口吃飯") # => "想到自己一緊張就口吃,我就沒胃口喫飯"
|
|
93
93
|
|
|
94
94
|
# Or
|
|
95
|
-
|
|
96
|
-
converter = OpenCC.new(:s2t)
|
|
95
|
+
# You *must* call close when you're finished
|
|
97
96
|
converter = OpenCC::Converter.new(:s2t)
|
|
98
97
|
converter.convert("汉字") # => '漢字'
|
|
99
98
|
converter.close # => true
|
|
100
|
-
converter.closed? # => true
|
|
101
99
|
converter.close # => false
|
|
102
|
-
converter.convert("汉字") # => nil
|
|
103
100
|
```
|
|
104
101
|
|
|
105
102
|
## Development
|
data/ext/opencc/opencc.c
CHANGED
|
@@ -1,38 +1,89 @@
|
|
|
1
1
|
#include "opencc.h"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
VALUE rb_mOpenCC_Mixin;
|
|
3
|
+
#define OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD "s2t.json"
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
opencc_t ptr;
|
|
5
|
+
VALUE mOpenCC;
|
|
6
|
+
VALUE cConverter;
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
typedef struct {
|
|
9
|
+
opencc_t opencc;
|
|
10
|
+
} opencc_converter_t;
|
|
11
|
+
|
|
12
|
+
static void opencc_converter_t_dfree(void* _ptr) {
|
|
13
|
+
opencc_converter_t* ptr = (opencc_converter_t*)_ptr;
|
|
14
|
+
if (ptr->opencc != NULL && ptr->opencc != (opencc_t) - 1) {
|
|
15
|
+
opencc_close(ptr->opencc);
|
|
16
|
+
}
|
|
17
|
+
free(ptr);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static size_t opencc_converter_t_dsize(const void* _ptr) {
|
|
21
|
+
return sizeof(opencc_converter_t);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static const rb_data_type_t opencc_converter_data_type = {
|
|
25
|
+
.wrap_struct_name = "opencc converter object",
|
|
26
|
+
.function = {
|
|
27
|
+
.dfree = opencc_converter_t_dfree,
|
|
28
|
+
.dsize = opencc_converter_t_dsize,
|
|
29
|
+
},
|
|
30
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
VALUE opencc_converter_t_alloc(VALUE self) {
|
|
34
|
+
opencc_converter_t *ptr = malloc(sizeof(opencc_converter_t));
|
|
35
|
+
|
|
36
|
+
return TypedData_Wrap_Struct(self, &opencc_converter_data_type, ptr);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static VALUE opencc_converter_t_initialize(int argc, VALUE* argv, VALUE self) {
|
|
40
|
+
opencc_converter_t *ptr;
|
|
41
|
+
const char *cfg = NULL;
|
|
42
|
+
|
|
43
|
+
if (argv[0] != Qnil) {
|
|
44
|
+
switch (TYPE(argv[0])) {
|
|
45
|
+
case T_SYMBOL:
|
|
46
|
+
cfg = RSTRING_PTR(rb_sym_to_s(argv[0]));
|
|
47
|
+
break;
|
|
48
|
+
case T_STRING:
|
|
49
|
+
cfg = RSTRING_PTR(argv[0]);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
12
52
|
}
|
|
13
53
|
|
|
14
|
-
|
|
54
|
+
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
|
|
55
|
+
|
|
56
|
+
ptr->opencc = opencc_open(cfg);
|
|
15
57
|
|
|
16
58
|
// On error the return value will be (opencc_t) -1.
|
|
17
|
-
if (ptr == (opencc_t) - 1) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return LONG2FIX((long)ptr);
|
|
59
|
+
if (ptr->opencc == (opencc_t) - 1) {
|
|
60
|
+
free(ptr);
|
|
61
|
+
rb_raise(rb_eException, "%s", "(opencc_open) failed to allocate instance of opencc");
|
|
21
62
|
}
|
|
63
|
+
return self;
|
|
22
64
|
}
|
|
23
65
|
|
|
24
|
-
static VALUE
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
66
|
+
static VALUE opencc_converter_t_convert(VALUE self, VALUE input) {
|
|
67
|
+
opencc_converter_t *ptr;
|
|
68
|
+
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
|
|
69
|
+
|
|
70
|
+
if (ptr->opencc != NULL) {
|
|
71
|
+
char * buff = opencc_convert_utf8(ptr->opencc, RSTRING_PTR(input), RSTRING_LEN(input));
|
|
72
|
+
VALUE result = rb_utf8_str_new_cstr(buff);
|
|
73
|
+
opencc_convert_utf8_free(buff);
|
|
74
|
+
return result;
|
|
75
|
+
} else {
|
|
76
|
+
rb_warn("%s", "opencc has been closed");
|
|
77
|
+
return Qnil;
|
|
78
|
+
}
|
|
30
79
|
}
|
|
31
80
|
|
|
32
|
-
static VALUE
|
|
33
|
-
|
|
81
|
+
static VALUE opencc_converter_t_close(VALUE self) {
|
|
82
|
+
opencc_converter_t *ptr;
|
|
83
|
+
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
|
|
34
84
|
|
|
35
|
-
if (opencc_close(ptr) == 0) {
|
|
85
|
+
if (ptr->opencc != NULL && opencc_close(ptr->opencc) == 0) {
|
|
86
|
+
ptr->opencc = NULL;
|
|
36
87
|
return Qtrue;
|
|
37
88
|
} else {
|
|
38
89
|
return Qfalse;
|
|
@@ -40,10 +91,11 @@ static VALUE rb_opencc_close(VALUE self, VALUE rb_opencc) {
|
|
|
40
91
|
}
|
|
41
92
|
|
|
42
93
|
void Init_opencc(void) {
|
|
43
|
-
|
|
44
|
-
|
|
94
|
+
mOpenCC = rb_define_module("OpenCC");
|
|
95
|
+
cConverter = rb_define_class_under(mOpenCC, "Converter", rb_cObject);
|
|
45
96
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
97
|
+
rb_define_alloc_func(cConverter, opencc_converter_t_alloc);
|
|
98
|
+
rb_define_method(cConverter, "initialize", opencc_converter_t_initialize, -1);
|
|
99
|
+
rb_define_method(cConverter, "convert", opencc_converter_t_convert, 1);
|
|
100
|
+
rb_define_method(cConverter, "close", opencc_converter_t_close, 0);
|
|
49
101
|
}
|
data/ext/opencc/opencc.h
CHANGED
|
@@ -4,8 +4,4 @@
|
|
|
4
4
|
#include <ruby.h>
|
|
5
5
|
#include <opencc/opencc.h>
|
|
6
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
7
|
#endif /* RUBY_OPENCC_H */
|
data/lib/opencc.rb
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
require "opencc/version"
|
|
2
2
|
require "opencc/opencc"
|
|
3
|
-
require "opencc/converter"
|
|
4
3
|
|
|
5
4
|
module OpenCC
|
|
6
5
|
CONFIGS = %w[ s2t t2s s2tw tw2s s2hk hk2s s2twp tw2sp hk2t t2hk t2jp jp2t tw2t ]
|
|
7
|
-
DEFAULT_CONFIG = "s2t"
|
|
8
6
|
|
|
9
7
|
class << self
|
|
10
8
|
def with(config, &block)
|
|
11
|
-
Converter.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
converter = Converter.new(config)
|
|
10
|
+
|
|
11
|
+
begin
|
|
12
|
+
if block.arity == 0
|
|
13
|
+
converter.instance_eval(&block)
|
|
14
|
+
else
|
|
15
|
+
yield converter
|
|
16
|
+
end
|
|
17
|
+
ensure
|
|
18
|
+
converter && converter.close
|
|
19
|
+
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
CONFIGS.each do |config|
|
|
23
23
|
define_method :"#{config}" do |input|
|
|
24
|
-
|
|
24
|
+
begin
|
|
25
|
+
converter = Converter.new(config)
|
|
25
26
|
converter.convert(input)
|
|
27
|
+
ensure
|
|
28
|
+
converter && converter.close
|
|
26
29
|
end
|
|
27
30
|
end
|
|
28
31
|
end
|
data/lib/opencc/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: opencc-rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- zsj
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-
|
|
11
|
+
date: 2021-08-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: This gem is for conversions between Traditional Chinese, Simplified Chinese
|
|
14
14
|
and Japanese Kanji (Shinjitai). It supports character-level and phrase-level conversion,
|
|
@@ -35,7 +35,6 @@ files:
|
|
|
35
35
|
- ext/opencc/opencc.c
|
|
36
36
|
- ext/opencc/opencc.h
|
|
37
37
|
- lib/opencc.rb
|
|
38
|
-
- lib/opencc/converter.rb
|
|
39
38
|
- lib/opencc/version.rb
|
|
40
39
|
- opencc.gemspec
|
|
41
40
|
homepage: https://github.com/songjiz/opencc-rb
|
|
@@ -59,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
59
58
|
- !ruby/object:Gem::Version
|
|
60
59
|
version: '0'
|
|
61
60
|
requirements: []
|
|
62
|
-
rubygems_version: 3.2.
|
|
61
|
+
rubygems_version: 3.2.22
|
|
63
62
|
signing_key:
|
|
64
63
|
specification_version: 4
|
|
65
64
|
summary: This gem is for conversions between Traditional Chinese, Simplified Chinese
|
data/lib/opencc/converter.rb
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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
|