Module: Railbow::NameCollisionResolver

Defined in:
lib/railbow/name_collision_resolver.rb

Overview

Detects and resolves collisions when multiple different names produce the same formatted output. Works by progressively expanding pattern tokens (F → FF → FFF → FFFF, then L) until every distinct raw name maps to a unique formatted string.

Names that share the same first+last name but differ only in middle name are treated as the same person and merged before collision detection.

NameCollisionResolver.resolve(["John Doe", "Jane Doe"], "F L")
# => {"John Doe" => "Jo D", "Jane Doe" => "Ja D"}

Constant Summary collapse

SUPERSCRIPTS =
%w[² ³      ].freeze

Class Method Summary collapse

Class Method Details

.resolve(names, pattern) ⇒ Hash{String => String}

Returns raw_name → disambiguated formatted name.

Parameters:

  • names (Array<String, nil>)

    raw author names (may contain duplicates/nils)

  • pattern (String)

    a NameFormatter pattern or preset name

Returns:

  • (Hash{String => String})

    raw_name → disambiguated formatted name



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/railbow/name_collision_resolver.rb', line 24

def resolve(names, pattern)
  pattern = NameFormatter::PRESETS.fetch(pattern, pattern)
  uniq_names = names.compact.reject(&:empty?).uniq

  # Merge names that are the same person (same first+last, differ only in middle)
  canonical, aliases = merge_same_person(uniq_names)

  # Format every canonical name with the base pattern
  result = {}
  canonical.each { |n| result[n] = NameFormatter.format(n, pattern) }

  # Find collision groups (different raw names → same formatted output)
  collisions = find_collisions(result)
  unless collisions.empty?
    tokens = parse_tokens(pattern)
    collisions.each do |_formatted, raw_names|
      resolve_group(raw_names, pattern, tokens, result)
    end
  end

  # Expand aliases: all variant spellings get the same formatted output
  aliases.each { |variant, canon| result[variant] = result[canon] }

  result
end