Module: Cul::Hydra::Solrizer::ModsFieldable
- Extended by:
- ActiveSupport::Concern, ClassMethods
- Includes:
- Solrizer::DefaultDescriptors::Normal
- Included in:
- Datastreams::ModsDocument
- Defined in:
- lib/cul_hydra/solrizer/mods_fieldable.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- MODS_NS =
{'mods'=>'http://www.loc.gov/mods/v3'}
Instance Method Summary collapse
- #all_subjects(node = mods) ⇒ Object
- #alternative_titles(node = mods) ⇒ Object
- #collections ⇒ Object
- #coordinates(node = mods) ⇒ Object
- #date_notes(node = mods) ⇒ Object
- #date_range_to_textual_date(start_year, end_year) ⇒ Object
- #dates(node = mods) ⇒ Object
- #formats(node = mods) ⇒ Object
- #item_in_context_url(node = mods) ⇒ Object
- #main_title(node = mods) ⇒ Object
- #mods ⇒ Object
- #names(role_authority = nil, role = nil) ⇒ Object
- #non_date_notes(node = mods) ⇒ Object
- #non_item_in_context_url(node = mods) ⇒ Object
- #origin_info_place(node = mods) ⇒ Object
- #origin_info_place_for_display(node = mods) ⇒ Object
- #project_url(node = mods) ⇒ Object
- #projects ⇒ Object
- #repository_code(node = mods) ⇒ Object
- #repository_text(node = mods) ⇒ Object
- #shelf_locators(node = mods) ⇒ Object
- #sort_title(node = mods) ⇒ Object
- #textual_dates(node = mods) ⇒ Object
- #titles(node = mods) ⇒ Object
- #to_solr(solr_doc = {}) ⇒ Object
- #translate_project_title(project_title, type) ⇒ Object
- #translate_repo_marc_code(code, type) ⇒ Object
- #zero_pad_year(year) ⇒ Object
Methods included from ClassMethods
map_field, map_value, maps_field?, normalize, value_mapper
Instance Method Details
#all_subjects(node = mods) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 263 def all_subjects(node=mods) list_of_subjects = [] node.xpath("./mods:subject/mods:topic", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:subject/mods:geographic", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:subject/mods:name", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:subject/mods:temporal", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:subject/mods:titleInfo", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:subject/mods:genre", MODS_NS).collect do |n| list_of_subjects << ModsFieldable.normalize(n.text, true) end return list_of_subjects end |
#alternative_titles(node = mods) ⇒ Object
96 97 98 99 100 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 96 def alternative_titles(node=mods) node.xpath('./mods:titleInfo[@type and (@type="alternative" or @type="abbreviated" or @type="translated" or @type="uniform")]', MODS_NS).collect do |t| ModsFieldable.normalize(t.text) end end |
#collections ⇒ Object
58 59 60 61 62 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 58 def collections mods.xpath("./mods:relatedItem[@type='host' and @displayLabel='Collection']", MODS_NS).collect do |p_node| ModsFieldable.normalize(main_title(p_node), true) end end |
#coordinates(node = mods) ⇒ Object
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 310 def coordinates(node=mods) coordinate_values = [] node.xpath("./mods:subject/mods:cartographics/mods:coordinates", MODS_NS).collect do |n| n = ModsFieldable.normalize(n.text, true) if n.match(/-*\d+\.\d+\s*,\s*-*\d+\.\d+\s*/) # Expected coordinate format: 40.123456,-73.5678 coordinate_values << n end end coordinate_values end |
#date_notes(node = mods) ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 223 def date_notes(node=mods) date_notes = [] node.xpath("./mods:note[@type = 'date' or @type = 'date source']", MODS_NS).collect do |n| date_notes << ModsFieldable.normalize(n.text, true) end return date_notes end |
#date_range_to_textual_date(start_year, end_year) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 208 def date_range_to_textual_date(start_year, end_year) start_year = start_year.to_i.to_s # Remove zero-padding if present end_year = end_year.to_i.to_s # Remove zero-padding if present if start_year == end_year return [start_year] else return [('Between ' + (start_year.to_i > 0 ? start_year : start_year[1,start_year.length] + ' BCE') + ' and ' + (end_year.to_i > 0 ? (start_year.to_i > 0 ? end_year : end_year + ' CE') : end_year[1,end_year.length] + ' BCE') )] end end |
#dates(node = mods) ⇒ Object
129 130 131 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 129 def dates(node=mods) # get all the dateIssued with keyDate = 'yes', but not point = 'end' end |
#formats(node = mods) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 133 def formats(node=mods) # get all the form values with authority != 'marcform' node.xpath("./mods:physicalDescription/mods:form[@authority != 'marcform']", MODS_NS).collect do |n| ModsFieldable.normalize(n.text) end end |
#item_in_context_url(node = mods) ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 239 def item_in_context_url(node=mods) item_in_context_url_val = [] node.xpath("./mods:location/mods:url[@access='object in context' and @usage='primary display']", MODS_NS).collect do |n| item_in_context_url_val << ModsFieldable.normalize(n.text, true) end item_in_context_url_val end |
#main_title(node = mods) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 78 def main_title(node=mods) # include only the untyped [!@type] titleInfo t = node.xpath('./mods:titleInfo[not(@type)]', MODS_NS).first if t ModsFieldable.normalize(t.text) else nil end end |
#mods ⇒ Object
48 49 50 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 48 def mods ng_xml.xpath('/mods:mods', MODS_NS).first end |
#names(role_authority = nil, role = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 102 def names(=nil, role=nil) # get all the name nodes # keep all child text except the role terms xpath = "./mods:name" unless .nil? xpath << "/mods:role/mods:roleTerm[@authority='#{.to_s}'" unless role.nil? xpath << " and normalize-space(text()) = '#{role.to_s.strip}'" end xpath << "]/ancestor::mods:name" end names = mods.xpath(xpath, MODS_NS).collect do |node| base_text = node.xpath('./mods:namePart', MODS_NS).collect { |c| c.text }.join(' ') ModsFieldable.normalize(base_text, true) end # Note: Removing subject names from name field extraction. # See: https://issues.cul.columbia.edu/browse/DCV-231 and https://issues.cul.columbia.edu/browse/SCV-102 #xpath = "./mods:subject" + xpath[1,xpath.length] #mods.xpath(xpath, MODS_NS).each do |node| # base_text = node.xpath('./mods:namePart', MODS_NS).collect { |c| c.text }.join(' ') # names << ModsFieldable.normalize(base_text, true) #end names end |
#non_date_notes(node = mods) ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 231 def non_date_notes(node=mods) non_date_notes = [] node.xpath("./mods:note[not(@type) or (@type != 'date' and @type != 'date source')]", MODS_NS).collect do |n| non_date_notes << ModsFieldable.normalize(n.text, true) end return non_date_notes end |
#non_item_in_context_url(node = mods) ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 247 def non_item_in_context_url(node=mods) non_item_in_context_url_val = [] node.xpath("./mods:location/mods:url[not(@access='object in context')]", MODS_NS).collect do |n| non_item_in_context_url_val << ModsFieldable.normalize(n.text, true) end non_item_in_context_url_val end |
#origin_info_place(node = mods) ⇒ Object
288 289 290 291 292 293 294 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 288 def origin_info_place(node=mods) places = [] node.xpath("./mods:originInfo/mods:place/mods:placeTerm", MODS_NS).collect do |n| places << ModsFieldable.normalize(n.text, true) end return places end |
#origin_info_place_for_display(node = mods) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 296 def origin_info_place_for_display(node=mods) # If there are multiple origin_info place elements, choose only the ones without valueURI attributes. Otherwise show the others. places_with_uri = [] places_without_uri = [] node.xpath("./mods:originInfo/mods:place/mods:placeTerm[@valueURI]", MODS_NS).collect do |n| places_with_uri << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:originInfo/mods:place/mods:placeTerm[not(@valueURI)]", MODS_NS).collect do |n| places_without_uri << ModsFieldable.normalize(n.text, true) end return (places_without_uri.length > 0 ? places_without_uri : places_with_uri) end |
#project_url(node = mods) ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 255 def project_url(node=mods) project_url_val = [] node.xpath("./mods:relatedItem[@type='host' and @displayLabel='Project']/mods:location/mods:url", MODS_NS).collect do |n| project_url_val << ModsFieldable.normalize(n.text, true) end project_url_val end |
#projects ⇒ Object
52 53 54 55 56 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 52 def projects mods.xpath("./mods:relatedItem[@type='host' and @displayLabel='Project']", MODS_NS).collect do |p_node| ModsFieldable.normalize(main_title(p_node), true) end end |
#repository_code(node = mods) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 140 def repository_code(node=mods) # get the location/physicalLocation[@authority = 'marcorg'] repo_code_node = node.xpath("./mods:location/mods:physicalLocation[@authority = 'marcorg']", MODS_NS).first if repo_code_node ModsFieldable.normalize(repo_code_node.text) else return nil end end |
#repository_text(node = mods) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 151 def repository_text(node=mods) # get the location/physicalLocation[not(@authority)] repo_text_node = node.xpath("./mods:location/mods:physicalLocation[not(@authority)]", MODS_NS).first if repo_text_node ModsFieldable.normalize(repo_text_node.text) else return nil end end |
#shelf_locators(node = mods) ⇒ Object
188 189 190 191 192 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 188 def shelf_locators(node=mods) node.xpath("./mods:location/mods:shelfLocator", MODS_NS).collect do |n| ModsFieldable.normalize(n.text, true) end end |
#sort_title(node = mods) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 64 def sort_title(node=mods) # include only the untyped [!@type] titleInfo, exclude noSort base_text = '' t = node.xpath('./mods:titleInfo[not(@type)]', MODS_NS).first if t t.children.each do |child| base_text << child.text unless child.name == 'nonSort' end end base_text = ModsFieldable.normalize(base_text, true) base_text = nil if base_text.empty? base_text end |
#textual_dates(node = mods) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 194 def textual_dates(node=mods) dates = [] node.xpath("./mods:originInfo/mods:dateCreated[not(@keyDate) and not(@point) and not(@w3cdtf)]", MODS_NS).collect do |n| dates << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:originInfo/mods:dateIssued[not(@keyDate) and not(@point) and not(@w3cdtf)]", MODS_NS).collect do |n| dates << ModsFieldable.normalize(n.text, true) end node.xpath("./mods:originInfo/mods:dateOther[not(@keyDate) and not(@point) and not(@w3cdtf)]", MODS_NS).collect do |n| dates << ModsFieldable.normalize(n.text, true) end return dates end |
#titles(node = mods) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 88 def titles(node=mods) # all titles without descending into relatedItems # For now, this only includes the main title and selected alternate_titles all_titles = [] all_titles << main_title unless main_title.nil? all_titles += alternative_titles unless alternative_titles.nil? end |
#to_solr(solr_doc = {}) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 321 def to_solr(solr_doc={}) solr_doc = (defined? super) ? super : solr_doc return solr_doc if mods.nil? # There is no mods. Return because there is nothing to process, otherwise NoMethodError will be raised by subsequent lines. solr_doc["all_text_teim"] ||= [] solr_doc["title_si"] = sort_title solr_doc["title_ssm"] = titles solr_doc["alternative_title_ssm"] = alternative_titles solr_doc["all_text_teim"] += solr_doc["alternative_title_ssm"] solr_doc["lib_collection_sim"] = collections solr_doc["lib_name_sim"] = names solr_doc["lib_name_teim"] = solr_doc["lib_name_sim"] solr_doc["all_text_teim"] += solr_doc["lib_name_teim"] solr_doc["lib_all_subjects_ssm"] = all_subjects solr_doc["lib_all_subjects_teim"] = solr_doc["lib_all_subjects_ssm"] solr_doc["all_text_teim"] += solr_doc["lib_all_subjects_teim"] solr_doc["lib_name_ssm"] = solr_doc["lib_name_sim"] solr_doc["lib_author_sim"] = names(:marcrelator, 'aut') solr_doc["lib_recipient_sim"] = names(:marcrelator, 'rcp') solr_doc["lib_format_sim"] = formats solr_doc["lib_shelf_sim"] = shelf_locators solr_doc["lib_date_textual_ssm"] = textual_dates solr_doc["lib_date_notes_ssm"] = date_notes solr_doc["lib_non_date_notes_ssm"] = non_date_notes solr_doc["lib_item_in_context_url_ssm"] = item_in_context_url solr_doc["lib_non_item_in_context_url_ssm"] = non_item_in_context_url solr_doc["lib_project_url_ssm"] = project_url solr_doc["origin_info_place_ssm"] = origin_info_place solr_doc["origin_info_place_for_display_ssm"] = origin_info_place_for_display repo_marc_code = repository_code unless repo_marc_code.nil? solr_doc["lib_repo_short_ssim"] = [translate_repo_marc_code(repo_marc_code, 'short')] solr_doc["lib_repo_long_sim"] = [translate_repo_marc_code(repo_marc_code, 'long')] solr_doc["lib_repo_full_ssim"] = [translate_repo_marc_code(repo_marc_code, 'full')] end solr_doc["lib_repo_text_ssm"] = repository_text project_titles = projects unless project_titles.nil? solr_doc["lib_project_short_ssim"] = [] solr_doc["lib_project_full_ssim"] = [] project_titles.each {|project_title| solr_doc["lib_project_short_ssim"] << translate_project_title(project_title, 'short') solr_doc["lib_project_full_ssim"] << translate_project_title(project_title, 'full') } solr_doc["lib_project_short_ssim"].uniq! solr_doc["lib_project_full_ssim"].uniq! end # Create convenient start and end date values based on one of the many possible originInfo/dateX elements. possible_start_date_fields = ['origin_info_date_issued_ssm', 'origin_info_date_issued_start_ssm', 'origin_info_date_created_ssm', 'origin_info_date_created_start_ssm', 'origin_info_date_other_ssm', 'origin_info_date_other_start_ssm'] possible_end_date_fields = ['origin_info_date_issued_end_ssm', 'origin_info_date_created_end_ssm', 'origin_info_date_other_end_ssm'] start_date = nil end_date = nil start_year = nil end_year = nil possible_start_date_fields.each{|key| if solr_doc.has_key?(key) start_date = solr_doc[key][0] break end } possible_end_date_fields.each{|key| if solr_doc.has_key?(key) end_date = solr_doc[key][0] break end } if start_date.present? end_date = start_date if end_date.blank? year_regex = /^(-?\d{1,4}).*/ start_year_match = start_date.match(year_regex) if start_year_match && start_year_match.captures.length > 0 start_year = start_year_match.captures[0] start_year = zero_pad_year(start_year) solr_doc["lib_start_date_year_itsi"] = start_year.to_i # TrieInt version for searches end end_year_match = end_date.match(year_regex) if end_year_match && end_year_match.captures.length > 0 end_year = end_year_match.captures[0] end_year = zero_pad_year(end_year) solr_doc["lib_end_date_year_itsi"] = end_year.to_i # TrieInt version for searches end solr_doc["lib_date_year_range_si"] = start_year + '-' + end_year if start_year # When no textual date is available, fall back to other date data (if available) if solr_doc["lib_date_textual_ssm"].blank? solr_doc["lib_date_textual_ssm"] = date_range_to_textual_date(start_year.to_i, end_year.to_i) end end # Geo data solr_doc["geo"] = coordinates solr_doc.each do |k, v| if self.class.maps_field? k solr_doc[k] = self.class.map_value(k, v) end end solr_doc end |
#translate_project_title(project_title, type) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 176 def translate_project_title(project_title, type) normalized_project_title = ModsFieldable.normalize(project_title) if type == 'short' return translate_with_default(SHORT_PROJ, normalized_project_title, normalized_project_title) elsif type == 'full' return translate_with_default(FULL_PROJ, normalized_project_title, normalized_project_title) end return nil end |
#translate_repo_marc_code(code, type) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 162 def translate_repo_marc_code(code, type) #code = ModsFieldable.normalize(code) if type == 'short' return translate_with_default(SHORT_REPO, code, 'Non-Columbia Location') elsif type == 'long' return translate_with_default(LONG_REPO, code, 'Non-Columbia Location') elsif type == 'full' return translate_with_default(FULL_REPO, code, 'Non-Columbia Location') end return nil end |
#zero_pad_year(year) ⇒ Object
433 434 435 436 437 438 439 440 441 442 |
# File 'lib/cul_hydra/solrizer/mods_fieldable.rb', line 433 def zero_pad_year(year) year = year.to_s is_negative = year.start_with?('-') year_without_sign = (is_negative ? year[1, year.length]: year) if year_without_sign.length < 4 year_without_sign = year_without_sign.rjust(4, '0') end return (is_negative ? '-' : '') + year_without_sign end |