Class: WordNet::WordNetDB

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

Overview

Represents the WordNet database, and provides some basic interaction.

Constant Summary collapse

@@path =

By default, use the bundled WordNet

File.join(File.dirname(__FILE__),"/../../WordNet-3.0/")
@@files =
{}

Class Method Summary collapse

Class Method Details

.closeObject

You should call this method after you’re done using WordNet.



39
40
41
# File 'lib/wordnet/wordnetdb.rb', line 39

def WordNetDB.close
  WordNetDB.finalize(0)
end

.finalize(id) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/wordnet/wordnetdb.rb', line 43

def WordNetDB.finalize(id)
  @@files.each_value do |handle| 
    begin
      handle.close
    rescue IOError
      ; # Keep going, close the next file.
    end
  end
end

.find(word) ⇒ Object

Look up a word in WordNet. Returns a list of lemmas occuring in any of the index files (noun, verb, adjective, adverb).



20
21
22
23
24
25
26
# File 'lib/wordnet/wordnetdb.rb', line 20

def WordNetDB.find(word)
  lemmas = []
  [NounIndex, VerbIndex, AdjectiveIndex, AdverbIndex].each do |index|
    lemmas.push index.instance.find(word)
  end
  return lemmas.flatten.reject { |x| x.nil? }
end

.open(path) ⇒ Object

Register a new DB file handle. You shouldn’t need to call this method; it’s called automatically every time you open an index or data file.



29
30
31
32
33
34
35
36
# File 'lib/wordnet/wordnetdb.rb', line 29

def WordNetDB.open(path)
  # If the file is already open, just return the handle.
  return @@files[path] if @@files.include?(path) and not @@files[path].closed?
  
  # Open and store 
  @@files[path] = File.open(path,"r")
  return @@files[path]
end

.pathObject

Returns the path to the WordNet installation currently in use. Defaults to the bundled version of WordNet.



15
16
17
# File 'lib/wordnet/wordnetdb.rb', line 15

def WordNetDB.path
  @@path
end

.path=(path_to_wordnet) ⇒ Object

To use your own WordNet installation (rather than the one bundled with rwordnet:



10
11
12
# File 'lib/wordnet/wordnetdb.rb', line 10

def WordNetDB.path=(path_to_wordnet)
  @@path = path_to_wordnet
end