Skip to content

Commit

Permalink
Fix MARC21 export to work with the new data model
Browse files Browse the repository at this point in the history
Acronym handling is not perfect, see #8
  • Loading branch information
danmichaelo committed Nov 27, 2015
1 parent 488ae48 commit ad297be
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions roald/models/marc21.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, concepts=None, created_by=None, vocabulary=None, language=Non
if concepts is not None:
self.load(concepts)

# Todo: Move into superclass
def load(self, data):
if type(data) == dict:
self.concepts = Concepts(data)
Expand Down Expand Up @@ -54,6 +55,35 @@ def global_cn(self, value):
else:
return '({}){}'.format(self.created_by, value)

def add_acronyms(self, builder, term, conceptType):

if term.get('hasAcronym'):
tag = {
'Temporal': '448',
'Topic': '450',
'Geographic': '451',
'GenreForm': '455',
}[conceptType]
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(term.get('hasAcronym'), code='a')

# Heading in the tracing field is an acronym for the heading in the 1XX field.
# Ref: http://www.loc.gov/marc/authority/adtracing.html
builder.subfield('d', code='g')

if term.get('acronymFor'):
tag = {
'Temporal': '448',
'Topic': '450',
'Geographic': '451',
'GenreForm': '455',
}[conceptType]
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(term.get('acronymFor'), code='a')

# No special indication possible?
# https://github.com/realfagstermer/roald/issues/8

def convert_concept(self, builder, concept, concepts):

if concept.get('created'):
Expand Down Expand Up @@ -140,7 +170,8 @@ def convert_concept(self, builder, concept, concepts):
with builder.datafield(tag=tag, ind1=' ', ind2=' '):

# Add the first component. Always use subfield $a. Correct???
builder.subfield(rel['prefLabel'][self.language.alpha2], code='a')
term = rel['prefLabel'][self.language.alpha2]
builder.subfield(term['value'], code='a')

# Add remaining components
for value in concept.get('component')[1:]:
Expand All @@ -155,10 +186,11 @@ def convert_concept(self, builder, concept, concepts):
}[rel['type'][0]]

# OBS! 150 har også $b.. Men når brukes egentlig den??
builder.subfield(rel['prefLabel'][self.language.alpha2], code=sf)
term = rel['prefLabel'][self.language.alpha2]
builder.subfield(term['value'], code=sf)

else: # Not a compound heading
for lang, value in concept.get('prefLabel').items():
for lang, term in concept.get('prefLabel').items():
tag = {
'Temporal': '148',
'Topic': '150',
Expand All @@ -169,34 +201,26 @@ def convert_concept(self, builder, concept, concepts):

# Always use subfield $a. Correct???
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(value, code='a')
builder.subfield(term['value'], code='a')

self.add_acronyms(builder, term, conceptType)


# Add 448/450/451/455 See from tracings
for lang, values in concept.get('altLabel', {}).items():
for lang, terms in concept.get('altLabel', {}).items():
tag = {
'Temporal': '448',
'Topic': '450',
'Geographic': '451',
'GenreForm': '455',
}[conceptType]
if lang == self.language.alpha2:
for value in values:
for term in terms:
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
# Always use subfield $a. Correct???
builder.subfield(value, code='a')
for value in concept.get('acronym', {}):
tag = {
'Temporal': '448',
'Topic': '450',
'Geographic': '451',
'GenreForm': '455',
}[conceptType]
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(value, code='a')
builder.subfield(term['value'], code='a')

# Heading in the tracing field is an acronym for the heading in the 1XX field.
# Ref: http://www.loc.gov/marc/authority/adtracing.html
builder.subfield('d', code='g')
self.add_acronyms(builder, term, conceptType)

# 548/550/551/555 See also
for value in concept.get('broader', []):
Expand All @@ -208,14 +232,14 @@ def convert_concept(self, builder, concept, concepts):
'GenreForm': '555',
}[rel['type'][0]]
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(rel['prefLabel'][self.language.alpha2], code='a')
builder.subfield(rel['prefLabel'][self.language.alpha2]['value'], code='a')
builder.subfield('g', code='w') # Ref: http://www.loc.gov/marc/authority/adtracing.html
builder.subfield(self.global_cn(value), code='0')

for value in self.narrower.get(concept['id'], []):
rel = concepts.get(id=value)
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(rel['prefLabel'][self.language.alpha2], code='a')
builder.subfield(rel['prefLabel'][self.language.alpha2]['value'], code='a')
builder.subfield('h', code='w') # Ref: http://www.loc.gov/marc/authority/adtracing.html
builder.subfield(self.global_cn(value), code='0')

Expand All @@ -228,7 +252,7 @@ def convert_concept(self, builder, concept, concepts):
'GenreForm': '555',
}[rel['type'][0]]
with builder.datafield(tag=tag, ind1=' ', ind2=' '):
builder.subfield(rel['prefLabel'][self.language.alpha2], code='a')
builder.subfield(rel['prefLabel'][self.language.alpha2]['value'], code='a')
builder.subfield(self.global_cn(value), code='0')

# 680 Notes
Expand Down

0 comments on commit ad297be

Please sign in to comment.