lib/authority_control/record_update.rb
def add_subject_uri(record:, conn:)
  tags_of_interest = %w[600 610 611 630 650 651]
  changed_rec = false
  bad_rec = false
  rec = duplicate_record(record)
  fields_of_interest = rec.fields.select do |field|
    tags_of_interest.include?(field.tag) && field.indicator2 == '0'
  end
  fields_of_interest.each do |field|
    next unless field['0'].nil?
    orig_tag = field.tag
    field_index = rec.fields.index(field)
    field_map = field.subfields.map(&:code)
    ### Ignore name subject headings that don't have subject subdivisions
    next if orig_tag != '650' && (field_map & %w[v x y z]).nil?
    heading_subfields = subject_subfields_for_tag(orig_tag)
    heading_string = get_subject_heading_from_field(field, heading_subfields)
    search_string = normalize_heading_for_local_search(heading: heading_string)
    target_lccn = get_lccn_from_local_db(conn: conn,
                                         heading: search_string,
                                         query: authorized_lcsh_lccn_query)
    if target_lccn.nil?
      target_lccn = get_lccn_from_local_db(conn: conn,
                                           heading: search_string,
                                           query: variant_lcsh_lccn_query)
    end
    next unless target_lccn
    heading_info = get_marc_and_uri_from_lccn(conn: conn,
                                              lccn: target_lccn)
    next unless heading_info
    target_uri = heading_info[:uri]
    auth_field = auth_field_from_marc_info(heading_info)
    next unless auth_field
    next if auth_field.tag[1] == '8'
    changed_rec = true
    auth_indicator1 = auth_field.indicator1
    auth_indicator2 = auth_field.indicator2
    auth_tag_portion = auth_field.tag[1..2]
    new_tag = orig_tag[0] + auth_tag_portion
    orig_field = ''
    field.subfields.each do |subfield|
      next unless heading_subfields.include?(subfield.code)
#      orig_field << subfield.code  Ignore code errors for now
      orig_field << subfield.value
    end
    orig_field.gsub!(/[[:punct:][:space:]]/, '')
    orig_field.unicode_normalize!(:nfd)
    auth_field_string = ''
    auth_field.subfields.each do |subfield|
#      auth_field_string << subfield.code  Ignore code errors for now
      auth_field_string << subfield.value
    end
    auth_field_string.gsub!(/[[:punct:][:space:]]/, '')
    auth_field_string.unicode_normalize!(:nfd)
    bad_rec = true unless (orig_field == auth_field_string) && orig_tag == new_tag
    ### Not a priority to identify incorrect indicators
#    bad_rec = true if auth_field.tag == '130' && auth_indicator2 != field.indicator1
#    bad_rec = true if auth_field.tag != '130' && auth_indicator1 != field.indicator1
    new_field = case orig_tag
                when /^6[015]/
                  MARC::DataField.new(new_tag,
                                      auth_indicator1,
                                      field.indicator2)
                when /^63/
                  MARC::DataField.new(new_tag,
                                      auth_indicator2,
                                      field.indicator2)
                end
    auth_subfields = auth_field.subfields
    auth_subfields.each do |auth_subfield|
      new_field.append(auth_subfield)
    end
    case orig_tag
    when /^6[013]0$|^651$/
      subfe = field.subfields.select { |subfield| subfield.code == 'e' }
      unless subfe.empty?
        new_field.subfields[-1].value += ',' unless new_field.subfields[-1].value[-1] == '-'
        subfe.each do |subfield|
          new_field.append(subfield)
        end
      end
    when /^611$/
      subfj = field.subfields.select { |subfield| subfield.code == 'j' }
      unless subfj.empty?
        new_field.subfields[-1].value += ',' unless new_field.subfields[-1].value[-1] == '-'
        subfj.each do |subfield|
          new_field.append(subfield)
        end
      end
    end
    new_field.subfields[-1].value = new_field.subfields[-1].value + '.' unless new_field.subfields[-1].value[-1] =~ /.*["\).\!\?\-]$/
    new_field = add_non_heading_subfields_to_subject_field(field, new_field)
    new_field.append(MARC::Subfield.new('0', target_uri))
    rec.fields[field_index] = new_field
  end
  { record: rec,
    changed_rec: changed_rec,
    bad_rec: bad_rec }
end
### Uses MySQL database to resolve a normalized LCSH to a URI;
###   then it gets the authorized form
Uses MySQL database to resolve a normalized LCSH to a URI;
then it gets the authorized form
Uses MySQL database to resolve a normalized LCSH to a URI ; then it gets the authorized form
def add_subject_uri(record:, conn:)
  tags_of_interest = %w[600 610 611 630 650 651]
  changed_rec = false
  bad_rec = false
  rec = duplicate_record(record)
  fields_of_interest = rec.fields.select do |field|
    tags_of_interest.include?(field.tag) && field.indicator2 == '0'
  end
  fields_of_interest.each do |field|
    next unless field['0'].nil?
    orig_tag = field.tag
    field_index = rec.fields.index(field)
    field_map = field.subfields.map(&:code)
    next if orig_tag != '650' && (field_map & %w[v x y z]).nil?
    heading_subfields = subject_subfields_for_tag(orig_tag)
    heading_string = get_subject_heading_from_field(field, heading_subfields)
    search_string = normalize_heading_for_local_search(heading: heading_string)
    target_lccn = get_lccn_from_local_db(conn: conn,
                                         heading: search_string,
                                         query: authorized_lcsh_lccn_query)
    if target_lccn.nil?
      target_lccn = get_lccn_from_local_db(conn: conn,
                                           heading: search_string,
                                           query: variant_lcsh_lccn_query)
    end
    next unless target_lccn
    heading_info = get_marc_and_uri_from_lccn(conn: conn,
                                              lccn: target_lccn)
    next unless heading_info
    target_uri = heading_info[:uri]
    auth_field = auth_field_from_marc_info(heading_info)
    next unless auth_field
    next if auth_field.tag[1] == '8'
    changed_rec = true
    auth_indicator1 = auth_field.indicator1
    auth_indicator2 = auth_field.indicator2
    auth_tag_portion = auth_field.tag[1..2]
    new_tag = orig_tag[0] + auth_tag_portion
    orig_field = ''
    field.subfields.each do |subfield|
      next unless heading_subfields.include?(subfield.code)
      orig_field << subfield.value
    end
    orig_field.gsub!(/[[:punct:][:space:]]/, '')
    orig_field.unicode_normalize!(:nfd)
    auth_field_string = ''
    auth_field.subfields.each do |subfield|
      auth_field_string << subfield.value
    end
    auth_field_string.gsub!(/[[:punct:][:space:]]/, '')
    auth_field_string.unicode_normalize!(:nfd)
    bad_rec = true unless (orig_field == auth_field_string) && orig_tag == new_tag
    new_field = case orig_tag
                when /^6[015]/
                  MARC::DataField.new(new_tag,
                                      auth_indicator1,
                                      field.indicator2)
                when /^63/
                  MARC::DataField.new(new_tag,
                                      auth_indicator2,
                                      field.indicator2)
                end
    auth_subfields = auth_field.subfields
    auth_subfields.each do |auth_subfield|
      new_field.append(auth_subfield)
    end
    case orig_tag
    when /^6[013]0$|^651$/
      subfe = field.subfields.select { |subfield| subfield.code == 'e' }
      unless subfe.empty?
        new_field.subfields[-1].value += ',' unless new_field.subfields[-1].value[-1] == '-'
        subfe.each do |subfield|
          new_field.append(subfield)
        end
      end
    when /^611$/
      subfj = field.subfields.select { |subfield| subfield.code == 'j' }
      unless subfj.empty?
        new_field.subfields[-1].value += ',' unless new_field.subfields[-1].value[-1] == '-'
        subfj.each do |subfield|
          new_field.append(subfield)
        end
      end
    end
    new_field.subfields[-1].value = new_field.subfields[-1].value + '.' unless new_field.subfields[-1].value[-1] =~ /.*["\).\!\?\-]$/
    new_field = add_non_heading_subfields_to_subject_field(field, new_field)
    new_field.append(MARC::Subfield.new('0', target_uri))
    rec.fields[field_index] = new_field
  end
  { record: rec,
    changed_rec: changed_rec,
    bad_rec: bad_rec }
end
def add_subject_uri ( record : , conn : ) tags_of_interest = %w[ 600 610 611 630 650 651 ] changed_rec = false bad_rec = false rec = duplicate_record ( record ) fields_of_interest = rec . fields . select do | field | tags_of_interest . include? ( field . tag ) && field . indicator2 == '0' end fields_of_interest . each do | field | next unless field [ '0' ] . nil? orig_tag = field . tag field_index = rec . fields . index ( field ) field_map = field . subfields . map ( & :code ) next if orig_tag != '650' && ( field_map & %w[ v x y z ] ) . nil? heading_subfields = subject_subfields_for_tag ( orig_tag ) heading_string = get_subject_heading_from_field ( field , heading_subfields ) search_string = normalize_heading_for_local_search ( heading : heading_string ) target_lccn = get_lccn_from_local_db ( conn : conn , heading : search_string , query : authorized_lcsh_lccn_query ) if target_lccn . nil? target_lccn = get_lccn_from_local_db ( conn : conn , heading : search_string , query : variant_lcsh_lccn_query ) end next unless target_lccn heading_info = get_marc_and_uri_from_lccn ( conn : conn , lccn : target_lccn ) next unless heading_info target_uri = heading_info [ :uri ] auth_field = auth_field_from_marc_info ( heading_info ) next unless auth_field next if auth_field . tag [ 1 ] == '8' changed_rec = true auth_indicator1 = auth_field . indicator1 auth_indicator2 = auth_field . indicator2 auth_tag_portion = auth_field . tag [ 1 .. 2 ] new_tag = orig_tag [ 0 ] + auth_tag_portion orig_field = '' field . subfields . each do | subfield | next unless heading_subfields . include? ( subfield . code ) orig_field << subfield . value end orig_field . gsub! ( / [[:punct:][:space:]] / , '' ) orig_field . unicode_normalize! ( :nfd ) auth_field_string = '' auth_field . subfields . each do | subfield | auth_field_string << subfield . value end auth_field_string . gsub! ( / [[:punct:][:space:]] / , '' ) auth_field_string . unicode_normalize! ( :nfd ) bad_rec = true unless ( orig_field == auth_field_string ) && orig_tag == new_tag new_field = case orig_tag when / ^6[015] / MARC :: DataField . new ( new_tag , auth_indicator1 , field . indicator2 ) when / ^63 / MARC :: DataField . new ( new_tag , auth_indicator2 , field . indicator2 ) end auth_subfields = auth_field . subfields auth_subfields . each do | auth_subfield | new_field . append ( auth_subfield ) end case orig_tag when / ^6[013]0$|^651$ / subfe = field . subfields . select { | subfield | subfield . code == 'e' } unless subfe . empty? new_field . subfields [ - 1 ] . value += ',' unless new_field . subfields [ - 1 ] . value [ - 1 ] == '-' subfe . each do | subfield | new_field . append ( subfield ) end end when / ^611$ / subfj = field . subfields . select { | subfield | subfield . code == 'j' } unless subfj . empty? new_field . subfields [ - 1 ] . value += ',' unless new_field . subfields [ - 1 ] . value [ - 1 ] == '-' subfj . each do | subfield | new_field . append ( subfield ) end end end new_field . subfields [ - 1 ] . value = new_field . subfields [ - 1 ] . value + '.' unless new_field . subfields [ - 1 ] . value [ - 1 ] =~ / .*[" \) . \! \? \- ]$ / new_field = add_non_heading_subfields_to_subject_field ( field , new_field ) new_field . append ( MARC :: Subfield . new ( '0' , target_uri ) ) rec . fields [ field_index ] = new_field end { record : rec , changed_rec : changed_rec , bad_rec : bad_rec } end
Uses MySQL database to resolve a normalized LCSH to a URI;
then it gets the authorized form
Uses MySQL database to resolve a normalized LCSH to a URI ; then it gets the authorized form
### Ignore name subject headings that don't have subject subdivisions #      orig_field << subfield.code  Ignore code errors for now #      auth_field_string << subfield.code  Ignore code errors for now ### Not a priority to identify incorrect indicators #    bad_rec = true if auth_field.tag == '130' && auth_indicator2 != field.indicator1 #    bad_rec = true if auth_field.tag != '130' && auth_indicator1 != field.indicator1
record
conn