Class: Words::TokyoWordnetConnection

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

Overview

Provides a pure tokyo cabinate connector to the Wordnet dataset.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_path, wordnet_path) ⇒ PureWordnetConnection

Constructs a new tokyo ruby connector for use with the words wordnet class.

Parameters:

  • data_path (Pathname)

    Specifies the directory within which constructed datasets can be found (tokyo index, evocations etc…)

  • wordnet_path (Pathname)

    Specifies the directory within which the wordnet dictionary can be found.

Raises:



43
44
45
46
47
48
49
50
51
52
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 43

def initialize(data_path, wordnet_path)

    @data_path, @wordnet_path, @connection_type, @connected = data_path + 'wordnet.tct', wordnet_path, :tokyo, false

    # ensure we have the rufus gem loaded, else there is little point in continuing...
    raise BadWordnetConnector, "Coulden't find the rufus-tokyo gem. Please ensure it's installed." unless Gem.available?('rufus-tokyo')

    open!

end

Instance Attribute Details

#connectedtrue, false (readonly) Also known as: connected?

Returns the current connection status of the wordnet object.

Returns:

  • (true, false)

    The current connection status of the wordnet object.



15
16
17
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 15

def connected
  @connected
end

#connection_typeSymbol (readonly)

Returns the type of the current wordnet connection.

Returns:

  • (Symbol)

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



25
26
27
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 25

def connection_type
  @connection_type
end

#data_pathPathname? (readonly)

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.



30
31
32
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 30

def data_path
  @data_path
end

#wordnet_pathPathname? (readonly)

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.



35
36
37
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 35

def wordnet_path
  @wordnet_path
end

Instance Method Details

#close!Object

Causes the current connection to wordnet to be closed.



74
75
76
77
78
79
80
81
82
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 74

def close!

    if connected?
  @connection.close
  @connected = false
    end
    return nil

end

#evocations(synset_id) ⇒ Object

Locates from a synset id any relevent evocations and constructs an evocations hash.

Parameters:

  • senset_id (String)

    The id number of a specific synset.

Raises:

See Also:



123
124
125
126
127
128
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 123

def evocations(synset_id)

    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?
    @connection[synset_id + "s"]

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.



111
112
113
114
115
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 111

def evocations?

    !evocations('n08112402').nil?

end

#homographs(term) ⇒ Object

Locates from a term any relevent homographs and constructs a homographs hash.

Parameters:

  • term (String)

    The specific term that is desired from within wordnet.

Raises:



89
90
91
92
93
94
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 89

def homographs(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?
    @connection[term]
    
end

#open!Object

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

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 57

def open!

    unless connected?
  if @data_path.exist?
      @connection = Rufus::Tokyo::Table.new(@data_path.to_s, :mode => 'r')
      @connected = true
  else
      @connected = false
      raise BadWordnetDataset, "Failed to locate the tokyo words dataset at #{@data_path}. Please insure you have created it using the words gems provided 'build_wordnet' command."
  end
    end
    return nil

end

#synset(synset_id) ⇒ Object

Locates from a synset_id a specific synset and constructs a synset hash.

Parameters:

  • synset_id (String)

    The synset id to locate.

Raises:



101
102
103
104
105
106
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 101

def synset(synset_id)

    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?
    @connection[synset_id]

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 tokyo mode with dataset at /opt/wordnet”



133
134
135
136
137
# File 'lib/wordnet_connectors/tokyo_wordnet_connection.rb', line 133

def to_s

    "Words running in tokyo mode with dataset at #{@dataset_path}"

end