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

Feature annotation data #5

Merged
merged 8 commits into from
Dec 15, 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
85 changes: 85 additions & 0 deletions app/models/concerns/annotation_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module AnnotationData
include JsonReader
extend ActiveSupport::Concern

#attr_accessor :annotation_url, :annotation_list

def read_annotation(url=nil)
return nil unless url #self[:annotation_url]
@annotation_list = JsonReader::Reader.new.from_url(url)
#self[:annotation_list] = JsonReader::Reader.new.from_str(self[:annotation_url])
end

def motivation_for_annotations
"oa:commenting"
end

def motivation_for_transcriptions
"sc:painting"
end

def annotations(annotation_list=nil)
# the motivation for annotations will be: "oa:commenting"
#return [] unless self[:annotation_url]
#self.read_annotation unless self[:annotation_list]
#return [] unless self[:annotation_list]
#return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "oa:commenting" }
return [] unless annotation_list
return annotation_list["resources"].select {|anno| anno["motivation"] == self.motivation_for_annotations }
end

def transcriptions(annotation_list=nil)
# the motivation for transcriptions will be: "sc:painting"
#return [] unless self[:annotation_url]
#self.read_annotation unless self[:annotation_list]
#return [] unless self[:annotation_list]
#return self[:annotation_list][:resources].select {|anno| anno["motivation"] == "sc:painting" }
return [] unless annotation_list
return annotation_list["resources"].select {|anno| anno["motivation"] == self.motivation_for_transcriptions }
end

def map_annotation(annotation=nil)
return {} unless annotation
anno = {}
anno['id'] = annotation["@id"]
anno['target_type'] = annotation["@type"]
anno['motivation'] = annotation["motivation"]
anno['target_url'] = annotation["on"]
anno['body_type'] = annotation["resource"]["@type"]
#TODO Convert format to human form?
anno['body_format'] = annotation["resource"]["format"]
anno['body_chars'] = annotation["resource"]["chars"]
if SEARCHWORKS_LANGUAGES.has_key?(annotation["resource"]["language"])
anno['body_language'] = SEARCHWORKS_LANGUAGES[annotation["resource"]["language"]]
else
anno['body_language'] = annotation["resource"]["language"]
end
anno['model'] = "Transcription" if annotation["motivation"] == self.motivation_for_transcriptions
anno['model'] = "Annotation" if annotation["motivation"] == self.motivation_for_annotations
anno
end

def annotation_to_solr(data={})
# data.keys = [:annotation, :manuscript, :folio, :url]
return {} unless data["annotation"]
anno = self.map_annotation(data["annotation"])
return {} unless (anno["id"])
solr_doc = {}
solr_doc["id"] = anno['id']
solr_doc["druid"] = self["druid"]
solr_doc["url_sfx"] = data["url"]
solr_doc["manifest_urls"] = self["iiif_manifest"]
solr_doc["collection"] = self["collection"]
solr_doc["folio"] = data["folio"]
solr_doc["manuscript_search"] = data["manuscript"]
solr_doc["model"] = anno["model"]
solr_doc["motivation"] = anno["motivation"]
solr_doc["target_url"] = anno["target_url"]
solr_doc["target_type"] = anno["target_type"]
solr_doc["body_type"] = anno["body_type"]
solr_doc["format"] = anno["body_format"]
solr_doc["body_chars_search"] = anno["body_chars"]
solr_doc["language"] = anno["body_language"]
solr_doc
end
end
28 changes: 28 additions & 0 deletions app/models/concerns/json_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module JsonReader
class Reader
def from_str(str)
JSON.parse(str)
end

def from_url(url, encoding = nil)
require 'open-uri'
begin
JSON.parse(open(url).read)
rescue OpenURI::HTTPError => the_error
puts "\nOpen URI error for #{url}\n\t#{the_error.message}" #TODO: Add to log
return nil
end
end

# Read in the contents of a JSON record from a file.
# @param filename (String) - path to file that has the annotations json as its content
# @return Hash
# @example
# foo = AnnotationData::Reader.new.from_file('/path/to/annotation/file.json')
def from_file(filename, encoding = nil)
file = File.read(filename)
JSON.parse(file)
end

end
end
1 change: 1 addition & 0 deletions app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class SolrDocument

include ModsData
include AnnotationData
include Blacklight::Solr::Document

attr_accessor :id, :purl, :collection, :manifest
Expand Down
254 changes: 254 additions & 0 deletions spec/fixtures/annotation_records/annotation-001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"@context": "http://www.shared-canvas.org/ns/context.json",
"@id": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/list/text-f8r.json",
"@type": "sc:AnnotationList",
"resources": [
{
"@id": "_:N43deaea09a5345379218db8cb72600c3",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "7377e5fe51c46454bb01b62a817a4d42",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "Erant aut[em] qui manducaverant",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,450,1017,166"
},
{
"@id": "_:N14eeb4370547431db7bb7fc075e43210",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "c15b3c62f8532a37ca4ba1c9deb84d6a",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "quasi quatuor milia hominu[m] et",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,616,1017,153"
},
{
"@id": "_:N1a08bd845f484bd9960cf323949e8f6e",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "bd8f9e22c2d2d6691e78f6d02143a80d",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "et dimisit eos Et stati[m] ascendens na-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,769,1017,162"
},
{
"@id": "_:N82e5366197f24bc1ae583248d73e36fc",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "d2771941ce394de6aea13779d4f058ea",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-ve[m] cu[m] discip[u]lis suis venit in par-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,931,1017,180"
},
{
"@id": "_:N8734944c5867422dbb2e05ce5a9386af",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "deabaa77448a61c561836d1fb064f885",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-tes dalmanutha [Et] exier[un]t phari-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1111,1017,162"
},
{
"@id": "_:Nec72601c72094655ae7b0df521dd3e7f",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "61574db3e19725509b692c3a099e0bc7",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-sei et ceper[un]t conquirere cu[m] eo que-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1274,1017,153"
},
{
"@id": "_:N689608cc1b53445c8cbc42ffb3929a6d",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "8f42470a851d3b5c1af46b8e7477f861",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-rentes ab illo signu[m] de celo te[m]pta[n]tes",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1427,1017,175"
},
{
"@id": "_:Nad1e54bc8428459b804639919f670908",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "677b8baf5f31cc035202320f74a59827",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "eu[m] [Et] ingemiscens sp[irit]u ait Q[ui]d g[e]n[er]a-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1602,1017,139"
},
{
"@id": "_:N51325f819f1e4bac8558b71419ae29a3",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "e91f46cf59fac81d268322d7af89378b",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-tio ista signu[m] querit Amen dico",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1742,1017,153"
},
{
"@id": "_:Ne1d69f7bca434ef583a1d3711237cbb7",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "654a116a63555dfcef929d207c5cc85d",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "vob[is] si dabit[ur] g[e]n[er]ationi isti signum",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,1895,1017,162"
},
{
"@id": "_:N34ba429a91c142f6b19a89b28780bff7",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "b2c3fdb829625d9edad520358ba24e2b",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "Et dimittens eos ascendens it[eru]m na-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2057,1017,153"
},
{
"@id": "_:N458e8ba2c83e48ef9f8039209aa28013",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "d75d079522a83fd9c29ffdc54e0c8433",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-ve[m] abiit t[ra]ns fredu[m] et obliti s[un]t sume-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2210,1017,148"
},
{
"@id": "_:Ne96dfb3e84ee42c39a24cad1ba1c75ec",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "ad687791dc66c33d889c65d28a919406",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-re panes et nisi unu[m] pane[m] secu[m] no[n]",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2359,1017,175"
},
{
"@id": "_:N8b42c7d2e7de4cf38a37ab0da9893059",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "ef1d64bc4e7fb0cb227a9dab4190116b",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "habebant in navi Et p[re]cipiebat",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2534,1017,139"
},
{
"@id": "_:N273697814e554ba3a26d0c49bf67daed",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "91c038e14ba8af0e9b5df0e98b830b2e",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "eis dicens Videte cavete a ferme[n]-",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2674,1017,189"
},
{
"@id": "_:N3aaf92430d484bf1b2869b333536395a",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "149e313d4be2f9ed97d51ba1a71a36c6",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "-to phariseo[rum] et fermento herodis",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2863,1017,135"
},
{
"@id": "_:N9dddfad68e9c47238f2827260742ba33",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "0ced5cdb34ba7d00348e25e435da7824",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "Et cogitabant ad alt[er]utru[m] dicentes",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,2998,1017,162"
},
{
"@id": "_:Nc9444cda88ad42ae888a42e15d17f83b",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "aff9de356f29c386cd506b86d2798997",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "Quia panes no[n] h[abe]m[u]s quo cognito",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3160,1017,184"
},
{
"@id": "_:Nbf9f14138d3d4046b5bbeb6989f1a2d7",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": {
"@id": "ed3bc7f8a8205ef288978bee5cc80410",
"@type": "cnt:ContentAsText",
"format": "text/plain",
"chars": "[Jesus] ait illis Quid cogitatis q[uia] pane",
"language": "lat"
},
"on": "http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/canvas/canvas-3#xywh=600,3344,1017,135"
}
]
}
Loading