Skip to content

Commit

Permalink
Merge pull request #272 from samvera/discogs-update
Browse files Browse the repository at this point in the history
Discogs and N-Triples update
  • Loading branch information
elrayle authored Oct 10, 2019
2 parents d427108 + d85d472 commit da5158f
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 107 deletions.
5 changes: 5 additions & 0 deletions app/controllers/qa/linked_data_terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,14 @@ def n3?
format.casecmp?('n3')
end

def ntriples?
format.casecmp?('ntriples')
end

def content_type_for_format
return 'application/ld+json' if jsonld?
return 'text/n3' if n3?
return 'application/n-triples' if ntriples?
'application/json'
end

Expand Down
5 changes: 5 additions & 0 deletions app/controllers/qa/terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ def n3?
format.casecmp?('n3')
end

def ntriples?
format.casecmp?('ntriples')
end

def content_type_for_format
return 'application/ld+json' if jsonld?
return 'text/n3' if n3?
return 'application/n-triples' if ntriples?
'application/json'
end
end
36 changes: 17 additions & 19 deletions lib/qa/authorities/discogs/discogs_instance_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def get_identifiers_stmts(response)
count = 1
return stmts unless response["identifiers"].present?
response["identifiers"].each do |activity|
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/identifiedBy", "Identifier#{count}")
stmts << contruct_stmt_uri_object("Identifier#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
stmts << contruct_stmt_literal_object("Identifier#{count}", rdfs_label_predicate, activity["value"])
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/identifiedBy", "iidn#{count}")
stmts << contruct_stmt_uri_object("iidn#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
stmts << contruct_stmt_literal_object("iidn#{count}", rdfs_label_predicate, activity["value"])
count += 1
end
stmts.concat(build_year_statements(response)) if response["released"].present?
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

Expand All @@ -64,7 +65,7 @@ def build_format_desc_stmts(descs, count)
if df.present?
stmts += build_format_characteristics(df, count)
else
stmts << contruct_stmt_literal_object("Instance1", "http://id.loc.gov/ontologies/bibframe/editionStatement", desc)
stmts << contruct_stmt_literal_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/editionStatement", desc)
end
end
stmts
Expand All @@ -77,15 +78,13 @@ def build_format_characteristics(df, count)
stmts = []
case df["type"]
when "playbackChannel"
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/soundCharacteristic", df["uri"])
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/soundCharacteristic", df["uri"])
stmts << contruct_stmt_uri_object(df["uri"], rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/PlaybackChannel")
stmts << contruct_stmt_literal_object(df["uri"], rdfs_label_predicate, df["label"])
when "dimension"
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/dimensions", df["label"])
stmts << contruct_stmt_literal_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/dimensions", df["label"])
when "playingSpeed"
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/soundCharacteristic", "PlayingSpeed#{count}")
stmts << contruct_stmt_uri_object("PlayingSpeed#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/PlayingSpeed")
stmts << contruct_stmt_literal_object("PlayingSpeed#{count}", rdfs_label_predicate, df["label"])
stmts.concat(build_playing_speed_stmts(df["label"], count))
end
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end
Expand All @@ -98,13 +97,13 @@ def build_format_name_stmts(name)
return stmts unless name.present?
dc = discogs_formats[name.gsub(/\s+/, "")]
if dc.present?
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/carrier", dc["uri"])
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/carrier", dc["uri"])
stmts << contruct_stmt_uri_object(dc["uri"], rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Carrier")
stmts << contruct_stmt_literal_object(dc["uri"], rdfs_label_predicate, dc["label"])
stmts.concat(build_base_materials(name))
else
# if it's not a carrier, it's an edition statement
stmts << contruct_stmt_literal_object("Instance1", "http://id.loc.gov/ontologies/bibframe/editionStatement", name)
stmts << contruct_stmt_literal_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/editionStatement", name)
end
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end
Expand All @@ -116,7 +115,7 @@ def build_base_materials(name)
return stmts unless name == "Vinyl" || name == "Shellac"
id = name == "Vinyl" ? "300014502" : "300014918"

stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/baseMaterial", "http://vocab.getty.edu/aat/" + id)
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/baseMaterial", "http://vocab.getty.edu/aat/" + id)
stmts << contruct_stmt_uri_object("http://vocab.getty.edu/aat/" + id, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/BaseMaterial")
stmts << contruct_stmt_literal_object("http://vocab.getty.edu/aat/" + id, rdfs_label_predicate, name)
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
Expand All @@ -129,13 +128,12 @@ def build_provision_activity_stmts(activities)
# need to distinguish among different provision activities and roles
count = 1
activities.each do |activity|
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/provisionActivity", "ProvisionActivity#{count}")
stmts << contruct_stmt_uri_object("ProvisionActivity#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/ProvisionActivity")
stmts << contruct_stmt_uri_object("ProvisionActivity#{count}", bf_agent_predicate, activity["name"])
stmts << contruct_stmt_uri_object(activity["name"], rdf_type_predicate, bf_agent_type_object)
stmts << contruct_stmt_uri_object(activity["name"], bf_role_predicate, "PA_Role#{count}")
stmts << contruct_stmt_uri_object("PA_Role#{count}", rdf_type_predicate, bf_role_type_object)
stmts << contruct_stmt_literal_object("PA_Role#{count}", rdfs_label_predicate, activity["entity_type_name"])
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/provisionActivity", "proact#{count}")
stmts << contruct_stmt_uri_object("proact#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/ProvisionActivity")
stmts << contruct_stmt_uri_object("proact#{count}", bf_agent_predicate, "agentn3#{count}")
stmts << contruct_stmt_uri_object("agentn3#{count}", rdf_type_predicate, bf_agent_type_object)
stmts << contruct_stmt_literal_object("agentn3#{count}", rdfs_label_predicate, activity["name"])
stmts += build_role_stmts("agentn3#{count}", "role3#{count}", activity["entity_type_name"])
count += 1
end
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
Expand Down
41 changes: 23 additions & 18 deletions lib/qa/authorities/discogs/discogs_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module DiscogsTranslation
# to the URIs of corresponding objects in the Library of Congress vocabulary.
# @param [Hash] the http response from discogs
# @param [String] the subauthority
# @return [Array, String] requested RDF serialzation (supports: jsonld Array, n3 String)
# @return [Array, String] requested RDF serialzation (supports: jsonld Array, n3 String, n-triples String)
def build_graph(response, subauthority = "", format: :jsonld)
graph = RDF::Graph.new

Expand All @@ -30,14 +30,17 @@ def compile_rdf_statements(response, subauthority)
# all we need is a work and not an instance. If there's no subauthority, we can determine
# if the discogs record is a master because it will have a main_release field.
if master_only(response, subauthority)
self.work_uri = response["uri"]
complete_rdf_stmts.concat(build_master_statements(response))
else
# If the subauthority is not "master," we need to define an instance as well as a
# work. If the discogs record has a master_id, fetch that and use the results to
# build the statements for the work.
master_resp = response["master_id"].present? ? json("https://api.discogs.com/masters/#{response['master_id']}") : response
self.work_uri = master_resp["uri"] if master_resp["uri"].present? && master_resp["uri"].include?("master")
complete_rdf_stmts.concat(build_master_statements(master_resp))
# Now do the statements for the instance.
self.instance_uri = response["uri"] if response["uri"].present?
complete_rdf_stmts.concat(build_instance_statements(response))
end
end
Expand Down Expand Up @@ -77,26 +80,27 @@ def build_instance_statements(response)
# @return [Array] rdf statements
def get_primary_work_definition(response)
stmts = []
stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/title", "Work1Title")
stmts << contruct_stmt_literal_object("Work1Title", bf_main_title_predicate, response["title"])
stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
stmts.concat(build_year_statements(response, "Work"))
stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen1")
stmts << contruct_stmt_literal_object("titlen1", bf_main_title_predicate, response["title"])
stmts << contruct_stmt_uri_object("titlen1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
stmts << contruct_stmt_literal_object(work_uri, "http://id.loc.gov/ontologies/bibframe/originDate", response["year"].to_s) if response["year"].present?
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

# @param [Hash] the http response from discogs
# @return [Array] rdf statements
def get_primary_instance_definition(response)
stmts = []
stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/hasInstance", "Instance1")
stmts << contruct_stmt_uri_object("Instance1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/title", "Instance1Title")
stmts << contruct_stmt_literal_object("Instance1Title", bf_main_title_predicate, response["title"])
stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/identifiedBy", "IdentifierPrimary")
stmts << contruct_stmt_uri_object("IdentifierPrimary", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
stmts << contruct_stmt_literal_object("IdentifierPrimary", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
stmts.concat(build_year_statements(response, "Instance"))
stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/hasInstance", instance_uri)
stmts << contruct_stmt_uri_object(instance_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen2")
stmts << contruct_stmt_literal_object("titlen2", bf_main_title_predicate, response["title"])
stmts << contruct_stmt_uri_object("titlen2", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/identifiedBy", "widn1")
stmts << contruct_stmt_uri_object("widn1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
stmts << contruct_stmt_literal_object("widn1", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

Expand All @@ -112,10 +116,11 @@ def get_primary_artists_stmts(response)
# we need the primary artists later when we loop through the track list, so build this array
primary_artists << artist

stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/contribution", "Work1PrimaryContribution#{count}")
stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", bf_agent_predicate, artist["name"])
stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn#{count}")
stmts << contruct_stmt_uri_object("contrbn#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
stmts << contruct_stmt_uri_object("contrbn#{count}", bf_agent_predicate, "agentn#{count}")
stmts << contruct_stmt_uri_object("agentn#{count}", rdf_type_predicate, bf_agent_type_object)
stmts << contruct_stmt_literal_object("agentn#{count}", rdfs_label_predicate, artist["name"])
count += 1
end
end
Expand Down
52 changes: 32 additions & 20 deletions lib/qa/authorities/discogs/discogs_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ module DiscogsUtils
# @param [String] either a string used to create a unique URI or an LOC uri in string format
# @param [String] or [Class] either a BIBFRAME property uri in string format or an RDF::URI
# @param [String] or [Class] strings can be a label or BIBFRAME class uri; class is always RDF::URI
# @return [Class] RDF::Statement with uri as the object
# @return [Class] RDF::Statement with either a uri or a bnode as the object
def contruct_stmt_uri_object(subject, predicate, object)
RDF::Statement(RDF::URI.new(subject), RDF::URI(predicate), RDF::URI.new(object))
s = subject.include?("http") ? RDF::URI.new(subject) : subject.to_sym
o = object.to_s.include?("http") ? RDF::URI.new(object) : object.to_sym
RDF::Statement(s, RDF::URI(predicate), o)
end

# Constructs an RDF statement where the subject and predicate are URIs and the object is a literal
Expand All @@ -20,7 +22,8 @@ def contruct_stmt_uri_object(subject, predicate, object)
# @param [String] or [Class] strings can be a label or BIBFRAME class uri; class is always RDF::URI
# @return [Class] RDF::Statement with a literal as the object
def contruct_stmt_literal_object(subject, predicate, object)
RDF::Statement(RDF::URI.new(subject), RDF::URI(predicate), RDF::Literal.new(object))
s = subject.include?("http") ? RDF::URI.new(subject) : subject.to_sym
RDF::Statement(s, RDF::URI(predicate), RDF::Literal.new(object))
end

# frequently used predicates and objects
Expand All @@ -41,15 +44,15 @@ def bf_agent_predicate
end

def bf_agent_type_object
RDF::URI("http://id.loc.gov/ontologies/bibframe/Agent")
"http://id.loc.gov/ontologies/bibframe/Agent"
end

def bf_role_predicate
RDF::URI("http://id.loc.gov/ontologies/bibframe/role")
end

def bf_role_type_object
RDF::URI("http://id.loc.gov/ontologies/bibframe/Role")
"http://id.loc.gov/ontologies/bibframe/Role"
end

def discogs_genres
Expand All @@ -62,27 +65,36 @@ def discogs_formats

# both the work and the instance require a statement for the release year
# @param [Hash] the http response from discogs
# @param [String] either "Work" or "Instance"
# @return [Array] rdf statements
def build_year_statements(response, type)
def build_year_statements(response)
year_stmts = []
if type == "Work" && response["year"].present?
year_stmts = get_year_rdf(type + "1", response["year"])
elsif response["released"].present?
year_stmts = get_year_rdf(type + "1", response["released"])
end
year_stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/provisionActivity", "daten1")
year_stmts << contruct_stmt_uri_object("daten1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Publication")
year_stmts << contruct_stmt_literal_object("daten1", RDF::URI("http://id.loc.gov/ontologies/bibframe/date"), response["released"].to_s)
year_stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

# @param [String] either "Work1" or "Instance1"
# @param [String] 4-digit year in string format
# @param [Hash] the extraartists defined at the release level, not the track level
# @param [Integer] gives the role a unique uri
# @param [String] the entity type name
# @return [Array] rdf statements
def get_year_rdf(type, year)
year_stmts = []
year_stmts << contruct_stmt_uri_object(type, "http://id.loc.gov/ontologies/bibframe/provisionActivity", "#{type}ProvisionActivityDate")
year_stmts << contruct_stmt_uri_object("#{type}ProvisionActivityDate", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/ProvisionActivity")
# Full RDF statement syntax as this one requires a datatype
year_stmts << RDF::Statement(RDF::URI.new("#{type}ProvisionActivityDate"), RDF::URI("http://id.loc.gov/ontologies/bibframe/date"), RDF::Literal.new(year.to_s, datatype: RDF::XSD.date))
def build_role_stmts(subject_node, role_node, label)
stmts = []
stmts << contruct_stmt_uri_object(subject_node, bf_role_predicate, role_node)
stmts << contruct_stmt_uri_object(role_node, rdf_type_predicate, bf_role_type_object)
stmts << contruct_stmt_literal_object(role_node, rdfs_label_predicate, label)
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

# @param [String] the playing speed in string format
# @param [Integer] gives the playing speed a unique uri
# @return [Array] rdf statements
def build_playing_speed_stmts(label, count)
stmts = []
stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/soundCharacteristic", "speed#{count}")
stmts << contruct_stmt_uri_object("speed#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/PlayingSpeed")
stmts << contruct_stmt_literal_object("speed#{count}", rdfs_label_predicate, label)
stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end
end
end
Expand Down
Loading

0 comments on commit da5158f

Please sign in to comment.