Class: WordcutA::Wordcut

Inherits:
Object
  • Object
show all
Defined in:
lib/wordcuta.rb

Instance Method Summary collapse

Constructor Details

#initialize(dict_path, cluster_rule_path = nil) ⇒ Wordcut

Returns a new instance of Wordcut.



29
30
31
32
33
34
35
36
37
38
# File 'lib/wordcuta.rb', line 29

def initialize(dict_path, cluster_rule_path = nil)
  if cluster_rule_path
    @wordcut_p = FFI::AutoPointer.new(
      WordcutFFI.wordcut_new_with_dict_and_cluster_rules(dict_path, cluster_rule_path),
      WordcutFFI.method(:delete_wordcut))
  else
    @wordcut_p = FFI::AutoPointer.new(WordcutFFI.wordcut_new_with_dict(dict_path),
                                      WordcutFFI.method(:delete_wordcut))
  end
end

Instance Method Details

#into_ranges(text) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wordcuta.rb', line 40

def into_ranges(text)
  ranges = []
  FFI::MemoryPointer.new(:size_t, 1) do |range_cnt|
    ranges_p = WordcutFFI.wordcut_into_text_ranges(@wordcut_p, text, range_cnt)
    ranges = range_cnt.get_uint(0)
               .times
               .map {WordcutFFI::TextRange.new(ranges_p + _1 * WordcutFFI::TextRange.size)}
               .map {TextRange.new(_1[:s], _1[:e])}
  end
  return ranges
end

#into_strings(text) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/wordcuta.rb', line 52

def into_strings(text)
  words = []
  FFI::MemoryPointer.new(:size_t, 1) do |words_cnt_p|
    words_p = WordcutFFI::wordcut_into_strings(@wordcut_p, text, words_cnt_p)
    words_cnt = words_cnt_p.get_uint(0)
    words = words_p.get_array_of_string(0, words_cnt)
    WordcutFFI::delete_strings(words_p, words_cnt)
  end
  return words.map {_1.force_encoding("UTF-8")}
end

#put_delimiters(text, delim) ⇒ Object



63
64
65
# File 'lib/wordcuta.rb', line 63

def put_delimiters(text, delim)
  WordcutFFI::wordcut_put_delimiters(@wordcut_p, text, delim).force_encoding("UTF-8")
end