Skip to content

Commit

Permalink
i2069: Add a new field for specific languages on the Show page
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbergja committed Mar 10, 2023
1 parent e3b8467 commit 0677f2a
Show file tree
Hide file tree
Showing 10 changed files with 844 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ end

gem "omniauth-rails_csrf_protection"

gem "human_languages", "~> 0.6.0"
gem "human_languages", "~> 0.7"
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ GEM
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
httpclient (2.8.3)
human_languages (0.6.0)
human_languages (0.7.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
iso-639 (0.3.5)
Expand Down Expand Up @@ -631,7 +631,7 @@ DEPENDENCIES
gyoku (~> 1.0)
high_voltage (~> 3.0)
honeybadger (~> 4.0)
human_languages (~> 0.6.0)
human_languages (~> 0.7)
jbuilder
jquery-rails
jquery-tablesorter (~> 1.21)
Expand Down
61 changes: 61 additions & 0 deletions marc_to_solr/lib/language_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# A class to pull out language information from
# a MARC record
class LanguageExtractor
def initialize(language_service, marc_record)
@marc_record = marc_record
@language_service = language_service
end

def specific_codes
codes = []
codes << fixed_field_code if fixed_field_code

if iso_041_fields.any?
codes.concat(iso_041_codes)
elsif all_041_fields.any?
codes.concat(all_041_codes)
end
codes.reject { |general_code| includes_more_specific_version?(codes, general_code) }
end

def specific_names
specific_codes.map { |code| @language_service.code_to_name(code) }
.compact
end

private

def includes_more_specific_version?(codes, code_to_check)
codes.any? { |individual| @language_service.macrolanguage_codes(individual).include? code_to_check }
end

def iso_041_fields
all_041_fields.select { |field| field['2'] == 'iso639-3' }
end

def all_041_fields
@marc_record.fields('041')
end

def fixed_field_code
value = @marc_record['008']&.value
fixed_field_code = value ? value[35, 3] : nil
end

def iso_041_codes
return [] unless iso_041_fields.any?
extract_from_multiple_041s(iso_041_fields)
end

def all_041_codes
return [] unless all_041_fields.any?
extract_from_multiple_041s(all_041_fields)
end

def extract_from_multiple_041s(fields)
fields.map do |field|
field.subfields.select { |sf| %(a h).include? sf.code }
.map(&:value)
end.flatten
end
end
24 changes: 23 additions & 1 deletion marc_to_solr/lib/language_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,34 @@ def loc_to_iana(loc)

def valid_language_code?(code)
return false if code.blank?
Languages[code].present? || iso639_5_collective_languages['code'].include?(code)
Languages[code].present? || iso_639_5_include?(code)
end

def code_to_name(code)
Languages[code]&.name || iso_639_5_name(code)
end

def macrolanguage_codes(individual_language_code)
individual = Languages[individual_language_code]
if individual.respond_to? :macrolanguage
[individual&.macrolanguage&.alpha3_bibliographic.to_s, individual&.macrolanguage&.iso639_3.to_s].uniq
else
[]
end
end

private

def iso639_5_collective_languages
@iso639_5_collective_languages ||= CSV.read(Rails.root.join('config', 'iso639-5.tsv'), headers: true, col_sep: "\t")
end

def iso_639_5_include?(code)
iso639_5_collective_languages['code'].include?(code)
end

def iso_639_5_name(code)
return unless iso_639_5_include?(code)
iso639_5_collective_languages.find { |row| row['code'] == code }['Label (English)']
end
end
5 changes: 5 additions & 0 deletions marc_to_solr/lib/traject_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative './access_facet_builder'
require_relative './augment_the_subject'
require_relative './language_service'
require_relative './language_extractor'
require_relative './pul_solr_json_writer'
require 'stringex'
require 'library_stdnums'
Expand Down Expand Up @@ -727,6 +728,10 @@
# 546 XX 3a
to_field 'language_display', extract_marc('5463a')

to_field 'language_name_display' do |record, accumulator|
accumulator.replace(LanguageExtractor.new(language_service, record).specific_names)
end

to_field 'language_facet', marc_languages

to_field 'publication_place_facet', extract_marc('008[15-17]') do |_record, accumulator, _context|
Expand Down
Loading

0 comments on commit 0677f2a

Please sign in to comment.