Changeset 103
- Timestamp:
- 09/24/08 18:24:27 (2 months ago)
- Location:
- trunk
- Files:
-
- 3 modified
-
Rakefile.local (modified) (1 diff)
-
convertdb.rb (modified) (6 diffs)
-
utils.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Rakefile.local
r102 r103 61 61 end 62 62 end 63 -
trunk/convertdb.rb
r102 r103 44 44 require 'optparse' 45 45 require 'fileutils' 46 require 'uri' 47 require 'net/http' 46 48 47 49 … … 79 81 Pathname.new( WordNet::Lexicon::DEFAULT_DB_ENV ).basename 80 82 83 # Paths to search for WordNet dictionary files 84 WNDB_LOCAL_PATHS = [ 85 '/usr/local/WordNet-3.0/dict', # Default install 86 '/usr/WordNet-3.0/dict', # Default with --prefix=/usr 87 '/usr/local/share/WordNet', # FreeBSD 88 './dict', # Extracted locally 89 ] 90 91 # URL to the latest database file archive 92 WNDB_TARBALL_URL = URI.parse( 'http://wordnet.princeton.edu/3.0/WordNet-3.0.tar.gz' ) 93 81 94 82 95 ### Create a new converter that will dump WordNet dictionary files into a BerkeleyDB … … 98 111 exit unless /^y/i =~ prompt_with_default("Continue?", "y") 99 112 100 # Open the database and check to be sure it's empty. Confirm overwrite if 101 # not. Checkpoint and set up logging proc if debugging. 113 # Confirm if we're going to clobber an existing database directory 102 114 if @builddir.exist? && ( @builddir + 'data' ).exist? 103 115 message ">>> Warning: Existing data in the Ruby-WordNet databases\n"\ … … 109 121 110 122 # Find the source data files 111 default = nil 112 wndirs = Pathname.glob( Pathname.getwd + 'WordNet-*' ) 113 localdict = Pathname.getwd + 'dict' 114 if !wndirs.empty? 115 default = wndirs.first + 'dict' 116 elsif localdict.exist? 117 default = localdict 118 else 119 default = '/usr/local/WordNet-3.0/dict' 120 end 121 122 message "Where can I find the WordNet data files?\n" 123 datadir = prompt_with_default( "Data directory", default ) 124 datadir = Pathname.new( datadir ) 125 126 abort( "Directory '#{datadir}' does not exist" ) unless datadir.exist? 127 abort( "'#{datadir}' is not a directory" ) unless datadir.directory? 123 datadir = find_data_files() 128 124 testfile = datadir + "data.noun" 129 abort( "'#{datadir}' doesn't seem to contain the necessary files." ) unless testfile.exist?125 abort( "'#{datadir}' doesn't seem to contain the necessary files." ) unless testfile.exist? 130 126 131 127 # Open the lexicon readwrite into the temporary datadir … … 201 197 end 202 198 203 204 #######205 private206 #######207 199 208 200 # Index entry patterns … … 375 367 end 376 368 369 370 ### Find the path to the WordNet dict directory with the files we're 371 ### going to parse in it. 372 def find_data_files 373 datadir = WNDB_LOCAL_PATHS.collect {|pn| Pathname.new(pn) }.find {|pn| pn.exist? } 374 375 unless datadir 376 message "Where can I find the WordNet data files?\n" 377 answer = prompt_with_default( "Data directory", WNDB_LOCAL_PATHS.first ) 378 datadir = Pathname.new( answer ) 379 end 380 381 abort( "Directory '#{datadir}' does not exist" ) unless datadir.exist? 382 abort( "'#{datadir}' is not a directory" ) unless datadir.directory? 383 384 return datadir 385 end 386 377 387 end # class WordNetConverter 378 388 -
trunk/utils.rb
r102 r103 294 294 ### Display a description of a potentially-dangerous task, and prompt 295 295 ### for confirmation. If the user answers with anything that begins 296 ### with 'y', yield to the block, else raise with an error. 297 def ask_for_confirmation( description ) 296 ### with 'y', yield to the block. If +abort_on_decline+ is +true+, 297 ### any non-'y' answer will fail with an error message. 298 def ask_for_confirmation( description, abort_on_decline=true ) 298 299 puts description 299 300 … … 302 303 end 303 304 304 case answer 305 when /^y/i 306 yield 307 else 305 if answer =~ /^y/i 306 return yield 307 elsif abort_on_decline 308 308 error "Aborted." 309 309 fail 310 310 end 311 312 return false 311 313 end 312 314 … … 810 812 end 811 813 end 814 815 816 ### Download the file at +sourceuri+ via HTTP and write it to +targetfile+. 817 def download( sourceuri, targetfile=nil ) 818 oldsync = $defout.sync 819 $defout.sync = true 820 require 'net/http' 821 require 'uri' 822 823 targetpath = Pathname.new( targetfile ) 824 825 log "Downloading %s to %s" % [sourceuri, targetfile] 826 targetpath.open( File::WRONLY|File::TRUNC|File::CREAT, 0644 ) do |ofh| 827 828 url = sourceuri.is_a?( URI ) ? sourceuri : URI.parse( sourceuri ) 829 downloaded = false 830 limit = 5 831 832 until downloaded or limit.zero? 833 Net::HTTP.start( url.host, url.port ) do |http| 834 req = Net::HTTP::Get.new( url.path ) 835 836 http.request( req ) do |res| 837 if res.is_a?( Net::HTTPSuccess ) 838 log "Downloading..." 839 res.read_body do |buf| 840 ofh.print( buf ) 841 end 842 downloaded = true 843 puts "done." 844 845 elsif res.is_a?( Net::HTTPRedirection ) 846 url = URI.parse( res['location'] ) 847 log "...following redirection to: %s" % [ url ] 848 limit -= 1 849 sleep 0.2 850 next 851 852 else 853 res.error! 854 end 855 end 856 end 857 end 858 859 end 860 861 return targetpath 862 ensure 863 $defout.sync = oldsync 864 end 812 865 813 866 end # module UtilityFunctions
