Class: JapaneseNames::Util::Ngram

Inherits:
Object
  • Object
show all
Defined in:
lib/japanese_names/util/ngram.rb

Overview

Provides methods for parsing Japanese name strings.

Class Method Summary collapse

Class Method Details

.index_partition(str, i) ⇒ Object

Partitions a string based on an index



17
18
19
# File 'lib/japanese_names/util/ngram.rb', line 17

def index_partition(str, i)
  [str[0...i], str[i..-1]]
end

.mask_left(str, mask) ⇒ Object

Masks a String from the left side and returns the remaining (right) portion of the String.

Example: mask_left(“abcde”, “ab”) #=> “cde”



36
37
38
# File 'lib/japanese_names/util/ngram.rb', line 36

def mask_left(str, mask)
  str.gsub(/\A#{mask}/, '')
end

.mask_right(str, mask) ⇒ Object

Masks a String from the right side and returns the remaining (left) portion of the String.

Example: mask_right(“abcde”, “de”) #=> “abc”



43
44
45
# File 'lib/japanese_names/util/ngram.rb', line 43

def mask_right(str, mask)
  str.gsub(/#{mask}\z/, '')
end

.ngram_partition(str) ⇒ Object

Generates middle-out partition n-grams for a string



9
10
11
12
13
14
# File 'lib/japanese_names/util/ngram.rb', line 9

def ngram_partition(str)
  size = str.size
  spiral_partition_indexes(size).map do |i|
    index_partition(str, i)
  end
end

.spiral_partition_indexes(size) ⇒ Object

Lists middle-out partition points for a given string length



22
23
24
25
26
27
28
29
30
31
# File 'lib/japanese_names/util/ngram.rb', line 22

def spiral_partition_indexes(size)
  ary = []
  last = size / 2
  ary << last
  (size - 2).times do |i|
    last += (i + 1) * (-1)**i
    ary << last
  end
  ary
end