Class: Words::Wordnet

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

Overview

The wordnet class provides a control come interface for interaction with the wordnet dataset of your choice. It creates a connection, based on specified paramaters, to a wordnet dataset and provides the means to interigate that dataset. In addition it provides control and information about that wordnet connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connector_type = :pure, wordnet_path = :search, data_path = :default) ⇒ Wordnet

Constructs a new wordnet connection object.

Parameters:

  • connector_type (Symbol) (defaults to: :pure)

    Specifies the connector type or mode desired. Current supported connectors are :pure and :tokyo.

  • wordnet_path (String, Symbol) (defaults to: :search)

    Specifies the directory within which the wordnet dictionary can be found. It can be set to :search to attempt to locate wordnet automatically.

  • data_path (String, Symbol) (defaults to: :default)

    Specifies the directory within which constructed datasets can be found (tokyo index, evocations etc…) It can be set to :default to use the standard location inside the gem directory.

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/words.rb', line 40

def initialize(connector_type = :pure, wordnet_path = :search, data_path = :default)

    # Check and specify useful paths
    wordnet_path = Wordnet::locate_wordnet(wordnet_path)
    data_path = (data_path == :default ? Pathname.new(File.join(File.dirname(__FILE__), '..', 'data')) : Pathname.new( data_path ))

    # Ensure we have a valid connector type
    raise BadWordnetConnector, "You specified an unsupported wordnet connector type. Supported connectors are: #{SUPPORTED_CONNECTIORS}" unless SUPPORTED_CONNECTIORS.include? connector_type

    # We can assume that the disired connector is now available
    desired_connector = SUPPORTED_CONNECTIORS[connector_type]

    # Assuming we have a valid connection type we can import the relevant code (the reason we do this dynamically is to reduce loadtime)
    require desired_connector
     
    # Construct the connector object
    @wordnet_connection = Words.const_get( File.basename(desired_connector, '.rb').gsub(/(^|_)(.)/) { $2.upcase } ).new(data_path, wordnet_path)
    
end

Instance Attribute Details

#wordnet_connectionPureWordnetConnection, TokyoWordnetConnection (readonly)

Returns the underlying wordnet connection object.

Returns:



31
32
33
# File 'lib/words.rb', line 31

def wordnet_connection
  @wordnet_connection
end

Instance Method Details

#close!Object

Causes the current connection to wordnet to be closed.



102
103
104
105
106
# File 'lib/words.rb', line 102

def close!

    @wordnet_connection.close!

end

#connected?true, false

Returns the current connection status of the wordnet object.

Returns:

  • (true, false)

    The current connection status of the wordnet object.



119
120
121
122
123
# File 'lib/words.rb', line 119

def connected?

    @wordnet_connection.connected?

end

#connection_typeSymbol

Returns the type of the current wordnet connection.

Returns:

  • (Symbol)

    The current wordnet connection type. Currently supported :pure & :tokyo.



76
77
78
79
80
# File 'lib/words.rb', line 76

def connection_type

    @wordnet_connection.connection_type

end

#data_pathPathname?

Returns the datapath currently in use (this may be irrelevent when using the pure connector and thus could be nil.)

Returns:

  • (Pathname, nil)

    The path to the data directory currently in use. Returns nil if unknown.



94
95
96
97
98
# File 'lib/words.rb', line 94

def data_path

    @wordnet_connection.data_path

end

#evocations?true, false

Returns wheter evocations are currently avalable to use with the current wordnet object. (More information on setting these up can be found within the README)

Returns:

  • (true, false)

    Whether evocations are currently available or not.



128
129
130
131
132
# File 'lib/words.rb', line 128

def evocations?

    @wordnet_connection.evocations?

end

#find(term) ⇒ Homographs

Locates the set of homographs within wordnet specific to the term entered.

Parameters:

  • term (String)

    The specific term that is desired from within wordnet. This is caps insensative & we do a small amount of cleanup.

Returns:

  • (Homographs)

    An object encaptulating the homographs of the desired term. If the term cannot be located within wordnet then nil is returned.

Raises:



65
66
67
68
69
70
71
# File 'lib/words.rb', line 65

def find(term)

    raise NoWordnetConnection, "There is presently no connection to wordnet. To attempt to reistablish a connection you should use the 'open!' command on the Wordnet object." unless connected?
    homographs = @wordnet_connection.homographs(term)
    Homographs.new(homographs, @wordnet_connection) unless homographs.nil?

end

#open!Object

Causes the connection specified within the wordnet object to be reopened if currently closed.



110
111
112
113
114
# File 'lib/words.rb', line 110

def open!

    @wordnet_connection.open!

end

#to_sString

Provides a textural description of the current connection state of the Wordnet object.

Returns:

  • (String)

    A textural description of the current connection state of the Wordnet object. e.g. “Words not Connected” or “Words running in pure mode using wordnet files found at /opt/wordnet”



137
138
139
140
141
142
# File 'lib/words.rb', line 137

def to_s

    # Return a description of the connector
    !connected? ? "Words not connected" : @wordnet_connection.to_s

end

#wordnet_pathPathname?

Returns the path to the wordnet collection currently in use (this may be irrelevent when using the tokyo connector and thus could be nil.)

Returns:

  • (Pathname, nil)

    The path to the wordnet collection currently in use. Returns nil if unknown.



85
86
87
88
89
# File 'lib/words.rb', line 85

def wordnet_path

    @wordnet_connection.wordnet_path

end