Class: Openphar::MonographMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/monograph_merger.rb

Overview

Merges Japanese content into existing English monograph JSON-LD files

This class:

  • Loads existing English JSON-LD monograph files
  • Parses Japanese HTML using JpJaHtmlParser
  • Matches monographs by Latin name
  • Merges Japanese content into the existing JSON-LD structure
  • Preserves language tags for all properties

Constant Summary collapse

SECTION_PROPERTY_MAP =

Mapping of Japanese section types to JSON-LD property names

{
  definition: "definition",
  macroscopic_description: "macroscopicDescription",
  microscopic_description: "microscopicDescription",
  identification: "identification",
  purity: "purity",
  foreign_matter: "foreignMatter",
  loss_on_drying: "lossOnDrying",
  total_ash: "totalAsh",
  acid_insoluble_ash: "acidInsolubleAsh",
  extractive: "extractive",
  assay: "assay",
  storage: "storageConditions",
  container: "storageContainer"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing_dir: nil, japanese_dir: nil) ⇒ MonographMerger

Initialize the merger

Parameters:

  • existing_dir (String) (defaults to: nil)

    Path to directory containing existing JSON-LD files

  • japanese_dir (String) (defaults to: nil)

    Path to Japanese HTML files (optional)



38
39
40
41
42
43
# File 'lib/openphar/monograph_merger.rb', line 38

def initialize(existing_dir: nil, japanese_dir: nil)
  @existing_dir = existing_dir
  @japanese_dir = japanese_dir
  @existing_monographs = {}
  @japanese_monographs = []
end

Instance Attribute Details

#existing_monographsObject (readonly)

Returns the value of attribute existing_monographs.



32
33
34
# File 'lib/openphar/monograph_merger.rb', line 32

def existing_monographs
  @existing_monographs
end

#japanese_monographsObject (readonly)

Returns the value of attribute japanese_monographs.



32
33
34
# File 'lib/openphar/monograph_merger.rb', line 32

def japanese_monographs
  @japanese_monographs
end

Instance Method Details

#load_existing_monographs(directory = nil) ⇒ Hash

Load existing monographs from JSON-LD files

Parameters:

  • directory (String) (defaults to: nil)

    Path to directory containing JSON-LD files

Returns:

  • (Hash)

    Hash of normalized Latin name => monograph data



49
50
51
52
53
54
55
56
57
58
# File 'lib/openphar/monograph_merger.rb', line 49

def load_existing_monographs(directory = nil)
  dir = directory || @existing_dir
  return {} unless dir && Dir.exist?(dir)

  Dir.glob(File.join(dir, "**", "*.jsonld")).each do |file|
    load_monograph_file(file)
  end

  @existing_monographs
end

#load_japanese_monographs(html_file = nil) ⇒ Array<Hash>

Load and parse Japanese monographs

Parameters:

  • html_file (String) (defaults to: nil)

    Path to Japanese HTML file

Returns:

  • (Array<Hash>)

    Parsed Japanese monographs



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openphar/monograph_merger.rb', line 64

def load_japanese_monographs(html_file = nil)
  file = html_file || @japanese_dir
  return [] unless file && File.exist?(file)

  parser = Openphar::Parsers::JpJaHtmlParser.new(
    File.read(file, encoding: "UTF-8")
  )
  @japanese_monographs = parser.parse

  # Index by normalized Latin name (standard Ruby, no ActiveSupport)
  @japanese_index = {}
  @japanese_monographs.each do |m|
    key = normalize_latin_name(m[:latin_name])
    @japanese_index[key] = m
  end
  @japanese_index
end

#merge_all(existing_monographs = nil, japanese_monographs = nil) ⇒ Array<Hash>

Merge all Japanese monographs into existing monographs

Parameters:

  • existing_monographs (Hash) (defaults to: nil)

    Existing monographs indexed by Latin name

  • japanese_monographs (Array<Hash>) (defaults to: nil)

    Parsed Japanese monographs

Returns:

  • (Array<Hash>)

    Updated monographs



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/openphar/monograph_merger.rb', line 138

def merge_all(existing_monographs = nil, japanese_monographs = nil)
  existing = existing_monographs || @existing_monographs
  japanese_index = @japanese_index || {}

  merged_results = []

  japanese_index.each do |latin_name, japanese_data|
    english_data = existing[latin_name]

    if english_data
      merged = merge_japanese_content(english_data.dup, japanese_data)
      merged_results << merged
    else
      # No matching English monograph found - could be a new monograph
      # or one that exists only in Japanese
      merged_results << create_japanese_only_monograph(japanese_data)
    end
  end

  merged_results
end

#merge_japanese_content(english_monograph, japanese_data) ⇒ Hash

Merge Japanese content into existing monographs

Parameters:

  • english_monograph (Hash)

    Existing JSON-LD monograph (as Ruby Hash)

  • japanese_data (Hash)

    Parsed Japanese monograph data

Returns:

  • (Hash)

    Updated monograph with bilingual content



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/openphar/monograph_merger.rb', line 87

def merge_japanese_content(english_monograph, japanese_data)
  return english_monograph unless japanese_data

  merged = english_monograph.dup

  # Add Japanese name to prefLabel
  if japanese_data[:japanese_name]
    merged["prefLabel"] ||= {}
    merged["prefLabel"]["ja"] = japanese_data[:japanese_name]
  end

  # Merge Japanese content into sections
  if japanese_data[:sections]
    japanese_data[:sections].each do |section_type, content|
      property_name = SECTION_PROPERTY_MAP[section_type]
      next unless property_name

      japanese_content = content.join("\n\n")

      case property_name
      when "definition"
        merge_language_content(merged, property_name, japanese_content)
      when "macroscopicDescription"
        merged["macroscopicDescription"] ||= {}
        merged["macroscopicDescription"]["ja"] = japanese_content
      when "microscopicDescription"
        merged["microscopicDescription"] ||= {}
        merged["microscopicDescription"]["ja"] = japanese_content
      when "storageConditions"
        merge_language_content(merged, property_name, japanese_content)
      when "identification"
        merge_identification_tests(merged, japanese_data)
      when "assay"
        merge_assay_tests(merged, japanese_data)
      when "purity"
        merge_purity_tests(merged, japanese_data)
      else
        # Generic handling for other test specifications
        merge_test_specifications(merged, section_type, japanese_content)
      end
    end
  end

  merged
end

#write_merged_monographs(monographs, output_dir) ⇒ Object

Write merged monographs to directory

Parameters:

  • monographs (Array<Hash>)

    Merged monographs

  • output_dir (String)

    Output directory path



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/openphar/monograph_merger.rb', line 164

def write_merged_monographs(monographs, output_dir)
  FileUtils.mkdir_p(output_dir)

  monographs.each do |monograph|
    next unless monograph["@id"]

    # Extract filename from IRI
    slug = monograph["@id"].split("/").last
    output_path = File.join(output_dir, "#{slug}.jsonld")

    File.write(output_path, JSON.pretty_generate(monograph))
  end
end