Class: Openphar::Linkers::HerbapediaLinker

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

Overview

Links pharmacopoeia monographs to data-herbapedia entities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(herbapedia_data_path: nil) ⇒ HerbapediaLinker

Returns a new instance of HerbapediaLinker.



9
10
11
12
13
14
15
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 9

def initialize(herbapedia_data_path: nil)
  @herbapedia_data_path = herbapedia_data_path
  @preparations_index = {}
  @tcm_profiles_index = {}
  @ayurveda_profiles_index = {}
  load_indexes if herbapedia_data_path
end

Instance Attribute Details

#ayurveda_profiles_indexObject (readonly)

Returns the value of attribute ayurveda_profiles_index.



7
8
9
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 7

def ayurveda_profiles_index
  @ayurveda_profiles_index
end

#preparations_indexObject (readonly)

Returns the value of attribute preparations_index.



7
8
9
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 7

def preparations_index
  @preparations_index
end

#tcm_profiles_indexObject (readonly)

Returns the value of attribute tcm_profiles_index.



7
8
9
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 7

def tcm_profiles_index
  @tcm_profiles_index
end

Instance Method Details

#find_ayurveda_profile(sanskrit:) ⇒ String?

Find an Ayurveda profile in data-herbapedia

Parameters:

  • sanskrit (String)

    The Sanskrit name

Returns:

  • (String, nil)

    The IRI of the matching Ayurveda profile



51
52
53
54
55
56
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 51

def find_ayurveda_profile(sanskrit:)
  return nil if ayurveda_profiles_index.empty?

  normalized = normalize_name(sanskrit)
  ayurveda_profiles_index[normalized]&.dig(:iri)
end

#find_preparation(latin_name:, chinese_name: nil) ⇒ String?

Find a HerbalPreparation in data-herbapedia

Parameters:

  • latin_name (String)

    The Latin pharmaceutical name

  • chinese_name (String, nil) (defaults to: nil)

    Optional Chinese name

Returns:

  • (String, nil)

    The IRI of the matching preparation



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 21

def find_preparation(latin_name:, chinese_name: nil)
  return nil if preparations_index.empty?

  # Try exact Latin name match first
  normalized = normalize_name(latin_name)
  match = preparations_index[normalized]

  # Try Chinese name if no match
  if !match && chinese_name
    match = preparations_index.values.find do |prep|
      prep[:names]&.any? { |n| n[:zh] == chinese_name }
    end
  end

  match&.dig(:iri)
end

#find_tcm_profile(pinyin:) ⇒ String?

Find a TCM profile in data-herbapedia

Parameters:

  • pinyin (String)

    The pinyin name

Returns:

  • (String, nil)

    The IRI of the matching TCM profile



41
42
43
44
45
46
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 41

def find_tcm_profile(pinyin:)
  return nil if tcm_profiles_index.empty?

  normalized = normalize_name(pinyin)
  tcm_profiles_index[normalized]&.dig(:iri)
end

Link a monograph to herbapedia entities

Parameters:

  • monograph_data (Hash)

    The monograph data

Returns:

  • (Hash)

    Updated monograph data with herbapedia links



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openphar/linkers/herbapedia_linker.rb', line 61

def link_monograph(monograph_data)
  result = monograph_data.dup

  # Try to find preparation by Latin name
  latin_name = monograph_data[:name] || monograph_data.dig(:pref_label, "en")
  chinese_name = monograph_data.dig(:pref_label, "zh") ||
                 monograph_data.dig(:pref_label, "zh-Hant")

  if latin_name
    prep_iri = find_preparation(latin_name: latin_name, chinese_name: chinese_name)
    result[:references_preparation] = prep_iri if prep_iri
  end

  result
end