Class: Natural::Synonym

Inherits:
Alternative show all
Defined in:
lib/natural/fragment.rb

Instance Attribute Summary

Attributes inherited from Fragment

#aggregator, #filter, #score, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Alternative

#score

Methods inherited from Fragment

#all_filters, #clone, #data, #id_range, #ids, #ids=, #initialize, #pretty_to_s, recurse_alternatives, #to_s

Constructor Details

This class inherits a constructor from Natural::Fragment

Class Method Details

.find(options) ⇒ Object



297
298
299
# File 'lib/natural/fragment.rb', line 297

def self.find(options)
  super options.merge(:looking_for => options[:synonyms].values.flatten)
end

Instance Method Details

#replacements(options) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/natural/fragment.rb', line 300

def replacements(options)
  options[:synonyms].values.each do |alternatives|
    if alternatives.include?(self.to_s.singularize.downcase)
      return (alternatives - [self.to_s]).map do |alternative_text|
        if self.node_height == 0
          alternative_text = alternative_text.pluralize if self.to_s.plural?
          alternative = self.class.new(:ids => self.ids, :text => alternative_text)
          alternative << Fragment.new(:ids => (alternative.ids.first..alternative.ids.last).to_a, :text => options[:text].split(' ')[alternative.ids.first..alternative.ids.last].join(' '))
          alternative
        else
          return [self]
        end
      end
    end
  end
end