class Sastrawi::Stemmer::Context::Context

Attributes

current_word[RW]
dictionary[R]
original_word[R]
prefix_visitors[R]
process_is_stopped[RW]
removals[RW]
result[RW]
suffix_visitors[R]
visitor_provider[R]
visitors[R]

Public Class Methods

new(original_word, dictionary, visitor_provider) click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 14
def initialize(original_word, dictionary, visitor_provider)
    @original_word = original_word
    @current_word = original_word
    @dictionary = dictionary
    @visitor_provider = visitor_provider

    @process_is_stopped = false
    @removals = []
    @visitors = nil
    @suffix_visitors = nil
    @prefix_visitors = nil
    @result = nil

    init_visitors
end

Public Instance Methods

accept(visitor) click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 136
def accept(visitor)
  visitor.visit(self)
end
accept_prefix_visitors(visitors) click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 150
def accept_prefix_visitors(visitors)
  removal_length = @removals.length

  visitors.each do |visitor|
    accept(visitor)

    return @current_word if @dictionary.contains?(@current_word)

    return @current_word if @process_is_stopped

    return if @removals.length > removal_length
  end
end
accept_visitors(visitors) click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 140
def accept_visitors(visitors)
  visitors.each do |visitor|
    accept(visitor)

    return @current_word if @dictionary.contains?(@current_word)

    return @current_word if @process_is_stopped
  end
end
add_removal(removal) click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 40
def add_removal(removal)
  @removals.push(removal)
end
execute() click to toggle source

Execute stemming process

# File lib/sastrawi/stemmer/context/context.rb, line 47
def execute
  start_stemming_process

  if @dictionary.contains?(@current_word)
    @result = @current_word
  else
    @result = @original_word
  end
end
init_visitors() click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 30
def init_visitors
  @visitors = @visitor_provider.visitors
  @suffix_visitors = @visitor_provider.suffix_visitors
  @prefix_visitors = @visitor_provider.prefix_visitors
end
loop_last_return() click to toggle source

ECS loop last return

# File lib/sastrawi/stemmer/context/context.rb, line 95
def loop_last_return
  restore_prefix

  removals = @removals
  reversed_removals = removals.reverse
  current_word = @current_word

  reversed_removals.each do |reverse_removal|
    next unless suffix_removal?(reverse_removal)

    if reverse_removal.removed_part == 'kan'
      @current_word = "#{reverse_removal.result}k"

      remove_prefixes
      return if @dictionary.contains?(@current_word)

      @current_word = "#{reverse_removal.result}kan"
    else
      @current_word = reverse_removal.subject
    end

    remove_prefixes
    return if @dictionary.contains?(@current_word)

    @removals = removals
    @current_word = current_word
  end
end
remove_prefixes() click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 124
def remove_prefixes
  3.times do
    accept_prefix_visitors(@prefix_visitors)

    return if @dictionary.contains?(@current_word)
  end
end
remove_suffixes() click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 132
def remove_suffixes
  accept_visitors(@suffix_visitors)
end
restore_prefix() click to toggle source

Restore prefix to proceed with ECS loop last return

# File lib/sastrawi/stemmer/context/context.rb, line 174
def restore_prefix
  @removals.each do |removal|
    if removal.affix_type == 'DP'
      @current_word = removal.subject
      break
    end
  end

  @removals.each do |removal|
    if removal.affix_type == 'DP'
      @removals.delete(removal)
    end
  end
end
start_stemming_process() click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 57
def start_stemming_process
  return if @dictionary.contains?(@current_word)

  accept_visitors(@visitors)

  return if @dictionary.contains?(@current_word)

  cs_precendence_adjustment_specification = Sastrawi::Stemmer::ConfixStripping::PrecedenceAdjustmentSpecification.new

  ##
  # Confix stripping
  # try to remove prefix before suffix if the specification is met

  if cs_precendence_adjustment_specification.satisfied_by?(@original_word)
    remove_prefixes
    return if @dictionary.contains?(@current_word)

    remove_suffixes
    if @dictionary.contains?(@current_word)
      return
    else
      @current_word = @original_word
      @removals = []
    end
  end

  remove_suffixes
  return if @dictionary.contains?(@current_word)

  remove_prefixes
  return if @dictionary.contains?(@current_word)

  loop_last_return
end
stop_process() click to toggle source
# File lib/sastrawi/stemmer/context/context.rb, line 36
def stop_process
  @process_is_stopped = true
end
suffix_removal?(removal) click to toggle source

Check whether the removed part is a suffix

# File lib/sastrawi/stemmer/context/context.rb, line 167
def suffix_removal?(removal)
  removal.affix_type == 'DS' || removal.affix_type == 'PP' || removal.affix_type == 'P'
end