Class: LittleWeasel::Preprocessors::WordPreprocessor

Inherits:
Object
  • Object
show all
Includes:
Modules::ClassNameToSymbol, Modules::Orderable
Defined in:
lib/LittleWeasel/preprocessors/word_preprocessor.rb

Overview

This is a base class that provides methods and functionality for word preprocessors. A “word preprocessor” is an object that manipulates a word before it is passed to any word filters and before it is compared against the dictionary for validity.

Direct Known Subclasses

EnUs::CapitalizePreprocessor

Instance Attribute Summary collapse

Attributes included from Modules::Orderable

#order

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Modules::OrderValidatable

validate, #validate_order

Methods included from Modules::ClassNameToSymbol

included, #to_sym

Constructor Details

#initialize(order:) ⇒ WordPreprocessor

order:Integer, the order in which this preprocessor should be applied. preprocessor_on:Boolean, whether or not this preprocessor should be applied to any words.



24
25
26
27
28
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 24

def initialize(order:)
  validate_order order: order
  self.order = order
  preprocessor_on!
end

Instance Attribute Details

#preprocessor_onObject

Returns the value of attribute preprocessor_on.



18
19
20
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 18

def preprocessor_on
  @preprocessor_on
end

Class Method Details

.preprocess(_word) ⇒ Object

This method should UNconditionally apply preprocessing to word ONLY if word meets the criteria for preprocessing (.preprocess?).

This method should return the following Array:

<preprocessed?>, <preprocessed word | nil>

Where:

<preprocessed?> == whether or not the word was preprocessed
  based on whether or not the word meets the preprocessing
  criteria (.preprocess?).

<preprocessed word | nil> == the preprocessed word (if word
  met the preprocessing criteria (.preprocessed?)) or nil if
  word was NOT preprocessed (word did NOT meet the preprocessing
  criteria).


58
59
60
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 58

def preprocess(_word)
  raise Errors::MustOverrideError
end

.preprocess?(_word) ⇒ Boolean

Should return true if word matches the preprocess criteria; false, otherwise. If this preprocessor has no preprocess criteria, simply return true. This class method is unlike the instance method in that it does not consider whether or not this preprocessor is “on” or “off”; it simply returns true or false based on whether or not the word matches the preprocess criteria.

Returns:

  • (Boolean)


37
38
39
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 37

def preprocess?(_word)
  true
end

Instance Method Details

#preprocess(word) ⇒ Object

Applies preprocessing to word if this preprocessor is “on” AND if word meets the criteria for preprocessing; no preprocessing is applied to word otherwise.

This method should return a Preprocessors::PreprocessedWord object.



84
85
86
87
88
89
90
91
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 84

def preprocess(word)
  preprocessed, preprocessed_word = if preprocessor_on?
    self.class.preprocess word
  else
    [false, nil]
  end
  preprocessed_word(original_word: word, preprocessed_word: preprocessed_word, preprocessed: preprocessed)
end

#preprocess?(word) ⇒ Boolean

Returns true if word meets the criteria for preprocessing. false is returned if word does not meet the criteria for preprocessing, or, if the preprocessor is “off”.

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 73

def preprocess?(word)
  return false if preprocessor_off?

  self.class.preprocess? word
end

#preprocessed_word(original_word:, preprocessed:, preprocessed_word:) ⇒ Object (private)



116
117
118
119
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 116

def preprocessed_word(original_word:, preprocessed:, preprocessed_word:)
  PreprocessedWord.new(original_word: original_word, preprocessed: preprocessed,
    preprocessed_word: preprocessed_word, preprocessor: to_sym, preprocessor_order: order)
end

#preprocessor_off!Object



110
111
112
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 110

def preprocessor_off!
  @preprocessor_on = false
end

#preprocessor_off?Boolean

Returns true if this preprocessor is “off”. Preprocessing should not be applied to a word if this preprocessor is “off”.

Returns:

  • (Boolean)


106
107
108
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 106

def preprocessor_off?
  !preprocessor_on?
end

#preprocessor_on!Object



100
101
102
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 100

def preprocessor_on!
  @preprocessor_on = true
end

#preprocessor_on?Boolean

Returns true if this preprocessor is “on”; false, otherwise. If this preprocessor is “on”, preprocessing should be applied to a word if word meets the criteria for preprocessing.

Returns:

  • (Boolean)


96
97
98
# File 'lib/LittleWeasel/preprocessors/word_preprocessor.rb', line 96

def preprocessor_on?
  preprocessor_on
end