lib/authority_control/lcgft_enrich.rb
### Add LCGFTs to a record based on form subdivisions (subfield v) in LCSH
Add LCGFTs to a record based on form subdivisions (subfield v) in LCSH
Add LCGFTs to a record based on form subdivisions ( subfield v ) in LCSH
def lcgft_from_lcsh(record:, conn:)
  form_subfield_codes = %w[v]
  changed_rec = false
  rec = duplicate_record(record)
  relevant_fields = rec.fields('600'..'699')
  return { record: rec, changed_rec: changed_rec } if relevant_fields.empty?
  last_subj_index = rec.fields.index(relevant_fields[-1])
  relevant_fields.each do |field|
    next unless field.indicator2 == '0'
    subf3 = field.subfields.select { |subfield| subfield.code == '3' }
    subf5 = field.subfields.select { |subfield| subfield.code == '5' }
    form_subfields = field.subfields.select do |subfield|
      form_subfield_codes.include?(subfield.code)
    end
    form_subfields.each do |subfield|
      heading_string = subfield.value
      next if heading_string =~ /^[Mmusic]$/
      heading = normalize_heading_for_local_search(heading: heading_string)
      target_lccn = get_lccn_from_local_db(conn: conn,
                                         heading: heading,
                                         query: authorized_lcgft_lccn_query)
      if target_lccn.nil?
        target_lccn = get_lccn_from_local_db(conn: conn,
                                           heading: heading,
                                           query: variant_lcgft_lccn_query)
      end
      next unless target_lccn
      target_uri = "http://id.loc.gov/authorities/genreForms/#{target_lccn}"
      pref_name = get_pref_lcgft_from_local_db(conn: conn,
                                               uri: target_uri,
                                               query: lcgft_heading_string_query)
      next unless pref_name
      pref_name += '.' unless pref_name[-1] =~ heading_final_punct_regex
      new_field = MARC::DataField.new('655', ' ', '7')
      new_field.append(MARC::Subfield.new('a', pref_name))
      new_field.append(MARC::Subfield.new('2', 'lcgft'))
      new_field.append(MARC::Subfield.new('0', target_uri))
      subf3.each do |subf|
        new_field.subfields.insert(0, subf)
      end
      subf5.each do |subf|
        new_field.subfields.append(subf)
      end
      changed_rec = true
      rec.fields.insert((last_subj_index + 1), new_field)
    end
  end
  f655 = rec.fields('655').reverse
  f655a = []
  f655.each do |field|
    next unless field['2'] == 'lcgft'
    index = rec.fields.index(field)
    if f655a.include?(field['a'])
      rec.fields.delete_at(index)
    else
      f655a << field['a']
    end
  end
  { record: rec, changed_rec: changed_rec }
end
def lcgft_from_lcsh ( record : , conn : ) form_subfield_codes = %w[ v ] changed_rec = false rec = duplicate_record ( record ) relevant_fields = rec . fields ( '600' .. '699' ) return { record : rec , changed_rec : changed_rec } if relevant_fields . empty? last_subj_index = rec . fields . index ( relevant_fields [ - 1 ] ) relevant_fields . each do | field | next unless field . indicator2 == '0' subf3 = field . subfields . select { | subfield | subfield . code == '3' } subf5 = field . subfields . select { | subfield | subfield . code == '5' } form_subfields = field . subfields . select do | subfield | form_subfield_codes . include? ( subfield . code ) end form_subfields . each do | subfield | heading_string = subfield . value next if heading_string =~ / ^[Mmusic]$ / heading = normalize_heading_for_local_search ( heading : heading_string ) target_lccn = get_lccn_from_local_db ( conn : conn , heading : heading , query : authorized_lcgft_lccn_query ) if target_lccn . nil? target_lccn = get_lccn_from_local_db ( conn : conn , heading : heading , query : variant_lcgft_lccn_query ) end next unless target_lccn target_uri = "http://id.loc.gov/authorities/genreForms/#{target_lccn}" pref_name = get_pref_lcgft_from_local_db ( conn : conn , uri : target_uri , query : lcgft_heading_string_query ) next unless pref_name pref_name += '.' unless pref_name [ - 1 ] =~ heading_final_punct_regex new_field = MARC :: DataField . new ( '655' , ' ' , '7' ) new_field . append ( MARC :: Subfield . new ( 'a' , pref_name ) ) new_field . append ( MARC :: Subfield . new ( '2' , 'lcgft' ) ) new_field . append ( MARC :: Subfield . new ( '0' , target_uri ) ) subf3 . each do | subf | new_field . subfields . insert ( 0 , subf ) end subf5 . each do | subf | new_field . subfields . append ( subf ) end changed_rec = true rec . fields . insert ( ( last_subj_index + 1 ) , new_field ) end end f655 = rec . fields ( '655' ) . reverse f655a = [ ] f655 . each do | field | next unless field [ '2' ] == 'lcgft' index = rec . fields . index ( field ) if f655a . include? ( field [ 'a' ] ) rec . fields . delete_at ( index ) else f655a << field [ 'a' ] end end { record : rec , changed_rec : changed_rec } end
Add LCGFTs to a record based on form subdivisions (subfield v) in LCSH
Add LCGFTs to a record based on form subdivisions ( subfield v ) in LCSH
### insert the LCGFTs after the last subject field
record
conn