Class: TextNlp::Synonyms

Inherits:
Object
  • Object
show all
Defined in:
lib/text_nlp/synonyms.rb

Instance Method Summary collapse

Constructor Details

#initialize(synonyms = []) ⇒ Synonyms

Returns a new instance of Synonyms.



6
7
8
9
10
11
12
13
# File 'lib/text_nlp/synonyms.rb', line 6

def initialize(synonyms = [])
  @synonyms = {}
  @expressions = Expressions.new
  synonyms.each do |synos|
    name = synos.shift
    register(name,synos)
  end
end

Instance Method Details

#register(name, synonyms) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/text_nlp/synonyms.rb', line 15

def register(name,synonyms)
  name.normalize!
  synonyms.each do |synonym|
    synonym.normalize!
    @expressions << synonym
    @synonyms[synonym] = name
  end
end

#transform(text) ⇒ Object



24
25
26
# File 'lib/text_nlp/synonyms.rb', line 24

def transform(text)
  @expressions.expressionize(text).map { |expr| @synonyms.key?(expr) ? @synonyms[expr] : expr }.compact.join(' ')
end