-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprefilter_main.py
executable file
·183 lines (151 loc) · 5.84 KB
/
prefilter_main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import csv
import os
from prefilter_conf import (
estc_csv_location,
sane_out,
false_out,
duplicated_out,
rec_id_table_output_location,
summaryfile_location
)
from lib.estc_marc import (
ESTCMARCEntry,
ESTCMARCEntryWriteBuffer)
from lib.estc_prepicker_common import (
get_file_len,
print_progress,
read_estc_csv,
create_prefilter_summary_file
)
def process_record_lines(record_lines,
processed_entries,
sane_buffer,
filter_buffer,
duplicated_buffer,
filterid_set=None,
force_write=False):
new_estc_entry = ESTCMARCEntry(record_lines, filterid_set)
if new_estc_entry.testrecord or not new_estc_entry.curives_sane:
master_record_list.append(new_estc_entry.record_seq)
filter_buffer.add_marc_entry(new_estc_entry)
processed_entries['category'].append("bad")
elif new_estc_entry.curives in processed_entries['estc_id']:
duplicated_buffer.add_marc_entry(new_estc_entry)
processed_entries['category'].append("duplicated")
else:
sane_buffer.add_marc_entry(new_estc_entry)
processed_entries['category'].append("sane")
processed_entries['record_seq'].append(new_estc_entry.record_seq)
processed_entries['estc_id'].append(new_estc_entry.curives)
if force_write:
sane_buffer.write_marc_entry_csv()
filter_buffer.write_marc_entry_csv()
duplicated_buffer.write_marc_entry_csv()
return processed_entries
def load_filterdata_set(filterdata_location):
with open(filterdata_location, 'r') as csvfile:
filterlist = []
csvreader = csv.reader(csvfile)
for line in csvreader:
line_split = line[0].split('(')
for item in line_split:
if len(item) > 0:
item = "(" + item
filterlist.append(item)
filterset = set(filterlist)
return filterset
def write_rec_id_table(processed_entries, output_location):
with open(output_location, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['record_seq', 'estc_id', 'category'])
for i in range(0, len(processed_entries['record_seq'])):
csvwriter.writerow([processed_entries['record_seq'][i],
processed_entries['estc_id'][i],
processed_entries['category'][i]])
def get_035z_values(estc_raw_csv):
values_035z = []
with open(estc_raw_csv, 'r') as csvfile:
csvreader = csv.DictReader(csvfile, delimiter='\t')
for row in csvreader:
if row['Field_code'] == '035' and row['Subfield_code'] == "z":
this_items = row['Value'].lower().split("(cu-rives)")
for item in this_items:
item_u = item.upper()
if len(item_u) > 0:
values_035z.append(item_u)
return set(values_035z)
# ------------------------------------------
# Main script
# ------------------------------------------
# !OBS Input and output location in ./prefilter_conf.py .
# generate filterdata:
print("Getting values in 035z for filtering bad ids ...")
filterid_set = get_035z_values(estc_csv_location)
print(" ... done!")
# Delete old output files if they exist. The script writes in append -mode,
# so if old files are not deleted they will be added to.
for filepath in [sane_out, false_out, duplicated_out]:
if os.path.exists(filepath):
os.remove(filepath)
# Setup output write buffers.
sane_estc_entry_buffer = ESTCMARCEntryWriteBuffer(sane_out)
filter_estc_entry_buffer = ESTCMARCEntryWriteBuffer(false_out)
duplicated_estc_entry_buffer = ESTCMARCEntryWriteBuffer(duplicated_out)
master_record_list = []
# Loop all lines in raw ESTC csv.
# Process each record, write outputs now and then.
prev_record_seq = None
record_lines = list()
# processed_curives = list()
processed_entries = {'record_seq': [],
'estc_id': [],
'category': []}
file_lines = get_file_len(estc_csv_location)
# Process each record line (and create marc entry)
i = 0
print("Processing ESTC csv ...")
for row in read_estc_csv(estc_csv_location):
i += 1
if i % 1000 == 0:
print_progress(i, file_lines)
# Special case for first entry
if prev_record_seq is None:
prev_record_seq = row.get('Record_seq')
current_record_seq = row.get('Record_seq')
# Check if Record_seq changes. If changed, process record and start new
if current_record_seq != prev_record_seq:
processed_entries = process_record_lines(
record_lines,
processed_entries,
sane_estc_entry_buffer,
filter_estc_entry_buffer,
duplicated_estc_entry_buffer,
filterid_set=filterid_set)
# processed_curives.append(processed_id_pair['curives'])
record_lines = [row]
prev_record_seq = current_record_seq
# If record seq does not change, append to working record buffer.
else:
record_lines.append(row)
# Process last lines left in read buffer and write stuff left in write buffers:
processed_entries = process_record_lines(
record_lines,
processed_entries,
sane_estc_entry_buffer,
filter_estc_entry_buffer,
duplicated_estc_entry_buffer,
filterid_set=filterid_set,
force_write=True)
print("")
print("Done!")
print("Writing record id table ...")
write_rec_id_table(processed_entries, rec_id_table_output_location)
print("")
print("Done!")
print("Writing summaryfile ...")
create_prefilter_summary_file(estc_csv_location,
sane_out,
false_out,
duplicated_out,
summaryfile_location)
print("Done!")