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

Change to use glossarist gem #31

Merged
merged 9 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 2 additions & 5 deletions exe/tc211-termbase-xlsx2yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,12 @@ end

# collection[1206].inspect

collection.to_file(File.join(output_dir, Pathname.new(filepath).basename.sub_ext(".yaml")))

collection_output_dir = File.join(output_dir, "concepts")

FileUtils.mkdir_p(collection_output_dir)

collection.keys.each do |id|
collection[id].to_file(File.join(collection_output_dir, "concept-#{id}.yaml"))
end
concept_collection = collection.to_concept_collection
concept_collection.save_to_files(collection_output_dir)

# french = workbook.language_sheet("French")
# french.sections[3].structure
Expand Down
1 change: 1 addition & 0 deletions lib/tc211/termbase.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "glossarist"
require "tc211/termbase/version"

module Tc211
Expand Down
127 changes: 88 additions & 39 deletions lib/tc211/termbase/concept.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,105 @@
module Tc211::Termbase
class Concept < Hash
attr_reader :id

class Concept < Hash
attr_accessor :id
attr_accessor :terms
DEFAULT_LANGUAGE = "eng"
DEFAULT_LANGUAGE = "eng".freeze

def initialize(options={})
terms = options.delete(:terms) || []
terms.each do |term|
add_term(term)
def initialize(options = {})
super

terms = options.delete(:terms) || []
terms.each do |term|
add_term(term)
end

options.each_pair do |k, v|
send("#{k}=", v)
end
end

options.each_pair do |k,v|
self.send("#{k}=", v)
# The concept id should ALWAYS be an integer.
# https://github.com/riboseinc/tc211-termbase/issues/1
def id=(newid)
@id = Integer(newid)
end
end

# The concept id should ALWAYS be an integer.
# https://github.com/riboseinc/tc211-termbase/issues/1
def id=(newid)
@id = Integer(newid)
end
def add_term(term)
self[term.language_code] = term
end

def add_term(term)
self[term.language_code] = term
end
def terms
values
end

def default_term
if self[DEFAULT_LANGUAGE]
self[DEFAULT_LANGUAGE]
else
puts "[tc211-termbase] term (lang: #{keys.first}, ID: #{id}) is missing a corresponding English term, probably needs updating."
self[keys.first]
def default_term
if self[DEFAULT_LANGUAGE]
self[DEFAULT_LANGUAGE]
else
puts "[tc211-termbase] term (lang: #{keys.first}, ID: #{id}) is \
missing a corresponding English term, probably needs updating."
self[keys.first]
end
end
end

def to_hash
default_hash = {
"term" => default_term.term,
"termid" => id
}
def to_hash
default_hash = {
"term" => default_term.term,
"termid" => id,
}

self.inject(default_hash) do |acc, (lang, term)|
acc.merge!(lang => term.to_hash)
inject(default_hash) do |acc, (lang, term)|
acc.merge!(lang => term.to_hash)
end
end
end

def to_file(filename)
File.open(filename,"w") do |file|
file.write(to_hash.to_yaml)
def to_file(filename)
File.open(filename, "w") do |file|
file.write(to_hash.to_yaml)
end
end
end

def to_glossarist_concept
concept = Glossarist::ManagedConcept.new(data: { id: id.to_s })

localized_concepts = []

terms.map do |term|
next if term.nil?

localized_concepts << term.to_localized_concept_hash
end

concept.localized_concepts = localized_concepts

concept
end
end
end
end

# term: abbreviation
# termid: 2
# eng:
# id: 2
# term: abbreviation
# definition: designation formed by omitting words or letters from a longer
# form and designating the same concept
# language_code: eng
# notes: []
# examples: []
# entry_status: valid
# classification: preferred
# authoritative_source:
# ref: ISO 1087-1:2000
# clause: 3.4.9
# link: https://www.iso.org/standard/20057.html
# lineage_source: ISO/TS 19104:2008
# lineage_source_similarity: 1
# date_accepted: 2008-11-15 00:00:00.000000000 +08:00
# review_date: 2013-01-29 00:00:00.000000000 +08:00
# review_status: final
# review_decision: accepted
# review_decision_date: 2016-10-01 00:00:00.000000000 +08:00
# review_decision_event: Publication of ISO 19104:2016
# review_decision_notes: Authoritative reference changed from ISO 1087-1:2000
# to ISO 1087-1:2000, 3.4.9. Lineage source added as ISO/TS 19104:2008
# release: '2'
50 changes: 29 additions & 21 deletions lib/tc211/termbase/concept_collection.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
require_relative "concept"

module Tc211::Termbase
class ConceptCollection < Hash
def add_term(term)
if self[term.id]
self[term.id].add_term(term)
else
self[term.id] = Concept.new(
id: term.id,
terms: [term],
)
end
end

class ConceptCollection < Hash

def add_term(term)
if self[term.id]
self[term.id].add_term(term)
else
self[term.id] = Concept.new(
id: term.id,
terms: [term]
)
def to_hash
inject({}) do |acc, (id, concept)|
acc.merge!(id => concept.to_hash)
end
end
end

def to_hash
self.inject({}) do |acc, (id, concept)|
acc.merge!(id => concept.to_hash)
def to_file(filename)
File.open(filename, "w") do |file|
file.write(to_hash.to_yaml)
end
end
end

def to_file(filename)
File.open(filename,"w") do |file|
file.write(to_hash.to_yaml)
def to_concept_collection
collection = Glossarist::ManagedConceptCollection.new

values.each do |term_concept|
next if term_concept.nil?

collection.store(term_concept.to_glossarist_concept)
end

collection
end
end

end

end
26 changes: 11 additions & 15 deletions lib/tc211/termbase/information_sheet.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
require_relative "terminology_sheet"

module Tc211::Termbase
class InformationSheet < TerminologySheet
def metadata_section
sheet_array = @sheet.simple_rows.to_a
MetadataSection.new(sheet_array)
end

class InformationSheet < TerminologySheet
def to_hash
{ "glossary" => metadata_section.to_hash }
end

def metadata_section
sheet_array = @sheet.simple_rows.to_a
section = MetadataSection.new(sheet_array)
def to_yaml
to_hash.to_yaml
end
end

def to_hash
{ "glossary" => metadata_section.to_hash }
end

def to_yaml
to_hash.to_yaml
end

end

end
Loading
Loading