Class: Treat::Workers::Lexicalizers::Sensers::Wordnet

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/lexicalizers/sensers/wordnet.rb

Overview

Sense information (synonyms, antonyms, hypernyms and hyponyms) obtained through a Ruby parser that accesses Wordnet flat files.

Original paper: George A. Miller (1995). WordNet: A Lexical Database for English. Communications of the ACM Vol. 38, No. 11: 39-41.

Constant Summary collapse

@@indexes =

Noun, adjective and verb indexes.

{}

Class Method Summary collapse

Class Method Details

.sense(word, options = nil) ⇒ Object

Obtain lexical information about a word using the ruby ‘wordnet’ gem.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/treat/workers/lexicalizers/sensers/wordnet.rb', line 28

def self.sense(word, options = nil)
  
  category = word.check_has(:category)
  
  if !options[:nym] 
    raise Treat::Exception, "You must supply " +
    "the :nym option ('synonyms', 'hypernyms', etc.)"
  end

  if !options[:nym].is_a?(Symbol)
    options[:nym] = options[:nym].intern
  end

  if ![:synonyms, :antonyms,
    :hypernyms, :hyponyms].include?(options[:nym])
    raise Treat::Exception, "You must supply " +
    "a valid :nym option ('synonyms', 'hypernyms', etc.)" 
  end
  
  unless ['noun', 'adjective', 'verb'].
    include?(word.category)
    return []
  end
  
  cat = category.to_s.capitalize
  
  @@indexes[cat] ||= 
  ::WordNet.const_get(cat + 'Index').instance
  lemma = @@indexes[cat].find(word.value.downcase)

  return [] if lemma.nil?
  synsets = []
  
  lemma.synsets.each do |synset|
    synsets << 
    Treat::Workers::Lexicalizers::Sensers::Wordnet::Synset.new(synset)
  end
  
  ((synsets.collect do |ss|
    ss.send(options[:nym])
  end - [word.value]).
  flatten).uniq.map do |token|
    token.gsub('_', ' ')
  end
end