module Chakin::Downloader

Public Instance Methods

download(number: nil, name: '', save_dir: './') click to toggle source

Download pre-trained word vector

# File lib/chakin-rb/chakin.rb, line 23
def download(number: nil, name: '', save_dir: './')
  df = load_datasets

  row = if !number.nil?
          df.row[number]
        elsif name
          df.df.where(df['Name'].eq(name))
        end

  url = row['URL']
  raise 'The word vector you specified was not found. Please specify correct name.' if url.nil?


  file_name = url.split('/')[-1]

  FileUtils.mkdir_p(save_dir) unless File.exist?(save_dir)

  save_path = File.join(save_dir, file_name)
  begin
    download_file(save_path, url)
  rescue Chakin::HttpRedirect => e
    download_file(save_path, e.new_url)
  end
  save_path
end
download_file(save_path, url) click to toggle source
# File lib/chakin-rb/chakin.rb, line 49
def download_file(save_path, url)
  progressbar = ProgressBar.create

  f = File.open(save_path, 'wb')
  begin
    my_uri = URI.parse(url)
    http = Net::HTTP.new(my_uri.host, my_uri.port)

    if my_uri.instance_of?(URI::HTTPS)
      http.use_ssl = true
    end

    http.request_get(my_uri.path) do |resp|
      total_size = resp.content_length
      progressbar.total = total_size

      if resp.code == "302"
        raise HttpRedirect.new(resp.header['Location'])
      end
      resp.read_body do |segment|
        progressbar.progress += segment.size
        f.write(segment)
      end
    end
  ensure
    f.close
  end
end
load_datasets(path = File.join(__dir__, 'datasets.csv')) click to toggle source
# File lib/chakin-rb/chakin.rb, line 17
def load_datasets(path = File.join(__dir__, 'datasets.csv'))
  Daru::DataFrame.from_csv(path)
end