Skip to content

Commit

Permalink
Extract mentions as "external resources". Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Nov 1, 2023
1 parent e941e5f commit fd317c6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/tess/rdf/course_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lib/tess/rdf/extraction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
1 change: 1 addition & 0 deletions lib/tess/rdf/material_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

0 comments on commit fd317c6

Please sign in to comment.