Class: Zabon::Analyzer

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

Class Method Summary collapse

Class Method Details

.segments(text) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zabon/analyzer.rb', line 14

def segments(text)
  result = [""] # we do this, so we can += to append to the last result item, without checking for nil
  previous_segment = nil

  split(text).each do |segment|
    current_segment = Segment.new(segment)

    # if the current segment is a beginning bracket => we look further
    if current_segment.bracket_begin?
      previous_segment = current_segment
      next
    end

    # if the current segment is an ending bracket =>
    # we append to the last entry of the result set and don't look back anymore,
    # we've reached the end of a segment and start a new one with the next iteration
    if current_segment.bracket_end?
      result[-1] += current_segment
      previous_segment = nil
      next
    end

    # if the previous segment is a beginning bracket =>
    # we stitch together previous segment & current segment to become a new segment
    # we don't look back anymore
    if previous_segment&.bracket_begin?
      current_segment = Segment.new(previous_segment + current_segment)
      previous_segment = nil
    end

    # if we are not at the start, the current segment is a particle or a period and
    # we are not looking back, we append to the last entry of the result set
    if result.size > 1 && current_segment.joshi_or_period? && previous_segment.nil?
      result[-1] += current_segment
      previous_segment = current_segment
      next
    end

    # if we are not at the start, the current segment is a particle or a period or
    # the previous segment is not a bracket or period or a conjunctive particle and the current segment is hiragana
    # we append to the last entry of the result set
    if (result.size > 2 && current_segment.joshi_or_period?) || (previous_segment&.keyword? && current_segment.hiragana? && !/^[とのに]$/.match?(previous_segment))
      result[-1] += current_segment
      # if the current segment is not a particle, we are no looking back anymore, we start a new segment
      previous_segment = current_segment.joshi? ? current_segment : nil
      next
    end

    # no stitching left, append the current segment to the result set
    result << current_segment
    previous_segment = current_segment
  end

  result.reject(&:empty?) # we clear out any possible blank strings in the result set
end

.split(text) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/zabon/analyzer.rb', line 6

def split(text)
  text.split(KEYWORDS)
      .flat_map { |segment| segment.split(JOSHI) }
      .flat_map { |segment| segment.split(BRACKETS_BEGIN) }
      .flat_map { |segment| segment.split(BRACKETS_END) }
      .flatten.reject(&:empty?)
end