Changeset 86 for trunk/lib/wordnet/lexicon.rb
- Timestamp:
- 07/08/08 07:25:54 (5 months ago)
- Files:
-
- 1 modified
-
trunk/lib/wordnet/lexicon.rb (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/wordnet/lexicon.rb
r79 r86 31 31 32 32 require 'rbconfig' 33 require 'pathname' 33 34 require 'bdb' 34 35 require 'sync' … … 65 66 # The path to the WordNet BerkeleyDB Env. It lives in the directory that 66 67 # this module is in. 67 D efaultDbEnv= File::join( Config::CONFIG['datadir'], "ruby-wordnet" )68 DEFAULT_DB_ENV = File::join( Config::CONFIG['datadir'], "ruby-wordnet" ) 68 69 69 70 # Options for the creation of the Env object 70 E nvOptions= {71 ENV_OPTIONS = { 71 72 :set_timeout => 50, 72 73 :set_lk_detect => 1, 73 74 :set_verbose => false, 75 :set_lk_max => 3000, 74 76 } 75 77 76 78 # Flags for the creation of the Env object (read-write and read-only) 77 EnvFlagsRW = BDB::CREATE|BDB::INIT_TRANSACTION|BDB::RECOVER|BDB::INIT_MPOOL 78 EnvFlagsRO = BDB::INIT_MPOOL 79 80 # Table names (actually database names in BerkeleyDB) 81 TableNames = { 82 :index => "index", 83 :data => "data", 84 :morph => "morph", 85 } 86 79 ENV_FLAGS_RW = BDB::CREATE|BDB::INIT_TRANSACTION|BDB::RECOVER|BDB::INIT_MPOOL 80 ENV_FLAGS_RO = BDB::INIT_MPOOL 87 81 88 82 … … 95 89 ### opened with the specified +mode+, which can either be a numeric 96 90 ### octal mode (e.g., 0444) or one of (:readonly, :readwrite). 97 def initialize( dbenv=DefaultDbEnv, mode=:readonly ) 98 raise ArgumentError, "Cannot find data directory '#{dbenv}'" unless 99 File::directory?( dbenv ) 100 91 def initialize( dbenv=DEFAULT_DB_ENV, mode=:readonly ) 101 92 @mode = normalize_mode( mode ) 102 debug_msg "Mode is: %04o" % [ mode ] if $DEBUG 93 debug_msg "Mode is: %04o" % [ mode ] 94 95 envflags = 0 96 dbflags = 0 103 97 104 98 unless self.readonly? 105 99 debug_msg "Using read/write flags" 106 envflags = E nvFlagsRW100 envflags = ENV_FLAGS_RW 107 101 dbflags = BDB::CREATE 108 102 else 109 103 debug_msg "Using readonly flags" 110 envflags = E nvFlagsRO104 envflags = ENV_FLAGS_RO 111 105 dbflags = 0 112 106 end … … 116 110 117 111 begin 118 @env = BDB::Env ::new( dbenv, envflags, EnvOptions)112 @env = BDB::Env.new( dbenv, envflags, ENV_OPTIONS ) 119 113 @index_db = @env.open_db( BDB::BTREE, "index", nil, dbflags, @mode ) 120 114 @data_db = @env.open_db( BDB::BTREE, "data", nil, dbflags, @mode ) … … 194 188 wordkey = self.make_word_key( word, part_of_speech ) 195 189 return nil unless @index_db.key?( wordkey ) 196 @index_db[ wordkey ].split( WordNet::S ubDelimRe).length190 @index_db[ wordkey ].split( WordNet::SUB_DELIM_RE ).length 197 191 end 198 192 … … 221 215 # Make synset keys from the entry, narrowing it to just the sense 222 216 # requested if one was specified. 223 synkeys = entry.split( S ubDelimRe).collect {|off| "#{off}%#{pos}" }217 synkeys = entry.split( SUB_DELIM_RE ).collect {|off| "#{off}%#{pos}" } 224 218 if sense 225 219 return lookup_synsets_by_key( synkeys[sense - 1] ) … … 321 315 # synset, add it 322 316 if indexdb.key?( word ) 323 indexdb[ word ] << S ubDelim<< synset.offset unless317 indexdb[ word ] << SUB_DELIM << synset.offset unless 324 318 indexdb[ word ].include?( synset.offset ) 325 319 else … … 355 349 if indexdb.key?( word ) 356 350 offsets = indexdb[ word ]. 357 split( S ubDelimRe).351 split( SUB_DELIM_RE ). 358 352 reject {|offset| offset == synset.offset} 359 353 360 354 unless offsets.empty? 361 index_db[ word ] = newoffsets.join( S ubDelim)355 index_db[ word ] = newoffsets.join( SUB_DELIM ) 362 356 else 363 357 index_db.delete( word ) … … 389 383 return WordNet::Noun if original.nil? 390 384 osym = original.to_s.intern 391 return WordNet::S yntacticCategories[ osym ] if392 WordNet::S yntacticCategories.key?( osym )393 return original if S yntacticSymbols.key?( original )385 return WordNet::SYNTACTIC_CATEGORIES[ osym ] if 386 WordNet::SYNTACTIC_CATEGORIES.key?( osym ) 387 return original if SYNTACTIC_SYMBOLS.key?( original ) 394 388 return nil 395 389 end
