Module: Linguistics::EN::WordNet
- Extended by:
- SingletonMethods
- Defined in:
- lib/linguistics/en/wordnet.rb
Overview
WordNet support for the English-language Linguistics module. It requires the Ruby-WordNet module to be installed; if it is not installed, calling the functions defined by this file will raise NotImplementedErrors.
# Test to be sure the WordNet module loaded okay.
Linguistics::EN.has_wordnet?
# => true
# Fetch the default synset for the word "balance"
"balance".en.synset
# => #<WordNet::Synset:0x40376844 balance (noun): "a state of equilibrium"
(derivations: 3, antonyms: 1, hypernyms: 1, hyponyms: 3)>
# Fetch the synset for the first verb sense of "balance"
"balance".en.synset( :verb )
# => #<WordNet::Synset:0x4033f448 balance, equilibrate, equilibrize, equilibrise
(verb): "bring into balance or equilibrium; "She has to balance work and her
domestic duties"; "balance the two weights"" (derivations: 7, antonyms: 1,
verbGroups: 2, hypernyms: 1, hyponyms: 5)>
# Fetch the second noun sense
"balance".en.synset( 2, :noun )
# => #<WordNet::Synset:0x404ebb24 balance (noun): "a scale for weighing; depends
on pull of gravity" (hypernyms: 1, hyponyms: 5)>
# Fetch the second noun sense's hypernyms (more-general words, like a superclass)
"balance".en.synset( 2, :noun ).hypernyms
# => [#<WordNet::Synset:0x404e5620 scale, weighing machine (noun): "a measuring
instrument for weighing; shows amount of mass" (derivations: 2, hypernyms: 1,
hyponyms: 2)>]
# A simpler way of doing the same thing:
"balance".en.hypernyms( 2, :noun )
# => [#<WordNet::Synset:0x404e5620 scale, weighing machine (noun): "a measuring
instrument for weighing; shows amount of mass" (derivations: 2, hypernyms: 1,
hyponyms: 2)>]
# Fetch the first hypernym's hypernyms
"balance".en.synset( 2, :noun ).hypernyms.first.hypernyms
# => [#<WordNet::Synset:0x404c60b8 measuring instrument, measuring system,
measuring device (noun): "instrument that shows the extent or amount or quantity
or degree of something" (hypernyms: 1, hyponyms: 83)>]
# Find the synset to which both the second noun sense of "balance" and the
# default sense of "shovel" belong.
("balance".en.synset( 2, :noun ) | "shovel".en.synset)
# => #<WordNet::Synset:0x40473da4 instrumentality, instrumentation (noun): "an
artifact (or system of artifacts) that is instrumental in accomplishing some
end" (derivations: 1, hypernyms: 1, hyponyms: 13)>
# Fetch just the words for the other kinds of "instruments"
"instrument".en.hyponyms.collect {|synset| synset.words}.flatten
# => ["analyzer", "analyser", "cautery", "cauterant", "drafting instrument",
"extractor", "instrument of execution", "instrument of punishment", "measuring
instrument", "measuring system", "measuring device", "medical instrument",
"navigational instrument", "optical instrument", "plotter", "scientific
instrument", "sonograph", "surveying instrument", "surveyor's instrument",
"tracer", "weapon", "arm", "weapon system", "whip"]
Defined Under Namespace
Modules: SingletonMethods
Class Method Summary collapse
-
.def_synset_function(name) ⇒ Object
Make a function that calls the method
meth
on the synset of an input word. -
.lexicon ⇒ Object
The instance of the WordNet::Lexicon used for all Linguistics WordNet functions.
-
.lexicon=(newlex) ⇒ Object
Set the WordNet::Lexicon used by the linguistic functions.
Instance Method Summary collapse
-
#synset(*args) ⇒ Object
Look up the synset associated with the given word or collocation in the WordNet lexicon and return a WordNet::Synset object.
-
#synsets(*args) ⇒ Object
Look up all the synsets associated with the given word or collocation in the WordNet lexicon and return an Array of WordNet::Synset objects.
Methods included from SingletonMethods
Class Method Details
.def_synset_function(name) ⇒ Object
Make a function that calls the method meth
on the synset of an input word.
118 119 120 121 122 123 |
# File 'lib/linguistics/en/wordnet.rb', line 118 def self::def_synset_function( name ) define_method( name ) do |*criteria| syn = self.synset( *criteria ) or return nil return syn.send( name ) end end |
.lexicon ⇒ Object
The instance of the WordNet::Lexicon used for all Linguistics WordNet functions.
104 105 106 107 |
# File 'lib/linguistics/en/wordnet.rb', line 104 def self::lexicon raise self.wordnet_error unless self.has_wordnet? @lexicon ||= WordNet::Lexicon::new end |
.lexicon=(newlex) ⇒ Object
Set the WordNet::Lexicon used by the linguistic functions.
111 112 113 |
# File 'lib/linguistics/en/wordnet.rb', line 111 def self::lexicon=( newlex ) @lexicon = newlex end |
Instance Method Details
#synset(*args) ⇒ Object
Look up the synset associated with the given word or collocation in the WordNet lexicon and return a WordNet::Synset object.
137 138 139 |
# File 'lib/linguistics/en/wordnet.rb', line 137 def synset( *args ) return Linguistics::EN::WordNet.lexicon[ self.to_s, *args ] end |
#synsets(*args) ⇒ Object
Look up all the synsets associated with the given word or collocation in the WordNet lexicon and return an Array of WordNet::Synset objects. If pos
is nil
, return synsets for all parts of speech.
145 146 147 |
# File 'lib/linguistics/en/wordnet.rb', line 145 def synsets( *args ) return Linguistics::EN::WordNet.lexicon.lookup_synsets( self.to_s, *args ) end |