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

Adding adapter for reading glossarist model V1 concepts #89

Merged
merged 1 commit into from
Nov 28, 2023
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
1 change: 1 addition & 0 deletions lib/glossarist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_relative "glossarist/concept_manager"
require_relative "glossarist/managed_concept"
require_relative "glossarist/non_verb_rep"
require_relative "glossarist/v1_reader"

require_relative "glossarist/collections"

Expand Down
29 changes: 22 additions & 7 deletions lib/glossarist/concept_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def load_from_files(collection: nil)
collection ||= ManagedConceptCollection.new

Dir.glob(concepts_glob) do |filename|
concept = load_concept_from_file(filename)
concept.localized_concepts.each do |_lang, id|
localized_concept = load_localized_concept(id)
concept.add_l10n(localized_concept)
end
concept = if v1_collection?
Glossarist::V1Reader.load_concept_from_file(filename)
else
load_concept_from_file(filename)
end

collection.store(concept)
end
Expand All @@ -35,7 +35,14 @@ def save_to_files(managed_concepts)
def load_concept_from_file(filename)
concept_hash = Psych.safe_load(File.read(filename), permitted_classes: [Date])
concept_hash["uuid"] = concept_hash["id"] || File.basename(filename, ".*")
ManagedConcept.new(concept_hash)

concept = ManagedConcept.new(concept_hash)
concept.localized_concepts.each do |_lang, id|
localized_concept = load_localized_concept(id)
concept.add_l10n(localized_concept)
end

concept
rescue Psych::SyntaxError => e
raise Glossarist::ParseError.new(filename: filename, line: e.line)
end
Expand Down Expand Up @@ -71,11 +78,19 @@ def save_concept_to_file(concept)
private

def concepts_glob
File.join(path, "concept", "*.{yaml,yml}")
if v1_collection?
File.join(path, "concept-*.{yaml,yml}")
else
File.join(path, "concept", "*.{yaml,yml}")
end
end

def localized_concept_path(id)
Dir.glob(File.join(path, "localized_concept", "#{id}.{yaml,yml}"))&.first
end

def v1_collection?
@v1_collection ||= !Dir.glob(File.join(path, "concept-*.{yaml,yml}")).empty?
end
end
end
28 changes: 28 additions & 0 deletions lib/glossarist/v1_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Glossarist
# An adapter to read concepts in V1 format, converts them to v2 format and
# load into glossarist concept model.
class V1Reader
def self.load_concept_from_file(filename)
new.load_concept_from_file(filename)
end

def load_concept_from_file(filename)
concept_hash = Psych.safe_load(File.read(filename), permitted_classes: [Date])
ManagedConcept.new(generate_v2_concept_hash(concept_hash))
end

private

def generate_v2_concept_hash(concept_hash)
v2_concept = { "groups" => concept_hash["groups"] }
v2_concept["data"] = {
"identifier" => concept_hash["termid"],
"localized_concepts" => concept_hash.values.grep(Hash),
}

v2_concept
end
end
end
44 changes: 44 additions & 0 deletions spec/features/v1_serialization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
RSpec.describe "Serialization and deserialization" do
let(:concept_folder) { "concept_collection_v1" }
let(:output_folder) { File.join("concept_collection_v1", "output") }
let(:concept_files) { Dir.glob(File.join(fixtures_path(output_folder), "concept-*.{yaml,yml}")) }

it "correctly loads concepts from files" do
collection = Glossarist::ManagedConceptCollection.new
collection.load_from_files(fixtures_path(concept_folder))

concept_files.each do |filename|
concept_from_file = load_yaml_file(filename)
concept = collection[concept_from_file["data"]["identifier"]]

expect(concept.to_h["data"]).to eq(concept_from_file["data"])

concept.localized_concepts.each do |lang, id|
localized_concept_path = File.join(localized_concepts_folder, "#{id}.yaml")
localized_concept = load_yaml_file(localized_concept_path)

expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h["data"])
end
end

Dir.mktmpdir do |tmp_path|
collection.save_to_files(tmp_path)

# check if concept and localized_concept folder exist
system "diff", fixtures_path(output_folder), tmp_path
expect($?.exitstatus).to eq(0) # no difference

# check content of conecept folder
system "diff", File.join(fixtures_path(output_folder), "concept"), File.join(tmp_path, "concept")
expect($?.exitstatus).to eq(0) # no difference

# check content of localized_conecept folder
system "diff", File.join(fixtures_path(output_folder), "localized_concept"), File.join(tmp_path, "localized_concept")
expect($?.exitstatus).to eq(0) # no difference
end
end

def load_yaml_file(filename)
Psych.safe_load(File.read(filename), permitted_classes: [Date])
end
end
26 changes: 26 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
termid: 3.1.1.1
term: entity
groups:
- foo
- bar
eng:
terms:
- type: expression
normative_status: preferred
designation: entity
definition:
- content: concrete or abstract thing that exists, did exist, or can possibly exist,
including associations among these things
notes: []
examples:
- content: "{{person,Person}}, object, event, idea,
process, etc."
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.1
link: https://www.iso.org/standard/79779.html
22 changes: 22 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
termid: 3.1.1.2
term: immaterial_entity
eng:
terms:
- type: expression
normative_status: preferred
designation: immaterial_entity
definition:
- content: "{{entity}} that does not occupy three-dimensional
space"
notes: []
examples:
- content: Idea, process, organization, etc.
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.2
link: https://www.iso.org/standard/79779.html
23 changes: 23 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
termid: 3.1.1.3
term: material entity
eng:
terms:
- type: expression
normative_status: preferred
designation: material entity
definition:
- content: "{{entity}} that occupies three-dimensional
space"
notes:
- content: All material entities have certain characteristics that can be described
and therefore this concept is important for ontology purposes.
examples: []
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.3
link: https://www.iso.org/standard/79779.html
21 changes: 21 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
termid: 3.1.1.4
term: non-biological entity
eng:
terms:
- type: expression
normative_status: preferred
designation: non-biological entity
definition:
- content: "{{material entity}} that is not and has
never been a living organism"
notes: []
examples: []
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.4
link: https://www.iso.org/standard/79779.html
21 changes: 21 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
termid: 3.1.1.5
term: biological_entity
eng:
terms:
- type: expression
normative_status: preferred
designation: biological_entity
definition:
- content: "{{material entity}} that was or is a living
organism"
notes: []
examples: []
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.5
link: https://www.iso.org/standard/79779.html
21 changes: 21 additions & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
termid: 3.1.1.6
term: person
eng:
terms:
- type: expression
normative_status: preferred
designation: person
definition:
- content: "{{biological_entity}} that is a human
being"
notes: []
examples: []
language_code: eng
entry_status: valid
sources:
- type: authoritative
origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.6
link: https://www.iso.org/standard/79779.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
data:
identifier: 3.1.1.1
localized_concepts:
eng: ae27e36e-858c-5026-9dc7-fa366c464f01
groups:
- foo
- bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
data:
identifier: 3.1.1.4
localized_concepts:
eng: 5bf347a5-f1e5-5c33-b6d3-9cc74a8f6e64
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
data:
identifier: 3.1.1.6
localized_concepts:
eng: cb1e4bff-262c-5a1c-98b1-10e9e9b21ba5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
data:
identifier: 3.1.1.5
localized_concepts:
eng: 87d81109-7a89-53a4-af7c-92f99bd07a71
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
data:
identifier: 3.1.1.3
localized_concepts:
eng: d2bd44d8-ef32-5695-9eab-ef82053e0d1d
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
data:
identifier: 3.1.1.2
localized_concepts:
eng: 48f1340a-517a-5533-ae55-99b304f0f8c9
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
data:
dates: []
definition:
- content: "{{entity}} that does not occupy three-dimensional space"
examples:
- content: Idea, process, organization, etc.
notes: []
sources:
- origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.2
link: https://www.iso.org/standard/79779.html
type: authoritative
terms:
- type: expression
normative_status: preferred
designation: immaterial_entity
language_code: eng
entry_status: valid
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
data:
dates: []
definition:
- content: "{{material entity}} that is not and has never been a living organism"
examples: []
notes: []
sources:
- origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.4
link: https://www.iso.org/standard/79779.html
type: authoritative
terms:
- type: expression
normative_status: preferred
designation: non-biological entity
language_code: eng
entry_status: valid
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
data:
dates: []
definition:
- content: "{{material entity}} that was or is a living organism"
examples: []
notes: []
sources:
- origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.5
link: https://www.iso.org/standard/79779.html
type: authoritative
terms:
- type: expression
normative_status: preferred
designation: biological_entity
language_code: eng
entry_status: valid
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
data:
dates: []
definition:
- content: concrete or abstract thing that exists, did exist, or can possibly exist,
including associations among these things
examples:
- content: "{{person,Person}}, object, event, idea, process, etc."
notes: []
sources:
- origin:
ref: ISO/TS 14812:2022
clause: 3.1.1.1
link: https://www.iso.org/standard/79779.html
type: authoritative
terms:
- type: expression
normative_status: preferred
designation: entity
language_code: eng
entry_status: valid
Loading