Class: Sastrawi::Stemmer::Filter::TextNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/sastrawi/stemmer/filter/text_normalizer.rb

Class Method Summary collapse

Class Method Details

.normalize_text(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sastrawi/stemmer/filter/text_normalizer.rb', line 7

def self.normalize_text(text)
  return "" if text.nil?

  unless text.is_a?(String)
    raise ArgumentError, "expected a String, got #{text.class}"
  end

  return "" if text.empty?

  lowercase_text = text.downcase
  replaced_text = lowercase_text.gsub(/[^a-z0-9 -]/im, ' ')
  replaced_text = replaced_text.gsub(/( +)/im, ' ')

  replaced_text.strip
end