Class: Openphar::Linkers::Chp::CrossEditionLinker

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

Overview

Links the same monograph across two ChP editions via sameSubstanceAs.

The matcher uses (book_id, normalized pinyin_title) as the match key — pinyin is publisher-assigned, stable across editions, and disambiguates within volume. When two editions have a monograph with the same key, both IRIs are emitted as sameSubstanceAs targets.

Input: two enumerables of monographs (anything that responds to pinyin_title, book_id, id). Output: a hash of {source_iri => [target_iris]} representing the bidirectional links.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edition_a:, edition_b:) ⇒ CrossEditionLinker

Returns a new instance of CrossEditionLinker.

Parameters:



24
25
26
27
28
29
# File 'lib/openphar/linkers/chp/cross_edition_linker.rb', line 24

def initialize(edition_a:, edition_b:)
  @edition_a = edition_a
  @edition_b = edition_b
  @matches = {}
  @stats = { matched: 0, unmatched_a: 0, unmatched_b: 0, multi_match: 0 }
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



20
21
22
# File 'lib/openphar/linkers/chp/cross_edition_linker.rb', line 20

def matches
  @matches
end

#statsObject (readonly)

Returns the value of attribute stats.



20
21
22
# File 'lib/openphar/linkers/chp/cross_edition_linker.rb', line 20

def stats
  @stats
end

Instance Method Details



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openphar/linkers/chp/cross_edition_linker.rb', line 31

def link
  index_b = build_index(@edition_b)
  index_a = build_index(@edition_a)

  # Match A -> B
  index_a.each do |key, iris_a|
    iris_b = index_b[key]
    next unless iris_b

    if iris_b.size > 1
      @stats[:multi_match] += 1
      next # ambiguous: don't link
    end

    iri_a = iris_a.first
    iri_b = iris_b.first
    @matches[iri_a] = [iri_b]
    @matches[iri_b] = [iri_a]
    @stats[:matched] += 1
  end

  @stats[:unmatched_a] = index_a.size - @stats[:matched]
  @stats[:unmatched_b] = index_b.size - @stats[:matched]
  @matches
end

#to_jsonldHash

Serialize links as a JSON-LD graph document.

Returns:

  • (Hash)

    JSON-serializable graph.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openphar/linkers/chp/cross_edition_linker.rb', line 59

def to_jsonld
  graph = @matches.map do |source_iri, targets|
    {
      "@id" => source_iri,
      "sameSubstanceAs" => targets,
    }
  end
  {
    "@context" => "https://www.openphar.org/ontology/context/chp.jsonld",
    "@type" => "LinkSet",
    "linkType" => "sameSubstanceAs",
    "matchCount" => @stats[:matched],
    "@graph" => graph,
  }
end