Class: Sastrawi::Stemmer::Context::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_word, dictionary, visitor_provider) ⇒ Context

Returns a new instance of Context.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#current_wordObject

Returns the value of attribute current_word.



12
13
14
# File 'lib/sastrawi/stemmer/context/context.rb', line 12

def current_word
  @current_word
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def dictionary
  @dictionary
end

#original_wordObject (readonly)

Returns the value of attribute original_word.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def original_word
  @original_word
end

#prefix_visitorsObject (readonly)

Returns the value of attribute prefix_visitors.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def prefix_visitors
  @prefix_visitors
end

#process_is_stoppedObject

Returns the value of attribute process_is_stopped.



12
13
14
# File 'lib/sastrawi/stemmer/context/context.rb', line 12

def process_is_stopped
  @process_is_stopped
end

#removalsObject

Returns the value of attribute removals.



12
13
14
# File 'lib/sastrawi/stemmer/context/context.rb', line 12

def removals
  @removals
end

#resultObject

Returns the value of attribute result.



12
13
14
# File 'lib/sastrawi/stemmer/context/context.rb', line 12

def result
  @result
end

#suffix_visitorsObject (readonly)

Returns the value of attribute suffix_visitors.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def suffix_visitors
  @suffix_visitors
end

#visitor_providerObject (readonly)

Returns the value of attribute visitor_provider.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def visitor_provider
  @visitor_provider
end

#visitorsObject (readonly)

Returns the value of attribute visitors.



11
12
13
# File 'lib/sastrawi/stemmer/context/context.rb', line 11

def visitors
  @visitors
end

Instance Method Details

#accept(visitor) ⇒ Object



136
137
138
# File 'lib/sastrawi/stemmer/context/context.rb', line 136

def accept(visitor)
  visitor.visit(self)
end

#accept_prefix_visitors(visitors) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
# 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) ⇒ Object



40
41
42
# File 'lib/sastrawi/stemmer/context/context.rb', line 40

def add_removal(removal)
  @removals.push(removal)
end

#executeObject

Execute stemming process



47
48
49
50
51
52
53
54
55
# 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_visitorsObject



30
31
32
33
34
# 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_returnObject

ECS loop last return



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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_prefixesObject



124
125
126
127
128
129
130
# 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_suffixesObject



132
133
134
# File 'lib/sastrawi/stemmer/context/context.rb', line 132

def remove_suffixes
  accept_visitors(@suffix_visitors)
end

#restore_prefixObject

Restore prefix to proceed with ECS loop last return



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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_processObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_processObject



36
37
38
# File 'lib/sastrawi/stemmer/context/context.rb', line 36

def stop_process
  @process_is_stopped = true
end

#suffix_removal?(removal) ⇒ Boolean

Check whether the removed part is a suffix

Returns:

  • (Boolean)


167
168
169
# 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