From 10980ac16526bae375c4fbff3f508617fc77c959 Mon Sep 17 00:00:00 2001 From: Ryan Baumann Date: Tue, 11 Aug 2015 16:54:55 -0400 Subject: [PATCH] Add reverse abbreviation lookup --- README.md | 13 ++++++++++++- lib/cite_mapper.rb | 36 +++++++++++++++++++++++++++++++++++- lib/cite_mapper/api.rb | 10 ++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 45cbe1f..da64211 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Or install it yourself as: Run `rackup` to start a webserver (its port defaults to 9292). -At the moment the server will only respond to one GET method. +At the moment the server will only respond to two GET methods. If you want to know what `urn:cts:greekLit:tlg0007.tlg015.perseus-eng1:16.1` means, you can ask the server ``` @@ -33,6 +33,17 @@ and it will respond in JSON: ``` { "author" : "Plut.", "work" : "Alc.", "section" : "16.1" } ``` + +If you want to know what `Xen. Cyrop. 5.3.11-17` means, you can ask the server +``` +http://localhost:9292/find_abbr?abbr=Xen.%20Cyrop.%205.3.11-17` +``` + +and it will respond in JSON: + +``` +{"urn":"urn:cts:greekLit:tlg0032:tlg007:5.3.11-5.3.17"} +``` ## Contributing diff --git a/lib/cite_mapper.rb b/lib/cite_mapper.rb index 1b55765..434749d 100644 --- a/lib/cite_mapper.rb +++ b/lib/cite_mapper.rb @@ -3,6 +3,7 @@ class CiteMapper def initialize @authors = {} + @author_ids = {} parse_abbr_file end @@ -13,6 +14,38 @@ def find_by_cite(urn_string) Result.new(author, work, cts.section, cts.edition) end + def find_by_abbr(abbr_string) + parts = abbr_string.split('.').map{|p| p.strip} + parsed_author = parts.shift.downcase + author_id = @author_ids[parsed_author] + author = @authors[author_id] + work = nil + sections = [] + passage = nil + unless author.nil? + while (work.nil? && !parts.empty?) do + parsed_work = parts.join(' ') + work = author.works.values.select{|w| w.name.downcase.tr('.','') == parsed_work.downcase}.first + if work.nil? + sections << parts.pop + end + end + if work.nil? + return { :urn => "urn:cts:greekLit:#{author_id}" } + elsif sections.empty? + return { :urn => "urn:cts:greekLit:#{author_id}:#{work.id}" } + else + if (sections[0].split('-').length == 2) && !sections[0].include?('.') + passage = sections[0].split('-').map{|passage_range| sections[1..-1].reverse.join('.') + '.' + passage_range}.join('-') + else + passage = sections.reverse.join('.') + end + return { :urn => "urn:cts:greekLit:#{author_id}:#{work.id}:#{passage}" } + end + end + return { :urn => nil } + end + private DATA_PATH = File.expand_path('../../data', __FILE__) @@ -42,6 +75,7 @@ def parse_cts(cts) end def add_author(id, name) + @author_ids[name.downcase.chomp('.')] ||= id @authors[id] ||= Author.new(id, name) end @@ -52,7 +86,7 @@ def add_entry(author, work, author_id, work_id) end class Author - attr_reader :id, :name + attr_reader :id, :name, :works def initialize(id, name) @id = id diff --git a/lib/cite_mapper/api.rb b/lib/cite_mapper/api.rb index 94a1041..d69c061 100644 --- a/lib/cite_mapper/api.rb +++ b/lib/cite_mapper/api.rb @@ -1,6 +1,7 @@ require 'sinatra/base' require 'sinatra/respond_with' require 'cite_mapper' +require 'json' class Api < Sinatra::Base register Sinatra::RespondWith @@ -20,6 +21,15 @@ class Api < Sinatra::Base end end + get '/find_abbr' do + abbr = params[:abbr] + res = mapper.find_by_abbr(abbr) + + respond_to do |f| + f.json { res.to_json } + end + end + def mapper @mapper ||= CiteMapper.new end