-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformations.py
139 lines (134 loc) · 5.52 KB
/
transformations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import csv
import argparse
import pandas as pd
from datetime import datetime
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file')
args = parser.parse_args()
if args.file:
filename = args.file
else:
filename = input('Enter filename (including \'.csv\'): ')
captureWords = [' and', 'Symbols', 'Names', ' for', ' of', 'chords', 'Chords',
'solfege', 'Solfege', 'Dance', 'dance', 'figures', 'concert',
'Concert', 'band']
itemList = []
with open(filename) as itemMetadataFile:
itemMetadata = csv.DictReader(itemMetadataFile)
for count, row in enumerate(itemMetadata):
row = row
fileIdentifier = row['fileIdentifier']
publish_date = row['field_publish_date_text']
box = fileIdentifier[5:8]
row['field_box'] = box.zfill(3)
id = fileIdentifier[-3:]
row['field_item_id'] = id.zfill(3)
title = row['dc.title']
authors = row['field_item_author']
authors = authors.split('|')
fullTitle = '{}.{} - {}'.format(str(box), str(id), title)
row['field_full_title'] = fullTitle
for author in authors:
if "(" in author:
role = author[author.find("(")+1:author.find(")")]
author = author.replace('('+role+')', '')
author = author.strip()
if '&' in role:
roles = role.split(' & ')
for r in roles:
existing = row.get(r)
if existing:
row[r] = existing+'|'+author
else:
row[r] = author
elif 'and' in role:
roles = role.split(' and ')
for r in roles:
existing = row.get(r)
if existing:
row[r] = existing+'|'+author
else:
row[r] = author
else:
existing = row.get(role)
if existing:
row[role] = existing+'|'+author
else:
row[role] = author
else:
author = author
existing = row.get('no_role')
if existing:
row['no_role'] = existing+'|'+author
else:
row['no_role'] = author
description = row['dc.description']
description = description.split("|")
for x in description:
if "Plate identifier: " in x:
x = x.replace('Plate identifier: ', '')
x = x.strip()
row['field_plate_number'] = x
elif "ad on" in x:
row['field_advertisement'] = x
elif "ads on" in x:
row['field_advertisement'] = x
elif "back cover" in x:
row['field_advertisement'] = x
elif "Johns Hopkins University, Levy Sheet Music Collection" in x:
row['boxitem'] = x
else:
x = x.strip()
x = x.rstrip('.')
x = x.split('.')
for component in x:
component = component.strip()
if '.' in component:
component = component.capitalize()
else:
component = component.capitalize()
component = component+'.'
existing = row.get('field_instrumentation')
if existing:
combined = existing+" "+component
row['field_instrumentation'] = combined
else:
row['field_instrumentation'] = component
field_dedicatee = row['field_dedicatee']
row['field_dedicatee'] = field_dedicatee.replace('|', ' ')
instrumentation = row.get('field_instrumentation')
if instrumentation is not None:
for word in captureWords:
if word in instrumentation:
instrumentation = instrumentation.replace(word, '')
instrumentation = instrumentation.lower()
instrumentation = instrumentation.strip()
instrumentation = instrumentation.replace('.', '')
instrumentation = instrumentation.replace(',', '')
instrumentation = instrumentation.split()
instrumentation = set(instrumentation)
instrumentation = list(instrumentation)
instrumentation = sorted(instrumentation)
instrumentation = "|".join(instrumentation)
row['field_instrumentation_metadata'] = instrumentation
alternative = row['dc.title.alternative']
alternative = alternative.split('|')
for x in alternative:
if "[first line]" in x:
x = x.replace('[first line]', '')
x = x.strip()
row['field_first_line'] = x
else:
x = x.replace('[first line of chorus]', '')
x = x.strip()
row['field_first_line_of_chorus'] = x
try:
publish_date = int(publish_date)
row['field_publish_date_year'] = str(publish_date)
except (TypeError, ValueError):
pass
itemList.append(row)
dt = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
df_1 = pd.DataFrame.from_records(itemList)
filename = filename[:-4]
df_1.to_csv(filename+'_1.csv', index=False, quoting=csv.QUOTE_ALL)