Merge pull request #105 from trosborn/master

fixes errors caused by wordnet 1.0.0
This commit is contained in:
Louis Mullie 2015-04-06 16:34:48 -04:00
commit f61adc8187
1 changed files with 22 additions and 24 deletions

View File

@ -1,35 +1,35 @@
# Sense information (synonyms, antonyms, hypernyms # Sense information (synonyms, antonyms, hypernyms
# and hyponyms) obtained through a Ruby parser that # and hyponyms) obtained through a Ruby parser that
# accesses Wordnet flat files. # accesses Wordnet flat files.
# #
# Original paper: George A. Miller (1995). WordNet: # Original paper: George A. Miller (1995). WordNet:
# A Lexical Database for English. Communications of # A Lexical Database for English. Communications of
# the ACM Vol. 38, No. 11: 39-41. # the ACM Vol. 38, No. 11: 39-41.
class Treat::Workers::Lexicalizers::Sensers::Wordnet class Treat::Workers::Lexicalizers::Sensers::Wordnet
# Require the 'wordnet' gem (install as 'rwordnet'). # Require the 'wordnet' gem (install as 'rwordnet').
require 'wordnet' require 'wordnet'
# Patch for bug. # Patch for bug.
::WordNet.module_eval do ::WordNet.module_eval do
remove_const(:SynsetType) remove_const(:SYNSET_TYPES)
const_set(:SynsetType, const_set(:SYNSET_TYPES,
{"n" => "noun", "v" => "verb", "a" => "adj"}) {"n" => "noun", "v" => "verb", "a" => "adj"})
end end
# Require an adaptor for Wordnet synsets. # Require an adaptor for Wordnet synsets.
require_relative 'wordnet/synset' require_relative 'wordnet/synset'
# Noun, adjective and verb indexes. # Noun, adjective and verb indexes.
@@indexes = {} @@indexes = {}
# Obtain lexical information about a word using the # Obtain lexical information about a word using the
# ruby 'wordnet' gem. # ruby 'wordnet' gem.
def self.sense(word, options = nil) def self.sense(word, options = nil)
category = word.check_has(:category) category = word.check_has(:category)
if !options[:nym] if !options[:nym]
raise Treat::Exception, "You must supply " + raise Treat::Exception, "You must supply " +
"the :nym option ('synonyms', 'hypernyms', etc.)" "the :nym option ('synonyms', 'hypernyms', etc.)"
end end
@ -37,32 +37,30 @@ class Treat::Workers::Lexicalizers::Sensers::Wordnet
if !options[:nym].is_a?(Symbol) if !options[:nym].is_a?(Symbol)
options[:nym] = options[:nym].intern options[:nym] = options[:nym].intern
end end
if ![:synonyms, :antonyms, if ![:synonyms, :antonyms,
:hypernyms, :hyponyms].include?(options[:nym]) :hypernyms, :hyponyms].include?(options[:nym])
raise Treat::Exception, "You must supply " + raise Treat::Exception, "You must supply " +
"a valid :nym option ('synonyms', 'hypernyms', etc.)" "a valid :nym option ('synonyms', 'hypernyms', etc.)"
end end
unless ['noun', 'adjective', 'verb']. unless ['noun', 'adjective', 'verb'].
include?(word.category) include?(word.category)
return [] return []
end end
cat = category.to_s.capitalize cat = category.to_s.capitalize
@@indexes[cat] ||= lemma = ::WordNet::Lemma.find(word.value.downcase, cat.to_sym)
::WordNet.const_get(cat + 'Index').instance
lemma = @@indexes[cat].find(word.value.downcase)
return [] if lemma.nil? return [] if lemma.nil?
synsets = [] synsets = []
lemma.synsets.each do |synset| lemma.synsets.each do |synset|
synsets << synsets <<
Treat::Workers::Lexicalizers::Sensers::Wordnet::Synset.new(synset) Treat::Workers::Lexicalizers::Sensers::Wordnet::Synset.new(synset)
end end
((synsets.collect do |ss| ((synsets.collect do |ss|
ss.send(options[:nym]) ss.send(options[:nym])
end - [word.value]). end - [word.value]).
@ -71,4 +69,4 @@ class Treat::Workers::Lexicalizers::Sensers::Wordnet
end end
end end
end end