Module: Cul::Scv::Hydra::Solrizer::ScvModsFieldable

Extended by:
ActiveSupport::Concern
Includes:
Solrizer::DefaultDescriptors::Normal
Included in:
Datastreams::ModsDocument
Defined in:
lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MODS_NS =
{'mods'=>'http://www.loc.gov/mods/v3'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize(t, strip_punctuation = false) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 397

def self.normalize(t, strip_punctuation=false)
  # strip whitespace
  n_t = t.dup.strip
  # collapse intermediate whitespace
  n_t.gsub!(/\s+/, ' ')
  # pull off paired punctuation, and any leading punctuation
  if strip_punctuation
    n_t = n_t.sub(/^\((.*)\)$/, "\\1")
    n_t = n_t.sub(/^\{(.*)\}$/, "\\1")
    n_t = n_t.sub(/^\[(.*)\]$/, "\\1")
    n_t = n_t.sub(/^"(.*)"$/, "\\1")
    n_t = n_t.sub(/^'(.*)'$/, "\\1")
    n_t = n_t.sub(/^<(.*)>$/, "\\1")
    #n_t = n_t.sub(/^\p{Ps}(.*)\p{Pe}/u, "\\1")
    n_t = n_t.sub(/^[[:punct:]]+/, '')
    # this may have 'created' leading/trailing space, so strip
    n_t.strip!
  end
  n_t
end

Instance Method Details

#all_subjects(node = mods) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 233

def all_subjects(node=mods)
	list_of_subjects = []

	node.xpath("./mods:subject/mods:topic", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:subject/mods:geographic", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:subject/mods:name", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:subject/mods:temporal", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:subject/mods:titleInfo", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:subject/mods:genre", MODS_NS).collect do |n|
      list_of_subjects << ScvModsFieldable.normalize(n.text, true)
    end

	return list_of_subjects
end

#alternative_titles(node = mods) ⇒ Object



74
75
76
77
78
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 74

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|
      ScvModsFieldable.normalize(t.text)
    end
end

#collectionsObject



36
37
38
39
40
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 36

def collections
  mods.xpath("./mods:relatedItem[@type='host' and @displayLabel='Collection']", MODS_NS).collect do |p_node|
    ScvModsFieldable.normalize(main_title(p_node), true)
  end
end

#date_notes(node = mods) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 201

def date_notes(node=mods)
	date_notes = []
	node.xpath("./mods:note[@type = 'date' or @type = 'date source']", MODS_NS).collect do |n|
      date_notes << ScvModsFieldable.normalize(n.text, true)
    end
	return date_notes
end

#date_range_to_textual_date(start_year, end_year) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 186

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



107
108
109
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 107

def dates(node=mods)
  # get all the dateIssued with keyDate = 'yes', but not point = 'end'
end

#formats(node = mods) ⇒ Object



111
112
113
114
115
116
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 111

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|
    ScvModsFieldable.normalize(n.text)
  end
end

#item_in_context_url(node = mods) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 217

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 << ScvModsFieldable.normalize(n.text, true)
    end
	item_in_context_url_val
end

#main_title(node = mods) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 56

def main_title(node=mods)
  # include only the untyped [!@type] titleInfo
  t = node.xpath('./mods:titleInfo[not(@type)]', MODS_NS).first
  if t
    ScvModsFieldable.normalize(t.text)
  else
    nil
  end
end

#modsObject



26
27
28
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 26

def mods
  ng_xml.xpath('/mods:mods', MODS_NS).first
end

#names(role_authority = nil, role = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 80

def names(role_authority=nil, role=nil)
  # get all the name nodes
  # keep all child text except the role terms
  xpath = "./mods:name"
  unless role_authority.nil?
    xpath << "/mods:role/mods:roleTerm[@authority='#{role_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(' ')
    ScvModsFieldable.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 << ScvModsFieldable.normalize(base_text, true)
  #end

  names
end

#non_date_notes(node = mods) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 209

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 << ScvModsFieldable.normalize(n.text, true)
    end
	return non_date_notes
end

#origin_info_place(node = mods) ⇒ Object



258
259
260
261
262
263
264
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 258

def origin_info_place(node=mods)
	places = []
	node.xpath("./mods:originInfo/mods:place/mods:placeTerm", MODS_NS).collect do |n|
      places << ScvModsFieldable.normalize(n.text, true)
    end
	return places
end

#origin_info_place_for_display(node = mods) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 266

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 << ScvModsFieldable.normalize(n.text, true)
    end
	node.xpath("./mods:originInfo/mods:place/mods:placeTerm[not(@valueURI)]", MODS_NS).collect do |n|
      places_without_uri << ScvModsFieldable.normalize(n.text, true)
    end

	return (places_without_uri.length > 0 ? places_without_uri : places_with_uri)
end

#project_url(node = mods) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 225

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 << ScvModsFieldable.normalize(n.text, true)
    end
	project_url_val
end

#projectsObject



30
31
32
33
34
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 30

def projects
  mods.xpath("./mods:relatedItem[@type='host' and @displayLabel='Project']", MODS_NS).collect do |p_node|
    ScvModsFieldable.normalize(main_title(p_node), true)
  end
end

#repository_code(node = mods) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 118

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
ScvModsFieldable.normalize(repo_code_node.text)
			else
return nil
			end
end

#repository_text(node = mods) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 129

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
ScvModsFieldable.normalize(repo_text_node.text)
			else
return nil
			end
end

#shelf_locators(node = mods) ⇒ Object



166
167
168
169
170
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 166

def shelf_locators(node=mods)
  node.xpath("./mods:location/mods:shelfLocator", MODS_NS).collect do |n|
    ScvModsFieldable.normalize(n.text, true)
  end
end

#sort_title(node = mods) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 42

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 = ScvModsFieldable.normalize(base_text, true)
  base_text = nil if base_text.empty?
  base_text
end

#textual_dates(node = mods) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 172

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 << ScvModsFieldable.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 << ScvModsFieldable.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 << ScvModsFieldable.normalize(n.text, true)
    end
	return dates
end

#titles(node = mods) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 66

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



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 280

def to_solr(solr_doc={})
  solr_doc = (defined? super) ? super : solr_doc

  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_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

  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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 154

def translate_project_title(project_title, type)
	normalized_project_title = ScvModsFieldable.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



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 140

def translate_repo_marc_code(code, type)
	#code = ScvModsFieldable.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



386
387
388
389
390
391
392
393
394
395
# File 'lib/cul_scv_hydra/solrizer/scv_mods_fieldable.rb', line 386

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