-
-
Save sixtyfive/7b94a213311af8f274f40252ee40ac19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require_relative 'config' | |
require_relative 'lib/ba-id/ba_id' | |
require 'sequel' | |
require 'logger' | |
db_file = File.join(__dir__, 'entries.sqlite3') | |
DB = Sequel.connect("sqlite://#{db_file}", loggers: [Logger.new($stdout)], sql_log_level: :debug) | |
class DamascusCatalog < Sequel::Model | |
# Magically accesses the table that has its name#underscorize | |
attr_accessor :place, :institution, :year_of_publication, :author, :volume | |
def add_ba_id_attributes | |
# Assigning extra attributes based on BA ID | |
parsed_ba_id_hash = BibliothecaArabicaID.parse(ba_id) | |
parsed_ba_id_hash.each do |k,v| | |
instance_variable_set("@#{k}".to_sym, v) # Need to be added to attr_accessor first! | |
values[k.to_sym] = v # Required for Sequel::Model#to_hash... | |
end | |
self | |
end | |
def self.where(str) | |
results = [] | |
super(str).each {|result| results << result.add_ba_id_attributes} | |
results | |
end | |
def self.first_by_id(str) | |
begin | |
where(ba_id: str).first | |
rescue Exception => e | |
puts "#{str}: No such entry (#{e})." | |
nil | |
end | |
end | |
def place | |
# TODO: enhance rakefile to extract list of institutions, create lookup table with normalized long-form names | |
# read from that lookup table here | |
# only THEN fall back on translation engine | |
return begin | |
case MACHINE_TRANSLATION_ENGINE | |
when :google_translate then | |
require 'google/cloud/translate' | |
t = Google::Cloud::Translate.new | |
t.translate place, to: 'en' | |
when :microsoft_bing then | |
require 'bing_translator' | |
t = BingTranslator.new(COGNITIVE_SUBSCRIPTION_KEY) | |
t.translate place, from: 'de', to: 'en' | |
else | |
throw | |
end | |
rescue | |
return @place | |
end | |
end | |
emd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment