Module: Oselvar::Var::Core::Sentences

Defined in:
lib/oselvar/var/core/sentences.rb

Constant Summary collapse

ABBREVIATIONS =
Set.new(['e.g.', 'i.e.', 'etc.', 'cf.', 'vs.']).freeze

Class Method Summary collapse

Class Method Details

.build_cp_to_u16(text) ⇒ Object

cp_to_u16 is the UTF-16 offset of text.



66
67
68
69
70
71
72
73
74
75
# File 'lib/oselvar/var/core/sentences.rb', line 66

def build_cp_to_u16(text)
  result = Array.new(text.length + 1, 0)
  u16 = 0
  text.each_char.with_index do |ch, i|
    result[i] = u16
    u16 += ch.ord > 0xFFFF ? 2 : 1
  end
  result[text.length] = u16
  result
end

.digit?(ch) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/oselvar/var/core/sentences.rb', line 105

def digit?(ch)
  !ch.empty? && ch.match?(/[0-9]/)
end

.inside_number_or_abbrev?(text, dot_pos) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oselvar/var/core/sentences.rb', line 77

def inside_number_or_abbrev?(text, dot_pos)
  prev = dot_pos.positive? ? text[dot_pos - 1] : ''
  nxt = dot_pos + 1 < text.length ? text[dot_pos + 1] : ''
  return true if digit?(prev) && digit?(nxt)

  ABBREVIATIONS.each do |abbrev|
    start = [0, dot_pos + 1 - abbrev.length].max
    return true if text[start...(dot_pos + 1)] == abbrev
  end
  lower?(nxt)
end

.lower?(ch) ⇒ Boolean

Unicode-aware "is a lowercase letter": has case and is already lower.

Returns:

  • (Boolean)


110
111
112
# File 'lib/oselvar/var/core/sentences.rb', line 110

def lower?(ch)
  !ch.empty? && ch != ch.upcase && ch == ch.downcase
end

.push_segment(out, text, start_cp, end_cp, cp_to_u16) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oselvar/var/core/sentences.rb', line 89

def push_segment(out, text, start_cp, end_cp, cp_to_u16)
  return if end_cp <= start_cp

  raw = text[start_cp...end_cp]
  stripped = raw.strip
  return if stripped.empty?

  lead = raw.length - raw.lstrip.length
  trail = raw.length - raw.rstrip.length
  out << Sentence.new(
    text: stripped,
    start_offset: cp_to_u16[start_cp + lead],
    end_offset: cp_to_u16[end_cp - trail]
  )
end

.split_sentences(text) ⇒ Object



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
# File 'lib/oselvar/var/core/sentences.rb', line 17

def split_sentences(text)
  cp_to_u16 = build_cp_to_u16(text)
  n = text.length
  out = []

  # Mark no-split zones (backtick spans, double-quoted strings).
  skip = Array.new(n, false)
  j = 0
  while j < n
    c = text[j]
    if ['`', '"'].include?(c)
      close = text.index(c, j + 1)
      break if close.nil?

      (j..close).each { |k| skip[k] = true }
      j = close + 1
      next
    end
    j += 1
  end

  i = 0
  segment_start = 0
  while i < n
    if skip[i]
      i += 1
      next
    end
    ch = text[i]
    if ["\n", '.', '!', '?'].include?(ch)
      if ch == '.' && inside_number_or_abbrev?(text, i)
        i += 1
        next
      end
      stop = i + 1
      push_segment(out, text, segment_start, stop, cp_to_u16)
      i = stop
      i += 1 while i < n && [' ', "\n"].include?(text[i])
      segment_start = i
      next
    end
    i += 1
  end

  push_segment(out, text, segment_start, n, cp_to_u16)
  out
end