Class: Words::Homographs

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

Constant Summary collapse

POS_TO_SYMBOL =
{"n" => :noun, "v" => :verb, "a" => :adjective, "r" => :adverb}
SYMBOL_TO_POS =
POS_TO_SYMBOL.invert

Instance Method Summary collapse

Constructor Details

#initialize(raw_homographs, wordnet_connection) ⇒ Homographs

Returns a new instance of Homographs.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/homographs.rb', line 13

def initialize(raw_homographs, wordnet_connection)

  @wordnet_connection = wordnet_connection
  @raw_homographs = raw_homographs

  # construct some conveniance menthods for relation type access
  SYMBOL_TO_POS.keys.each do |pos|
    self.class.send(:define_method, "#{pos}s?") do
      size(pos) > 0
    end
    self.class.send(:define_method, "#{pos}s") do
      synsets(pos)
    end
    self.class.send(:define_method, "#{pos}_count") do
      size(pos)
    end
    self.class.send(:define_method, "#{pos}_ids") do
      synset_ids(pos)
    end
  end
  
end

Instance Method Details

#available_posObject Also known as: pos



48
49
50
51
52
# File 'lib/homographs.rb', line 48

def available_pos

  @available_pos ||= synset_ids.map { |synset_id| POS_TO_SYMBOL[synset_id[0,1]] }.uniq

end

#inspectObject



87
88
89
90
91
# File 'lib/homographs.rb', line 87

def inspect

  @raw_homographs.inspect

end

#lemmaObject Also known as: word



42
43
44
45
46
# File 'lib/homographs.rb', line 42

def lemma

  @lemma ||= @raw_homographs["lemma"].gsub('_', ' ')

end

#size(pos = :all) ⇒ Object



60
61
62
63
64
# File 'lib/homographs.rb', line 60

def size(pos = :all)

  synset_ids(pos).size

end

#synset_ids(pos = :all) ⇒ Object Also known as: sense_ids



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/homographs.rb', line 72

def synset_ids(pos = :all)

  @synset_ids ||= @raw_homographs["synset_ids"].split('|')

  case
  when SYMBOL_TO_POS.include?(pos.to_sym)
    @synset_ids.select { |synset_id| synset_id[0,1] == SYMBOL_TO_POS[pos.to_sym] }
  when POS_TO_SYMBOL.include?(pos.to_s)
    @synset_ids.select { |synset_id| synset_id[0,1] == pos.to_s }
  else
    @synset_ids
  end

end

#synsets(pos = :all) ⇒ Object Also known as: senses



66
67
68
69
70
# File 'lib/homographs.rb', line 66

def synsets(pos = :all)

  synset_ids(pos).map { |synset_id| Synset.new synset_id, @wordnet_connection, self }

end

#tagsense_countsObject



36
37
38
39
40
# File 'lib/homographs.rb', line 36

def tagsense_counts

  @tagsense_counts ||= @raw_homographs["tagsense_counts"].split('|').map { |count| { POS_TO_SYMBOL[count[0,1]] => count[1..-1].to_i }  }

end

#to_sObject



54
55
56
57
58
# File 'lib/homographs.rb', line 54

def to_s

  @to_s ||= [lemma, " " + available_pos.join("/")].join(",")

end