Changeset 103

Show
Ignore:
Timestamp:
09/24/08 18:24:27 (2 months ago)
Author:
deveiant
Message:

Made the db converter look in more paths for the WordNet? dict files.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/Rakefile.local

    r102 r103  
    6161    end 
    6262end 
     63 
  • trunk/convertdb.rb

    r102 r103  
    4444require 'optparse' 
    4545require 'fileutils' 
     46require 'uri' 
     47require 'net/http' 
    4648 
    4749 
     
    7981               Pathname.new( WordNet::Lexicon::DEFAULT_DB_ENV ).basename 
    8082 
     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 
    8194 
    8295    ### Create a new converter that will dump WordNet dictionary files into a BerkeleyDB  
     
    98111        exit unless /^y/i =~ prompt_with_default("Continue?", "y") 
    99112 
    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 
    102114        if @builddir.exist? && ( @builddir + 'data' ).exist? 
    103115            message ">>> Warning: Existing data in the Ruby-WordNet databases\n"\ 
     
    109121 
    110122        # 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() 
    128124        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? 
    130126 
    131127        # Open the lexicon readwrite into the temporary datadir 
     
    201197    end 
    202198 
    203  
    204     ####### 
    205     private 
    206     ####### 
    207199 
    208200    # Index entry patterns 
     
    375367    end 
    376368 
     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     
    377387end # class WordNetConverter 
    378388 
  • trunk/utils.rb

    r102 r103  
    294294    ### Display a description of a potentially-dangerous task, and prompt 
    295295    ### 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 ) 
    298299        puts description 
    299300 
     
    302303        end 
    303304 
    304         case answer 
    305         when /^y/i 
    306             yield 
    307         else 
     305        if answer =~ /^y/i 
     306            return yield 
     307        elsif abort_on_decline 
    308308            error "Aborted." 
    309309            fail 
    310310        end 
     311 
     312        return false 
    311313    end 
    312314 
     
    810812        end 
    811813    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 
    812865 
    813866end # module UtilityFunctions