-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpunctuation.py
More file actions
2189 lines (2177 loc) · 104 KB
/
Copy pathpunctuation.py
File metadata and controls
2189 lines (2177 loc) · 104 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pymarc import MARCReader, Field
from helpers import *
from string import *
# The ASCII and ANSEL punctuation marks, plus the Euro sign and a space
any_punct = ['!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-',
'.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^',
'_', '`', '{', '|', '}', '~', 'ʹ', '·', '♭', '®', '±', 'ʺ', '£',
'°', '℗', '©', '♯', '¿', '¡', '€', ' ']
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
undefined_fields = [
'011', '021', '023', '039', '053', '054', '056', '057', '058', '059',
'062', '063', '064', '065', '067', '068', '069', '073', '075', '076',
'077', '078', '079', '081', '087', '089', '091', '093', '094', '095',
'097', '101', '102', '103', '104', '105', '106', '107', '108', '109',
'112', '113', '114', '115', '116', '117', '118', '119', '120', '121',
'122', '123', '124', '125', '126', '127', '128', '129', '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', '184', '185', '186', '187', '188', '189', '190', '191', '192',
'193', '194', '195', '196', '197', '198', '199', '200', '201', '202',
'203', '204', '205', '206', '207', '208', '209', '211', '212', '213',
'214', '215', '216', '217', '218', '219', '220', '221', '223', '224',
'225', '226', '227', '228', '229', '230', '231', '232', '233', '234',
'235', '236', '237', '238', '239', '241', '244', '248', '249', '252',
'253', '259', '265', '266', '267', '268', '269', '271', '272', '273',
'274', '275', '276', '277', '278', '279', '280', '281', '282', '283',
'284', '285', '286', '287', '288', '289', '290', '291', '292', '293',
'294', '295', '296', '297', '298', '299', '301', '302', '303', '304',
'305', '308', '309', '311', '312', '313', '314', '315', '316', '317',
'318', '319', '320', '322', '323', '324', '325', '326', '327', '328',
'329', '330', '331', '332', '333', '334', '335', '339', '349', '350',
'353', '354', '356', '358', '359', '360', '361', '364', '367', '368',
'369', '371', '372', '373', '374', '375', '376', '378', '379', '387',
'389', '390', '391', '392', '393', '394', '395', '396', '397', '398',
'399', '401', '402', '403', '404', '405', '406', '407', '408', '409',
'412', '413', '414', '415', '416', '417', '418', '419', '420', '421',
'422', '423', '424', '425', '426', '427', '428', '429', '430', '431',
'432', '433', '434', '435', '436', '437', '438', '439', '441', '442',
'443', '444', '445', '446', '447', '448', '449', '450', '451', '452',
'453', '454', '455', '456', '457', '458', '459', '460', '461', '462',
'463', '464', '465', '466', '467', '468', '469', '470', '471', '472',
'473', '474', '475', '476', '477', '478', '479', '480', '481', '482',
'483', '484', '485', '486', '487', '488', '489', '491', '492', '493',
'494', '495', '496', '497', '498', '499', '503', '509', '512', '517',
'519', '523', '527', '528', '529', '531', '537', '543', '548', '549',
'551', '553', '554', '557', '558', '559', '560', '564', '566', '568',
'569', '570', '571', '572', '573', '574', '575', '576', '577', '578',
'579', '582', '587', '589', '601', '602', '603', '604', '605', '606',
'607', '608', '609', '612', '613', '614', '615', '616', '617', '618',
'619', '620', '621', '622', '623', '624', '625', '626', '627', '628',
'629', '631', '632', '633', '634', '635', '636', '637', '638', '639',
'640', '641', '642', '643', '644', '645', '646', '649', '652', '659',
'660', '661', '663', '664', '665', '666', '667', '668', '669', '670',
'671', '672', '673', '674', '675', '676', '677', '678', '679', '680',
'681', '682', '683', '684', '685', '686', '687', '689', '692', '693',
'694', '701', '702', '703', '704', '705', '706', '707', '708', '709',
'712', '713', '714', '715', '716', '717', '718', '719', '721', '722',
'723', '724', '725', '726', '727', '728', '729', '731', '732', '733',
'734', '735', '736', '737', '738', '739', '741', '742', '743', '744',
'745', '746', '747', '748', '749', '750', '755', '756', '757', '759',
'761', '763', '764', '766', '768', '769', '771', '778', '779', '781',
'782', '783', '784', '788', '789', '794', '795', '801', '802', '803',
'804', '805', '806', '807', '808', '809', '812', '813', '814', '815',
'816', '817', '818', '819', '820', '821', '822', '823', '824', '825',
'826', '827', '828', '829', '831', '832', '833', '834', '835', '836',
'837', '838', '839', '840', '846', '847', '848', '849', '857', '858',
'859', '860', '861', '862', '869', '870', '871', '872', '873', '874',
'875', '879', '881', '888', '889', '890', '892', '893', '894', '895',
'908', '909', '912', '913', '914', '915', '916', '917', '918', '919',
'920', '921', '922', '923', '924', '925', '926', '927', '928', '929',
'931', '932', '933', '934', '935', '937', '939', '940', '941', '942',
'943', '944', '950', '951', '952', '953', '954', '955', '957', '958',
'959', '960', '961', '962', '963', '964', '965', '966', '967', '968',
'969', '970', '971', '972', '973', '974', '975', '976', '977', '978',
'979', '984', '985', '986', '988', '989', '991', '992', '993', '995',
'996', '997', '998', '999'
]
# This list keeps track of every field without LC or OCLC punctuation
# instructions
# Note that 880 should have the same punctuation as the tag given in
# subfield 6.
no_instructions = [
'013', '014', '016', '027', '029', '031', '038', '040', '041', '042',
'043', '047', '048', '061', '066', '071', '072', '074', '080', '084',
'085', '088', '090', '096', '098', '099', '539', '542', '599', '648',
'662', '690', '691', '695', '720', '751', '758', '841', '842', '844',
'851', '852', '853', '854', '855', '856', '863', '864', '865', '866',
'867', '868', '876', '877', '878', '880', '882', '883', '884', '885',
'886', '887', '891', '901', '902', '903', '904', '905', '906', '907',
'945', '946', '947', '948', '949', '990'
]
linking_entry_fields = ['760', '762', '765', '767', '770', '772', '773',
'774', '775', '776', '777', '780', '785', '786',
'787']
def punctuate(r, f):
"""Adds the punctuation to a field. Seperated from main() to
reduce indentation."""
count505 = len(r.get_fields('505'))
# Introductory relationship subfields should end in a
# colon.
try:
if f.subfields[0] == 'i':
append_punct(f, 1, ':')
elif f.subfields[0] == '3' and f.subfields[3] == 'i':
append_punct(f, 3, ':')
except IndexError:
print(f)
# Remove any ending punctuation without removing
# significant spaces.
if f.tag in ['010']:
del_from_end(f, exempt = [' '], abbrev_exempt = False)
elif f.tag in ['015']:
# Field does not end with a mark of punctuation
# unless ending in an abbreviation or data ending
# with a mark of punctuation
del_from_end(f, exempt = ['...', '-', ')', '!', '?'],
abbrev_exempt = True)
for n, sub in enumerate(f.subfields):
# Remove parentheses enclosing subfield a
if sub in ['a', 'z'] and n % 2 == 0:
del_from_start(f, n+1, del_list = ['('])
del_from_end(f, n+1, del_list = [')'])
# Enclose subfields q in parentheses
if sub == 'q' and n % 2 == 0:
if n == 0 or f.subfields[n-2] != 'q':
prepend_punct(f, n+1, '(')
if (last_subcode_index(f) == n
or f.subfields[n+2] != 'q'):
append_punct(f, n+1, ')')
else:
append_punct(f, n+1, ' ;')
elif f.tag in ['017', '018', '535', '583']:
# Field does not end with a mark of punctuation
# unless ending in an abbreviation or data ending
# with a mark of punctuation.
del_from_end(f, exempt = ['?', '!', '-', '"'],
abbrev_exempt = True)
elif f.tag in ['020', '024']:
# Field does not end with a mark of punctuation
# unless ending in an abbreviation or data ending
# with a mark of punctuation.
del_from_end(f, exempt = ['...', '-', ')', '!', '?'],
abbrev_exempt = True)
for n, sub in enumerate(f.subfields):
end_punct = ''
if n % 2 != 0 and current_sub(f, n) == 'q':
# Begin qualifying data with a parenthesis.
if n == 1 or prev_sub(f, n) != 'q':
prepend_punct(f, n, '(')
# Separate subfields q with space-
# semicolon.
if (n == last_subdata_index(f)
or next_sub(f, n) != 'q'):
end_punct = ''.join([end_punct, ')'])
elif next_sub(f, n) == 'q':
end_punct = ''.join([end_punct, ' ;'])
# Precede subfield c with space-colon.
if (n < last_subdata_index(f)
and next_sub(f, n) == 'c'):
end_punct = ''.join([end_punct, ' :'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct)
elif f.tag in ['022']:
# Field does not end with a period.
del_from_end(f, del_list = ['.'])
elif f.tag in ['025']:
# Field does not end in a mark of punctuation
del_from_end(f)
# Field does not include spaces
remove_punct(f, subfields = ['a'], punct = [' ', '(', ')'])
elif f.tag in ['027', '028']:
# Field does not end with a mark of punctuation
# unless ending in an abbreviation or data ending
# with a mark of punctuation.
del_from_end(f, exempt = ['...', '-', ')', '!', '?'],
abbrev_exempt = True)
for n, sub in enumerate(f.subfields):
end_punct = ''
if n % 2 != 0 and current_sub(f, n) == 'q':
# Begin qualifying data with a parenthesis.
if n == 1 or prev_sub(f, n) != 'q':
prepend_punct(f, n, '(')
# Separate subfields q with space-
# semicolon.
if (n == last_subdata_index(f)
or next_sub(f, n) != 'q'):
end_punct = ''.join([end_punct, ')'])
elif next_sub(f, n) == 'q':
end_punct = ''.join([end_punct, ' ;'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct)
# Hyphens and spaces are not carried in the record.
elif f.tag in ['030']:
remove_punct(f, subfields = ['a'], punct = [' ', '-'])
# Hyphens are not carried in the MARC record.
elif f.tag in ['032']:
remove_punct(f, subfields = ['a'], punct = ['-'])
# Omit decimal point that usually precedes cutter.
elif f.tag in ['033']:
for n, sub in enumerate(f.subfields):
if n % 2 == 0 and sub == 'c':
del_from_start(f, n+1, del_list = ['.'])
# Field does not end in a mark of punctuation
elif f.tag in ['034']:
del_from_end(f)
# No space occurs between the parenthetical MARC code
# and the first character position of the control
# number.
elif f.tag in ['035']:
for n, sub in enumerate(f.subfields):
if sub == 'a' and n % 2 == 0:
data = f.subfields[n+1].replace(') ', ')')
f.subfields[n+1] = data
# Field ends with a mark of punctuation
elif f.tag in ['036']:
append_punct(f, last_subdata_index(f), '.',
exempt = ['-', ')', '!', '?'])
elif f.tag in ['037', '653', '654', '655', '753']:
# subfields does not end with a mark of punctuation
# unless ending in an abbreviation or data ending
# with a mark of punctuation.
for n, sub in enumerate(f.subfields):
if n % 2 != 0:
del_from_end(f, n, exempt = ['?', '!', '-', ')'],
abbrev_exempt = True)
# Trailing hyphens are carried in the MARC record
elif f.tag in ['043']:
for n, sub in enumerate(f.subfields):
if n % 2 == 0 and sub == 'a':
while len(f.subfields[n+1]) < 7:
f.subfields[n+1] = f.subfields[n+1] + '-'
# For 2-character codes, the trailing blank is omitted.
elif f.tag in ['044']:
for n, sub in enumerate(f.subfields):
if n % 2 == 0 and sub in ['a', 'b', 'c']:
data = f.subfields[n+1]
if len(data) == 3 and data[2] == ' ':
f.subfields[n+1] = data[:2]
# Hyphens are the only marks of punctuation used.
elif f.tag in ['045']:
punct = [x for x in any_punct if x != '-']
remove_punct(f, punct = punct)
# Dates should not be padded with zeros
elif f.tag in ['046']:
for n, sub in enumerate(f.subfields):
if current_sub(f, n) in ['b', 'c', 'd', 'e']:
while sub[0] == '0':
sub = sub[1:]
elif f.tag in ['049']:
for n, sub in enumerate(f.subfields):
# Enclose subfields d & m in brackets.
if current_sub(f, n) in ['d', 'm']:
prepend_punct(f, n, '[')
append_punct(f, n, ']')
# Do not enclose subfield n in brackets.
elif current_sub(f, n) == 'n':
del_from_start(f, n, del_list = ['['])
del_from_end(f, n, del_list = ['['])
# Do not retain enclosing parentheses or brackets in record.
elif f.tag in ['050']:
for n, sub in enumerate(f.subfields):
del_from_start(f, n, del_list = ['[', '('])
del_from_end(f, n, del_list = [']', ')'])
# Field ends in a period.
elif f.tag in ['051', '254', '256']:
append_punct(f, last_subdata_index(f), '.')
elif f.tag in ['052']:
for n, sub in enumerate(f.subfields):
# Cutter number does not get the starting period.
if current_sub(f, n) == 'b':
del_from_start(f, n, del_list = ['.'])
# Field does not end in a period.
del_from_end(f, last_subdata_index(f), del_list = ['.'])
# Field does not end in a period.
elif f.tag in ['055', '070', '086', '850']:
del_from_end(f, last_subdata_index(f), '.', abbrev_exempt = True)
elif f.tag in ['060']:
# Do not retain enclosing brackets in the record.
for n, sub in enumerate(f.subfields):
del_list = [']']
# Do not retain separating slashes in the record.
if next_sub(f, n) == 'a':
del_list.append('/')
del_from_start(f, n, del_list = ['['])
del_from_end(f, n, del_list = del_list)
append_punct(f, last_subdata_index(f), '.')
# Do not retain enclosing brackets in record.
elif f.tag in ['082', '083']:
for n, sub in enumerate(f.subfields):
del_from_start(f, n, del_list = ['['])
del_from_end(f, n, del_list = [']'])
# Slashes should not be entered in subfield a.
elif f.tag in ['092']:
for n, sub in enumerate(f.subfields):
if current_sub(f, n) == 'a':
f.subfields[n].replace('/', '')
elif f.tag in ['100']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = ['"']
# Enclose subfield in parentheses.
if n % 2 != 0 and current_sub(f, n) in ['q']:
prepend_punct(f, n, '(')
end_punct = ''.join([end_punct, ')'])
# Do not add punctuation to open dates.
elif current_sub(f, n) in ['d']:
for i in ['-', '?']: exempt.append(i)
# Subfield c is sometimes enclosed in parantheses, other
# times preceded by a comma. As yet I have not determined
# a method of distinguishing cases.
elif current_sub(f, n) == 'c':
pass
if n < last_subdata_index(f) and n % 2 != 0:
# Precede subfields with comma.
if next_sub(f, n) in ['d', 'e', 'j']:
end_punct = ''.join([end_punct, ','])
# Precede subfields with period.
elif next_sub(f, n) in ['f', 'k', 'l', 'n', 't']:
end_punct = ''.join([end_punct, '.'])
# Precede with period unless following a
# number
elif next_sub(f, n) in ['p']:
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
# Enclose additions to the name of a family
# in a single set of parentheses,
# separating each addition by a space-colon.
elif next_sub(f, n) == 'g':
# If the term 'family' is part of
# qualifying information, precede it
# with a left parenthesis.
if current_sub(f, n) == 'a':
if 'family' in sub:
if '(family' not in sub:
sub.replace('family', '(family')
f.subfields[n] = sub
end_punct = ''.join([end_punct, ' :'])
elif current_sub(f, n) == 'g':
end_punct = ''.join([end_punct, ' :'])
# Do not precede with any punctuation
elif next_sub(f, n) == 'u':
for i in end_punct: exempt.append(i)
del_from_end(f, n, exempt = exempt,
abbrev_exempt = True)
# Do not precede subfield with a period if
# the previous subfield ends in a comma.
if next_sub(f, n) == 'n':
exempt = [',']
# End field with terminal punctuation
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
term_punct = tuple(['.', '-', ')', '!', '?'])
if (not sub.endswith(term_punct)
and not end_punct.endswith(term_punct)):
end_punct = ''.join([end_punct, '.'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['110']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = ['"']
if n % 2 != 0 and current_sub(f, n) in ['c', 'd', 'g']:
# Separate c subfields with space-semicolon
if (current_sub(f, n) == 'c'
and next_sub(f, n) == 'c'):
end_punct = ';'
# Close qualifying subfields off from following
# subfields
elif (n == last_subdata_index(f)
or next_sub(f, n) not in ['c', 'd',
'g', 'n']):
end_punct = ')'
# Separate subfields with space-colon.
else:
end_punct = ' :'
# Close qualifying subfields off from preceding
# subfields
if (n < 3
or prev_sub(f, n) not in ['c', 'd',
'g', 'n']):
start_punct = '('
# Check for use of subfield n as part/section
elif current_sub(f, n) == 'n':
if (prev_sub(f, n) not in ['k', 't']
and next_sub(f, n) != 'p'):
start_punct = '('
if next_sub(f, n) in ['c', 'd', 'g']:
end_punct = ' :'
elif next_sub(f, n) == 'p':
end_punct = ','
elif next_sub(f, n)=='n':
end_punct = '.'
if n < last_subdata_index(f) and n % 2 != 0:
# Precede subfields with period.
if next_sub(f, n) in ['b', 'f', 'k', 'l', 't']:
end_punct = ''.join([end_punct, '.'])
# Precede relator with comma.
elif next_sub(f, n) in ['e']:
end_punct = ''.join([end_punct, ','])
# Precede part/section with period.
elif next_sub(f, n) in ['n']:
if current_sub(f, n) in ['k', 't']:
end_punct = '.'
# Do not precede with punctuation
elif next_sub(f, n) == 'u':
for i in end_punct: exempt.append(i)
del_from_end(f, n, exempt = exempt,
abbrev_exempt = True)
# End field with terminal punctuation
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
term_punct = tuple(['.', '-', ')', '!', '?'])
if (not sub.endswith(term_punct)
and not end_punct.endswith(term_punct)):
end_punct = ''.join([end_punct, '.'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['111']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
if current_sub(f, n) in ['c', 'd', 'g']:
# Separate c subfields with space-semicolon
if (current_sub(f, n) == 'c'
and next_sub(f, n) == 'c'):
end_punct = ';'
# Close qualifying subfields off from following
# subfields
elif next_sub(f, n) not in ['c', 'd', 'g', 'n']:
end_punct = ')'
# Separate subfields with space-colon.
else:
end_punct = ' :'
# Close qualifying subfields off from preceding
# subfields
if prev_sub(f, n) not in ['c', 'd', 'g', 'n']:
start_punct = '('
# Do not add punctuation to open dates.
if current_sub(f, n) == 'd':
for i in ['-', '?']: exempt.append(i)
# Check for use of subfield n as part/section
elif current_sub(f, n) == 'n':
if (prev_sub(f, n) not in ['k', 't']
and next_sub(f,n) != 'p'):
start_punct = '('
if next_sub(f, n) in ['c', 'd', 'g']:
end_punct = ' :'
elif next_sub(f, n) == 'p':
end_punct = ','
elif next_sub(f, n)== 'n':
end_punct = '.'
# Precede subfields with period.
if next_sub(f, n) in ['e', 'f', 'k', 'l', 'q', 't']:
end_punct = ''.join([end_punct, '.'])
elif next_sub(f, n) in ['j']:
end_punct = ''.join([end_punct, ','])
elif next_sub(f, n) == 'n' and current_sub(f, n) in ['k', 't']:
end_punct = ''.join([end_punct, '.'])
# Do not precede with punctuation
elif next_sub(f, n) == 'u':
for i in end_punct: exempt.append(i)
del_from_end(f, n, exempt = exempt,
abbrev_exempt = True)
# End field with terminal punctuation
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
term_punct = tuple(['.', '-', ')', '!', '?'])
if (not sub.endswith(term_punct)
and not end_punct.endswith(term_punct)):
end_punct = ''.join([end_punct, '.'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['130']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = ['"']
# Enclose treaty dates in parentheses.
if current_sub(f, n) == 'd':
prepend_punct(f, n, '(')
end_punct = ')'
# Precede subfields with period.
if next_sub(f, n) in ['f', 'g', 'h', 'k', 'l', 't']:
end_punct = ''.join([end_punct, '.'])
# Precede subfields with comma.
elif next_sub(f, n) in ['m', 'r']:
end_punct = ''.join([end_punct, ','])
# Choose punctuation to precede subfield n
# using context to guess how it is being used.
elif next_sub(f, n) == ['n']:
exempt.append(',')
exempt.append('.')
if f.subfields[n+1][0] != '(':
if current_sub(f, n) in ['a', 'd', 'k', 'n']:
end_punct = ''.join([end_punct, '.'])
elif current_sub(f, n) in ['m', 'p', 't']:
end_punct = ''.join([end_punct, ','])
elif next_sub(f, n) == 'o':
end_punct = ''.join([end_punct, ';'])
# Preceding punctuation of subfield p is based
# the subfield before it.
elif next_sub(f, n) == 'p':
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
elif next_sub(f, n) == 's':
if f.subfields[n+1][0] != '(':
end_punct = ''.join([end_punct, '.'])
# End field with terminal punctuation
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
term_punct = tuple(['.', '-', ')', '!', '?'])
if (not sub.endswith(term_punct)
and not end_punct.endswith(term_punct)):
end_punct = ''.join([end_punct, '.'])
# Add any necessary ending punctuation.
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['210', '222']:
for n, sub in enumerate(f.subfields):
# Enclose subfield b in parentheses.
if current_sub(f, n) == 'b':
prepend_punct(f, n, '(')
append_punct(f, n, ')')
# Omit preceding and terminal punctuation from subfields.
if n % 2 != 0:
del_from_end(f, n,
exempt = ['...', '!', '-', '?', ']', '>', ')'],
abbrev_exempt = True)
elif f.tag in ['240', '243']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
# Brackets that customarily enclose a uniform title are
# not carried in the record.
del_from_start(f, n, del_list = ['['])
del_from_end(f, n, del_list = [']'])
# Precede subfields with a comma.
if next_sub(f, n) in ['d', 'm', 'r']:
end_punct = ''.join([end_punct, ','])
# Precede subfields with a period.
elif next_sub(f, n) in ['f', 'k', 'l', 'p', 's']:
end_punct = ''.join([end_punct, '.'])
# Precede subfields with a semicolon.
elif next_sub(f, n) == 'o':
end_punct = ''.join([end_punct, ';'])
elif next_sub(f, n) == 'n':
for i in [',', '.']: exempt.append(i)
# Subfield n is not preceded by punctuation if it is
# enclosed in parenthese.
if f.subfields[n+2][0] != '(':
if current_sub(f, n) in ['m']:
end_punct = ''.join([end_punct, ','])
elif current_sub(f, n) in ['n']:
end_punct = ''.join([end_punct, '.'])
elif next_sub(f, n) == 's':
if f.subfields[n+2][0] != '(':
end_punct = ''.join([end_punct, '.'])
# Field does not end in a mark of punctuation.
if n == last_subdata_index(f):
exempt.append(end_punct)
del_from_end(f, n, exempt = exempt, abbrev_exempt = True)
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['242']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = ['!', '-', '?']
if current_sub(f, n) == 'h':
prepend_punct(f, n, '[')
end_punct = ']'
# Omit punctuation in subfield y.
elif current_sub(f, n) == 'y':
f.subfields[n] = [x for x in sub if x not in any_punct]
# If preceding punctuation has been entered at the
# beginning of the subfield b data, take that as
# conclusive evidence of the nature of the data. Otherwise,
# assume the punctuation should be a space-colon.
if next_sub(f, n) == 'b':
next_data = f.subfields[n+2]
for i in ['=', ';', ':']:
if next_data[0] == i:
f.subfields[n+2] = next_data[1:]
end_punct = ''.join([end_punct, i])
if not end_punct:
end_punct = ''.join([end_punct, ' :'])
# Precede with ' /'.
elif next_sub(f, n) == 'c':
end_punct = ''.join([end_punct, ' /'])
elif next_sub(f, n) in ['n']:
end_punct = ''.join([end_punct, '.'])
# Precede subfield p with a period unless it follows
# subfield p.
elif next_sub(f, n) == 'p':
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
# Last subfield preceding subfield y ends with a period.
if n == last_subdata_index(f):
if current_sub(f, n) == 'f':
index = n - 2
else:
index = n
exempt.append(end_punct)
append_punct(f, index, exempt = exempt, abbrev_exempt = True)
elif f.tag in ['245']:
varying_title_fields = r.get_fields('246')
added_entries = r.get_fields('700', '710', '711')
uniform_titles = r.get_fields('730')
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
if current_sub(f, n) == 'g':
prepend_punct(f, n, '(')
end_punct = ')'
elif current_sub(f, n) == 'h':
prepend_punct(f, n, '[')
end_punct = ']'
if next_sub(f, n) == 'b':
next_data = f.subfields[n+2]
# If preceding punctuation has been entered at the
# beginning of the subfield b data, take that as
# conclusive evidence of the nature of the data.
for i in ['=', ';', ':']:
if next_data[0] == i:
f.subfields[n+2] = next_data[1:]
end_punct = ''.join([end_punct, i])
# Otherwise, look to other title fields for evidence.
if not end_punct:
# Search for each 246 parallel title in the 245.
for vtf in varying_title_fields:
if vtf.indicator2 == '1':
parallel = vtf['a'].lower()
# Remove any ending punctuation in search
# string.
while parallel[-1] in any_punct:
parallel = parallel[:-1]
# If the parallel title begins the 245b,
# precede the subfield with ' ='.
if next_data.lower().find(parallel) == 0:
end_punct = ''.join([end_punct, ' ='])
if not end_punct:
# Search for added entry titles in the 245.
for entry in added_entries:
same_author = ''
diff_author = ''
if entry.indicator2 == '2' and entry['t']:
# Get the corresponding type of main entry.
main_entry = str(int(entry.tag)-600)
if (r[main_entry]
and r[main_entry]['a'] == entry['a']):
same_author = entry['t'].lower()
else:
diff_author = entry['t'].lower()
if same_author:
while same_author[-1] in any_punct:
same_author = same_author[:1]
if next_data.lower().find(same_author) == 0:
end_punct = ''.join([end_punct, ' ;'])
if diff_author:
while diff_author[-1] in any_punct:
diff_author = diff_author[:1]
if next_data.lower().find(diff_author) == 0:
end_punct = ''.join([end_punct, '.'])
if not end_punct:
# Search for uniform titles in the 245.
for ut in uniform_titles:
if ut['a']:
ut = ut['a'].lower()
while ut[-1] in any_punct:
ut = ut[:-1]
if next_data.lower().find(ut) == 0:
end_punct = ''.join([end_punct, '.'])
if not end_punct:
end_punct = ''.join([end_punct, ' :'])
# Precede statement of responsibility with ' /'.
elif next_sub(f, n) == 'c':
end_punct = ''.join([end_punct, ' /'])
# Precede subfields with a comma.
elif next_sub(f, n) == 'f':
end_punct = ''.join([end_punct, ','])
# Precede subfield k with ' :' if there is a subfield a.
elif next_sub(f, n) == 'k' and f['a']:
end_punct = ''.join([end_punct, ' :'])
# Precede subfield with '.' unless the
# preceding subfield ends with an '!', '-', or '?'.
elif next_sub(f, n) in ['n', 's']:
end_punct = ''.join([end_punct, '.'])
for i in ['!', '-', '?']:
exempt.append(i)
# Precede subfield p with a period unless it follows
# subfield p.
elif next_sub(f, n) == 'p':
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
# End field with terminal punctuation.
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
term_punct = tuple(['!', '?'])
if (not sub.endswith(term_punct)
and not end_punct.endswith(term_punct)):
end_punct = ''.join([end_punct, '.'])
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['246']:
varying_title_fields = r.get_fields('246')
# Field does not end with final punctuation.
del_from_end(f, abbrev_exempt = True)
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
# Do not enter final punctuation at the beginning or the
# end of subfield f, except '-' or '<'/'>'.
if current_sub(f, n) == 'f':
del_from_start(f, n, exempt = ['-', '<'])
del_from_end(f, n, exempt = ['-', '>'])
# Enclose in parentheses.
elif current_sub(f, n) == 'g':
prepend_punct(f, n, '(')
end_punct = ')'
# Enclose in brackets.
elif current_sub(f, n) == 'h':
prepend_punct(f, n, '[')
end_punct = ']'
# Subfield i is taken care of before the tag is checked.
if next_sub(f, n) == 'b':
next_data = f.subfields[n+2]
# If preceding punctuation has been entered at the
# beginning of the subfield b data, take that as
# conclusive evidence of the nature of the data.
for i in ['=', ';', ':']:
if next_data[0] == i:
f.subfields[n+2] = next_data[1:]
end_punct = ''.join([end_punct, i])
# Otherwise, look to other title fields for evidence.
if not end_punct:
# Search for each 246 parallel title in the 245.
for vtf in varying_title_fields:
if vtf.indicator2 == '1':
parallel = vtf['a'].lower()
# Remove any ending punctuation in search
# string.
while parallel[-1] in any_punct:
parallel = parallel[:-1]
# If the parallel title begins the 245b,
# precede the subfield with ' ='.
if next_data.lower().find(parallel) == 0:
end_punct = ''.join([end_punct, ' ='])
if not end_punct:
# Search for added entry titles in the 245.
for entry in added_entries:
same_author = ''
diff_author = ''
if entry.indicator2 == '2' and entry['t']:
# Get the corresponding type of main entry.
main_entry = str(int(entry.tag)-600)
if (r[main_entry]
and r[main_entry]['a'] == entry['a']):
same_author = entry['t'].lower()
else:
diff_author = entry['t'].lower()
if same_author:
while same_author[-1] in any_punct:
same_author = same_author[:1]
if next_data.lower().find(same_author) == 0:
end_punct = ''.join([end_punct, ' ;'])
if diff_author:
while diff_author[-1] in any_punct:
diff_author = diff_author[:1]
if next_data.lower().find(diff_author) == 0:
end_punct = ''.join([end_punct, '.'])
if not end_punct:
# Search for uniform titles in the 245.
for ut in uniform_titles:
if ut['a']:
ut = ut['a'].lower()
while ut[-1] in any_punct:
ut = ut[:-1]
if next_data.lower().find(ut) == 0:
end_punct = ''.join([end_punct, '.'])
if not end_punct:
end_punct = ''.join([end_punct, ' :'])
# Do not enter punctuation in the subfield preceding
# subfield f unless the preceding subfield ends with an
# abbreviation or with parentheses.
elif next_sub(f, n) == 'f':
del_from_end(f, n, exempt = [')'], abbrev_exempt = True)
# Precede subfield n with a period.
elif next_sub(f, n) == 'n':
end_punct = ''.join([end_punct, '.'])
# Precede subfield p with '.' unless it follows subfield n.
elif next_sub(f, n) == 'p':
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
# Field does not end in a mark of punctuation.
if n == last_subdata_index(f):
exempt.append(end_punct)
del_from_end(f, n, exempt = exempt, abbrev_exempt = True)
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['247']:
end_punct = ''
exempt = ['']
for n, sub in enumerate(f.subfields):
# Incomplete dates should be contained in angle brackets.
if current_sub(f, n) == 'f':
if f.subfields[n][0] == '-':
prepend_punct(f, n, '<')
exempt.append('->')
# Enclose subfield in brackets.
elif current_sub(f, n) == 'h':
prepend_punct(f, n, '[')
end_punct = ']'
# Enclose subfield g in parentheses.
elif current_sub(f, n) == 'g':
prepend_punct(f, n, '(')
end_punct = ''.join([end_punct, ')'])
# If preceding punctuation has been entered at the
# beginning of the subfield b data, take that as
# conclusive evidence of the nature of the data. Otherwise,
# assume the punctuation should be a space-colon.
if next_sub(f, n) == 'b':
next_data = f.subfields[n+2]
for i in ['=', ';', ':']:
if next_data[0] == i:
f.subfields[n+2] = next_data[1:]
end_punct = ''.join([end_punct, i])
if not end_punct:
end_punct = ''.join([end_punct, ' :'])
# Precede subfield with period.
elif next_sub(f, n) == 'n':
end_punct = ''.join([end_punct, '.'])
# Precede subfield p with a period unless it follows
# subfield p.
elif next_sub(f, n) == 'p':
if current_sub(f, n) == 'n':
end_punct = ''.join([end_punct, ','])
else:
end_punct = ''.join([end_punct, '.'])
# Field does not end in a mark of punctuation.
if n == last_subdata_index(f):
exempt.append(end_punct)
del_from_end(f, n, exempt = exempt, abbrev_exempt = True)
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['250']:
end_punct = ''
for n, sub in enumerate(f.subfields):
# There is not much to go on to determine if subfield b is
# being used for a parallel edition statement or statement
# of responsibility. Assume it is a statement of
# responsibility unless there is a '=' beginning or
# preceding it.
if next_sub(f, n) == 'b':
if f.subfields[n+1][0] == '=':
del_from_start(f, n+1, '=')
append_punct(f, n, ' =')
else:
append_punct(f, n, ' /', exempt = ['='])
# Field ends with a period.
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
append_punct(f, n, '.')
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
elif f.tag in ['251', '270', '340', '341', '342', '355', '357', '363', '365',
'366', '377', '380', '381', '382', '384', '385', '386', '388',
'938', '956', '987']:
# Omit preceding from subfields, with the common OCLC end
# punctuation exceptions.
for n, sub in enumerate(f.subfields):
if n % 2 != 0 and n != last_subdata_index(n):
del_from_end(f, n,
exempt = ['...', '!', '-', '?', ']', '>', ')'],
abbrev_exempt = True)
# Omit terminal punctuation except ending abbreviations.
del_from_end(f, abbrev_exempt = True)
elif f.tag in ['255']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
# subfields c, d, and e are enclosed in a single set of
# parentheses.
if current_sub(f, n) in ['c', 'd', 'e']:
if prev_sub(f, n) not in ['c', 'd', 'e']:
prepend_punct(f, n, '(')
if next_sub(f, n) in ['c', 'd', 'e']:
end_punct = ' ;'
else:
end_punct = ')'
# Projection statement is preceded by ' ;'.
if next_sub(f, n) == 'b':
end_punct = ''.join([end_punct, ' ;'])
# Field ends with a period.
if n == last_subdata_index(f):
del_from_end(f, del_list = ',')
end_punct = ''.join([end_punct, '.'])
if end_punct:
append_punct(f, n, end_punct, exempt = exempt)
# Omit punctuation for the end of any subfield, except the last
# which ends in a period.
elif f.tag in ['257']:
exempt = ['...', '!', '-', '?', ']', '>', ')']
for n, sub in enumerate(f.subfields):
if n % 2 != 0 and n != last_subdata_index(f):
del_from_end(f, n, exempt = exempt, abbrev_exempt = True)
elif n == last_subdata_index(f):
append_punct(f, n, '.', exempt = exempt)
elif f.tag in ['258']:
for n, sub in enumerate(f.subfields):
# Precede subfield by a ' :'.
if next_sub(f, n) == 'b':
append_punct(f, n, ' :')
# Add a terminal period.
if n == last_subdata_index(f):
append_punct(f, n, '.',
exempt = ['...', '!', '-', '?', ']', '>', ')'])
elif f.tag in ['260']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
if sub[0] == '[' and sub[-1] != ']':
end_punct = ']'
elif sub[0] != '[' and sub[-1] == ']':
prepend_punct(f, n, '[')
# Enclose subfields e, f, and g within 1 set of parentheses.
if current_sub(f, n) == ['e', 'f', 'g']:
if prev_sub(f, n) not in ['e', 'f', 'g']:
append_punct(f, n, '(')
if next_sub(f, n) in ['e', 'f', 'g']:
end_punct = ''.join([end_punct, ' :'])
else:
end_punct = ')'
# Precede subfield 3 with ':', or ' :' if following an open
# date.
if n == 1 and current_sub(f, n) == '3':
if f.subfields[n].endswith(tuple(['-', '- ', '->'])):
end_punct = ''.join([end_punct, ' :'])
else:
end_punct = ''.join([end_punct, ':'])
# Otherwise, precede any subfield a with ' ;'.
elif next_sub(f, n) == 'a':
end_punct = ''.join([end_punct, ' ;'])
# Precede subfield b with a ' :'.
elif next_sub(f, n) == 'b':
end_punct = ''.join([end_punct, ' :'])
# Precede subfield c with a comma.
elif next_sub(f, n) == 'c':
end_punct = ''.join([end_punct, ','])
# Field ends with a period.
if n == last_subdata_index(f):
end_punct = ''.join([end_punct, '.'])
exempt = ['...', '!', '-', '?', ']', '>', ')']
if end_punct:
if end_punct[-1] != ',':
del_from_end(f, n, del_list = [','])
append_punct(f, n, end_punct, exempt = exempt)
# All punctuation for this field is taken from LC's examples,
# although subfield guidelines and most subfields are shown with
# contradictory preceding punctuation.
elif f.tag in ['261']:
for n, sub in enumerate(f.subfields):
end_punct = ''
exempt = []
if next_sub(f, n) == 'a':
end_punct = ''.join([end_punct, ';'])
exempt.append(',')
elif next_sub(f, n) == 'b' or n == last_subdata_index(f):
end_punct = ''.join([end_punct, '.'])
exempt.append(',')
elif next_sub(f, n) == 'd':
end_punct = ''.join([end_punct, ','])
elif next_sub(f, n) == 'd':
end_punct = ''.join([end_punct, ','])
exempt.append('.')
if end_punct: