|
| 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" |
0 commit comments