cmigemo 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +7 -0
- data/README.rdoc +68 -0
- data/Rakefile +74 -0
- data/ext/cmigemo.c +117 -0
- data/ext/extconf.rb +6 -0
- data/test/test_cmigemo.rb +41 -0
- metadata +77 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
= Ruby/CMigemo
|
2
|
+
|
3
|
+
* http://github.com/Constellation/ruby-cmigemo
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Ruby Binding for C/Migemo
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* get regexp text which match to Japanese phrase
|
12
|
+
* get regexp text faster
|
13
|
+
* load dict file which have any encoding
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
require 'cmigemo'
|
18
|
+
$KCODE = 'u'
|
19
|
+
dict_path = File.expand_path('~/dict')
|
20
|
+
cmigemo = CMigemo.new
|
21
|
+
{
|
22
|
+
"migemo-dict" => CMigemo::MIGEMO,
|
23
|
+
"han2zen.dat" => CMigemo::HAN2ZEN,
|
24
|
+
"hira2kata.dat" => CMigemo::HIRA2KATA,
|
25
|
+
"roma2hira.dat" => CMigemo::ROMA2HIRA,
|
26
|
+
}.each do |dict_file, type|
|
27
|
+
cmigemo.load(type, File.join(dict_path, dict_file));
|
28
|
+
end
|
29
|
+
|
30
|
+
# get regexp text
|
31
|
+
reg = Regexp.new(cmigemo.query('seiza'))
|
32
|
+
|
33
|
+
puts "match!" if "星座" =~ reg
|
34
|
+
puts "match!" if "正座" =~ reg
|
35
|
+
|
36
|
+
== REQUIREMENTS:
|
37
|
+
|
38
|
+
* C/Migemo
|
39
|
+
|
40
|
+
== INSTALL:
|
41
|
+
|
42
|
+
gem source -a http://gemcutter.org
|
43
|
+
sudo gem install cmigemo
|
44
|
+
|
45
|
+
== LICENSE:
|
46
|
+
|
47
|
+
(The MIT License)
|
48
|
+
|
49
|
+
Copyright (c) 2009 Constellation
|
50
|
+
|
51
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
52
|
+
a copy of this software and associated documentation files (the
|
53
|
+
'Software'), to deal in the Software without restriction, including
|
54
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
55
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
56
|
+
permit persons to whom the Software is furnished to do so, subject to
|
57
|
+
the following conditions:
|
58
|
+
|
59
|
+
The above copyright notice and this permission notice shall be
|
60
|
+
included in all copies or substantial portions of the Software.
|
61
|
+
|
62
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
63
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
64
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
65
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
66
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
67
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
68
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'hoe'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
$version = '1.0.0'
|
7
|
+
$readme = 'README.rdoc'
|
8
|
+
$rdoc_opts = %W(--main #{$readme} --charset utf-8 --line-numbers)
|
9
|
+
|
10
|
+
Hoe.spec'cmigemo' do |p|
|
11
|
+
developer('Constellation', 'utatane.tea@gmail.com')
|
12
|
+
p.description = <<-EOF
|
13
|
+
Ruby binding for C/Migemo
|
14
|
+
use any encoding dictionary
|
15
|
+
faster response
|
16
|
+
EOF
|
17
|
+
p.name = 'cmigemo'
|
18
|
+
p.readme_file = $readme
|
19
|
+
p.extra_rdoc_files = FileList[$readme]
|
20
|
+
p.need_tar = false
|
21
|
+
p.need_zip = false
|
22
|
+
p.summary = 'Ruby binding for C/Migemo'
|
23
|
+
p.test_globs = 'spec/**/*_spec.rb'
|
24
|
+
p.url = 'http://github.com/Constellation/ruby-cmigemo'
|
25
|
+
p.version = $version
|
26
|
+
p.spec_extras[:extensions] = FileList["ext/extconf.rb"].to_a
|
27
|
+
spec_extras[:rdoc_options] = $rdoc_opts
|
28
|
+
# p.spec_extras[:rdoc_options] = $rdoc_opts
|
29
|
+
#p.clean_globs =
|
30
|
+
# self.rubyforge_name = 'cmigemox' # if different than 'cmigemo'
|
31
|
+
end
|
32
|
+
|
33
|
+
ext = "ext"
|
34
|
+
ext_so = "ext/cmigemo.so"
|
35
|
+
ext_files = FileList[
|
36
|
+
"ext/cmigemo.c",
|
37
|
+
"ext/extconf.rb",
|
38
|
+
"ext/Makefile",
|
39
|
+
"lib"
|
40
|
+
]
|
41
|
+
|
42
|
+
# ext tasks
|
43
|
+
desc "build ext"
|
44
|
+
task :ext => ["ext/Makefile", ext_so]
|
45
|
+
file "ext/Makefile" => ["ext/extconf.rb"] do
|
46
|
+
Dir.chdir(ext) do
|
47
|
+
ruby "extconf.rb"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
file ext_so => ext_files do
|
51
|
+
Dir.chdir(ext) do
|
52
|
+
sh 'make'
|
53
|
+
end
|
54
|
+
cp ext_so, "lib"
|
55
|
+
end
|
56
|
+
directory "lib"
|
57
|
+
|
58
|
+
#namespace :dict do
|
59
|
+
# desc "get cp932 dict"
|
60
|
+
# task :cp932 do
|
61
|
+
# puts "get cp932 dict"
|
62
|
+
# end
|
63
|
+
# desc "get utf-8 dict"
|
64
|
+
# task :utf8 do
|
65
|
+
# puts "get utf8 dict"
|
66
|
+
# end
|
67
|
+
# desc "get euc-jp dict"
|
68
|
+
# task :eucjp do
|
69
|
+
# puts "get eucjp dict"
|
70
|
+
# end
|
71
|
+
#end
|
72
|
+
|
73
|
+
|
74
|
+
# vim: syntax=ruby
|
data/ext/cmigemo.c
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "migemo.h"
|
3
|
+
|
4
|
+
struct RMigemo {
|
5
|
+
int open;
|
6
|
+
migemo *migemo;
|
7
|
+
};
|
8
|
+
|
9
|
+
static void rb_cmigemo_free(struct RMigemo*);
|
10
|
+
static VALUE rb_cmigemo_close(VALUE);
|
11
|
+
static VALUE rb_cmigemo_s_allocate(VALUE);
|
12
|
+
static VALUE rb_cmigemo_initialize(VALUE);
|
13
|
+
static VALUE rb_cmigemo_load(VALUE, VALUE, VALUE);
|
14
|
+
static VALUE rb_cmigemo_query(VALUE, VALUE);
|
15
|
+
|
16
|
+
static VALUE cCMigemo;
|
17
|
+
|
18
|
+
static VALUE
|
19
|
+
rb_cmigemo_s_allocate(VALUE klass)
|
20
|
+
{
|
21
|
+
VALUE obj;
|
22
|
+
struct RMigemo *rm;
|
23
|
+
obj = Data_Make_Struct(klass, struct RMigemo, NULL, rb_cmigemo_free, rm);
|
24
|
+
rm->open = 0;
|
25
|
+
rm->migemo = NULL;
|
26
|
+
return obj;
|
27
|
+
}
|
28
|
+
|
29
|
+
static void rb_cmigemo_free(struct RMigemo *rm)
|
30
|
+
{
|
31
|
+
if(rm->open) migemo_close(rm->migemo);
|
32
|
+
free(rm);
|
33
|
+
}
|
34
|
+
|
35
|
+
static VALUE
|
36
|
+
rb_cmigemo_initialize(VALUE self)
|
37
|
+
{
|
38
|
+
char *dict;
|
39
|
+
struct RMigemo *rm;
|
40
|
+
int result;
|
41
|
+
|
42
|
+
Data_Get_Struct(self, struct RMigemo, rm);
|
43
|
+
rm->migemo = migemo_open(NULL);
|
44
|
+
rm->open = 1;
|
45
|
+
|
46
|
+
return self;
|
47
|
+
}
|
48
|
+
|
49
|
+
static VALUE
|
50
|
+
rb_cmigemo_close(VALUE self)
|
51
|
+
{
|
52
|
+
struct RMigemo *rm;
|
53
|
+
Data_Get_Struct(self, struct RMigemo, rm);
|
54
|
+
if(rm->open){
|
55
|
+
migemo_close(rm->migemo);
|
56
|
+
rm->open = 0;
|
57
|
+
}
|
58
|
+
return Qnil;
|
59
|
+
}
|
60
|
+
|
61
|
+
static VALUE
|
62
|
+
rb_cmigemo_query(VALUE self, VALUE rstr)
|
63
|
+
{
|
64
|
+
struct RMigemo *rm;
|
65
|
+
unsigned char* str;
|
66
|
+
migemo *m;
|
67
|
+
VALUE result;
|
68
|
+
|
69
|
+
Data_Get_Struct(self, struct RMigemo, rm);
|
70
|
+
if(rm->open){
|
71
|
+
str = migemo_query(rm->migemo, (unsigned char*)StringValuePtr(rstr));
|
72
|
+
result = rb_tainted_str_new2(str);
|
73
|
+
migemo_release(rm->migemo, str);
|
74
|
+
return result;
|
75
|
+
} else {
|
76
|
+
rb_raise(rb_eException, "migemo closed");
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
static VALUE
|
81
|
+
rb_cmigemo_load(VALUE self, VALUE id, VALUE rstr)
|
82
|
+
{
|
83
|
+
struct RMigemo *rm;
|
84
|
+
int result;
|
85
|
+
int dict_id = NUM2INT(id);
|
86
|
+
char *dict = StringValuePtr(rstr);
|
87
|
+
Data_Get_Struct(self, struct RMigemo, rm);
|
88
|
+
|
89
|
+
if(rm->open){
|
90
|
+
result = migemo_load(rm->migemo, dict_id, dict);
|
91
|
+
if(result == MIGEMO_DICTID_INVALID){
|
92
|
+
rb_raise(rb_eArgError, "invalid dict file");
|
93
|
+
}
|
94
|
+
|
95
|
+
return INT2NUM(result);
|
96
|
+
} else {
|
97
|
+
rb_raise(rb_eException, "migemo closed");
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
void Init_cmigemo()
|
102
|
+
{
|
103
|
+
cCMigemo = rb_define_class("CMigemo", rb_cData);
|
104
|
+
|
105
|
+
rb_define_alloc_func(cCMigemo, rb_cmigemo_s_allocate);
|
106
|
+
rb_define_private_method(cCMigemo, "initialize", rb_cmigemo_initialize, 0);
|
107
|
+
rb_define_method(cCMigemo, "query", rb_cmigemo_query, 1);
|
108
|
+
rb_define_method(cCMigemo, "load", rb_cmigemo_load, 2);
|
109
|
+
rb_define_method(cCMigemo, "close", rb_cmigemo_close, 0);
|
110
|
+
|
111
|
+
rb_define_const(cCMigemo, "MIGEMO", INT2NUM(MIGEMO_DICTID_MIGEMO));
|
112
|
+
rb_define_const(cCMigemo, "ROMA2HIRA", INT2NUM(MIGEMO_DICTID_ROMA2HIRA));
|
113
|
+
rb_define_const(cCMigemo, "HIRA2KATA", INT2NUM(MIGEMO_DICTID_HIRA2KATA));
|
114
|
+
rb_define_const(cCMigemo, "HAN2ZEN", INT2NUM(MIGEMO_DICTID_HAN2ZEN));
|
115
|
+
rb_define_const(cCMigemo, "ZEN2HAN", INT2NUM(MIGEMO_DICTID_ZEN2HAN));
|
116
|
+
}
|
117
|
+
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/cmigemo'
|
2
|
+
$KCODE = 'u'
|
3
|
+
class TestCmigemo < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@cmigemo = CMigemo::new
|
7
|
+
d = File.expand_path(File.join(File.dirname(__FILE__), '../dict/utf-8'))
|
8
|
+
@dicts = {
|
9
|
+
File.join(d, "migemo-dict") => CMigemo::MIGEMO,
|
10
|
+
File.join(d, "han2zen.dat") => CMigemo::HAN2ZEN,
|
11
|
+
File.join(d, "hira2kata.dat") => CMigemo::HIRA2KATA,
|
12
|
+
File.join(d, "roma2hira.dat") => CMigemo::ROMA2HIRA,
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_new
|
17
|
+
assert_instance_of CMigemo, @cmigemo
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_dicts c=@cmigemo
|
21
|
+
@dicts.each do |dict, type|
|
22
|
+
c.load(type, dict)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_query
|
27
|
+
load_dicts
|
28
|
+
reg = Regexp.new(@cmigemo.query 'seiza')
|
29
|
+
assert reg =~ "正座"
|
30
|
+
assert reg =~ "星座"
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_close
|
34
|
+
cmigemo = CMigemo::new
|
35
|
+
cmigemo.close
|
36
|
+
assert_raise Exception do
|
37
|
+
cmigemo.query 'seiza'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmigemo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Constellation
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-09 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.3
|
24
|
+
version:
|
25
|
+
description: " Ruby binding for C/Migemo\n use any encoding dictionary\n faster response\n"
|
26
|
+
email:
|
27
|
+
- utatane.tea@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions:
|
31
|
+
- ext/extconf.rb
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.rdoc
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- ext/cmigemo.c
|
42
|
+
- ext/extconf.rb
|
43
|
+
- test/test_cmigemo.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/Constellation/ruby-cmigemo
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --main
|
51
|
+
- README.rdoc
|
52
|
+
- --charset
|
53
|
+
- utf-8
|
54
|
+
- --line-numbers
|
55
|
+
require_paths:
|
56
|
+
- ext
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: cmigemo
|
72
|
+
rubygems_version: 1.3.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Ruby binding for C/Migemo
|
76
|
+
test_files: []
|
77
|
+
|