class Sastrawi::Stemmer::CachedStemmer

Attributes

cache[R]
delegated_stemmer[R]

Public Class Methods

new(cache, delegated_stemmer) click to toggle source
# File lib/sastrawi/stemmer/cached_stemmer.rb, line 8
def initialize(cache, delegated_stemmer)
  @cache = cache
  @delegated_stemmer = delegated_stemmer
end

Public Instance Methods

stem(text) click to toggle source
# File lib/sastrawi/stemmer/cached_stemmer.rb, line 13
def stem(text)
  normalized_text = Sastrawi::Stemmer::Filter::TextNormalizer.normalize_text(text)

  words = normalized_text.split(' ')
  stems = []

  words.each do |word|
    if @cache.has?(word)
      stems.push(@cache.get(word))
    else
      stem = @delegated_stemmer.stem(word)
      @cache.set(word, stem)
      stems.push(stem)
    end
  end

  stems.join(' ')
end