Class: Wordfor::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/wordfor/lookup.rb

Instance Method Summary collapse

Constructor Details

#initializeLookup

Returns a new instance of Lookup.



6
7
8
9
# File 'lib/wordfor/lookup.rb', line 6

def initialize
  @lexicon = WordNet::Lexicon.new

end

Instance Method Details

#lookup(word) ⇒ Object

Based on example code:

lex = WordNet::Lexicon.new lex.lookup_synsets(‘time’).map {|o|

o.traverse(:hyponyms).with_index.map {|ss, i|
  [ss.part_of_speech.to_sym, ss.words.map(&:lemma)]
}

}.reject {|l| l.size == 0}



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wordfor/lookup.rb', line 20

def lookup(word)
  @input_word = word
  @visited = {}
  @pos_map = {}

  origins = @lexicon.lookup_synsets word

  origins.each {|o|
    break if visited?(o)
    add_synset_to_map o, :synonyms

    o.traverse(:hyponyms).with_index.each {|ss|
      break if visited?(ss)
      add_synset_to_map ss, :hyponyms
    }

    o.traverse(:hypernyms).with_index.each {|ss|
      break if visited?(ss)
      add_synset_to_map ss, :hypernyms
    }

    o.traverse(:antonyms).with_index.each {|ss|
      break if visited?(ss)
      add_synset_to_map ss, :antonyms
    }
  }

  @pos_map
end