Class: WordNet::Synset

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

Overview

Represents a synset (or group of synonymous words) in WordNet. Synsets are related to each other by various (and numerous!) relationships, including Hypernym (x is a hypernym of y <=> x is a parent of y) and Hyponym (x is a child of y)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos, offset) ⇒ Synset

Create a new synset by reading from the data file specified by pos, at offset bytes into the file. This is how the WordNet database is organized. You shouldn’t be creating Synsets directly; instead, use Lemma#synsets.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/wordnet/synset.rb', line 10

def initialize(pos, offset)
  data = File.open(File.join(WordNetDB.path,"dict","data.#{SynsetType[pos]}"),"r")
  data.seek(offset)
  data_line = data.readline.strip
  data.close
  
  info_line, @gloss = data_line.split(" | ")
  line = info_line.split(" ")
  
  @synset_offset = line.shift
  @lex_filenum = line.shift
  @ss_type = line.shift
  @w_cnt = line.shift.to_i
  @wordcounts = {}
  @w_cnt.times do
    @wordcounts[line.shift] = line.shift.to_i
  end
  
  @p_cnt = line.shift.to_i
  @pointers = []
  @p_cnt.times do
    pointer = Pointer.new
    pointer[:symbol] = line.shift,
    pointer[:offset] = line.shift.to_i
    pointer[:pos] = line.shift
    pointer[:source] = line.shift
    pointer[:is_semantic?] = (pointer[:source] == "0000")
    pointer[:target] = pointer[:source][2..3]
    pointer[:source] = pointer[:source][0..1]
    pointer[:symbol] = pointer[:symbol][0]
    @pointers.push pointer
  end
end

Instance Attribute Details

#glossObject (readonly)

Returns the value of attribute gloss.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def gloss
  @gloss
end

#lex_filenumObject (readonly)

Returns the value of attribute lex_filenum.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def lex_filenum
  @lex_filenum
end

#ss_typeObject (readonly)

Returns the value of attribute ss_type.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def ss_type
  @ss_type
end

#synset_offsetObject (readonly)

Returns the value of attribute synset_offset.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def synset_offset
  @synset_offset
end

#w_cntObject (readonly)

Returns the value of attribute w_cnt.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def w_cnt
  @w_cnt
end

#wordcountsObject (readonly)

Returns the value of attribute wordcounts.



6
7
8
# File 'lib/wordnet/synset.rb', line 6

def wordcounts
  @wordcounts
end

Instance Method Details

#antonymObject

Get the Synset of this sense’s antonym



60
61
62
# File 'lib/wordnet/synset.rb', line 60

def antonym
  get_relation(Antonym)
end

#expanded_hypernymObject

Get the entire hypernym tree (from this synset all the way up to entity) as an array.



75
76
77
78
79
80
# File 'lib/wordnet/synset.rb', line 75

def expanded_hypernym
  parent = self.hypernym
  return [] if parent.nil?
  
  return [parent, parent.expanded_hypernym].flatten
end

#get_relation(pointer_symbol) ⇒ Object

List of valid pointer_symbols is in pointers.rb



55
56
57
# File 'lib/wordnet/synset.rb', line 55

def get_relation(pointer_symbol)
  @pointers.reject { |pointer| pointer.symbol != pointer_symbol }.map { |pointer| Synset.new(@ss_type, pointer.offset) }
end

#hypernymObject Also known as: parent

Get the parent synset (higher-level category, i.e. fruit -> reproductive_structure).



65
66
67
# File 'lib/wordnet/synset.rb', line 65

def hypernym
  get_relation(Hypernym)[0]
end

#hyponymObject Also known as: children

Get the child synset(s) (i.e., lower-level categories, i.e. fruit -> edible_fruit)



70
71
72
# File 'lib/wordnet/synset.rb', line 70

def hyponym
  get_relation(Hyponym)
end

#sizeObject

How many words does this Synset include?



45
46
47
# File 'lib/wordnet/synset.rb', line 45

def size
  @wordcounts.size
end

#to_sObject



82
83
84
# File 'lib/wordnet/synset.rb', line 82

def to_s
  "(#{@ss_type}) #{words.map {|x| x.gsub('_',' ')}.join(', ')} (#{@gloss})"
end

#wordsObject

Get a list of words included in this Synset



50
51
52
# File 'lib/wordnet/synset.rb', line 50

def words
  @wordcounts.keys
end