yai_mecab 0.1.0
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/.rubocop.yml +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +5 -0
- data/Rakefile +23 -0
- data/ext/mecab/extconf.rb +39 -0
- data/ext/mecab/mecab.c +14 -0
- data/ext/mecab/segment.cc +111 -0
- data/ext/mecab/segment.h +17 -0
- data/lib/mecab/segment.rb +21 -0
- data/lib/mecab/version.rb +5 -0
- data/lib/mecab.rb +10 -0
- data/sig/mecab.rbs +4 -0
- metadata +57 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ff66ae0a73b3690ae65c68520506010730daa821e4fe201849dd7ff279587dbe
|
|
4
|
+
data.tar.gz: 7936ac39dba1d0922d0a66ea39121fbb86c73ae15092db2da21536ce002b0d47
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 380dc7cb4c5bccf5562f6e1bfc018f9e782957e45dd6814e92b7d8c0891e775febb12506120aa698a76a1311a695a43f33974218778a54cfe9e3426a9d447a2a
|
|
7
|
+
data.tar.gz: 1aaea04a676fb8a754b5e081e45c333a6c2609b844a4e6e7a91ec936c352aa95215259105c5d7513c941b670a397d83a225ed8ece7ba4e42511a1740fd2c4cd7
|
data/.rubocop.yml
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Xinyu Wang
|
|
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
data/Rakefile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "minitest/test_task"
|
|
5
|
+
|
|
6
|
+
Minitest::TestTask.create
|
|
7
|
+
|
|
8
|
+
require "rubocop/rake_task"
|
|
9
|
+
|
|
10
|
+
RuboCop::RakeTask.new
|
|
11
|
+
|
|
12
|
+
require "rake/extensiontask"
|
|
13
|
+
|
|
14
|
+
desc "Build Ruby Gem"
|
|
15
|
+
task build: :compile
|
|
16
|
+
|
|
17
|
+
GEMSPEC = Gem::Specification.load("mecab.gemspec")
|
|
18
|
+
|
|
19
|
+
Rake::ExtensionTask.new("mecab", GEMSPEC) do |ext|
|
|
20
|
+
ext.lib_dir = "lib/mecab"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
task default: %i[clobber compile test rubocop]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
find_executable("mecab-config") or abort "mecab-config not found"
|
|
7
|
+
rescue SystemExit
|
|
8
|
+
install_text = case RUBY_PLATFORM
|
|
9
|
+
when /linux/
|
|
10
|
+
"apt install libmecab-dev mecab-ipadic-utf8"
|
|
11
|
+
when /darwin/
|
|
12
|
+
"brew install mecab mecab-ipadic"
|
|
13
|
+
else
|
|
14
|
+
"We don't know how to install mecab on your platform"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
abort <<~MSG
|
|
18
|
+
mecab is missing. You can install it by running:
|
|
19
|
+
#{install_text}
|
|
20
|
+
MSG
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
mecab_config = with_config("mecab-config", "mecab-config")
|
|
24
|
+
enable_config("mecab-config")
|
|
25
|
+
|
|
26
|
+
append_cflags("-fvisibility=hidden -std=c11 -O3 -g")
|
|
27
|
+
append_cflags(`#{mecab_config} --cflags`.chomp)
|
|
28
|
+
|
|
29
|
+
append_cppflags("-fvisibility=hidden -std=c++11 -O3 -g")
|
|
30
|
+
append_cppflags(`#{mecab_config} --cflags`.chomp)
|
|
31
|
+
|
|
32
|
+
append_ldflags(`#{mecab_config} --libs`.chomp)
|
|
33
|
+
|
|
34
|
+
includes = [RbConfig::CONFIG["includedir"]]
|
|
35
|
+
includes += `#{mecab_config} --inc-dir`.chomp.split
|
|
36
|
+
libs = [RbConfig::CONFIG["libdir"]]
|
|
37
|
+
dir_config("mecab", includes, libs)
|
|
38
|
+
|
|
39
|
+
create_makefile("mecab/mecab")
|
data/ext/mecab/mecab.c
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#include <ruby.h>
|
|
2
|
+
|
|
3
|
+
#include <mecab.h>
|
|
4
|
+
|
|
5
|
+
#include "segment.h"
|
|
6
|
+
|
|
7
|
+
RUBY_FUNC_EXPORTED void
|
|
8
|
+
Init_mecab(void)
|
|
9
|
+
{
|
|
10
|
+
VALUE rb_mMecab = rb_define_module("Mecab");
|
|
11
|
+
rb_define_const(rb_mMecab, "CPP_VERSION", rb_str_new2(mecab_version()));
|
|
12
|
+
|
|
13
|
+
init_segment(rb_mMecab);
|
|
14
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#include "segment.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <vector>
|
|
5
|
+
|
|
6
|
+
#include <ruby/encoding.h>
|
|
7
|
+
|
|
8
|
+
#include <mecab.h>
|
|
9
|
+
|
|
10
|
+
namespace ext_mecab
|
|
11
|
+
{
|
|
12
|
+
// Only support UTF-8 encoding
|
|
13
|
+
static rb_encoding *utf8_encoding;
|
|
14
|
+
|
|
15
|
+
struct SegmentWrapper
|
|
16
|
+
{
|
|
17
|
+
MeCab::Model *model;
|
|
18
|
+
MeCab::Tagger *tagger;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
void segment_free(void *data)
|
|
22
|
+
{
|
|
23
|
+
SegmentWrapper *wrapper = (SegmentWrapper *)data;
|
|
24
|
+
|
|
25
|
+
if (wrapper->tagger)
|
|
26
|
+
{
|
|
27
|
+
delete wrapper->tagger;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (wrapper->model)
|
|
31
|
+
{
|
|
32
|
+
delete wrapper->model;
|
|
33
|
+
}
|
|
34
|
+
delete wrapper;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
size_t segment_size(const void *data)
|
|
38
|
+
{
|
|
39
|
+
return sizeof(SegmentWrapper);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static const rb_data_type_t segment_data_type = {
|
|
43
|
+
.wrap_struct_name = "MeCabSegment",
|
|
44
|
+
.function = {
|
|
45
|
+
.dmark = NULL,
|
|
46
|
+
.dfree = segment_free,
|
|
47
|
+
.dsize = segment_size,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
VALUE segment_alloc(VALUE self)
|
|
52
|
+
{
|
|
53
|
+
SegmentWrapper *wrapper = new SegmentWrapper();
|
|
54
|
+
return TypedData_Wrap_Struct(self, &segment_data_type, wrapper);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void segment_initialize(VALUE self, VALUE model_argv)
|
|
58
|
+
{
|
|
59
|
+
SegmentWrapper *wrapper;
|
|
60
|
+
TypedData_Get_Struct(self, SegmentWrapper, &segment_data_type, wrapper);
|
|
61
|
+
|
|
62
|
+
wrapper->model = MeCab::Model::create(StringValueCStr(model_argv));
|
|
63
|
+
if (!wrapper->model)
|
|
64
|
+
{
|
|
65
|
+
rb_raise(rb_eRuntimeError, "Failed to create MeCab model: %s", MeCab::getLastError());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
wrapper->tagger = wrapper->model->createTagger();
|
|
69
|
+
if (!wrapper->tagger)
|
|
70
|
+
{
|
|
71
|
+
rb_raise(rb_eRuntimeError, "Failed to create MeCab tagger: %s", MeCab::getLastError());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
VALUE segment_cut(VALUE self, VALUE text_rb_str)
|
|
76
|
+
{
|
|
77
|
+
std::string text = StringValueCStr(text_rb_str);
|
|
78
|
+
|
|
79
|
+
SegmentWrapper *wrapper;
|
|
80
|
+
TypedData_Get_Struct(self, SegmentWrapper, &segment_data_type, wrapper);
|
|
81
|
+
|
|
82
|
+
VALUE surface = rb_ary_new();
|
|
83
|
+
const MeCab::Node *node = wrapper->tagger->parseToNode(text.c_str());
|
|
84
|
+
for (; node; node = node->next)
|
|
85
|
+
{
|
|
86
|
+
if (node->stat == MECAB_BOS_NODE || node->stat == MECAB_EOS_NODE)
|
|
87
|
+
{
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
rb_ary_push(surface, rb_enc_str_new(node->surface, node->length, utf8_encoding));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return surface;
|
|
95
|
+
}
|
|
96
|
+
} // End of namespace ext_mecab
|
|
97
|
+
|
|
98
|
+
extern "C"
|
|
99
|
+
{
|
|
100
|
+
void init_segment(VALUE rb_mMecab)
|
|
101
|
+
{
|
|
102
|
+
// Initialize UTF-8 encoding
|
|
103
|
+
ext_mecab::utf8_encoding = rb_utf8_encoding();
|
|
104
|
+
|
|
105
|
+
VALUE cSegment = rb_define_class_under(rb_mMecab, "Segment", rb_cObject);
|
|
106
|
+
|
|
107
|
+
rb_define_alloc_func(cSegment, ext_mecab::segment_alloc);
|
|
108
|
+
rb_define_method(cSegment, "_initialize", RUBY_METHOD_FUNC(ext_mecab::segment_initialize), 1);
|
|
109
|
+
rb_define_method(cSegment, "_cut", RUBY_METHOD_FUNC(ext_mecab::segment_cut), 1);
|
|
110
|
+
}
|
|
111
|
+
} // End of extern "C"
|
data/ext/mecab/segment.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mecab
|
|
4
|
+
# Ruby wrapper for the native extension
|
|
5
|
+
class Segment
|
|
6
|
+
attr_reader :model_argv
|
|
7
|
+
|
|
8
|
+
# https://manpages.org/mecab for argument details
|
|
9
|
+
# Example:
|
|
10
|
+
# Mecab::Segment.new('-d /opt/homebrew/opt/mecab-ipadic/lib/mecab/dic/ipadic')
|
|
11
|
+
def initialize(model_argv = "")
|
|
12
|
+
@model_argv = model_argv
|
|
13
|
+
|
|
14
|
+
_initialize(model_argv)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def cut(sentence)
|
|
18
|
+
_cut(sentence)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/mecab.rb
ADDED
data/sig/mecab.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: yai_mecab
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yet Another AI
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2025-03-04 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Ruby binding for mecab
|
|
13
|
+
email:
|
|
14
|
+
- rubygems@yetanother.ai
|
|
15
|
+
executables: []
|
|
16
|
+
extensions:
|
|
17
|
+
- ext/mecab/extconf.rb
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".rubocop.yml"
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- ext/mecab/extconf.rb
|
|
25
|
+
- ext/mecab/mecab.c
|
|
26
|
+
- ext/mecab/segment.cc
|
|
27
|
+
- ext/mecab/segment.h
|
|
28
|
+
- lib/mecab.rb
|
|
29
|
+
- lib/mecab/segment.rb
|
|
30
|
+
- lib/mecab/version.rb
|
|
31
|
+
- sig/mecab.rbs
|
|
32
|
+
homepage: https://github.com/yet-another-ai/mecab.rb
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
allowed_push_host: https://rubygems.org
|
|
37
|
+
homepage_uri: https://github.com/yet-another-ai/mecab.rb
|
|
38
|
+
source_code_uri: https://github.com/yet-another-ai/mecab.rb
|
|
39
|
+
rubygems_mfa_required: 'true'
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.1.0
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.6.2
|
|
55
|
+
specification_version: 4
|
|
56
|
+
summary: mecab Ruby binding
|
|
57
|
+
test_files: []
|