Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse abbreviation lookup #1

Merged
merged 1 commit into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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

Expand Down
36 changes: 35 additions & 1 deletion lib/cite_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class CiteMapper
def initialize
@authors = {}
@author_ids = {}
parse_abbr_file
end

Expand All @@ -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__)
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/cite_mapper/api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'sinatra/base'
require 'sinatra/respond_with'
require 'cite_mapper'
require 'json'

class Api < Sinatra::Base
register Sinatra::RespondWith
Expand All @@ -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
Expand Down