From fd317c69c75e3945975111814137c586c92a582f Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 1 Nov 2023 15:35:52 +0000 Subject: [PATCH] Extract `mentions` as "external resources". Fixes #2 --- lib/tess/rdf/course_extractor.rb | 2 +- lib/tess/rdf/extraction.rb | 7 +++++++ lib/tess/rdf/material_extractor.rb | 1 + test/field_test.rb | 33 ++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/tess/rdf/course_extractor.rb b/lib/tess/rdf/course_extractor.rb index ed02cee..a41a74b 100644 --- a/lib/tess/rdf/course_extractor.rb +++ b/lib/tess/rdf/course_extractor.rb @@ -16,7 +16,7 @@ def extract_params end # course_params[:difficulty_level] ||= # extract_names_or_values(RDF::Vocab::SCHEMA.educationalLevel, subject: course).first - + course_params[:external_resources_attributes] = extract_mentions # ...and override with more specific metadata from CourseInstance course_instance_params = super course_params.merge(course_instance_params) diff --git a/lib/tess/rdf/extraction.rb b/lib/tess/rdf/extraction.rb index 4be1885..61e9eb5 100644 --- a/lib/tess/rdf/extraction.rb +++ b/lib/tess/rdf/extraction.rb @@ -248,6 +248,13 @@ def extract_audience(subject: resource) [:audience, RDF::RDFS.label, :target_audience, { optional: true }]).map { |a| a[:target_audience] }.compact end + def extract_mentions(subject: resource) + query( + [subject, RDF::Vocab::SCHEMA.mentions, :mention], + [:mention, RDF::Vocab::SCHEMA.name, :name, { optional: true }], + [:mention, RDF::Vocab::SCHEMA.url, :url, { optional: true }]).map { |a| { title: a[:name], url: a[:url] } }.compact + end + def parse_value(value) # Using 'value.class.name' instead of just 'value' here or things like RDF::Literal::DateTime fall into the RDF::Literal block # Not using 'value.class' because 'case' uses '===' for comparison and RDF::URI === RDF::URI is false! diff --git a/lib/tess/rdf/material_extractor.rb b/lib/tess/rdf/material_extractor.rb index f729a59..119efb9 100644 --- a/lib/tess/rdf/material_extractor.rb +++ b/lib/tess/rdf/material_extractor.rb @@ -24,6 +24,7 @@ def extract_params params[:contributors] = extract_names(RDF::Vocab::SCHEMA.contributor) params[:target_audience] = extract_audience params[:resource_type] = extract_values(RDF::Vocab::SCHEMA.learningResourceType) + params[:external_resources_attributes] = extract_mentions remove_blanks(params) end diff --git a/test/field_test.rb b/test/field_test.rb index b7bc64b..dd841f8 100644 --- a/test/field_test.rb +++ b/test/field_test.rb @@ -197,6 +197,33 @@ class FieldTest < Test::Unit::TestCase assert_equal '2023-10-02', course_instance_extractor(json).extract_params[:end].to_s end + test 'extract tools from mentions' do + json = %( +[{ + "@context": "http://schema.org", + "@type": "LearningResource", + "http://purl.org/dc/terms/conformsTo": { + "@id": "https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE", + "@type": "CreativeWork" + }, + "mentions": [ + { + "@type": "SoftwareApplication", + "name": "Galaxy", + "url": "https://bio.tools/galaxy", + "description": "Open, web-based platform for data intensive biomedical research" + },{ + "@type": "Dataset", + "name": "European Genome-phenome Archive", + "url": "https://www.ebi.ac.uk/ega/home", + "description": "The EGA archives a large number of datasets, the access to which is controlled by a Data Access Committee (DAC)." + } + ] +}]) + assert_equal [{ title: 'Galaxy', url: 'https://bio.tools/galaxy' }, + { title: 'European Genome-phenome Archive', url: 'https://www.ebi.ac.uk/ega/home' }], learning_resource_extractor(json).send(:extract_mentions) + end + private def course_extractor(fixture, format: :jsonld, base_uri: 'https://example.com/my.json') @@ -210,4 +237,10 @@ def course_instance_extractor(fixture, format: :jsonld, base_uri: 'https://examp ex.instance_variable_set(:@_temp_resource, ex.resources.first.individual) ex end + + def learning_resource_extractor(fixture, format: :jsonld, base_uri: 'https://example.com/my.json') + ex = Tess::Rdf::LearningResourceExtractor.new(StringIO.new(fixture), format, base_uri: base_uri) + ex.instance_variable_set(:@_temp_resource, ex.resources.first.individual) + ex + end end \ No newline at end of file