Class: WordNet::Lemma

Inherits:
Object
  • Object
show all
Defined in:
lib/wordnet/lemma.rb

Overview

Represents a single word in the WordNet lexicon, which can be used to look up a set of synsets.

Constant Summary collapse

SPACE =
' '
@@cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lexicon_line, id) ⇒ Lemma

Create a lemma from a line in an lexicon file. You should be creating Lemmas by hand; instead, use the WordNet::Lemma.find and WordNet::Lemma.find_all methods to find the Lemma for a word.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wordnet/lemma.rb', line 9

def initialize(lexicon_line, id)
  @id = id
  line = lexicon_line.split(" ")

  @word = line.shift
  @pos = line.shift
  synset_count = line.shift.to_i
  @pointer_symbols = line.slice!(0, line.shift.to_i)
  line.shift # Throw away redundant sense_cnt
  @tagsense_count = line.shift.to_i
  @synset_offsets = line.slice!(0, synset_count).map(&:to_i)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def id
  @id
end

#pointer_symbolsObject

Returns the value of attribute pointer_symbols.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def pointer_symbols
  @pointer_symbols
end

#posObject

Returns the value of attribute pos.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def pos
  @pos
end

#synset_offsetsObject

Returns the value of attribute synset_offsets.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def synset_offsets
  @synset_offsets
end

#tagsense_countObject

Returns the value of attribute tagsense_count.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def tagsense_count
  @tagsense_count
end

#wordObject

Returns the value of attribute word.



5
6
7
# File 'lib/wordnet/lemma.rb', line 5

def word
  @word
end

Class Method Details

.find(word, pos) ⇒ Object

Find a lemma for a given word and pos



41
42
43
44
45
46
# File 'lib/wordnet/lemma.rb', line 41

def find(word, pos)
  cache = @@cache[pos] ||= build_cache(pos)
  if found = cache[word]
    Lemma.new(*found)
  end
end

.find_all(word) ⇒ Object



34
35
36
37
38
# File 'lib/wordnet/lemma.rb', line 34

def find_all(word)
  [:noun, :verb, :adj, :adv].flat_map do |pos|
    find(word, pos) || []
  end
end

Instance Method Details

#synsetsObject

Return a list of synsets for this Lemma. Each synset represents a different sense, or meaning, of the word.



23
24
25
# File 'lib/wordnet/lemma.rb', line 23

def synsets
  @synset_offsets.map { |offset| Synset.new(@pos, offset) }
end

#to_sObject



27
28
29
# File 'lib/wordnet/lemma.rb', line 27

def to_s
  [@word, @pos].join(",")
end