Module: Unitsdb::Commands::SiTtlParser
- Defined in:
- lib/unitsdb/commands/si_ttl_parser.rb
Overview
Parser for SI TTL files
Constant Summary collapse
- SI_URI_PREFIX =
"http://si-digital-framework.org/SI/"
Class Method Summary collapse
-
.add_symbols_to_entities(entities, graph, si) ⇒ Object
Add symbols to entities.
-
.extract_base_entities(graph, namespace, skos) ⇒ Object
Extract base entities from graph.
-
.extract_entities_from_ttl(entity_type, graph) ⇒ Object
Extract entities from TTL based on entity type.
-
.extract_identifying_suffix(uri) ⇒ Object
Extract suffix from URI for display.
-
.parse_ttl_files(dir) ⇒ Object
Parse TTL files and return RDF graph.
Class Method Details
.add_symbols_to_entities(entities, graph, si) ⇒ Object
Add symbols to entities
76 77 78 79 80 81 82 |
# File 'lib/unitsdb/commands/si_ttl_parser.rb', line 76 def add_symbols_to_entities(entities, graph, si) entities.each do |entity| symbol = RDF::Query.new({ RDF::URI(entity[:uri]) => { si.hasSymbol => :value } }) .execute(graph).first&.value&.to_s entity[:symbol] = symbol if symbol end end |
.extract_base_entities(graph, namespace, skos) ⇒ Object
Extract base entities from graph
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/unitsdb/commands/si_ttl_parser.rb', line 46 def extract_base_entities(graph, namespace, skos) entities = [] processed_uris = {} RDF::Query.new({ entity: { skos.prefLabel => :label } }) .execute(graph).each do |solution| entity_uri = solution.entity.to_s next unless entity_uri.start_with?(namespace.to_s) next if processed_uris[entity_uri] processed_uris[entity_uri] = true entity_name = entity_uri.split("/").last label = RDF::Query.new({ RDF::URI(entity_uri) => { skos.prefLabel => :value } }) .execute(graph).first&.value&.to_s alt_label = RDF::Query.new({ RDF::URI(entity_uri) => { skos.altLabel => :value } }) .execute(graph).first&.value&.to_s entities << { uri: entity_uri, name: entity_name, label: label, alt_label: alt_label } end entities end |
.extract_entities_from_ttl(entity_type, graph) ⇒ Object
Extract entities from TTL based on entity type
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/unitsdb/commands/si_ttl_parser.rb', line 28 def extract_entities_from_ttl(entity_type, graph) skos = RDF::Vocabulary.new("http://www.w3.org/2004/02/skos/core#") si = RDF::Vocabulary.new("http://si-digital-framework.org/SI#") namespace_uri = case entity_type when "units" then "http://si-digital-framework.org/SI/units/" when "quantities" then "http://si-digital-framework.org/quantities/" when "prefixes" then "http://si-digital-framework.org/SI/prefixes/" else return [] end namespace = RDF::Vocabulary.new(namespace_uri) entities = extract_base_entities(graph, namespace, skos) add_symbols_to_entities(entities, graph, si) if %w[units prefixes].include?(entity_type) entities end |
.extract_identifying_suffix(uri) ⇒ Object
Extract suffix from URI for display
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/unitsdb/commands/si_ttl_parser.rb', line 85 def (uri) return "" unless uri # For display, we need to format as exactly like the original # This helps format the comma-separated multi-units correctly if uri.include?("/units/") # Return units/name format for units (without duplicating "units/") "units/#{uri.split("/").last}" else # Otherwise strip the prefix uri.gsub(SI_URI_PREFIX, "") end end |
.parse_ttl_files(dir) ⇒ Object
Parse TTL files and return RDF graph
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/unitsdb/commands/si_ttl_parser.rb', line 15 def parse_ttl_files(dir) puts "Parsing TTL files in #{dir}..." graph = RDF::Graph.new Dir.glob(File.join(dir, "*.ttl")).each do |file| puts " Reading #{File.basename(file)}" graph.load(file, format: :ttl) end graph end |