File: Tools.pm

package info (click to toggle)
libebook-tools-perl 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,236 kB
  • sloc: perl: 15,242; xml: 62; makefile: 3
file content (12279 lines) | stat: -rw-r--r-- 431,332 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
package EBook::Tools;
use warnings; use strict; use utf8;
use v5.10.1; # Needed for smart-match operator and given/when
use English qw( -no_match_vars );
use version 0.74; our $VERSION = qv("0.5.4");

#use warnings::unused;

# Perl Critic overrides:
## no critic (Package variable)
# RequireBriefOpen seems to be way too brief to be useful
## no critic (RequireBriefOpen)
# Double-sigils are needed for lexical filehandles in clear print statements
## no critic (Double-sigil dereference)
our $debug = 0;



=head1 NAME

EBook::Tools - Object class for manipulating and generating E-books


=head1 DESCRIPTION

This module provides an object interface and a number of related
procedures intended to create or modify documents centered around the
International Digital Publishing Forum (IDPF) standards, currently
both OEBPS v1.2 and OPS/OPF v2.0.

=cut

=head1 SYNOPSIS

 use EBook::Tools qw(split_metadata system_tidy_xml);
 $EBook::Tools::tidysafety = 2;

 my $opffile = split_metadata('ebook.html');
 my $otheropffile = 'alternate.opf';
 my $retval = system_tidy_xml($opffile,'tidy-backup.xml');
 my $ebook = EBook::Tools->new($opffile);
 $ebook->fix_opf20;
 $ebook->fix_misc;
 $ebook->print;
 $ebook->save;

 $ebook->init($otheropffile);
 $ebook->fix_oeb12;
 $ebook->gen_epub;


=head1 DEPENDENCIES

=head2 Perl Modules

=over

=item Archive::Zip

=item Data::UUID (or OSSP::UUID)

=item Date::Manip

Note that Date::Manip will die on MS Windows system unless the
TZ environment variable is set in a specific manner. See:

http://search.cpan.org/perldoc?Date::Manip#TIME_ZONES

=item File::MimeInfo

=item HTML::Parser

=item Lingua::EN::NameParse

=item Tie::IxHash

=item Time::Local

=item URI::Escape

=item XML::Twig

=back

=head2 Other Programs

=over

=item Tidy

The command "tidy" needs to be available, and ideally on the path.  If
it isn't on the path, package variable L</$tidycmd> can be set to its
absolute path.  If tidy cannot be found, L</system_tidy_xml()> and
L</system_tidy_xhtml()> will be nonfunctional.

=back

=cut


require Exporter;
use base qw(Exporter);

our @EXPORT_OK;
@EXPORT_OK = qw (
    &capitalize
    &clean_filename
    &create_epub_container
    &create_epub_mimetype
    &debug
    &excerpt_line
    &fix_datestring
    &find_in_path
    &find_links
    &find_opffile
    &hashvalue_key_self
    &hexstring
    &get_container_rootfile
    &print_memory
    &split_metadata
    &split_pre
    &strip_script
    &system_result
    &system_tidy_xml
    &system_tidy_xhtml
    &trim
    &twigelt_create_uuid
    &twigelt_fix_oeb12_atts
    &twigelt_fix_opf20_atts
    &twigelt_is_author
    &usedir
    &userconfigdir
    &ymd_validate
    );
our %EXPORT_TAGS = ('all' => [@EXPORT_OK]);

# OSSP::UUID will provide Data::UUID on systems such as Debian that do
# not distribute the original Data::UUID.
use Data::UUID;

use Archive::Zip qw( :CONSTANTS :ERROR_CODES );
use Carp;
use Cwd qw(getcwd realpath);
use Date::Manip;
require EBook::Tools::BISG;
use Encode;
require Encode::Detect;
use File::Basename qw(basename dirname fileparse);
# File::MimeInfo::Magic gets *.css right, but detects all html as
# text/html, even if it has an XML header.
use File::MimeInfo::Magic;
# File::MMagic gets text/xml right (though it still doesn't properly
# detect XHTML), but detects CSS as x-system, and has a number of
# other weird bugs.
#use File::MMagic;
use File::Path;     # Exports 'mkpath' and 'rmtree'
use File::Temp;
use HTML::Entities qw(decode_entities _decode_entities %entity2char);
#use HTML::Tidy;
use Lingua::EN::NameParse qw(case_surname);
use Tie::IxHash;
use Time::Local;
use URI::Escape;
use XML::Twig;


=head1 CONFIGURABLE PACKAGE VARIABLES

=over

=item C<%bisacsubjects>

A mapping of lowercase BISAC codes and text descriptions to standard
capitalized text descriptions.  As BISG claims copyright on this and
does not allow the lists to be redistributed, this list must be
downloaded and cached locally via C<ebook dlbisac> before it is
available.

Running the unit tests will cause this to happen automatically.

=item C<%bisactolc>

An extremely incomplete mapping of lowercase BISAC codes and text
descriptions to Library of Congress standard subjects.

=item C<%booktypes>

A hash mapping all-lowercase terms to a standard vocabulary to be used
in <dc:type> elements.

=item C<%dcelements12>

A tied IxHash mapping an all-lowercase list of Dublin Core metadata
element names to the capitalization dictated by the OEB 1.2
specification, used by the fix_oeb12() and fix_oeb12_dcmeta() methods.
Changing the tags in this list will change the tags recognized and
placed inside the <dc-metadata> element.

Order is preserved and significant -- fix_oeb12 will output DC
metadata elements in the same order as in this hash, though order for
tags of the same name is preserved from the input file.

=item C<%dcelements20>

A tied IxHash mapping an all-lowercase list of Dublin Core metadata
element names to the all-lowercase form dictated by the OPF 2.0
specification (which means it maps the all-lowercase tags to
themselves).  It is used by the fix_opf20() and fix_opf20_dcmeta()
methods.  Changing the tags in this list will change the tags
recognized and placed directly under the <metadata> element.

Order is preserved and significant -- fix_opf20 will output DC
metadata elements in the same order as in this hash, though order for
tags of the same name is preserved from the input file.

=item C<%lcsubjects>

An extremely incomplete mapping of lowercase terms to Library of
Congress subject classifications.  This is used for automatic
normalization of subject elements.  Every value in the hash has a
lowercase representation of itself as a key in addition to any
aliases.

This MUST NOT contain mappings from BISAC codes or descriptors to
Library of Congress subjects.  Use %bisactolc for that.

=item C<%nonxmlentity2char>

This is the %entity2char conversion map from HTML::Entities with the 5
pre-defined XML entities (amp, gt, lt, quot, apos) removed.  This is
used during by L</init()> to sanitize the OPF file data before
parsing.  This hash can be modified to allow and convert other
non-standard entities to unicode characters.  See HTML::Entities for
details.

=item C<%publishermap>

A hash mapping known variants of publisher names to a canonical form,
used by L</fix_publisher()>, and thus also indirectly by
L</fix_misc()>.

Keys should be entered in lowercase.  The hash can also be set empty
to prevent fix_publisher() from taking any action at all.

=item C<%referencetypes>

A hash mapping valid OPF 2.0 reference types to themselves, along with
common variants to standard types.

=item C<%relatorcodes>

A hash mapping the MARC Relator Codes (see:
http://www.loc.gov/marc/relators/relacode.html) to their descriptive
names.

=item C<%sexcodes>

A hash normalizing subject tags for erotic fiction, as used by
StoriesOnline, ASSTR, and Literotica, among others.

Unlike the other mappings, this one is *not* lowercased, and the keys
are case-sensitive.  The hash maps only to the canonical base code,
but subject normalization will also add a prefix, defaulting to the
BISAC format of 'FICTION / Erotica / '.

=item C<%strangenames>

=item C<%strangefileas>

Hashes mapping mapping known incorrect outputs of name normalization
to correct format.  The first handles the main name display, the
second the file-as output.

=item C<$tidycmd>

The tidy executable name.  This has to be a fully qualified pathname
if tidy isn't on the path.  Defaults to 'tidy'.

=item C<$tidyxhtmlerrors>

The name of the error output file from system_tidy_xhtml().  Defaults
to 'tidyxhtml-errors.txt'

=item C<$tidyxmlerrors>

The name of the error output file from system_tidy_xml().  Defaults to
'tidyxml-errors.txt'

=item C<$tidysafety>

The safety level to use when running tidy (default is 1).  Potential
values are:

=over

=item C<$tidysafety < 1>:

No checks performed, no error files kept, works like a clean tidy -m

This setting is DANGEROUS!

=item C<$tidysafety == 1>:

Overwrites original file if there were no errors, but even if there
were warnings.  Keeps a log of errors, but not warnings.

=item C<$tidysafety == 2>:

Overwrites original file if there were no errors, but even if there
were warnings.  Keeps a log of both errors and warnings.

=item C<$tidysafety == 3>:

Overwrites original file only if there were no errors or warnings.
Keeps a log of both errors and warnings.

=item C<$tidysafety >= 4>:

Never overwrites original file.  Keeps a log of both errors and
warnings.

=back

=item C<%validspecs>

A hash mapping valid specification strings to themselves, primarily
used to undefine unrecognized values.  Default valid values are 'OEB12'
and 'OPF20'.

=back

=cut

our $mobi2htmlcmd = 'mobi2html';
our $tidycmd = 'tidy'; # Specify full pathname if not on path
our $tidyxhtmlerrors = 'tidyxhtml-errors.txt';
our $tidyxmlerrors = 'tidyxml-errors.txt';
our $tidysafety = 1;


our $utf8xmldec = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
our $oeb12doctype =
    '<!DOCTYPE package' . "\n" .
    '  PUBLIC "+//ISBN 0-9673008-1-9//DTD OEB 1.2 Package//EN"' . "\n" .
    '  "http://openebook.org/dtds/oeb-1.2/oebpkg12.dtd">' . "\n";
our $opf20package =
    '<package version="2.0" xmlns="http://www.idpf.org/2007/opf">' . "\n";

my $bisg = EBook::Tools::BISG->new();
our %bisacsubjects = $bisg->bisac();

our %bisactolc = (
    # The entire ANTXXX line is ambiguous -- it could map to either
    # subdivisions of Antiques or Collectibles.  Work started here
    # before this was realized is being left intact for now, but the
    # list is incomplete for this reason.
    'ant000000'				=> 'Antiques',
    'ant001000'				=> 'Antiques--Americana',
    'ant002000'				=> 'Antiques--Art',
    'ant003000'				=> 'Autographs',
    'ant004000'				=> 'Antiques--Baskets',
    'ant005000'				=> 'Antiques--Books',
    'ant006000'				=> 'Antiques--Bottles',
    'ant010000'				=> 'Antiques--Clocks and watches',
    'ant012000'				=> 'Antiques--Cartoons',
    'ant015000'				=> 'Antiques--Dolls',
    'ant016000'				=> 'Antiques--Firearms',
    'ant017000'				=> 'Antiques--Furniture',
    'ant018000'				=> 'Antiques--Glassware',
    'ant021000'				=> 'Antiques--Jewelry',
    'ant022000'				=> 'Antiques--Kitchen utensils',
    'ant023000'				=> 'Antiques--Periodicals',
    'ant026000'				=> 'Antiques--Musical instruments',
    'ant031000'				=> 'Political collectibles',
    'ant052000'				=> 'Collectibles--Popular culture',
    'ant053000'				=> 'Antiques--Figurines',
    'arc000000'				=> 'Architecture',
    'arc005000'				=> 'Architecture--History',
    'arc007000'				=> 'Interior decoration',
    'art000000'				=> 'Art',
    'art002000'				=> 'Airbrush art',
    'art003000'				=> 'Calligraphy--Technique',
    'art004000'				=> 'Cartooning',
    'art006000'				=> 'Art collections',
    'art008000'				=> 'Conceptual art',
    'art009000'				=> 'Art criticism',
    'art010000'				=> 'Drawing',
    'art013000'				=> 'Folk art',
    'art015010'				=> 'Art, African',
    'art015020'				=> 'Art, American',
    'art015030'				=> 'Art, European',
    'art015040'				=> 'Art, Canadian',
    'art016000'				=> 'Artists',
    'art017000'				=> 'Mixed media (Art)',
    'art019000'				=> 'Art, Asian',
    'art020000'				=> 'Painting--Technique',
    'art021000'				=> 'Pastel drawing--Technique',
    'art023000'				=> 'Popular culture',
    'art024000'				=> 'Prints--Technique',
    'art026000'				=> 'Sculpture',
    'art027000'				=> 'Arts--Study and teaching',
    'art028000'				=> 'Art--Technique',
    'art029000'				=> 'Watercolor painting--Technique',
    'art033000'				=> 'Pen drawing--Technique',
    'art034000'				=> 'Pencil drawing--Technique',
    'art035000'				=> 'Religious art',
    'art037000'				=> 'Politics in art',
    'art038000'				=> 'African American art',
    'art039000'				=> 'Asian American art',
    'art040000'				=> 'Hispanic American art',
    'art042000'				=> 'Art, Australian',
    'art043000'				=> 'Art and business',
    'art044000'				=> 'Art, Latin American',
    'art045000'				=> 'Pottery',
    'art046000'				=> 'Digital art',
    'art047000'				=> 'Art--Middle East',
    'art049000'				=> 'Art, Russian',
    'art050000'				=> 'Art--Themes, motives',
    'art050010'				=> 'Human beings in art',
    'art050020'				=> 'Landscapes in art',
    # 'ART050030' could translate to either 'Animals in art' or 'Plants in art'
    'art050040'				=> 'Portraits',
    'art050050'				=> 'Erotic art',
    'art051000'				=> 'Color in art',
    'art052000'				=> 'Human figure in art',
    'art053000'				=> 'Sculpture--Technique',
    'art055000'				=> 'Body art',
    'art056000'				=> 'Art objects--Conservation and restoration',
    'art057000'				=> 'Video art',
    'art058000'				=> 'Street art',
    'art060000'				=> 'Performance art',
    'bib000000'				=> 'Bible',
    'bio000000'				=> 'Biography',
    'bio001000'				=> 'Artists--Biography',
    'bio008000'				=> 'Military biography',
    'bio009000'				=> 'Philosophers--Biography',
    'bio011000'				=> 'Heads of state--Biography',
    'bio022000'				=> 'Women--Biography',
    'bio023000'				=> 'Explorers--Biography',
    'bio024000'				=> 'Criminals--Biography',
    'bio030000'				=> 'Environmentalists--Biography',
    'bus000000'				=> 'Economics',
    'bus001000'				=> 'Accounting',
    'bus001030'				=> 'International business enterprises--Accounting',
    'bus001050'				=> 'Accounting--Standards',
    'bus002000'				=> 'Advertising',
    'bus003000'				=> 'Auditing',
    'bus004000'				=> 'Banks and banking',
    'bus005000'				=> 'Bookkeeping',
    'bus006000'				=> 'Budget',
    'bus007000'				=> 'Business communication',
    'bus007010'				=> 'Business meetings',
    'bus008000'				=> 'Business ethics',
    'bus009000'				=> 'Business etiquette',
    'bus010000'				=> 'Commercial law',
    'bus011000'				=> 'Business writing',
    'bus012000'				=> 'Occupations',
    'bus012010'				=> 'Internship programs',
    'bus013000'				=> 'Commercial policy',
    'bus014000'				=> 'Commodity exchanges',
    'bus015000'				=> 'Consolidation and merger of corporations',
    'bus016000'				=> 'Consumer behavior',
    'bus017000'				=> 'Corporations--Finance',
    'bus018000'				=> 'Customer relations',
    'bus019000'				=> 'Decision making',
    'bus020000'				=> 'Business--Development',
    'bus021000'				=> 'Econometrics',
    'bus022000'				=> 'Economic conditions',
    'bus023000'				=> 'Economic history',
    'bus024000'				=> 'Business education',
    'bus025000'				=> 'Entrepreneurship',
    'bus026000'				=> 'International trade',
    'bus027000'				=> 'Finance',
    'bus028000'				=> 'Foreign exchange',
    'bus029000'				=> 'Free enterprise',
    'bus030000'				=> 'Personnel management',
    'bus031000'				=> 'Inflation (Finance)',
    'bus032000'				=> 'Infrastructure (Economics)',
    'bus033000'				=> 'Insurance',
    'bus033010'				=> 'Automobile insurance',
    'bus033020'				=> 'Casualty insurance',
    'bus033040'				=> 'Health insurance',
    'bus033050'				=> 'Liability insurance',
    'bus033060'				=> 'Life insurance',
    'bus033070'				=> 'Risk (Insurance)',
    'bus033080'				=> 'Property insurance',
    'bus034000'				=> 'Interest',
    'bus035000'				=> 'International business enterprises',
    'bus036000'				=> 'Investments',
    'bus036010'				=> 'Bonds',
    'bus036020'				=> 'Futures',
    'bus036030'				=> 'Mutual funds',
    'bus036040'				=> 'Options (Finance)',
    'bus036050'				=> 'Real estate investment',
    'bus036060'				=> 'Stocks',
    'bus037000'				=> 'Job hunting',
    'bus037020'				=> 'Job hunting',
    'bus038000'				=> 'Labor',
    'bus039000'				=> 'Macroeconomics',
    'bus040000'				=> 'Mail-order business',
    'bus041000'				=> 'Management',
    'bus042000'				=> 'Management science',
    'bus043000'				=> 'Marketing',
    'bus043010'				=> 'Direct marketing',
    'bus043020'				=> 'Industrial marketing',
    'bus043030'				=> 'Export marketing',
    'bus043040'				=> 'Multilevel marketing',
    'bus043050'				=> 'Telemarketing',
    'bus043060'				=> 'Marketing research',
    'bus044000'				=> 'Microeconomics',
    'bus045000'				=> 'Monetary policy',
    'bus046000'				=> 'Achievement motivation',
    'bus047000'				=> 'Negotiation',
    'bus048000'				=> 'New business enterprises',
    'bus049000'				=> 'Operations research',
    'bus050000'				=> 'Finance, Personal',
    'bus050010'				=> 'Budgets, Personal',
    'bus050020'				=> 'Investments',
    'bus050040'				=> 'Retirement--Planning',
    'bus050050'				=> 'Income tax',
    'bus051000'				=> 'Finance, Public',
    'bus052000'				=> 'Public relations',
    'bus053000'				=> 'Quality control',
    'bus054000'				=> 'Real property',
    # 'bus054010' could be either House Buying or House Selling
    'bus054020'				=> 'Commercial real estate',
    'bus054030'				=> 'Mortgages',
    'bus055000'				=> 'Business--Reference books',
    'bus056030'				=> 'Résumés (Employment)',
    'bus057000'				=> 'Retail trade',
    # 'bus058000' could be either Sales or Selling
    'bus058010'				=> 'Sales management',
    'bus059000'				=> 'Business--Training',
    'bus060000'				=> 'Small business',
    # 'bus061000' could be either 'Commercial statistics' or 'Economic statistics'
    'bus062000'				=> 'Structural adjustment (Economic policy)',
    'bus063000'				=> 'Strategic planning',
    'bus064000'				=> 'Taxation',
    'bus064010'				=> 'Corporations--Taxation',
    'bus064020'				=> 'International business enterprises--Taxation',
    'bus064030'				=> 'Small business--Taxation',
    'bus065000'				=> 'Total quality management',
    'bus066000'				=> 'Business--Training',
    'bus067000'				=> 'Regional economics',
    'bus068000'				=> 'Economic development',
    'bus069000'				=> 'Economics',
    'bus069010'				=> 'Comparative economics',
    'bus069020'				=> 'Competition, International',
    'bus069030'				=> 'Economics',
    'bus070000'				=> 'Industries',
    'bus070010'				=> 'Agricultural industries',
    'bus070020'				=> 'Automobile industry and trade',
    'bus070030'				=> 'Computer industry',
    'bus070040'				=> 'Energy industries',
    'bus070050'				=> 'Manufacturing industries',
    'bus070060'				=> 'Communication and traffic',
    'bus070070'				=> 'Parks--Management',
    'bus070080'				=> 'Service industries',
    'bus070090'				=> 'Textile industry',
    'bus070100'				=> 'Transportation',
    'bus070110'				=> 'Entertainment industry',
    'bus070120'				=> 'Food industry and trade',
    'bus070130'				=> 'Biotechnology industries',
    'bus070140'				=> 'Financial services industry',
    'bus070150'				=> 'Natural resources',
    'bus071000'				=> 'Leadership',
    'bus072000'				=> 'Sustainable development',
    'bus073000'				=> 'Commerce',
    'bus074000'				=> 'Nonprofit organizations',
    # 'bus075000' is 'Consulting' which has no exact LC match
    'bus076000'				=> 'Purchasing',
    'bus077000'				=> 'Commerce--History',
    'bus078000'				=> 'Distribution (Economic theory)',
    'bus079000'				=> 'Industrial policy',
    'bus080000'				=> 'Home-based businesses',
    'bus081000'				=> 'Tourism',
    'bus082000'				=> 'Industrial management',
    'bus083000'				=> 'Information resources management',
    'bus084000'				=> 'Office practice--Automation',
    'bus085000'				=> 'Organizational behavior',
    'bus086000'				=> 'Business forecasting',
    'bus087000'				=> 'Production management',
    'bus088000'				=> 'Time management',
    'bus089000'				=> 'Secretaries--Training',
    'bus090000'				=> 'Electronic commerce',
    'bus090010'				=> 'Internet advertising',
    'bus090020'				=> 'Internet banking',
    'bus090030'				=> 'Electronic trading of securities',
    'bus090040'				=> 'Internet auctions',
    'bus091000'				=> 'Business mathematics',
    'bus092000'				=> 'Economic development',
    'bus093000'				=> 'Facility management',
    'bus094000'				=> 'Business enterprises--Environmental aspects',
    'bus095000'				=> 'Office equipment and supplies',
    'bus096000'				=> 'Office management',
    'bus097000'				=> 'Work environment',
    'bus098000'				=> 'Intellectual capital',
    'bus099000'				=> 'Environmental economics',
    'bus100000'				=> 'Museum techniques',
    'bus101000'				=> 'Project management',
    'bus102000'				=> 'Contracting out',
    'bus103000'				=> 'Organizational change',
    'bus104000'				=> 'Corporate governance',
    'bus105000'				=> 'Franchises (Retail trade)',
    'bus106000'				=> 'Executive coaching',
    'bus107000'				=> 'Success',
    'bus108000'				=> 'Research and development projects',
    'bus109000'				=> 'Businesswomen',
    'bus110000'				=> 'Conflict management',
    'cgn000000'				=> 'Comic books, strips, etc',
    'cgn004080'				=> 'Superheroes',
    'ckb000000'				=> 'Cooking',
    'ckb001000'				=> 'Cooking, African',
    'ckb002000'				=> 'Cooking, American',
    'ckb002010'				=> 'Cooking, American--California style',
    'ckb002020'				=> 'Cooking, American--Middle Atlantic States',
    'ckb002030'				=> 'Cooking, American--Midwestern style',
    'ckb002040'				=> 'Cooking, American--New England style',
    'ckb002050'				=> 'Cooking, American--Northwestern States',
    'ckb002060'				=> 'Cooking, American--Southern style',
    'ckb002070'				=> 'Cooking, American--Southwestern style',
    'ckb002080'				=> 'Cooking, American--Western style',
    'ckb003000'				=> 'Appetizers',
    'ckb004000'				=> 'Baking',
    'ckb005000'				=> 'Barbecuing',
    'ckb006000'				=> 'Bartending',
    'ckb007000'				=> 'Cooking (Beer)',
    'ckb008000'				=> 'Non-alcoholic beverages',
    'ckb009000'				=> 'Cooking (Bread)',
    'ckb010000'				=> 'Breakfasts',
    # 'ckb011000' could be 'Cooking, English', 'Cooking, Scottish', or 'Cooking, Welsh'
    'ckb012000'				=> 'Brunches',
    # 'ckb013000' could be either 'Cooking, Cajun' or 'Cooking, Creole'
    'ckb014000'				=> 'Cake',
    'ckb015000'				=> 'Canning and preserving',
    # 'ckb016000' could be either 'Cooking, Caribbean' or 'Cooking, West Indian'
    'ckb017000'				=> 'Cooking, Chinese',
    'ckb018000'				=> 'Cooking (Chocolate)',
    # 'ckb019000' could be either 'Cooking (Coffee)' or 'Cooking (Tea)'
    'ckb020000'				=> 'Cooking for one',
    'ckb021000'				=> 'Cookies',
    'ckb024000'				=> 'Desserts',
    'ckb026000'				=> 'Weight loss',
    'ckb029000'				=> 'Entertaining',
    'ckb033000'				=> 'Garnishes (Cooking)',
    'ckb034000'				=> 'Cooking, French',
    'ckb036000'				=> 'Cooking, German',
    'ckb038000'				=> 'Cooking, Greek',
    # 'ckb040000' could be 'Cooking (Herbs)', 'Cooking (Spices)', or 'Condiments'
    'ckb041000'				=> 'Cooking--History',
    'ckb042000'				=> 'Holiday cooking',
    'ckb050000'				=> 'Low-cholesterol diet',
    'ckb051000'				=> 'Low-fat diet',
    'ckb057000'				=> 'Microwave cooking',
    'ckb060000'				=> 'Outdoor cooking',
    'ckb062000'				=> 'Pastry',
    'ckb063000'				=> 'Pies',
    'ckb064000'				=> 'Pizza',
    'ckb069000'				=> 'Quantity cooking',
    'ckb070000'				=> 'Quick and easy cooking',
    'ckb073000'				=> 'Salads',
    # 'ckb079000' could map to either 'Soups' or 'Stews'
    'ckb088000'				=> 'Cooking (Liquors)',
    'ckb089000'				=> 'Wok cooking',
    'ckb090000'				=> 'Cooking, Asian',
    'ckb091000'				=> 'Cooking, Canadian',
    'ckb092000'				=> 'Cooking, European',
    'ckb095000'				=> 'Confectionery',
    'ckb099000'				=> 'Cooking, Latin American',
    'ckb100000'				=> 'Beverages',
    'ckb101000'				=> 'Entrées (Cooking)',
    'ckb102000'				=> 'Sauces',
    'ckb103000'				=> 'Cancer--Nutritional aspects',
    'ckb104000'				=> 'Heart--Diseases--Nutritional aspects',
    'ckb106000'				=> 'Food allergy',
    'ckb107000'				=> 'Baby foods',
    'ckb108000'				=> 'Low-carbohydrate diet',
    'ckb110000'				=> 'Raw foods',
    'ckb111000'				=> 'Gluten-free foods',
    'ckb112000'				=> 'Casserole cooking',
    'com000000'				=> 'Computers',
    'com001000'				=> 'Computer peripherals',
    'com003000'				=> 'Application software',
    'com004000'				=> 'Artificial intelligence',
    "com006000"				=> "Computers--Buyers' guides",
    'com007000'				=> 'CAD/CAM systems',
    'com008000'				=> 'Calculators',
    'com009000'				=> 'Optical disks',
    'com010000'				=> 'Compilers (Computer programs)',
    'com011000'				=> 'Computer architecture',
    'com012000'				=> 'Computer graphics',
    'com012040'				=> 'Computer games--Programming',
    'com012050'				=> 'Image processing',
    'com013000'				=> 'Computer literacy',
    'com014000'				=> 'Computer science',
    'com015000'				=> 'Malware (Computer software)',
    # 'com016000' could be either 'Computer vision' or 'Pattern recognition systems'
    'com018000'				=> 'Data processing',
    'com020000'				=> 'Data transmission systems',
    'com020010'				=> 'Electronic data interchange',
    'com020050'				=> 'Broadband communication systems',
    'com020060'				=> 'Modems',
    'com020090'				=> 'Wireless communication systems',
    'com021000'				=> 'Databases',
    'com021030'				=> 'Data mining',
    'com021040'				=> 'Data warehousing',
    'com023000'				=> 'Computer software--Education',
    'com024000'				=> 'Computer games',
    'com025000'				=> 'Expert systems (Computer science)',
    'com027000'				=> 'Finance, Personal--Software',
    'com031000'				=> 'Information theory',
    'com032000'				=> 'Information technology',
    'com034000'				=> 'Interactive computer systems',
    'com035000'				=> 'Keyboarding',
    'com036000'				=> 'Logic design',
    'com037000'				=> 'Machine theory',
    'com039000'				=> 'Management information systems',
    'com040000'				=> 'Memory management (Computer science)',
    'com041000'				=> 'Microprocessors',
    'com042000'				=> 'Computational linguistics',
    'com043000'				=> 'Computer networks',
    'com043020'				=> 'Local area networks (Computer networks)',
    'com043040'				=> 'Computer network protocols',
    'com043050'				=> 'Computer networks--Security measures',
    'com044000'				=> 'Neural networks (Computer science)',
    'com046000'				=> 'Operating systems (Computers)',
    'com046090'				=> 'Virtual computer systems',
    'com047000'				=> 'Optical data processing',
    'com048000'				=> 'Electronic data processing--Distributed processing',
    'com049000'				=> 'Computer peripherals',
    'com050000'				=> 'Microcomputers',
    'com050020'				=> 'Macintosh-compatible computers',
    'com051000'				=> 'Computer programming',
    'com051010'				=> 'Programming languages (Electronic computers)',
    'com051020'				=> 'Ada (Computer program language)',
    'com051030'				=> 'C (Computer program language)',
    'com051040'				=> 'Assembly languages (Electronic computers)',
    "com051050"				=> 'BASIC (Computer program language)',
    'com051060'				=> 'C (Computer program language)',
    'com051070'				=> 'C++ (Computer program language)',
    'com051080'				=> 'COBOL (Computer program language)',
    'com051090'				=> 'FORTRAN (Computer program language)',
    'com051100'				=> 'LISP (Computer program language)',
    'com051110'				=> 'C (Computer program language)',
    'com051120'				=> 'Modula-2 (Computer program language)',
    'com051130'				=> 'Pascal (Computer program language)',
    'com051140'				=> 'Prolog (Computer program language)',
    'com051150'				=> 'C (Computer program language)',
    'com051160'				=> 'Smalltalk (Computer program language)',
    'com051170'				=> 'SQL (Computer program language)',
    'com051180'				=> 'C (Computer program language)',
    'com051190'				=> 'Pascal (Computer program language)',
    'com051210'				=> 'Object-oriented programming (Computer science)',
    'com051220'				=> 'Parallel programming (Computer science)',
    # 'com051230' could be either 'Computer software--Development' or 'Software engineering'
    'com051240'				=> 'System analysis',
    'com051260'				=> 'JavaScript (Computer program language)',
    'com051270'				=> 'HTML (Document markup language)',
    'com051280'				=> 'Java (Computer program language)',
    'com051290'				=> 'RPG (Computer program language)',
    'com051300'				=> 'Computer programming--Algorithms',
    'com051310'				=> 'C# (Computer program language)',
    'com051320'				=> 'XML (Document markup language)',
    'com051330'				=> 'Computer software--Quality assurance',
    'com051340'				=> 'Computer network protocols',
    'com051350'				=> 'Perl (Computer program language)',
    'com051360'				=> 'Python (Computer program language)',
    'com051400'				=> 'PHP (Computer program language)',
    'com051410'				=> 'Ruby (Computer program language)',
    'com051420'				=> 'VBScript (Computer program language)',
    'com051430'				=> 'Software engineering--Project management',
    'com051450'				=> 'UML (Computer science)',
    'com052000'				=> 'Computers--Reference',
    'com053000'				=> 'Computer security',
    'com054000'				=> 'Electronic spreadsheets--Software',
    'com054010'				=> 'Electronic spreadsheets--Software',
    'com054020'				=> 'Electronic spreadsheets--Software',
    'com055000'				=> 'Computer technicians--Certification--Study guides',
    'com057000'				=> 'Virtual reality',
    'com058000'				=> 'Word processing--Software',
    'com058010'				=> 'Word processing--Software',
    'com058020'				=> 'Word processing--Software',
    'com059000'				=> 'Computer engineering',
    'com060000'				=> 'Internet',
    # 'com060030' could be either 'Extranets (Computer networks)' or
    # 'Intranets (Computer networks)'
    'com060010'				=> 'Browsers (Computer programs)',
    'com060040'				=> 'Internet--Safety measures',
    'com060070'				=> 'Web sites--Directories',
    'com060080'				=> 'World Wide Web',
    'com060090'				=> 'Application software--Internet--Development',
    'com060100'				=> 'Blogs',
    'com060110'				=> 'Webcasting',
    'com060120'				=> 'Web search engines',
    'com060130'				=> 'Web sites--Design',
    'com060140'				=> 'Online social networks',
    'com060150'				=> 'User-generated content',
    'com060160'				=> 'Web site development',
    'com060180'				=> 'Web services',
    'com063000'				=> 'Document imaging systems',
    'com064000'				=> 'Electronic commerce',
    'com065000'				=> 'Electronic publishing',
    'com067000'				=> 'Computers',
    'com069000'				=> 'Online information services',
    'com069010'				=> 'Online information services',
    'com070000'				=> 'User interfaces (Computer systems)',
    'com071000'				=> 'Computer animation',
    'com072000'				=> 'Computer simulation',
    'com073000'				=> 'Speech processing systems',
    'com074000'				=> 'Mobile computing',
    'com076000'				=> 'Computers',
    'com077000'				=> 'Computer software--Mathematics',
    'com078000'				=> 'Presentation graphics software',
    'com079000'				=> 'Computers--Social aspects',
    'com079010'				=> 'Human-computer interaction',
    'com080000'				=> 'Computers--History',
    'com081000'				=> 'Project management--Software',
    'com082000'				=> 'Bioinformatics',
    'com083000'				=> 'Cryptography',
    'com084000'				=> 'Application software',
    'com084010'				=> 'Databases',
    'com084020'				=> 'Electronic mail systems',
    'com085000'				=> 'Technical writing',
    'com087000'				=> 'Digital media',
    'com088000'				=> 'Computers--Administration',
    'com090000'				=> 'Tablet computers',
    'computers/programming languages/javascript' => 'JavaScript (Computer program language)',
    'computers/programming languages'	=> 'Programming languages (Electronic computers)',
    'computers/reference'		=> 'Computers--Reference',
    'juv000000'				=> 'Fiction, Juvenile',
    'fic000000'				=> 'Fiction',
    'fic009000'				=> 'Fantasy fiction',
    'fic014000'				=> 'Historical fiction',
    'fic027000'				=> 'Love stories',
    'occ000000'				=> 'Superstition',
    'occ003000'				=> 'Channeling (Spiritualism)',
    'occ004000'				=> 'Crystals',
    'occ005000'				=> 'Divination',
    'occ006000'				=> 'Dreams',
    'occ007000'				=> 'Extrasensory perception',
    'occ008000'				=> 'Fortune-telling',
    'occ009000'				=> 'Horoscopes',
    'occ010000'				=> 'Meditation',
    'occ011000'				=> 'Mental healing',
    'occ011010'				=> 'Energy medicine',
    'occ011020'				=> 'Spiritual healing',
    'occ014000'				=> 'New Thought',
    'occ015000'				=> 'Numerology',
    'occ016000'				=> 'Occultism',
    'occ017000'				=> 'Palmistry',
    'occ018000'				=> 'Parapsychology',
    'occ019000'				=> 'Inspiration--Religious aspects',
    'occ020000'				=> 'Prophecy',
    'occ022000'				=> 'Immortality',
    'occ023000'				=> 'Supernatural',
    'occ024000'				=> 'Tarot',
    'occ025000'				=> 'Unidentified flying objects',
    'occ026000'				=> 'Witchcraft',
    'occ027000'				=> 'Spiritualism',
    'occ028000'				=> 'Magic',
    'occ031000'				=> 'Controversial literature',
    'occ032000'				=> 'Guides (Spiritualism)',
    'occ034000'				=> 'Near-death experiences',
    'occ035000'				=> 'Astral projection',
    'occ036000'				=> 'Spirituality',
    'occ036030'				=> 'Shamanism',
    'occ036050'				=> 'Goddess religion',
    'occ037000'				=> 'Feng shui',
    'occ039000'				=> 'Hallucinogenic drugs',
    'occ040000'				=> 'Hermetism',
    'occ041000'				=> 'Sex--Religious aspects',
   );

our %booktypes = (
    # Fiction types, by length (definitions are not standardized)
    'micro fiction'	=> 'Micro Fiction',	# Below 100 words
    'micro-fiction'	=> 'Micro Fiction',
    'drabble'		=> 'Drabble',		# 100 words, exactly
    'flash fiction'	=> 'Flash Fiction',	# Below 1000 words
    'short short'	=> 'Flash Fiction',
    'short short story'	=> 'Flash Fiction',
    'short story'	=> 'Short Story',	# Generally 1k-10k words
    'short'		=> 'Short Story',
    'novelette'		=> 'Novelette',		# Generally 7.5k-15k words
    'novella'		=> 'Novella',		# Generally 12k-40k words
    'novel'		=> 'Novel',		# Over 50k words
    'anthology'		=> 'Anthology',		# Multiple novellas or shorter
    'omnibus'		=> 'Omnibus',		# Multiple novels
    'complete works'	=> 'Complete Works',	# Complete collection of all works by an author
    'opera omnia'	=> 'Complete Works',
    # Age categories
    'picture book'	=> 'Picture Book',	# For pre-readers or children ages 0-5
    'early reader'	=> 'Early Reader',	# For ages 5-7
    'chapter book'	=> 'Chapter Book',	# For ages 7-12
    'young-adult novel'	=> 'Young-Adult Novel',	# Subcategory of Novel aimed at ages 12-18
    'ya novel'		=> 'Young-Adult Novel',
    # Non-fiction types
    'academic paper'	=> 'Academic Paper',	# Creditable, peer-reviewed sources
    'almanac'		=> 'Almanac',
    'autobiography'	=> 'Autobiography',
    'biography'		=> 'Biography',
    'book report'	=> 'Book Report',
    'creative nonfiction' => 'Creative Nonfiction', # Flexible imaginings of actual events
    'design document'	=> 'Design Document',
    'diary'		=> 'Diary',
    'dictionary',	=> 'Dictionary',
    'encyclopædia'	=> 'Encyclopedia',
    'encyclopaedia'	=> 'Encyclopedia',
    'encyclopedia'	=> 'Encyclopedia',
    'essay'		=> 'Essay',
    'essay collection'	=> 'Essay Collection',
    'journal'		=> 'Journal',		# Collection of papers, academic or technical, with or without review
    'journalism'	=> 'Journalism',
    'letter'		=> 'Letters',		# Correspondence, singular or collective
    'letters'		=> 'Letters',
    'memoir'		=> 'Memoir',
    'popular science'	=> 'Popular Science',	# Interpretation of science for a general audience
    'reference'		=> 'Reference',		# Includes blueprints, guides, manuals, diagrams,
						# but see also 'Technical Writing' below
    'technical writing'	=> 'Technical Writing',	# Includes 'how-to' books even on nontechnical topics
    'technical'		=> 'Technical Writing',
    'thesaurus'		=> 'Thesaurus',
    'travelogue'	=> 'Travelogue',
    # Other types
    'art'		=> 'Art',		# Collection of artistic images
    'comic book'	=> 'Comic Book',
    'graphic novel'	=> 'Graphic Novel',
    'photography'	=> 'Photography',
    'poetry'		=> 'Poetry',		# One or more structured poems
    'prose poetry'	=> 'Prose',		# One or more unstructured prose works
    'prose'		=> 'Prose',
);

our %dcelements12;
tie %dcelements12, 'Tie::IxHash', (
    "dc:identifier"  => "dc:Identifier",
    "dc:title"       => "dc:Title",
    "dc:creator"     => "dc:Creator",
    "dc:contributor" => "dc:Contributor",
    "dc:subject"     => "dc:Subject",
    "dc:description" => "dc:Description",
    "dc:publisher"   => "dc:Publisher",
    "dc:date"        => "dc:Date",
    "dc:type"        => "dc:Type",
    "dc:format"      => "dc:Format",
    "dc:source"      => "dc:Source",
    "dc:language"    => "dc:Language",
    "dc:relation"    => "dc:Relation",
    "dc:coverage"    => "dc:Coverage",
    "dc:rights"      => "dc:Rights",
    "dc:copyrights"  => "dc:Rights"
    );

our %dcelements20;
tie %dcelements20, 'Tie::IxHash', (
    "dc:identifier"  => "dc:identifier",
    "dc:title"       => "dc:title",
    "dc:creator"     => "dc:creator",
    "dc:contributor" => "dc:contributor",
    "dc:subject"     => "dc:subject",
    "dc:description" => "dc:description",
    "dc:publisher"   => "dc:publisher",
    "dc:date"        => "dc:date",
    "dc:type"        => "dc:type",
    "dc:format"      => "dc:format",
    "dc:source"      => "dc:source",
    "dc:language"    => "dc:language",
    "dc:relation"    => "dc:relation",
    "dc:coverage"    => "dc:coverage",
    "dc:rights"      => "dc:rights",
    "dc:copyrights"  => "dc:rights"
    );

our %lcsubjects = (
    'abolition of capital punishment'	=> 'Capital punishment',
    'abolition of slavery'		=> 'Slavery',
    'abolition'				=> 'Slavery',
    'absent treatment'			=> 'Mental healing',
    'abstract automata'			=> 'Machine theory',
    'abstract machines'			=> 'Machine theory',
    'absurd (philosophy)'		=> 'Absurd (Philosophy)',
    'absurdist (philosophy)'		=> 'Absurd (Philosophy)',
    'absurdist'				=> 'Absurd (Philosophy)',
    'abuse of children'			=> 'Child abuse',
    'accountancy'			=> 'Accounting',
    'accounting'			=> 'Accounting',
    'accounting standards'		=> 'Accounting--Standards',
    'accounting--standards'		=> 'Accounting--Standards',
    'achievement motivation'		=> 'Achievement motivation',
    'aching, tiffany (fictitious character)' => 'Aching, Tiffany (Fictitious character)',
    'aching, tiffany'			=> 'Aching, Tiffany (Fictitious character)',
    'acquisition of corporations'	=> 'Consolidation and merger of corporations',
    'acquisitions and mergers'		=> 'Consolidation and merger of corporations',
    'action research'			=> 'Action research',
    'acts of terrorism'			=> 'Terrorism',
    'ada (computer program language)'	=> 'Ada (Computer program language)',
    'ada'				=> 'Ada (Computer program language)',
    'adding machines'			=> 'Calculators',
    'adding-machines'			=> 'Calculators',
    'adiposity'				=> 'Obesity',
    'administration'			=> 'Management',
    'administrative communication'	=> 'Business communication',
    'ads'				=> 'Advertising',
    'adventure'				=> 'Adventure stories',
    'adventure stories'			=> 'Adventure stories',
    'advertisements'			=> 'Advertising',
    'advertising'			=> 'Advertising',
    'african art'			=> 'Art, African',
    'african american art'		=> 'African American art',
    'african cooking'			=> 'Cooking, African',
    'afro-american art'			=> 'African American art',
    'afterlife'				=> 'Immortality',
    'agents, search'			=> 'Search engines',
    'agoraphobia'			=> 'Agoraphobia',
    'agribusiness'			=> 'Agricultural industries',
    'agricultural industries'		=> 'Agricultural industries',
    'aias (greek mythology)'		=> 'Ajax (Greek mythology)',
    'ajax (greek mythology)'		=> 'Ajax (Greek mythology)',
    'ajax (legendary character)'	=> 'Ajax (Greek mythology)',
    'ajax (web site development technology)' => 'Ajax (Web site development technology)',
    'ajax the greater (greek mythology)' => 'Ajax (Greek mythology)',
    'ajax the greater'			=> 'Ajax (Greek mythology)',
    'algorism'				=> 'Algorithms',
    'algorithmic knowledge discovery'	=> 'Data mining',
    'algorithms'			=> 'Algorithms',
    'allocation of time'		=> 'Time management',
    'amalgamation of corporations'	=> 'Consolidation and merger of corporations',
    'american cooking'			=> 'Cooking, American',
    'americana'				=> 'Americana',
    'androids'				=> 'Androids',
    'animation, computer'		=> 'Computer animation',
    'angelology'			=> 'Angels',
    'angels'				=> 'Angels',
    'angleterre'			=> 'England',
    'anglii͡a'				=> 'England',
    'animals in art'			=> 'Animals in art',
    'antique and classic cars'		=> 'Antique and classic cars',
    'antique cars'			=> 'Antique and classic cars',
    'antiques'				=> 'Antiques',
    'antiques--americana'		=> 'Antiques--Americana',
    'antiques--art'			=> 'Antiques--Art',
    'antiques--baskets'			=> 'Antiques--Baskets',
    'antiques--books'			=> 'Antiques--Books',
    'antiques--bottles'			=> 'Antiques--Bottles',
    'antiques--buttons'			=> 'Antiques--Buttons',
    'antiques--cartoons'		=> 'Antiques--Cartoons',
    'antiques--clocks and watches'	=> 'Antiques--Clocks and watches',
    'antiques--coins'			=> 'Antiques--Coins',
    'antiques--dolls'			=> 'Antiques--Dolls',
    'antiques--figurines'		=> 'Antiques--Figurines',
    'antiques--firearms'		=> 'Antiques--Firearms',
    'antiques--furniture'		=> 'Antiques--Furniture',
    'antiques--glassware'		=> 'Antiques--Glassware',
    'antiques--jewelry'			=> 'Antiques--Jewelry',
    'antiques--kitchen utensils'	=> 'Antiques--Kitchen utensils',
    'antiques--musical instruments'	=> 'Antiques--Musical instruments',
    'antiques--periodicals'		=> 'Antiques--Periodicals',
    'antislavery'			=> 'Slavery',
    'appearance bias'			=> 'Physical-appearance-based bias',
    'appearance discrimination'		=> 'Physical-appearance-based bias',
    'appearance-based bias'		=> 'Physical-appearance-based bias',
    'appearance-based discrimination'	=> 'Physical-appearance-based bias',
    'ai'				=> 'Artificial intelligence',
    'airbrush art'			=> 'Airbrush art',
    'airbrush art--technique'		=> 'Airbrush art',
    'ais'				=> 'Artificial intelligence',
    'alimentation',			=> 'Nutrition',
    'alsatian cooking'			=> 'Cooking, French--Alsatian style',
    'alternate histories'		=> 'Alternative histories (Fiction)',
    'alternate histories (fiction)'	=> 'Alternative histories (Fiction)',
    'alternate history'			=> 'Alternative histories (Fiction)',
    'alternative histories'		=> 'Alternative histories (Fiction)',
    'alternative histories (fiction)'	=> 'Alternative histories (Fiction)',
    'alternative history'		=> 'Alternative histories (Fiction)',
    'american art'			=> 'Art, American',
    'american science fiction and fantasy' => 'Speculative fiction',
    'appetizers'			=> 'Appetizers',
    'application computer programs'	=> 'Application software',
    'application computer software'	=> 'Application software',
    'application software'		=> 'Application software',
    'application software--development'	=> 'Application software--Development',
    'application software--internet'	=> 'Application software--Internet',
    'application software--internet--development' => 'Application software--Internet--Development',
    'applications software'		=> 'Application software',
    'apps (computer software)'		=> 'Application software',
    'apps'				=> 'Application software',
    'ararat, mount (turkey)'		=> 'Ararat, Mount (Turkey)',
    'architecture'			=> 'Architecture',
    'architecture, computer'		=> 'Computer architecture',
    'architecture--history'		=> 'Architecture--History',
    'arithmetic'			=> 'Mathematics',
    'art'				=> 'Art',
    'art and business'			=> 'Art and business',
    'art collections'			=> 'Art collections',
    'art criticism'			=> 'Art criticism',
    'art objects'			=> 'Art objects',
    'art objects--conservation and restoration'
      => 'Art objects--Conservation and restoration',
    'art technique'			=> 'Art--Technique',
    'art, african'			=> 'Art, African',
    'art, african american'		=> 'African American art',
    'art, american'			=> 'Art, American',
    'art, asian'			=> 'Art, Asian',
    'art, asian american'		=> 'Asian American art',
    'art, asiatic'			=> 'Art, Asian',
    'art, australian'			=> 'Art, Australian',
    'art, canadian'			=> 'Art, Canadian',
    'art, caribbean'			=> 'Art, Caribbean',
    'art, conceptual'			=> 'Conceptual art',
    'art, digital'			=> 'Digital art',
    'art, erotic'			=> 'Erotic art',
    'art, european'			=> 'Art, European',
    'art, folk'				=> 'Folk art',
    'art, hispanic american'		=> 'Hispanic American art',
    'art, latin american'		=> 'Art, Latin American',
    'art, middle east'			=> 'Art--Middle East',
    'art, middle eastern'		=> 'Art--Middle East',
    'art, modern'			=> 'Art, Modern',
    'art, modern--europe'		=> 'Art, Modern--Europe',
    'art, oriental'			=> 'Art, Asian',
    'art, performance'			=> 'Performance art',
    'art, russian'			=> 'Art, Russian',
    'art, spanish american'		=> 'Art, Latin American',
    'art, street'			=> 'Street art',
    'art, video'			=> 'Video art',
    'art, wall'				=> 'Street art',
    'art--criticism'			=> 'Art criticism',
    'art--middle east'			=> 'Art--Middle East',
    'art--subjects'			=> 'Art--Themes, motives',
    'art--technique'			=> 'Art--Technique',
    'art--themes, motives'		=> 'Art--Themes, motives',
    'artificial intelligence'		=> 'Artificial intelligence',
    'artificial neural networks'	=> 'Neural networks (Computer science)',
    'artificial thinking'		=> 'Artificial intelligence',
    'artistic technique'		=> 'Art--Technique',
    'artists'				=> 'Artists',
    'artists--biography'		=> 'Artists--Biography',
    'arts--criticism'			=> 'Art criticism',
    'arts--study and teaching'		=> 'Arts--Study and teaching',
    'asian american art'		=> 'Asian American art',
    'asian cookery'			=> 'Cooking, Asian',
    'asian cooking'			=> 'Cooking, Asian',
    'asiatic art'			=> 'Art, Asian',
    'assembler language (computer program language)'	=> 'Assembly languages (Electronic computers)',
    'assembler language (electronic computers)'	=> 'Assembly languages (Electronic computers)',
    'assembler language'		=> 'Assembly languages (Electronic computers)',
    'assembler languages (electronic computers)' => 'Assembly languages (Electronic computers)',
    'assembler languages'		=> 'Assembly languages (Electronic computers)',
    'assembly language (electronic computers)'	=> 'Assembly languages (Electronic computers)',
    'assembly language'			=> 'Assembly languages (Electronic computers)',
    'assembly languages (electronic computers)'	=> 'Assembly languages (Electronic computers)',
    'assembly languages'		=> 'Assembly languages (Electronic computers)',
    'assurance (insurance)'		=> 'Insurance',
    'astral projection'			=> 'Astral projection',
    'astral travel'			=> 'Astral projection',
    'astrology'				=> 'Astrology',
    'astrology, chinese'		=> 'Astrology, Chinese',
    'asynchronous javascript and xml'	=> 'Ajax (Web site development technology)',
    'athletic competitions'		=> 'Sports',
    'athletics'				=> 'Sports',
    'attacks, terrorist'		=> 'Terrorism',
    'auditing'				=> 'Auditing',
    'audits'				=> 'Auditing',
    'augury'				=> 'Divination',
    'austen, jane, 1775-1817'		=> 'Austen, Jane, 1775-1817',
    'austen, jane'			=> 'Austen, Jane, 1775-1817',
    'australian art'			=> 'Art, Australian',
    'authoring (authorship)'		=> 'Authorship',
    'authoring'				=> 'Authorship',
    'authorship'			=> 'Authorship',
    'autobiography'			=> 'Biography',
    'autographs'			=> 'Autographs',
    'automatic computers'		=> 'Computers',
    'automatic data processing equipment' => 'Computers',
    'automatic data processors'		=> 'Computers',
    'automatic drafting'		=> 'Computer graphics',
    'automatic language processing'	=> 'Computational linguistics',
    'automobile industry'		=> 'Automobile industry and trade',
    'automobile industry and trade'	=> 'Automobile industry and trade',
    'automobile collision insurance'	=> 'Automobile insurance',
    'automobile insurance'		=> 'Automobile insurance',
    'automobile liability insurance'	=> 'Automobile insurance',
    'automobiles'			=> 'Automobiles',
    'automotive industry'		=> 'Automobile industry and trade',
    'auvergnat cooking'			=> 'Cooking, French--Auvergne style',
    'auvergne cooking'			=> 'Cooking, French--Auvergne style',
    'b-to-b marketing'			=> 'Industrial marketing',
    'b2b marketing'			=> 'Industrial marketing',
    'babies'				=> 'Infants',
    'baby foods'			=> 'Baby foods',
    'bagdad (iraq)'			=> 'Baghdad (Iraq)',
    'bagdad'				=> 'Baghdad (Iraq)',
    'bagddad (iraq)'			=> 'Baghdad (Iraq)',
    'bagddad'				=> 'Baghdad (Iraq)',
    'baghdad (iraq)'			=> 'Baghdad (Iraq)',
    'baghdad'				=> 'Baghdad (Iraq)',
    'baking bread'			=> 'Cooking (Bread)',
    'baking'				=> 'Baking',
    'bank robberies'			=> 'Bank robberies',
    'banking industry'			=> 'Banks and banking',
    'banking'				=> 'Banks and banking',
    'banks and banking'			=> 'Banks and banking',
    'barbecue cookery'			=> 'Barbecuing',
    'barbecue cooking'			=> 'Barbecuing',
    'barbecuing'			=> 'Barbecuing',
    'barbeque cookery'			=> 'Barbecuing',
    'barbeque cooking'			=> 'Barbecuing',
    'barbequing'			=> 'Barbecuing',
    'bargaining'			=> 'Negotiation',
    'bartending'			=> 'Bartending',
    'basic (computer program language)'	=> 'BASIC (Computer program language)',
    'basic rights'			=> 'Civil rights',
    'baskets'				=> 'Baskets',
    'bavarian cooking'			=> 'Cooking, German--Bavarian style',
    'bbq cooking'			=> 'Barbecuing',
    'beaurocracy'			=> 'Bureaucracy',
    'bedouin'				=> 'Bedouins',
    'bedouins'				=> 'Bedouins',
    'beduin'				=> 'Bedouins',
    'beduins'				=> 'Bedouins',
    'beer'				=> 'Beer',
    'beer--use in cooking'		=> 'Cooking (Beer)',
    "beginner's all-purpose symbolic instruction code"	=> 'BASIC (Computer program language)',
    'behavior in organizations'		=> 'Organizational behavior',
    'beverages'				=> 'Beverages',
    'beverages, non-alcoholic'		=> 'Non-alcoholic beverages',
    'bi-sexuality'			=> 'Bisexuality',
    'bible'				=> 'Bible',
    'big guns'				=> 'Ordnance',
    'bio-informatics'			=> 'Bioinformatics',
    'bio-terrorism'			=> 'Bioterrorism',
    'biography'				=> 'Biography',
    'bioinformatics'			=> 'Bioinformatics',
    'biological informatics'		=> 'Bioinformatics',
    'biological neural networks'	=> 'Neural networks (Neurobiology)',
    'bionics'				=> 'Bionics',
    'biotechnology industries'		=> 'Biotechnology industries',
    'biotechnology'			=> 'Biotechnology',
    'bioterrorism'			=> 'Bioterrorism',
    'bioterrorism--social aspects'	=> 'Bioterrorism--Social aspects',
    'biscuits, english'			=> 'Cookies',
    'bisexuality'			=> 'Bisexuality',
    'blogging'				=> 'Blogs',
    'blogs'				=> 'Blogs',
    'boats'				=> 'Boats',
    'body art'				=> 'Body art',
    'body building'			=> 'Bodybuilding',
    'body care'				=> 'Hygiene',
    'body sculpting'			=> 'Bodybuilding',
    'body sculpting (bodybuilding)'	=> 'Bodybuilding',
    'bodybuilding'			=> 'Bodybuilding',
    'bodysculpting'			=> 'Bodybuilding',
    'bodysculpting (bodybuilding)'	=> 'Bodybuilding',
    'bond issues'			=> 'Bonds',
    'bonds'				=> 'Bonds',
    'bookkeeping'			=> 'Bookkeeping',
    'books'				=> 'Books',
    'books--appraisal'			=> 'Criticism',
    'bottles'				=> 'Bottles',
    'bread'				=> 'Bread',
    'bread--use in cooking'		=> 'Cooking (Bread)',
    'breads'				=> 'Bread',
    'breakfasts'			=> 'Breakfasts',
    'breton cooking'			=> 'Cooking, French--Brittany style',
    'bric-a-brac'			=> 'Art objects',
    'brittany cooking'			=> 'Cooking, French--Brittany style',
    'broadband communication systems'	=> 'Broadband communication systems',
    'browsers (computer programs)'	=> 'Browsers (Computer programs)',
    'brunches'				=> 'Brunches',
    'budget'				=> 'Budget',
    'budgeting'				=> 'Budget',
    'budgets, family'			=> 'Budgets, Personal',
    'budgets, personal'			=> 'Budgets, Personal',
    'budgets, time'			=> 'Time management',
    'bureaucracy'			=> 'Bureaucracy',
    'burgundy cooking'			=> 'Cooking, French--Burgundy style',
    'business'				=> 'Commerce',
    'business administration'		=> 'Industrial management',
    'business and art'			=> 'Art and business',
    'business coaching'			=> 'Executive coaching',
    'business combinations'		=> 'Consolidation and merger of corporations',
    'business communication'		=> 'Business communication',
    'business data interchange, electronic' => 'Electronic data interchange',
    'business development'		=> 'Business--Development',
    'business education'		=> 'Business education',
    'business enterprises'		=> 'Business enterprises',
    'business enterprises, home'	=> 'Home-based businesses',
    'business enterprises, international' => 'International business enterprises',
    'business enterprises--environmental aspects' =>
      'Business enterprises--Environmental aspects',
    'business enterprises--management'	=> 'Industrial management',
    'business espionage'		=> 'Business intelligence',
    'business ethics'			=> 'Business ethics',
    'business etiquette'		=> 'Business etiquette',
    'business finance'			=> 'Corporations--Finance',
    'business forecasting'		=> 'Business forecasting',
    'business forecasts'		=> 'Business forecasting',
    'business graphics software'	=> 'Presentation graphics software',
    'business history'			=> 'Commerce--History',
    'business intelligence'		=> 'Business intelligence',
    'business law'			=> 'Commercial law',
    'business machines'			=> 'Office equipment and supplies',
    'business management'		=> 'Industrial management',
    'business math'			=> 'Business mathematics',
    'business mathematics'		=> 'Business mathematics',
    'business meetings'			=> 'Business meetings',
    'business mergers'			=> 'Consolidation and merger of corporations',
    'business organizations'		=> 'Business enterprises',
    'business presentation software'	=> 'Presentation graphics software',
    'business starts'			=> 'New business enterprises',
    'business statistics'		=> 'Commercial statistics',
    'business to business marketing'	=> 'Industrial marketing',
    'business training'			=> 'Business--Training',
    'business writing'			=> 'Business writing',
    'business--development'		=> 'Business--Development',
    'business--forecasting'		=> 'Business forecasting',
    'business--government policy'	=> 'Industrial policy',
    'business--public relations'	=> 'Public relations',
    'business--reference books'		=> 'Business--Reference books',
    'business--statistical methods'	=> 'Commercial statistics',
    'business--statistics'		=> 'Commercial statistics',
    'business--study and teaching'	=> 'Business education',
    'business--training'		=> 'Business--Training',
    'business-to-business marketing'	=> 'Industrial marketing',
    'businesses'			=> 'Business enterprises',
    'businesses, home'			=> 'Home-based businesses',
    'businesses, small'			=> 'Small business',
    'businesswomen'			=> 'Businesswomen',
    'buttons'				=> 'Buttons',
    'buyer behavior'			=> 'Consumer behavior',
    "buyers' guides"			=> "Buyers' guides",
    'buying'				=> 'Purchasing',
    'buyouts, corporate'		=> 'Consolidation and merger of corporations',
    'c (computer program language)'	=> 'C (Computer program language)',
    'c plus plus'			=> 'C++ (Computer program language)',
    'c# (computer program language)'	=> 'C# (Computer program language)',
    'c++ (computer program language)'	=> 'C++ (Computer program language)',
    'c++'				=> 'C++ (Computer program language)',
    'c-sharp (computer program language)' => 'C# (Computer program language)',
    'cad'				=> 'Computer-aided design',
    'cad/cam systems'			=> 'CAD/CAM systems',
    'cajan cooking'			=> 'Cooking, Cajun',
    'cajun cooking'			=> 'Cooking, Cajun',
    'cake'				=> 'Cake',
    'cakes'				=> 'Cake',
    'calculating machines'		=> 'Calculators',
    'calculating-machines'		=> 'Calculators',
    'calculators'			=> 'Calculators',
    'california cooking'		=> 'Cooking, American--California style',
    'call options'			=> 'Options (Finance)',
    'calligraphy'			=> 'Calligraphy',
    'calligraphy--technique'		=> 'Calligraphy--Technique',
    'calls (finance)'			=> 'Options (Finance)',
    'caloric content of food'		=> 'Food--Caloric content',
    'caloric content of foods'		=> 'Food--Caloric content',
    'calories (food)'			=> 'Food--Caloric content',
    'cambistry'				=> 'Foreign exchange',
    'camellia sinensis'			=> 'Tea',
    'camellia thea'			=> 'Tea',
    'camellia theifera'			=> 'Tea',
    'cameralistics'			=> 'Finance, Public',
    'camp cooking'			=> 'Outdoor cooking',
    'campfire cooking'			=> 'Outdoor cooking',
    'canadian art'			=> 'Art, Canadian',
    'canadian cooking'			=> 'Cooking, Canadian',
    'canapes'				=> 'Appetizers',
    'canapés'				=> 'Appetizers',
    'cancer'				=> 'Cancer',
    'cancer--nutritional aspects'	=> 'Cancer--Nutritional aspects',
    'cancers'				=> 'Cancer',
    'canning and preserving'		=> 'Canning and preserving',
    'cannon'				=> 'Ordnance',
    'cannons'				=> 'Ordnance',
    'capital equipment'			=> 'Industrial equipment',
    'capital goods'			=> 'Industrial equipment',
    'capital punishment'		=> 'Capital punishment',
    'capital punishment, abolition of'	=> 'Capital punishment',
    'capital, intellectual'		=> 'Intellectual capital',
    'capital, knowledge'		=> 'Intellectual capital',
    'carcinoma'				=> 'Cancer',
    'careers'				=> 'Occupations',
    'caribbean art'			=> 'Art, Caribbean',
    'caribbean cooking'			=> 'Cooking, Caribbean',
    'caricatures and cartoons'		=> 'Caricatures and cartoons',
    'cars (automobiles)'		=> 'Automobiles',
    'cars'				=> 'Automobiles',
    'cars, antique'			=> 'Antique and classic cars',
    'cars, classic'			=> 'Antique and classic cars',
    'cars, vintage'			=> 'Antique and classic cars',
    'cartooning'			=> 'Cartooning',
    'cartooning--technique'		=> 'Cartooning',
    'cartoons'				=> 'Caricatures and cartoons',
    'carvings'				=> 'Sculpture',
    'case studies'			=> 'Case studies',
    'casserole cookery'			=> 'Casserole cooking',
    'casserole cooking'			=> 'Casserole cooking',
    'casualty insurance'		=> 'Casualty insurance',
    'cats'				=> 'Cats',
    'cds'	       			=> 'Compact discs',
    'ceramic art'			=> 'Pottery',
    'ceramics (art)'			=> 'Pottery',
    'ceramics'				=> 'Pottery',
    'certification'			=> 'Certification',
    'change, organizational'		=> 'Organizational change',
    'channeling (spiritualism)'		=> 'Channeling (Spiritualism)',
    'channelling (spiritualism)'	=> 'Channeling (Spiritualism)',
    'cheirognomy'			=> 'Palmistry',
    'cheiromancy'			=> 'Palmistry',
    'cheirosophy'			=> 'Palmistry',
    'cherubim'				=> 'Angels',
    'cherubs (spirits)'			=> 'Angels',
    'cherubs'				=> 'Angels',
    'child abuse'			=> 'Child abuse',
    'child maltreatment'		=> 'Child abuse',
    'child molestation'			=> 'Child sexual abuse',
    'child molesting'			=> 'Child sexual abuse',
    'child neglect'			=> 'Child abuse',
    'child sexual abuse'		=> 'Child sexual abuse',
    'children'				=> 'Children',
    'children--abuse of'		=> 'Child abuse',
    'chinaware'				=> 'Pottery',
    'chinese astrology'			=> 'Astrology, Chinese',
    'chinese cooking'			=> 'Cooking, Chinese',
    'chinese historical fiction'	=> 'Historical fiction, Chinese',
    'chirognomy'			=> 'Palmistry',
    'chiromancy'			=> 'Palmistry',
    'chirosophy'			=> 'Palmistry',
    'chocolate'				=> 'Chocolate',
    'chocolate--use in cooking'		=> 'Cooking (Chocolate)',
    # 'chowders' could map to either 'Soups' or 'Stews'
    'christianity'			=> 'Christianity',
    'cipher stories'			=> 'Code and cipher stories',
    'ciphers'				=> 'Ciphers',
    'ciphers--fiction'			=> 'Code and cipher stories',
    'ciphers--juvenile fiction'		=> 'Code and cipher stories',
    'cities and towns'			=> 'Cities and towns',
    'cities and towns--economic aspects' => 'Urban economics',
    'cities'				=> 'Cities and towns',
    'cities, imaginary'			=> 'Imaginary places',
    'city economics'			=> 'Urban economics',
    'city of london (england)'		=> 'City of London (England)',
    'civil liberties'			=> 'Civil rights',
    'civil resistance'			=> 'Government, Resistance to',
    'civil rights'			=> 'Civil rights',
    'class distinction'			=> 'Social classes',
    'classes, social'			=> 'Social classes',
    'classic automobiles'		=> 'Antique and classic cars',
    'classic cars'			=> 'Antique and classic cars',
    'cleanliness'			=> 'Hygiene',
    'climate science'			=> 'Climatology',
    'climate'				=> 'Weather',
    'climate, workplace'		=> 'Work environment',
    'climatology'			=> 'Climatology',
    'clinical informatics'		=> 'Medical informatics',
    'clinical sciences'			=> 'Medicine',
    'clocks and watches'		=> 'Clocks and watches',
    'clocks'				=> 'Clocks and watches',
    'clones of macintosh computers'	=> 'Macintosh-compatible computers',
    'coaching, business'		=> 'Executive coaching',
    'coaching, executive'		=> 'Executive coaching',
    'cobol (computer program language)'	=> 'COBOL (Computer program language)',
    'cobol'				=> 'COBOL (Computer program language)',
    'code and cipher stories'		=> 'Code and cipher stories',
    'code stories'			=> 'Code and cipher stories',
    'codes'				=> 'Ciphers',
    'coffea arabica'			=> 'Coffee',
    'coffea'				=> 'Coffee',
    'coffee'				=> 'Coffee',
    'coffee--use in cooking'		=> 'Cooking (Coffee)',
    'cognitive psychology'		=> 'Cognitive psychology',
    'coins'				=> 'Coins',
    'cold war'				=> 'Cold War',
    'collectables'			=> 'Collectibles',
    'collectibles'			=> 'Collectibles',
    'collectibles, political'		=> 'Political collectibles',
    'collectibles--popular culture'	=> 'Collectibles--Popular culture',
    'collision insurance, automobile'	=> 'Automobile insurance',
    'color in art'			=> 'Color in art',
    'colors in art'			=> 'Color in art',
    'comic book heroes'			=> 'Superheroes',
    'comic books'			=> 'Comic books, strips, etc',
    'comic books, strips, etc'		=> 'Comic books, strips, etc',
    'comic strips'			=> 'Comic books, strips, etc',
    'comics'				=> 'Comic books, strips, etc',
    'coming of age'			=> 'Coming of age',
    'command of troops'			=> 'Command of troops',
    'command of troops--case studies'	=> 'Command of troops--Case studies',
    'commerce'				=> 'Commerce',
    'commerce--history'			=> 'Commerce--History',
    'commerce--statistical methods'	=> 'Commercial statistics',
    'commerce--statistics'		=> 'Commercial statistics',
    'commercial accounting'		=> 'Accounting',
    'commercial education'		=> 'Business education',
    'commercial ethics'			=> 'Business ethics',
    'commercial goods'			=> 'Commercial products',
    'commercial law'			=> 'Commercial law',
    'commercial policy'			=> 'Commercial policy',
    'commercial products'		=> 'Commercial products',
    'commercial property'		=> 'Commercial real estate',
    'commercial real estate'		=> 'Commercial real estate',
    'commercial real property'		=> 'Commercial real estate',
    'commercial space (real estate)'	=> 'Commercial real estate',
    'commercial space'			=> 'Commercial real estate',
    'commercial speech'			=> 'Advertising',
    'commercial statistics'		=> 'Commercial statistics',
    'commercial trade'			=> 'Commerce',
    'commodities exchange'		=> 'Commodity exchanges',
    'commodities exchanges'		=> 'Commodity exchanges',
    'commodities'			=> 'Commercial products',
    'commodity exchanges'		=> 'Commodity exchanges',
    'commodity markets'			=> 'Commodity exchanges',
    'common business-oriented language'	=> 'COBOL (Computer program language)',
    'common shares'			=> 'Stocks',
    'common stocks'			=> 'Stocks',
    'communication and traffic'		=> 'Communication and traffic',
    'communication systems, computer'	=> 'Computer networks',
    'communication systems, wireless'	=> 'Wireless communication systems',
    'communication theory'		=> 'Information theory',
    'communication, administrative'	=> 'Business communication',
    'communication, business'		=> 'Business communication',
    'communication, industrial'		=> 'Business communication',
    'communications industries'		=> 'Communication and traffic',
    'community health'			=> 'Public health',
    'compact discs'			=> 'Compact discs',
    'compact disks'			=> 'Compact discs',
    'companies'				=> 'Business enterprises',
    'comparative economic systems'	=> 'Comparative economics',
    'comparative economics'		=> 'Comparative economics',
    'compatible computers, macintosh'	=> 'Macintosh-compatible computers',
    'competitive intelligence'		=> 'Business intelligence',
    'compilers (computer programs)'	=> 'Compilers (Computer programs)',
    'compilers'				=> 'Compilers (Computer programs)',
    'compiling programs'		=> 'Compilers (Computer programs)',
    'computer graphics'			=> 'Computer graphics',
    "computers--buyers' guides"		=> "Computers--Buyers' guides",
    'competition, international'	=> 'Competition, International',
    'comprehensive health planning'	=> 'Health planning',
    'computational linguistics'		=> 'Computational linguistics',
    'computer aided design'		=> 'Computer-aided design',
    'computer aided manufacturing systems' => 'CAD/CAM systems',
    'computer animation'		=> 'Computer animation',
    'computer architecture'		=> 'Computer architecture',
    'computer assisted design'		=> 'Computer-aided design',
    'computer assisted animation'	=> 'Computer animation',
    'computer assisted filmmaking'	=> 'Computer animation',
    'computer code, malicious'		=> 'Malware (Computer software)',
    'computer communication systems'	=> 'Computer networks',
    'computer crimes'			=> 'Computer crimes',
    'computer engineering'		=> 'Computer engineering',
    'computer game programming'		=> 'Computer games--Programming',
    'computer games'			=> 'Computer games',
    'computer games--programming'	=> 'Computer games--Programming',
    'computer hardware'			=> 'Computers',
    'computer history'			=> 'Computers--History',
    'computer industry'			=> 'Computer industry',
    'computer languages'		=> 'Programming languages (Electronic computers)',
    'computer literacy'			=> 'Computer literacy',
    'computer modeling'			=> 'Computer simulation',
    'computer models'			=> 'Computer simulation',
    'computer network protocols'	=> 'Computer network protocols',
    'computer network security'		=> 'Computer networks--Security measures',
    'computer networks'			=> 'Computer networks',
    'computer networks--security measures' => 'Computer networks--Security measures',
    'computer operating systems'	=> 'Operating systems (Computers)',
    'computer peripherals'		=> 'Computer peripherals',
    'computer privacy'			=> 'Computer security',
    'computer program languages'	=> 'Programming languages (Electronic computers)',
    'computer programming languages'	=> 'Programming languages (Electronic computers)',
    'computer programming'		=> 'Computer programming',
    'computer programming--algorithms'	=> 'Computer programming--Algorithms',
    'computer science'			=> 'Computer science',
    'computer security'			=> 'Computer security',
    'computer simulation'		=> 'Computer simulation',
    'computer software'			=> 'Computer software',
    'computer software--development'	=> 'Computer software--Development',
    'computer software--education'	=> 'Computer software--Education',
    'computer software--mathematics'	=> 'Computer software--Mathematics',
    'computer software--quality assurance' => 'Computer software--Quality assurance',
    'computer system security'		=> 'Computer security',
    'computer systems--security measures' => 'Computer security',
    'computer systems--security'	=> 'Computer security',
    'computer technicians'		=> 'Computer technicians',
    'computer technicians--certification--study guides' =>
      'Computer technicians--Certification--Study guides',
    'computer technologists'		=> 'Computer technicians',
    'computer vision'			=> 'Computer vision',
    'computer-aided design'		=> 'Computer-aided design',
    'computer-aided manufacturing systems' => 'CAD/CAM systems',
    'computer-assisted design'		=> 'Computer-aided design',
    'computer-assisted animation'	=> 'Computer animation',
    'computer-assisted filmmaking'	=> 'Computer animation',
    'computer-generated animation'	=> 'Computer animation',
    'computer-human interaction'	=> 'Human-computer interaction',
    'computers'				=> 'Computers',
    'computers, mechanical'		=> 'Calculators',
    'computers--administration'		=> 'Computers--Administration',
    'computers--design and construction' => 'Computer engineering',
    'computers--history'		=> 'Computers--History',
    'computers--operating systems'	=> 'Operating systems (Computers)',
    'computers--programming'		=> 'Computer programming',
    'computers--reference'		=> 'Computers--Reference',
    'computers--security measures'	=> 'Computer security',
    'computers--security'		=> 'Computer security',
    'computers--social aspects'		=> 'Computers--Social aspects',
    'computing history'			=> 'Computers--History',
    'computing machines'		=> 'Computers',
    'concatenation, file (statistics)'	=> 'Statistical matching',
    'concept art'			=> 'Conceptual art',
    'conceptual art'			=> 'Conceptual art',
    'condiments'			=> 'Condiments',
    'confectionary'			=> 'Confectionery',
    'confectionery'			=> 'Confectionery',
    'confections'			=> 'Confectionery',
    'confederate cooking'		=> 'Cooking, American--Southern style',
    'conflict control'			=> 'Conflict management',
    'conflict management'		=> 'Conflict management',
    'conflict resolution'		=> 'Conflict management',
    'consciousness'			=> 'Consciousness',
    'consciousness-expanding drugs'	=> 'Hallucinogenic drugs',
    'conservation of art objects'       => 'Art objects--Conservation and restoration',
    'consolidation and merger of corporations' => 'Consolidation and merger of corporations',
    'conspiracy'			=> 'Conspiracy',
    'constitutional rights'		=> 'Civil rights',
    'consumer advertising'		=> 'Advertising',
    'consumer behavior'			=> 'Consumer behavior',
    'consumer goods--marketing'		=> 'Marketing',
    'contract services'			=> 'Contracting out',
    'contracting for services'		=> 'Contracting out',
    'contracting out'			=> 'Contracting out',
    'contractions (topology)'		=> 'Contractions (Topology)',
    'controversial literature'		=> 'Controversial literature',
    'convenience cooking'		=> 'Quick and easy cooking',
    'cookery (appetizers)'		=> 'Appetizers',
    'cookery (baby foods)'		=> 'Baby foods',
    'cookery (beer)'			=> 'Cooking (Beer)',
    'cookery (bread)'			=> 'Cooking (Bread)',
    'cookery (chocolate)'		=> 'Cooking (Chocolate)',
    'cookery (coffee)'			=> 'Cooking (Coffee)',
    'cookery (entrées)'			=> 'Entrées (Cooking)',
    'cookery (garnishes)'		=> 'Garnishes (Cooking)',
    'cookery (herbs)'			=> 'Cooking (Herbs)',
    'cookery (liquors)'			=> 'Cooking (Liquors)',
    'cookery (spices)'			=> 'Cooking (Spices)',
    'cookery (tea)'			=> 'Cooking (Tea)',
    'cookery for one'			=> 'Cooking for one',
    'cookery'				=> 'Cooking',
    'cookery, african'			=> 'Cooking, African',
    'cookery, american'			=> 'Cooking, American',
    'cookery, asian'			=> 'Cooking, Asian',
    'cookery, cajan'			=> 'Cooking, Cajun',
    'cookery, cajun'			=> 'Cooking, Cajun',
    'cookery, canadian'			=> 'Cooking, Canadian',
    'cookery, caribbean'		=> 'Cooking, Caribbean',
    'cookery, chinese'			=> 'Cooking, Chinese',
    'cookery, english'			=> 'Cooking, English',
    'cookery, european'			=> 'Cooking, European',
    'cookery, french'			=> 'Cooking, French',
    'cookery, french-canadian'		=> 'Cooking, French-Canadian',
    'cookery, german'			=> 'Cooking, German',
    'cookery, greek'			=> 'Cooking, Greek',
    'cookery, latin american'		=> 'Cooking, Latin American',
    'cookery, oriental'			=> 'Cooking, Asian',
    'cookery, provencal'		=> 'Cooking, French--Provençal style',
    'cookery, provençal'		=> 'Cooking, French--Provençal style',
    'cookery, scottish'			=> 'Cooking, Scottish',
    'cookery, welsh'			=> 'Cooking, Welsh',
    'cookery, west indian'		=> 'Cooking, West Indian',
    'cookery--herbs'			=> 'Cooking (Herbs)',
    'cookery--history'			=> 'Cooking--History',
    'cookies (computer science)'	=> 'Cookies (Computer science)',
    'cookies'				=> 'Cookies',
    'cooking (appetizers)'		=> 'Appetizers',
    'cooking (beer)'			=> 'Cooking (Beer)',
    'cooking (bread)'			=> 'Cooking (Bread)',
    'cooking (chocolate)'		=> 'Cooking (Chocolate)',
    'cooking (coffee)'			=> 'Cooking (Coffee)',
    'cooking (entrées)'			=> 'Entrées (Cooking)',
    'cooking (garnishes)'		=> 'Garnishes (Cooking)',
    'cooking (herbs)'			=> 'Cooking (Herbs)',
    'cooking (liquors)'			=> 'Cooking (Liquors)',
    'cooking (spices)'			=> 'Cooking (Spices)',
    'cooking (spirits)'			=> 'Cooking (Liquors)',
    'cooking (spirits, alcoholic)'	=> 'Cooking (Liquors)',
    'cooking (tea)'			=> 'Cooking (Tea)',
    'cooking bread'			=> 'Cooking (Bread)',
    'cooking for one'			=> 'Cooking for one',
    'cooking for large numbers'		=> 'Quantity cooking',
    'cooking for many people'		=> 'Quantity cooking',
    'cooking history'			=> 'Cooking--History',
    'cooking utensils'			=> 'Kitchen utensils',
    'cooking with beer'			=> 'Cooking (Beer)',
    'cooking with bread'		=> 'Cooking (Bread)',
    'cooking with chocolate'		=> 'Cooking (Chocolate)',
    'cooking with coffee'		=> 'Cooking (Coffee)',
    'cooking with herbs'		=> 'Cooking (Herbs)',
    'cooking with liquors'		=> 'Cooking (Liquors)',
    'cooking with spices'		=> 'Cooking (Spices)',
    'cooking with tea'			=> 'Cooking (Tea)',
    'cooking'				=> 'Cooking',
    'cooking, african'			=> 'Cooking, African',
    'cooking, american'			=> 'Cooking, American',
    'cooking, american--california style'	=> 'Cooking, American--California style',
    'cooking, american--confederate style'	=> 'Cooking, American--Southern style',
    'cooking, american--middle atlantic states'	=> 'Cooking, American--Middle Atlantic States',
    'cooking, american--middle western style'	=> 'Cooking, American--Midwestern style',
    'cooking, american--midwestern style'	=> 'Cooking, American--Midwestern style',
    'cooking, american--new england style'	=> 'Cooking, American--New England style',
    'cooking, american--northwestern states'	=> 'Cooking, American--Northwestern States',
    'cooking, american--southern style'	=> 'Cooking, American--Southern style',
    'cooking, american--southwestern style'	=> 'Cooking, American--Southwestern style',
    'cooking, american--western style'	=> 'Cooking, American--Western style',
    'cooking, alsatian'			=> 'Cooking, French--Alsatian style',
    'cooking, asian'			=> 'Cooking, Asian',
    'cooking, auvergnat'		=> 'Cooking, French--Auvergne style',
    'cooking, auvergne'			=> 'Cooking, French--Auvergne style',
    'cooking, bavarian'			=> 'Cooking, German--Bavarian style',
    'cooking, breton'			=> 'Cooking, French--Brittany style',
    'cooking, brittany'			=> 'Cooking, French--Brittany style',
    'cooking, burgundy'			=> 'Cooking, French--Burgundy style',
    'cooking, cajan'			=> 'Cooking, Cajun',
    'cooking, cajun'			=> 'Cooking, Cajun',
    'cooking, california'		=> 'Cooking, American--California style',
    'cooking, canadian'			=> 'Cooking, Canadian',
    'cooking, caribbean'		=> 'Cooking, Caribbean',
    'cooking, central american and south american'	=> 'Cooking, Latin American',
    'cooking, central and south american'	=> 'Cooking, Latin American',
    'cooking, chinese'			=> 'Cooking, Chinese',
    'cooking, confederate style'	=> 'Cooking, American--Southern style',
    'cooking, confederate'		=> 'Cooking, American--Southern style',
    'cooking, cornish'			=> 'Cooking, English--Cornish style',
    'cooking, corsican'			=> 'Cooking, French--Corsican style',
    'cooking, creole'			=> 'Cooking, Creole',
    'cooking, cretan'			=> 'Cooking, Greek--Cretan style',
    'cooking, devon'			=> 'Cooking, English--Devon style',
    'cooking, devonshire'		=> 'Cooking, English--Devon style',
    'cooking, english'			=> 'Cooking, English',
    'cooking, english--cornish style'	=> 'Cooking, English--Cornish style',
    'cooking, english--devon style'	=> 'Cooking, English--Devon style',
    'cooking, european'			=> 'Cooking, European',
    'cooking, french'			=> 'Cooking, French',
    'cooking, french--alsatian style'	=> 'Cooking, French--Alsatian style',
    'cooking, french--auvergne style'	=> 'Cooking, French--Auvergne style',
    'cooking, french--brittany style'	=> 'Cooking, French--Brittany style',
    'cooking, french--burgundy style'	=> 'Cooking, French--Burgundy style',
    'cooking, french--corsican style'	=> 'Cooking, French--Corsican style',
    'cooking, french--gascony style'	=> 'Cooking, French--Gascony style',
    'cooking, french--lorraine style'	=> 'Cooking, French--Lorraine style',
    'cooking, french--normandy style'	=> 'Cooking, French--Normandy style',
    'cooking, french--provencal style'	=> 'Cooking, French--Provençal style',
    'cooking, french--provençal style'	=> 'Cooking, French--Provençal style',
    'cooking, french-canadian'		=> 'Cooking, French-Canadian',
    'cooking, gascon'			=> 'Cooking, French--Gascony style',
    'cooking, gascony'			=> 'Cooking, French--Gascony style',
    'cooking, german'			=> 'Cooking, German',
    'cooking, german--bavarian style'	=> 'Cooking, German--Bavarian style',
    'cooking, german--schleswig-holstein style'	=> 'Cooking, German--Schleswig-Holstein style',
    'cooking, german--southern style'	=> 'Cooking, German--Southern style',
    'cooking, german--westphalian style'	=> 'Cooking, German--Westphalian style',
    'cooking, greek'			=> 'Cooking, Greek',
    'cooking, greek--cretan style'	=> 'Cooking, Greek--Cretan style',
    'cooking, latin american'		=> 'Cooking, Latin American',
    'cooking, lorraine'			=> 'Cooking, French--Lorraine style',
    'cooking, microwave'		=> 'Microwave cooking',
    'cooking, midwestern style'		=> 'Cooking, American--Midwestern style',
    'cooking, midwestern'		=> 'Cooking, American--Midwestern style',
    'cooking, new england style'	=> 'Cooking, American--New England style',
    'cooking, new england'		=> 'Cooking, American--New England style',
    'cooking, norman'			=> 'Cooking, French--Normandy style',
    'cooking, normandy'			=> 'Cooking, French--Normandy style',
    'cooking, northwestern states'	=> 'Cooking, American--Northwestern States',
    'cooking, oriental'			=> 'Cooking, Asian',
    'cooking, provencal'		=> 'Cooking, French--Provençal style',
    'cooking, provençal'		=> 'Cooking, French--Provençal style',
    'cooking, schleswig-holstein'	=> 'Cooking, German--Schleswig-Holstein style',
    'cooking, scottish'			=> 'Cooking, Scottish',
    'cooking, southern (united states)'	=> 'Cooking, American--Southern style',
    'cooking, southern (germany)'	=> 'Cooking, German--Southern style',
    'cooking, southwestern (united states)'	=> 'Cooking, American--Southwestern style',
    'cooking, welsh'			=> 'Cooking, Welsh',
    'cooking, west indian'		=> 'Cooking, West Indian',
    'cooking, western (united states)'	=> 'Cooking, American--Western style',
    'cooking, westphalian'		=> 'Cooking, German--Westphalian style',
    'cooking--herbs'			=> 'Cooking (Herbs)',
    'cooking--history'			=> 'Cooking--History',
    'cornish cooking'			=> 'Cooking, English--Cornish style',
    'corporate acquisitions'		=> 'Consolidation and merger of corporations',
    'corporate buyouts'			=> 'Consolidation and merger of corporations',
    'corporate coaching'		=> 'Executive coaching',
    'corporate espionage'		=> 'Business intelligence',
    'corporate ethics'			=> 'Business ethics',
    'corporate finance'			=> 'Corporations--Finance',
    'corporate financial management'	=> 'Corporations--Finance',
    'corporate governance'		=> 'Corporate governance',
    'corporate income tax'		=> 'Corporations--Taxation',
    'corporate income taxes'		=> 'Corporations--Taxation',
    'corporate intelligence'		=> 'Business intelligence',
    'corporate management'		=> 'Industrial management',
    'corporate mergers'			=> 'Consolidation and merger of corporations',
    'corporate power'			=> 'Corporate power',
    'corporate takeovers'		=> 'Consolidation and merger of corporations',
    'corporate tax'			=> 'Corporations--Taxation',
    'corporate taxes'			=> 'Corporations--Taxation',
    'corporation ethics'		=> 'Business ethics',
    'corporation finance'		=> 'Corporations--Finance',
    'corporation income tax'		=> 'Corporations--Taxation',
    'corporation income taxes'		=> 'Corporations--Taxation',
    'corporation tax'			=> 'Corporations--Taxation',
    'corporation taxes'			=> 'Corporations--Taxation',
    'corporations'			=> 'Corporations',
    'corporations, federal'		=> 'Corporations, Government',
    'corporations, government'		=> 'Corporations, Government',
    'corporations, international'	=> 'International business enterprises',
    'corporations, nonprofit'		=> 'Nonprofit organizations',
    'corporations, religious'		=> 'Corporations, Religious',
    'corporations--consolidation'	=> 'Consolidation and merger of corporations',
    'corporations--finance'		=> 'Corporations--Finance',
    'corporations--management'		=> 'Industrial management',
    'corporations--mergers'		=> 'Consolidation and merger of corporations',
    'corporations--taxation'		=> 'Corporations--Taxation',
    'corpulence'			=> 'Obesity',
    'correspondence'			=> 'Letters',
    'corrupt practices'			=> 'Corruption',
    'corruption'			=> 'Corruption',
    'corsican cooking'			=> 'Cooking, French--Corsican style',
    'county parks'			=> 'Parks',
    'courting'				=> 'Courtship',
    'courtship'				=> 'Courtship',
    'courtship--fiction'		=> 'Courtship--Fiction',
    'coverage, insurance'		=> 'Insurance',
    'creole cooking'			=> 'Cooking, Creole',
    'cretan cooking'			=> 'Cooking, Greek--Cretan style',
    'criminals'				=> 'Criminals',
    'criminals--biography'		=> 'Criminals--Biography',
    'criticism'				=> 'Criticism',
    'crockery'				=> 'Pottery',
    'crockett, david, 1786-1836'	=> 'Crockett, Davy, 1786-1836',
    'crockett, davy, 1786-1836'		=> 'Crockett, Davy, 1786-1836',
    'cruelty to children'		=> 'Child abuse',
    'cryptanalysis'			=> 'Cryptography',
    'cryptogram stories'		=> 'Code and cipher stories',
    'cryptography'			=> 'Cryptography',
    'cryptography--fiction'		=> 'Code and cipher stories',
    'cryptography--juvenile fiction'	=> 'Code and cipher stories',
    'cryptology'			=> 'Cryptography',
    'crystals'				=> 'Crystals',
    'cuisine'				=> 'Cooking',
    'culture and politics'		=> 'Politics and culture',
    'culture conflict'			=> 'Culture conflict',
    'culture, popular'			=> 'Popular culture',
    'culture--political aspects'	=> 'Politics and culture',
    'currency exchange'			=> 'Foreign exchange',
    'currency'				=> 'Money',
    'curricula vitae'			=> 'Résumés (Employment)',
    'customer relations'		=> 'Customer relations',
    'cyber casting'			=> 'Webcasting',
    'cyber commerce'			=> 'Electronic commerce',
    'cyber crime'			=> 'Computer crimes',
    'cybercasting'			=> 'Webcasting',
    'cybercommerce'			=> 'Electronic commerce',
    'cybercrime'			=> 'Computer crimes',
    'cybernetics'			=> 'Cybernetics',
    'cyborg'				=> 'Cyborgs',
    'cyborgs'				=> 'Cyborgs',
    'c♯ (computer program language)'	=> 'C# (Computer program language)',
    'daniels, kate (fictitious character)'	=> 'Daniels, Kate (Fictitious character)',
    'daniels, kate--fiction'		=> 'Daniels, Kate (Fictitious character)',
    'data banks'			=> 'Databases',
    'data bases'			=> 'Databases',
    'data matching (statistics)'	=> 'Statistical matching',
    'data matching'			=> 'Statistical matching',
    'data merging (statistics)'		=> 'Statistical matching',
    'data merging'			=> 'Statistical matching',
    'data mining'			=> 'Data mining',
    'data networks'			=> 'Computer networks',
    'data networks, computer'		=> 'Computer networks',
    'data processing equipment'		=> 'Computers',
    'data processing'			=> 'Data processing',
    'data communication systems'	=> 'Data transmission systems',
    'data transmission systems'		=> 'Data transmission systems',
    'data transmission systems, broadband' => 'Broadband communication systems',
    'data transmission systems, wideband' => 'Broadband communication systems',
    'data transmission systems, wireless' => 'Wireless communication systems',
    'data warehousing'			=> 'Data warehousing',
    'databanks'				=> 'Databases',
    'database systems'			=> 'Databases',
    'databases'				=> 'Databases',
    'datamining'			=> 'Data mining',
    'death (personification)'		=> 'Death (Personification)',
    'death penalty'			=> 'Capital punishment',
    'death sentence'			=> 'Capital punishment',
    'death'				=> 'Death',
    'death--philosophy'			=> 'Death',
    'debentures'			=> 'Bonds',
    'debian (computer system)'		=> 'Debian (Computer system)',
    'debian'				=> 'Debian (Computer system)',
    'deciding'				=> 'Decision making',
    'decision (psychology)'		=> 'Decision making',
    'decision analysis'			=> 'Decision making',
    'decision making'			=> 'Decision making',
    'decision processes'		=> 'Decision making',
    'decoration of food'		=> 'Garnishes (Cooking)',
    'decoration, interior'		=> 'Interior decoration',
    'decoy ships'			=> 'Q-ships',
    'decriminalization of illegal drugs' => 'Drug legalization',
    'delivery of health care'		=> 'Medical care',
    'delivery of medical care'		=> 'Medical care',
    'depository institutions'		=> 'Banks and banking',
    'deprivation, sensory'		=> 'Sensory deprivation',
    'deprivation, sleep'		=> 'Sleep deprivation',
    'design of logic systems'		=> 'Logic design',
    'design, logic'			=> 'Logic design',
    'desk calculators'			=> 'Calculators',
    'desserts'				=> 'Desserts',
    'destitution'			=> 'Poverty',
    'devaluation of currency'		=> 'Monetary policy',
    'development of application software' => 'Application software--Development',
    'development of web sites'		=> 'Web site development',
    'development stage businesses'	=> 'New business enterprises',
    'development stage enterprises'	=> 'New business enterprises',
    'development, business'		=> 'Business--Development',
    'development, economic'		=> 'Economic development',
    'development, sustainable'		=> 'Sustainable development',
    'devon cooking'			=> 'Cooking, English--Devon style',
    'devonshire cooking'		=> 'Cooking, English--Devon style',
    'dickering'				=> 'Negotiation',
    'diet'				=> 'Diet',
    'diets'				=> 'Reducing diets',
    'digital art'			=> 'Digital art',
    'digital compact discs'		=> 'Compact discs',
    'digital compact disks'		=> 'Compact discs',
    'digital media'			=> 'Digital media',
    'digital publishing'		=> 'Electronic publishing',
    'dimensions'			=> 'Dimensions',
    'dip systems'			=> 'Document imaging systems',
    'direct mail campaigns'		=> 'Direct marketing',
    'direct marketing'			=> 'Direct marketing',
    'discoverers'			=> 'Explorers',
    'disc world (imaginary place)'	=> 'Discworld (Imaginary place)',
    'disc world'			=> 'Discworld (Imaginary place)',
    'discs, compact'			=> 'Compact discs',
    'discs, optical'			=> 'Optical disks',
    'discworld (imaginary place)'	=> 'Discworld (Imaginary place)',
    'discworld'				=> 'Discworld (Imaginary place)',
    'disk operating systems'		=> 'Operating systems (Computers)',
    'disks, compact'			=> 'Compact discs',
    'disks, optical'			=> 'Optical disks',
    'dispute settlement'		=> 'Conflict management',
    'distilled beverages'		=> 'Liquors',
    'distributed computer systems'	=> 'Electronic data processing--Distributed processing',
    'distributed computing systems'	=> 'Electronic data processing--Distributed processing',
    'distributed computing'		=> 'Electronic data processing--Distributed processing',
    'distributed processing'		=> 'Electronic data processing--Distributed processing',
    'distribution (economic theory)'	=> 'Distribution (Economic theory)',
    'distributorship, multilevel'	=> 'Multilevel marketing',
    'divination'			=> 'Divination',
    'divine healing'			=> 'Spiritual healing',
    'divine messengers'			=> 'Angels',
    'djin'				=> 'Jinn',
    'djinn'				=> 'Jinn',
    'djinni'				=> 'Jinn',
    'djinns'				=> 'Jinn',
    'djins'				=> 'Jinn',
    'depuy, william e. (william eugene), 1919-1992'
      => 'DePuy, William E. (William Eugene), 1919-1992',
    'doctrinal and controversial works'	=> 'Controversial literature',
    'document image processing'		=> 'Document imaging systems',
    'document imaging systems'		=> 'Document imaging systems',
    'document management systems'	=> 'Document imaging systems',
    'document management'		=> 'Document imaging systems',
    'doffing'				=> 'Textile industry',
    'dolls'				=> 'Dolls',
    'domestic fiction'			=> 'Domestic fiction',
    'domestic marketing'		=> 'Marketing',
    'domestic novels'			=> 'Domestic fiction',
    'domestic rabbit'			=> 'Rabbits',
    'domestic rabbits'			=> 'Rabbits',
    'double entry bookkeeping'		=> 'Bookkeeping',
    'drawing technique'			=> 'Drawing',
    'drawing'				=> 'Drawing',
    'drawing--technique'		=> 'Drawing',
    'dreaming'				=> 'Dreams',
    'dreams'				=> 'Dreams',
    'drinking water'			=> 'Drinking water',
    'drinks'				=> 'Beverages',
    'drug decriminalization'		=> 'Drug legalization',
    'drug legalization'			=> 'Drug legalization',
    'drugs'				=> 'Drugs',
    'dying'				=> 'Death',
    'e-business'			=> 'Electronic commerce',
    'e-commerce'			=> 'Electronic commerce',
    'e-mail correspondence'		=> 'Electronic mail messages',
    'e-mail messages'			=> 'Electronic mail messages',
    'e-mail software'			=> 'Electronic mail systems',
    'e-mail systems'			=> 'Electronic mail systems',
    'e-mail'				=> 'Electronic mail messages',
    'e-tailing'				=> 'Electronic commerce',
    'earthenware'			=> 'Pottery',
    'easy and quick cooking'		=> 'Quick and easy cooking',
    'ebusiness'				=> 'Electronic commerce',
    'ecclesiastical corporations'	=> 'Corporations, Religious',
    'ecological monitoring'		=> 'Environmental monitoring',
    'ecologically sustainable development' => 'Sustainable development',
    'ecommerce'				=> 'Electronic commerce',
    'econometrics'			=> 'Econometrics',
    'economic conditions'		=> 'Economic conditions',
    'economic development'		=> 'Economic development',
    'economic development, sustainable' => 'Sustainable development',
    'economic espionage'		=> 'Business intelligence',
    'economic goods'			=> 'Commercial products',
    'economic growth'			=> 'Economic development',
    # Works on the history of economics as a discipline are entered under Economics--History
    'economic history'			=> 'Economic history',
    'economic infrastructure'		=> 'Infrastructure (Economics)',
    'economic statistics'		=> 'Economic statistics',
    'economic sustainability'		=> 'Sustainable development',
    'economic theory'			=> 'Economics',
    'economics of cities'		=> 'Urban economics',
    'economics'				=> 'Economics',
    'economics, comparative'		=> 'Comparative economics',
    'economics--environmental aspects'	=> 'Environmental economics',
    'economics--history'		=> 'Economics--History',
    'edi (electronic data interchange)' => 'Electronic data interchange',
    'edi'				=> 'Electronic data interchange',
    'edinburgh (scotland)'		=> 'Edinburgh (Scotland)',
    'edinburgh, scotland'		=> 'Edinburgh (Scotland)',
    'education software'		=> 'Computer software--Education',
    'education'				=> 'Education',
    'education, business'		=> 'Business education',
    'educational software'		=> 'Computer software--Education',
    # Note that 'Educators' is different from 'Teachers'
    'educators'				=> 'Educators',
    'election monitoring'		=> 'Election monitoring',
    'election observation'		=> 'Election monitoring',
    'elections'				=> 'Elections',
    'electoral politics'		=> 'Elections',
    'electronic banking'		=> 'Internet banking',
    'electronic brains'			=> 'Artificial intelligence',
    'electronic business data interchange' => 'Electronic data interchange',
    'electronic business'		=> 'Electronic commerce',
    'electronic calculating machines'	=> 'Computers',
    'electronic calculating-machines'	=> 'Computers',
    'electronic commerce'		=> 'Electronic commerce',
    'electronic communication networks'	=> 'Computer networks',
    'electronic computer programming'	=> 'Computer programming',
    'electronic computers'		=> 'Computers',
    'electronic data interchange'	=> 'Electronic data interchange',
    'electronic data processing'	=> 'Data processing',
    'electronic data processing--distributed processing' => 'Electronic data processing--Distributed processing',
    'electronic data processing--programming'	=> 'Computer programming',
    'electronic document imaging systems' => 'Document imaging systems',
    'electronic document management systems' => 'Document imaging systems',
    'electronic information services'	=> 'Online information services',
    'electronic mail messages'		=> 'Electronic mail messages',
    'electronic mail systems'		=> 'Electronic mail systems',
    'electronic marketing'		=> 'Telemarketing',
    'electronic media'			=> 'Digital media',
    'electronic publishing'		=> 'Electronic publishing',
    'electronic social networks'	=> 'Online social networks',
    'electronic spread sheets'		=> 'Electronic spreadsheets',
    'electronic spreadsheet software'	=> 'Electronic spreadsheets--Software',
    'electronic spreadsheets'		=> 'Electronic spreadsheets',
    'electronic spreadsheets--software' => 'Electronic spreadsheets--Software',
    'electronic trading'		=> 'Electronic trading of securities',
    'electronic trading of securities'	=> 'Electronic trading of securities',
    'email correspondence'		=> 'Electronic mail messages',
    'email messages'			=> 'Electronic mail messages',
    'email software'			=> 'Electronic mail systems',
    'email systems'			=> 'Electronic mail systems',
    'email'				=> 'Electronic mail messages',
    'employment management'		=> 'Personnel management',
    'enchanters'			=> 'Wizards',
    'end of life'			=> 'Death',
    'energetic healing'			=> 'Energy medicine',
    'energy healing'			=> 'Energy medicine',
    'energy industries'			=> 'Energy industries',
    'energy medicine'			=> 'Energy medicine',
    'engeland'				=> 'England',
    'engineering design'	        => 'Engineering design',
    'engineering--authorship'		=> 'Technical writing',
    'engines, search'			=> 'Search engines',
    'england'				=> 'England',
    'england--fiction'			=> 'England--Fiction',
    'england--social life and customs'	=> 'England--Social life and customs',
    'england--social life and customs--19th century--fiction' => 'England--Social life and customs--19th century--Fiction',
    'english biscuits'			=> 'Cookies',
    'english cooking'			=> 'Cooking, English',
    'english customs'			=> 'England--Social life and customs',
    'english fiction'			=> 'English fiction',
    'english grammar'			=> 'English language--Grammar',
    'english language'			=> 'English language',
    'english language--grammar'		=> 'English language--Grammar',
    'english literature'		=> 'English literature',
    'enochian magic'			=> 'Enochian magic',
    'enochian magick'			=> 'Enochian magic',
    'enslavement'			=> 'Slavery',
    'enterprises'			=> 'Business enterprises',
    'enterprises, business'		=> 'Business enterprises',
    'entertaining'			=> 'Entertaining',
    # 'Entertainment industry' is not an official LC subject, but is submitted as one
    'entertainment industry'		=> 'Entertainment industry',
    'entheogens'			=> 'Hallucinogenic drugs',
    'entrées (cookery)'			=> 'Entrées (Cooking)',
    'entrées (cooking)'			=> 'Entrées (Cooking)',
    'entrepreneur'			=> 'Entrepreneurship',
    'entrepreneurs, women'		=> 'Businesswomen',
    'entrepreneurship'			=> 'Entrepreneurship',
    'environment, work'			=> 'Work environment',
    'environmental economics'		=> 'Environmental economics',
    'environmental monitoring'		=> 'Environmental monitoring',
    'environmental quality'		=> 'Environmental quality',
    'environmental quality--economic aspects' => 'Environmental economics',
    'environmental quality--measurement' => 'Environmental monitoring',
    'environmental quality--monitoring' => 'Environmental monitoring',
    'environmentalists'			=> 'Environmentalists',
    'environmentalists--biography'	=> 'Environmentalists--Biography',
    'environments, virtual'		=> 'Virtual reality',
    'equipment, capital'		=> 'Industrial equipment',
    'equipment, industrial'		=> 'Industrial equipment',
    'equities'				=> 'Stocks',
    'equity capital'			=> 'Stocks',
    'equity financing'			=> 'Stocks',
    'erotic art'			=> 'Erotic art',
    'erotic fiction'			=> 'Erotic stories',
    'erotic stories'			=> 'Erotic stories',
    'erotic'				=> 'Erotic stories',
    'erotica'				=> 'Erotic stories',
    'esd (ecologically sustainable development)' => 'Sustainable development',
    'esme weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'esme weatherwax'			=> 'Weatherwax, Granny (Fictitious character)',
    'esmeralda weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'esmeralda weatherwax'		=> 'Weatherwax, Granny (Fictitious character)',
    'esp'				=> 'Extrasensory perception',
    'espionage, business'		=> 'Business intelligence',
    'espionage, corporate'		=> 'Business intelligence',
    'espionage, economic'		=> 'Business intelligence',
    'espionage, industrial'		=> 'Business intelligence',
    'eternal life'			=> 'Immortality',
    'etheric world intelligences'	=> 'Guides (Spiritualism)',
    'ethics in government'		=> 'Political ethics',
    'ethics'				=> 'Ethics',
    'ethics, political'			=> 'Political ethics',
    'ethics, primitive'			=> 'Ethics',
    'ethology'				=> 'Ethics',
    'etiquette'				=> 'Etiquette',
    'european art'			=> 'Art, European',
    'european cooking'			=> 'Cooking, European',
    'evidence control'			=> 'Evidence preservation',
    'evidence preservation'		=> 'Evidence preservation',
    'evidence, criminal--preservation'	=> 'Evidence preservation',
    'ex-convicts'			=> 'Ex-convicts',
    'exchange, foreign'			=> 'Foreign exchange',
    'exchanges, commodity'		=> 'Commodity exchanges',
    'exchanges, produce'		=> 'Commodity exchanges',
    'excursions'			=> 'Tourism',
    'executive coaching'		=> 'Executive coaching',
    'executive information systems'	=> 'Management information systems',
    'executive power'			=> 'Executive power',
    'executive power--united states'	=> 'Executive power--United States',
    'exercise'				=> 'Physical fitness',
    'expert systems (computer science)'	=> 'Expert systems (Computer science)',
    'expert systems'			=> 'Expert systems (Computer science)',
    'explorers'				=> 'Explorers',
    'explorers--biography'		=> 'Explorers--Biography',
    'export marketing'			=> 'Export marketing',
    'exports & imports'			=> 'International trade',
    'exports and imports'		=> 'International trade',
    'extendible markup language'	=> 'XML (Document markup language)',
    'extensible markup language'	=> 'XML (Document markup language)',
    'external trade'			=> 'International trade',
    'extranets (computer networks)'	=> 'Extranets (Computer networks)',
    'extranets'				=> 'Extranets (Computer networks)',
    'extrasensory perception'		=> 'Extrasensory perception',
    'facilities management'		=> 'Facility management',
    'facility management'		=> 'Facility management',
    'factual data analysis'		=> 'Data mining',
    'faerie'				=> 'Fairies',
    'fairie'				=> 'Fairies',
    'fairies'				=> 'Fairies',
    'fairy tales'			=> 'Fairy tales',
    'fairy'				=> 'Fairies',
    'faith healing'			=> 'Spiritual healing',
    'faith-cure'			=> 'Spiritual healing',
    'family budgets'			=> 'Budgets, Personal',
    'family'				=> 'Family',
    'fantastic fiction'			=> 'Fantasy fiction',
    'fantasy fiction'			=> 'Fantasy fiction',
    'fantasy fiction--authorship'	=> 'Fantasy fiction--Authorship',
    'fantasy fiction--technique'	=> 'Fantasy fiction--Technique',
    'fantasy'				=> 'Fantasy fiction',
    'fat-free diet'			=> 'Low-fat diet',
    'fatness'				=> 'Obesity',
    'fear of being alone'		=> 'Agoraphobia',
    'fear of isolation'			=> 'Agoraphobia',
    'fear of open space'		=> 'Agoraphobia',
    'fear of open spaces'		=> 'Agoraphobia',
    'federal corporation tax'		=> 'Corporations--Taxation',
    'feng shui'				=> 'Feng shui',
    'festive cooking'			=> 'Holiday cooking',
    'fiction action adventure'		=> 'Adventure stories',
    'fiction'				=> 'Fiction',
    'fiction, juvenile'			=> 'Fiction, Juvenile',
    'fiction, paranormal'		=> 'Paranormal fiction',
    'fiction--fantasy'			=> 'Fantasy fiction',
    'fiction--paranormal'		=> 'Paranormal fiction',
    'fiction--science fiction'		=> 'Science fiction',
    'fictitious places'			=> 'Imaginary places',
    'figurines'				=> 'Figurines',
    'file concatenation (statistics)'	=> 'Statistical matching',
    'finance'				=> 'Finance',
    'finance, personal'			=> 'Finance, Personal',
    'finance, personal--planning'	=> 'Finance, Personal',
    'finance, personal--software'	=> 'Finance, Personal--Software',
    'finance, public'			=> 'Finance, Public',
    'financial analysis of corporations' => 'Corporations--Finance',
    'financial management of corporations' => 'Corporations--Finance',
    'financial management, corporate'	=> 'Corporations--Finance',
    'financial management, personal'	=> 'Finance, Personal',
    'financial planning of corporations' => 'Corporations--Finance',
    'financial planning, personal'	=> 'Finance, Personal',
    'financial risk management'		=> 'Financial risk management',
    'financial services industry'	=> 'Financial services industry',
    'financial services'		=> 'Financial services industry',
    'firearms'				=> 'Firearms',
    'firms'				=> 'Business enterprises',
    # 'First contact' is not directly a LC heading, but submitted as a recommendation
    'first contact'			=> 'First contact',
    'fitness, physical'			=> 'Physical fitness',
    'flasks'				=> 'Bottles',
    'flatboats'				=> 'Flatboats',
    'flight'				=> 'Flight',
    'flying saucers'			=> 'Unidentified flying objects',
    'flying-machines'			=> 'Flying-machines',
    'folk art'				=> 'Folk art',
    'food allergy'			=> 'Food allergy',
    'food calories'			=> 'Food--Caloric content',
    'food decoration'			=> 'Garnishes (Cooking)',
    'food industry and trade'		=> 'Food industry and trade',
    'food preparation industry'		=> 'Food industry and trade',
    'food preparation'			=> 'Cooking',
    'food processing industry'		=> 'Food industry and trade',
    'food processing'			=> 'Food industry and trade',
    'food trade'			=> 'Food industry and trade',
    'food, baby'			=> 'Baby foods',
    'food, raw'				=> 'Raw foods',
    'food--caloric content'		=> 'Food--Caloric content',
    'food--health aspects',		=> 'Nutrition',
    'foods, baby'			=> 'Baby foods',
    'foods, infant'			=> 'Baby foods',
    'foods, raw'			=> 'Raw foods',
    'forecasting, business'		=> 'Business forecasting',
    'foreign commerce'			=> 'International trade',
    'foreign exchange'			=> 'Foreign exchange',
    'foreign languages'			=> 'Language and languages',
    'foreign trade'			=> 'International trade',
    'foreign trade policy'		=> 'Commercial policy',
    'forex'				=> 'Foreign exchange',
    'fortran (computer program language)' => 'FORTRAN (Computer program language)',
    'fortune-telling'			=> 'Fortune-telling',
    'fortunetelling'			=> 'Fortune-telling',
    'franchises (retail trade)'		=> 'Franchises (Retail trade)',
    'franchises, retail'		=> 'Franchises (Retail trade)',
    'franchises, taxation of'		=> 'Corporations--Taxation',
    'franchises, taxation'		=> 'Corporations--Taxation',
    'franchises--taxation'		=> 'Corporations--Taxation',
    'free enterprise'			=> 'Free enterprise',
    'free markets'			=> 'Free enterprise',
    'french cooking'			=> 'Cooking, French',
    'french-canadian cooking'		=> 'Cooking, French-Canadian',
    'fundamental rights'		=> 'Civil rights',
    'funding'				=> 'Finance',
    'funnies'				=> 'Comic books, strips, etc',
    'furniture'				=> 'Furniture',
    'fusion of corporations'		=> 'Consolidation and merger of corporations',
    'fusion, data (statistics)'		=> 'Statistical matching',
    'fusion, data'			=> 'Statistical matching',
    # 'Future life' is a separate LC Subject meaning 'life after
    # death', not 'life in the future', but it is so confusing that it
    # just gets folded into 'Immortality' in this code.
    'future life'			=> 'Immortality',
    # 'Futures' is futures trading, not potential future events
    'futures contracts'			=> 'Futures',
    'futures trading'			=> 'Futures',
    'futures'				=> 'Futures',
    'fx (finance)'			=> 'Foreign exchange',
    'gaap (accounting)'			=> 'Accounting--Standards',
    'gaap'				=> 'Accounting--Standards',
    'gadgets'				=> 'Implements, utensils, etc',
    'game programming'			=> 'Computer games--Programming',
    'garnish, harry (fictitious character)' => 'Garnish, Harry (Fictitious character)',
    'garnishes (cookery)'		=> 'Garnishes (Cooking)',
    'garnishes (cooking)'		=> 'Garnishes (Cooking)',
    'garnishes'				=> 'Garnishes (Cooking)',
    'garnishing (cookery)'		=> 'Garnishes (Cooking)',
    'garnishing (cooking)'		=> 'Garnishes (Cooking)',
    'garnishing'			=> 'Garnishes (Cooking)',
    'gascon cooking'			=> 'Cooking, French--Gascony style',
    'gascony cooking'			=> 'Cooking, French--Gascony style',
    'gasoline automobiles'		=> 'Automobiles',
    'generally accepted accounting principles' => 'Accounting--Standards',
    'generals--united states'		=> 'Generals--United States',
    'genies'				=> 'Jinn',
    'genii'				=> 'Jinn',
    'german cooking'			=> 'Cooking, German',
    'ghost stories'			=> 'Ghost stories',
    'ghosts'				=> 'Ghost stories',
    'glass'				=> 'Glass',
    'glassman, josh (fictitious character)' => 'Glassman, Josh (Fictitious character)',
    'glassman, josh'			=> 'Glassman, Josh (Fictitious character)',
    'glassware'				=> 'Glassware',
    'global cities'			=> 'Cities and towns',
    'global commerce'			=> 'International trade',
    'global corporations'		=> 'International business enterprises',
    'global terrorism'			=> 'Terrorism',
    'global trade'			=> 'International trade',
    'gluten-free foods'			=> 'Gluten-free foods',
    'goal programming'			=> 'Programming (Mathematics)',
    'goblins'				=> 'Goblins',
    'goddess religion'			=> 'Goddess religion',
    'government'			=> 'Government',
    'government companies'		=> 'Corporations, Government',
    'government corporations'		=> 'Corporations, Government',
    'government ethics'			=> 'Political ethics',
    'government, resistance to'		=> 'Government, Resistance to',
    'government, resistance to--united states' => 'Government, Resistance to--United States',
    'graffiti'				=> 'Graffiti',
    'graffiti artists'			=> 'Graffiti artists',
    'graffiti culture'			=> 'Graffiti',
    'graphic data processing'		=> 'Computer graphics',
    'graphics, computer'		=> 'Computer graphics',
    'granny weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'granny weatherwax'			=> 'Weatherwax, Granny (Fictitious character)',
    'greek cooking'			=> 'Cooking, Greek',
    'greek mythology'			=> 'Mythology, Greek',
    'green business'			=> 'Business enterprises--Environmental aspects',
    'grief'				=> 'Grief',
    'grilling (cooking)'		=> 'Barbecuing',
    'grim reaper'			=> 'Death (Personification)',
    'growing up'			=> 'Coming of age',
    'guests, entertaining'		=> 'Entertaining',
    'guides (spiritualism)'		=> 'Guides (Spiritualism)',
    'guides, spirit'			=> 'Guides (Spiritualism)',
    'gunning'				=> 'Shooting',
    'guns'				=> 'Firearms',
    'haggling'				=> 'Negotiation',
    'hares'				=> 'Rabbits',
    'harry garnish (fictitious character)' => 'Garnish, Harry (Fictitious character)',
    'higgling'				=> 'Negotiation',
    'hallucinations and illusions'	=> 'Hallucinations and illusions',
    'hallucinogenic drugs'		=> 'Hallucinogenic drugs',
    'hallucinogens'			=> 'Hallucinogenic drugs',
    'hardware, computer'		=> 'Computers',
    'heads of government'		=> 'Heads of state',
    'heads of state'			=> 'Heads of state',
    'heads of state--biography'		=> 'Heads of state--Biography',
    'healing'				=> 'Healing',
    'health & healing'			=> 'Healing',
    'health & fitness'			=> 'Physical fitness',
    'health and fitness'		=> 'Physical fitness',
    'health care delivery'		=> 'Medical care',
    'health care planning'		=> 'Health planning',
    'health care'			=> 'Medical care',
    'health informatics'		=> 'Medical informatics',
    'health insurance'			=> 'Health insurance',
    'health planning'			=> 'Health planning',
    'health plans'			=> 'Health planning',
    'health plans, prepaid'		=> 'Health insurance',
    'health services planning'		=> 'Health planning',
    'health services'			=> 'Medical care',
    'health thoughts'			=> 'Mental healing',
    'healthcare'			=> 'Medical care',
    'heart--diseases--nutritional aspects'	=> 'Heart--Diseases--Nutritional aspects',
    'heavy weapons'			=> 'Ordnance',
    'herbal cooking'			=> 'Cooking (Herbs)',
    'herbs'				=> 'Herbs',
    'herbs--use in cooking'		=> 'Cooking (Herbs)',
    'hermeticism'			=> 'Hermetism',
    'hermetism'				=> 'Hermetism',
    'high tech'				=> 'High technology',
    'high technology'			=> 'High technology',
    'hispanic american art'		=> 'Hispanic American art',
    'historical fiction'		=> 'Historical fiction',
    'historical fiction, chinese'	=> 'Historical fiction, Chinese',
    'history of cooking'		=> 'Cooking--History',
    'history'				=> 'History',
    'history--20th century'		=> 'History--20th century',
    'history--cooking'			=> 'Cooking--History',
    'history, economic'			=> 'Economic history',
    'history, military'			=> 'History, Military',
    'holiday cookery'			=> 'Holiday cooking',
    'holiday cooking'			=> 'Holiday cooking',
    'holiday industry'			=> 'Tourism',
    'home businesses'			=> 'Home-based businesses',
    'home buying'			=> 'House buying',
    'home computers'			=> 'Microcomputers',
    'home decoration'			=> 'Interior decoration',
    'home purchase'			=> 'House buying',
    'home purchasing'			=> 'House buying',
    'home selling'			=> 'House selling',
    'home-based businesses'		=> 'Home-based businesses',
    'homosexuality'			=> 'Homosexuality',
    'horoscopes'			=> 'Horoscopes',
    "hors d'oeuvres"			=> 'Appetizers',
    'hostile takeovers of corporations'	=> 'Consolidation and merger of corporations',
    'hostile takeovers'			=> 'Consolidation and merger of corporations',
    'hot dish cooking'			=> 'Casserole cooking',
    'hotdish cooking'			=> 'Casserole cooking',
    'house buying'			=> 'House buying',
    'house decoration'			=> 'Interior decoration',
    'house guests, entertaining'	=> 'Entertaining',
    'house hunting'			=> 'House buying',
    'house selling'			=> 'House selling',
    'houseguests, entertaining'		=> 'Entertaining',
    'household utensils'		=> 'Implements, utensils, etc',
    'how to start a business'		=> 'New business enterprises',
    'how to start businesses'		=> 'New business enterprises',
    'horror'				=> 'Horror',
    'html (document markup language)'	=> 'HTML (Document markup language)',
    'html'				=> 'HTML (Document markup language)',
    'http cookies (computer science)'	=> 'Cookies (Computer science)',
    'http cookies'			=> 'Cookies (Computer science)',
    # 'Human beings in art' is for representations of humans in art
    'human beings in art'		=> 'Human beings in art',
    'human body in art'			=> 'Human figure in art',
    'human body--care and hygiene'	=> 'Hygiene',
    'human factors in computing systems' => 'Human-computer interaction',
    'human females'			=> 'Women',
    # 'Human figure in art' is for art technique, not representations
    'human figure in art'		=> 'Human figure in art',
    'human resource management'		=> 'Personnel management',
    'human resources management'	=> 'Personnel management',
    'human rights'			=> 'Human rights',
    'human sexuality'			=> 'Sex',
    'human-computer interaction'	=> 'Human-computer interaction',
    'humans in art'			=> 'Human beings in art',
    'humanx'		 		=> 'Humanx Commonwealth (Imaginary organization)',
    'humanx commonwealth' 		=> 'Humanx Commonwealth (Imaginary organization)',
    'humanx commonwealth (imaginary organization)'
      => 'Humanx Commonwealth (Imaginary organization)',
    'humanx commonwealth (imaginary organization)--fiction'
      => 'Humanx Commonwealth (Imaginary organization)',
    'humor'				=> 'Humor',
    'humor adult humor and comedy'	=> 'Humor',
    'humour'				=> 'Humor',
    'hygiene'				=> 'Hygiene',
    'hygiene, public'			=> 'Public health',
    'hygiene, social'			=> 'Public health',
    'hypertext markup language'		=> 'HTML (Document markup language)',
    'icebreakers (ships)'		=> 'Icebreakers (Ships)',
    'illiteracy'			=> 'Literacy',
    'illusions'				=> 'Hallucinations and illusions',
    'illusions, optical'		=> 'Optical illusions',
    'image processing'			=> 'Image processing',
    'imaginary cities'			=> 'Imaginary places',
    'imaginary islands'			=> 'Imaginary places',
    'imaginary places'			=> 'Imaginary places',
    # 'Immortalism' refers to living indefinitely in the flesh
    'immortalism'			=> 'Immortalism',
    # 'Immortality' refers to survival of the soul after death
    'immortality'			=> 'Immortality',
    'implements, utensils, etc'		=> 'Implements, utensils, etc',
    'imports & exports'			=> 'International trade',
    'imports and exports'		=> 'International trade',
    'imputation, mass (statistics)'	=> 'Statistical matching',
    'income tax'			=> 'Income tax',
    'indemnity insurance'		=> 'Insurance',
    'industrial administration'		=> 'Industrial management',
    'industrial communication'		=> 'Business communication',
    'industrial equipment'		=> 'Industrial equipment',
    'industrial equipment--marketing'	=> 'Industrial marketing',
    'industrial espionage'		=> 'Business intelligence',
    'industrial management'		=> 'Industrial management',
    'industrial marketing'		=> 'Industrial marketing',
    'industrial policy'			=> 'Industrial policy',
    'industrial production'		=> 'Industries',
    'industrial project management'	=> 'Project management',
    'industries'			=> 'Industries',
    'industries--equipment and supplies' => 'Industrial equipment',
    'industries--government policy'	=> 'Industrial policy',
    'industries--public relations'	=> 'Public relations',
    'industry and state'		=> 'Industrial policy',
    'industry'				=> 'Industries',
    'infancy'				=> 'Infants',
    'infant foods'			=> 'Baby foods',
    'infants'				=> 'Infants',
    'infants--food'			=> 'Baby foods',
    'inflation (finance)'		=> 'Inflation (Finance)',
    'inflation'				=> 'Inflation (Finance)',
    'informatics'			=> 'Computer science',
    'information resources management'	=> 'Information resources management',
    'information systems management'	=> 'Information resources management',
    'information technology'		=> 'Information technology',
    'information theory'		=> 'Information theory',
    'information warehousing'		=> 'Data warehousing',
    'infrastructure (economics)'	=> 'Infrastructure (Economics)',
    'inghilterra'			=> 'England',
    'inglaterra'			=> 'England',
    'ink drawing'			=> 'Pen drawing',
    'inspiration'			=> 'Inspiration',
    'inspiration--religious aspects'	=> 'Inspiration--Religious aspects',
    'instruction'			=> 'Education',
    'instructors'			=> 'Teachers',
    'instruments, musical'		=> 'Musical instruments',
    'insurance coverage'		=> 'Insurance',
    'insurance industry'		=> 'Insurance',
    'insurance protection'		=> 'Insurance',
    'insurance risk'			=> 'Risk (Insurance)',
    'insurance'				=> 'Insurance',
    'insurance, automobile'		=> 'Automobile insurance',
    'insurance, casualty'		=> 'Casualty insurance',
    'insurance, health'			=> 'Health insurance',
    'insurance, liability'		=> 'Liability insurance',
    'insurance, life'			=> 'Life insurance',
    'insurance, property'		=> 'Property insurance',
    'insurance--risk'			=> 'Risk (Insurance)',
    'intellectronics'			=> 'Artificial intelligence',
    'intellectual capital'		=> 'Intellectual capital',
    'intelligence, artificial'		=> 'Artificial intelligence',
    'intelligence, business'		=> 'Business intelligence',
    'intelligence, corporate'		=> 'Business intelligence',
    'intelligences, etheric world'	=> 'Guides (Spiritualism)',
    'intelligent machines'		=> 'Artificial intelligence',
    'interaction, human-computer'	=> 'Human-computer interaction',
    'interactive computer systems'	=> 'Interactive computer systems',
    'intercourse, sexual'		=> 'Sex',
    'interest and usury'		=> 'Interest',
    'interest'				=> 'Interest',
    'interfaces, user (computer systems)' => 'User interfaces (Computer systems)',
    'interfaces, user'			=> 'User interfaces (Computer systems)',
    'interior decoration'		=> 'Interior decoration',
    'interior design'			=> 'Interior decoration',
    'internal internets (computer networks)' => 'Intranets (Computer networks)',
    'internal internets'		=> 'Intranets (Computer networks)',
    'international accounting'		=> 'International business enterprises--Accounting',
    'international business enterprises' => 'International business enterprises',
    'international business enterprises--accounting' =>
      'International business enterprises--Accounting',
    'international business enterprises--taxation' =>
      'International business enterprises--Taxation',
    'international competition'		=> 'Competition, International',
    'international corporations'	=> 'International business enterprises',
    'international economics'		=> 'Competition, International',
    'international exchange'		=> 'Foreign exchange',
    'international marketing'		=> 'Export marketing',
    'international taxation'		=> 'International business enterprises--Taxation',
    'international taxes'		=> 'International business enterprises--Taxation',
    'international terrorism'		=> 'Terrorism',
    'international trade policy'	=> 'Commercial policy',
    'international trade'		=> 'International trade',
    'internet advertising'		=> 'Internet advertising',
    'internet application development'	=> 'Application software--Internet--Development',
    'internet application software'	=> 'Application software--Internet',
    'internet applications'		=> 'Application software--Internet',
    'internet apps'			=> 'Application software--Internet',
    'internet auctions'			=> 'Internet auctions',
    'internet banking'			=> 'Internet banking',
    'internet bookstores'		=> 'Internet bookstores',
    'internet broadcasting'		=> 'Webcasting',
    'internet browsers'			=> 'Browsers (Computer programs)',
    'internet commerce'			=> 'Electronic commerce',
    'internet drugstores'		=> 'Internet pharmacies',
    'internet marketing'		=> 'Internet advertising',
    'internet pharmacies'		=> 'Internet pharmacies',
    'internet retailing'		=> 'Electronic commerce',
    'internet'				=> 'Internet',
    'internet--safety measures'		=> 'Internet--Safety measures',
    'internship programs'		=> 'Internship programs',
    'internships'			=> 'Internship programs',
    'intranets (computer networks)'	=> 'Intranets (Computer networks)',
    'intranets'				=> 'Intranets (Computer networks)',
    'intrapreneur'			=> 'Entrepreneurship',
    'investing'				=> 'Investments',
    'investment companies'		=> 'Mutual funds',
    'investment in real estate'		=> 'Real estate investment',
    'investment management'		=> 'Investments',
    'investment options'		=> 'Options (Finance)',
    'investment trusts'			=> 'Mutual funds',
    'investments'			=> 'Investments',
    'irrationalism (philosophy)'	=> 'Irrationalism (Philosophy)',
    'irrationalism'			=> 'Irrationalism (Philosophy)',
    'irrationalist (philosophy)'	=> 'Irrationalism (Philosophy)',
    'irrationalist'			=> 'Irrationalism (Philosophy)',
    'islands, imaginary'		=> 'Imaginary places',
    'isolation'				=> 'Social isolation',
    'isolation, fear of'		=> 'Agoraphobia',
    'isolation, perceptual'		=> 'Sensory deprivation',
    'it (information technology)'	=> 'Information technology',
    'jackrabbits'			=> 'Rabbits',
    'jane austen'			=> 'Austen, Jane, 1775-1817',
    'jane austen, 1775-1817'		=> 'Austen, Jane, 1775-1817',
    'java (computer program language)'	=> 'Java (Computer program language)',
    'javascript (computer program language)' => 'JavaScript (Computer program language)',
    'javascript'			=> 'JavaScript (Computer program language)',
    'jewellery'				=> 'Jewelry',
    'jewelry'				=> 'Jewelry',
    'jewels'				=> 'Jewelry',
    'jinn'				=> 'Jinn',
    'jinni'				=> 'Jinn',
    'jinns'				=> 'Jinn',
    'job hunting'			=> 'Job hunting',
    'job résumés'			=> 'Résumés (Employment)',
    'job searching'			=> 'Job hunting',
    'joblessness'			=> 'Unemployment',
    'jobs'				=> 'Occupations',
    'josh glassman'			=> 'Glassman, Josh (Fictitious character)',
    'kate daniels (fictitious character)' => 'Daniels, Kate (Fictitious character)',
    'kate daniels'			=> 'Daniels, Kate (Fictitious character)',
    'kdd (information retrieval)'	=> 'Data mining',
    'kdd'				=> 'Data mining',
    'ketogenic diet'			=> 'Ketogenic diet',
    'keyboarding'			=> 'Keyboarding',
    'kids'				=> 'Children',
    'kitchen utensils'			=> 'Kitchen utensils',
    'kitchenware'			=> 'Kitchen utensils',
    'kittens'			        => 'Kittens',
    'knowledge capital'			=> 'Intellectual capital',
    'knowledge discovery in data'	=> 'Data mining',
    'knowledge discovery in databases'	=> 'Data mining',
    'knowledge-based systems'		=> 'Expert systems (Computer science)',
    'korean war'			=> 'Korean War, 1950-1953',
    'korean war, 1950-1953'		=> 'Korean War, 1950-1953',
    'labor and laboring classes'	=> 'Labor',
    'labor'				=> 'Labor',
    'laissez faire'			=> 'Free enterprise',
    'laissez-faire'			=> 'Free enterprise',
    'latin american art'		=> 'Art, Latin American',
    'landscapes'			=> 'Landscapes',
    'landscapes in art'			=> 'Landscapes in art',
    'language and languages'		=> 'Language and languages',
    'language and languages--data processing' => 'Computational linguistics',
    'language art'			=> 'Conceptual art',
    'language art (fine arts)'		=> 'Conceptual art',
    'language data processing'		=> 'Computational linguistics',
    'languages'				=> 'Language and languages',
    'lans (computer networks)'		=> 'Local area networks (Computer networks)',
    'lans'				=> 'Local area networks (Computer networks)',
    'laser discs'			=> 'Optical disks',
    'laser disks'			=> 'Optical disks',
    'laserdiscs'			=> 'Optical disks',
    'laserdisks'			=> 'Optical disks',
    'later life (human life cycle)'	=> 'Old age',
    'latin american cooking'		=> 'Cooking, Latin American',
    'law'				=> 'Law',
    'law, business'			=> 'Commercial law',
    'law, commercial'			=> 'Commercial law',
    'laws (statutes)'			=> 'Statutes',
    'leadership coaching'		=> 'Executive coaching',
    'leadership'			=> 'Leadership',
    'legalization of illegal drugs'	=> 'Drug legalization',
    'legislative acts'			=> 'Statutes',
    'legislative enactments'		=> 'Statutes',
    'letter writing'			=> 'Letter writing',
    'letters'				=> 'Letters',
    'liability insurance'		=> 'Liability insurance',
    'liability insurance, automobile'	=> 'Automobile insurance',
    'life'				=> 'Life',
    'life after death'			=> 'Immortality',
    'life drawing'			=> 'Human figure in art',
    'life insurance'			=> 'Life insurance',
    'life on other planets'		=> 'Life on other planets',
    'linguistic science'		=> 'Linguistics',
    'linguistics'			=> 'Linguistics',
    'linguistics--data processing'	=> 'Computational linguistics',
    'liquors'				=> 'Liquors',
    'liquors--use in cooking'		=> 'Cooking (Liquors)',
    'lisp (computer program language)'	=> 'LISP (Computer program language)',
    'list processing computer language'	=> 'LISP (Computer program language)',
    'listed options'			=> 'Options (Finance)',
    'literacy'				=> 'Literacy',
    'literacy, computer'		=> 'Computer literacy',
    'literary criticism'		=> 'Criticism',
    'literature/poetry'			=> 'Poetry',
    'local area computer networks'	=> 'Local area networks (Computer networks)',
    'local area networks (computer networks)'	=> 'Local area networks (Computer networks)',
    'local area networks'		=> 'Local area networks (Computer networks)',
    'logic design'			=> 'Logic design',
    'london'				=> 'City of London (England)',
    'london, england'			=> 'City of London (England)',
    'look-ism'				=> 'Physical-appearance-based bias',
    'lookism'				=> 'Physical-appearance-based bias',
    'looks-ism'				=> 'Physical-appearance-based bias',
    'looksism'				=> 'Physical-appearance-based bias',
    'lorraine cooking'			=> 'Cooking, French--Lorraine style',
    'losing weight'			=> 'Weight loss',
    'loss of weight'			=> 'Weight loss',
    'love stories'			=> 'Love stories',
    'lovemaking'			=> 'Sex',
    'low salt diet'			=> 'Salt-free diet',
    'low sodium diet'			=> 'Salt-free diet',
    'low-carb diet'			=> 'Low-carbohydrate diet',
    'low-carbohydrate diet'		=> 'Low-carbohydrate diet',
    'low-cholesterol diet'		=> 'Low-cholesterol diet',
    'low-fat diet'			=> 'Low-fat diet',
    'low-salt diet'			=> 'Salt-free diet',
    'low-sodium diet'			=> 'Salt-free diet',
    'machine intelligence'		=> 'Artificial intelligence',
    'machine systems, virtual'		=> 'Virtual computer systems',
    'machine theory'			=> 'Machine theory',
    'machine vision'			=> 'Computer vision',
    'mac clones'			=> 'Macintosh-compatible computers',
    'mac computers'			=> 'Macintosh-compatible computers',
    'machine language'			=> 'Programming languages (Electronic computers)',
    'machine languages'			=> 'Programming languages (Electronic computers)',
    'macintosh clones'			=> 'Macintosh-compatible computers',
    'macintosh compatibles (computers)' => 'Macintosh-compatible computers',
    'macintosh compatibles'		=> 'Macintosh-compatible computers',
    'macintosh-compatible computers'	=> 'Macintosh-compatible computers',
    'macintosh computers'		=> 'Macintosh-compatible computers',
    'macroeconomics'			=> 'Macroeconomics',
    'magazines'				=> 'Periodicals',
    'magic cookies (computer science)'	=> 'Cookies (Computer science)',
    'magic'				=> 'Magic',
    'magic--fiction'			=> 'Magic',
    'magicians'				=> 'Magicians',
    'magicians--fiction'		=> 'Magicians--Fiction',
    'mail-order business'		=> 'Mail-order business',
    'mail-order houses'			=> 'Mail-order business',
    'main courses (cooking)'		=> 'Entrées (Cooking)',
    'main dishes (cooking)'		=> 'Entrées (Cooking)',
    'making bread'			=> 'Cooking (Bread)',
    'making decisions'			=> 'Decision making',
    'malevolent software'		=> 'Malware (Computer software)',
    'malicious computer code'		=> 'Malware (Computer software)',
    'malicious software'		=> 'Malware (Computer software)',
    'malignancy (cancer)'		=> 'Cancer',
    'malignant tumors'			=> 'Cancer',
    'maltreatment of children'		=> 'Child abuse',
    'malware (computer software)'	=> 'Malware (Computer software)',
    'malware'				=> 'Malware (Computer software)',
    'management coaching'		=> 'Executive coaching',
    'management decisions'		=> 'Decision making',
    'management information systems'	=> 'Management information systems',
    'management of computer memory'	=> 'Memory management (Computer science)',
    'management of conflict'		=> 'Conflict management',
    'management science'		=> 'Management science',
    'management'			=> 'Management',
    'management, industrial'		=> 'Industrial management',
    'management, sales'			=> 'Sales management',
    'managing conflict'			=> 'Conflict management',
    'manga'				=> 'Comic books, strips, etc',
    'manhua'				=> 'Comic books, strips, etc',
    'manhwa'				=> 'Comic books, strips, etc',
    'manpower utilization'		=> 'Personnel management',
    'manufacturing industries'		=> 'Manufacturing industries',
    'manufacturing management'		=> 'Production management',
    'market research'			=> 'Marketing research',
    'marketing research'		=> 'Marketing research',
    'marketing'				=> 'Marketing',
    'marketing, multilevel'		=> 'Multilevel marketing',
    'marketing--research'		=> 'Marketing research',
    'markets'				=> 'Markets',
    'markets, free'			=> 'Free enterprise',
    'markets--research'			=> 'Marketing research',
    'marksmanship'			=> 'Shooting',
    'marshall, george c. (george catlett), 1880-1959'
      => 'Marshall, George C. (George Catlett), 1880-1959',
    'mass communication'		=> 'Communication and traffic',
    'mass culture'			=> 'Popular culture',
    'mass imputation'			=> 'Statistical matching',
    'massacres'				=> 'Massacres',
    'massacres--vietnam'		=> 'Massacres--Vietnam',
    'matching, data (statistics)'	=> 'Statistical matching',
    'matching, statistical'		=> 'Statistical matching',
    'math'				=> 'Mathematics',
    'mathematical machine theory'	=> 'Machine theory',
    'mathematical machines'		=> 'Calculators',
    'mathematical programming'		=> 'Programming (Mathematics)',
    'mathematics'			=> 'Mathematics',
    'mechanical computers'		=> 'Calculators',
    'medical and health care industry'	=> 'Medical care',
    'medical and healthcare industry'	=> 'Medical care',
    'medical care planning'		=> 'Health planning',
    'medical care'			=> 'Medical care',
    'medical care, prepaid'		=> 'Health insurance',
    'medical care--planning'		=> 'Health planning',
    'medical care--social aspects'	=> 'Social medicine',
    'medical informatics'		=> 'Medical informatics',
    'medical information science'	=> 'Medical informatics',
    'medical insurance'			=> 'Health insurance',
    'medical profession'		=> 'Medicine',
    'medical services'			=> 'Medical care',
    'medical sociology'			=> 'Social medicine',
    'medical'				=> 'Medical care',
    'medicaments'			=> 'Drugs',
    'medications'			=> 'Drugs',
    'medications'			=> 'Drugs',
    'medicine (drugs)'			=> 'Drugs',
    'medicine'				=> 'Medicine',
    'medicine, social'			=> 'Social medicine',
    'medicine--social aspects'		=> 'Social medicine',
    'medicines (drugs)'			=> 'Drugs',
    'medicines'				=> 'Drugs',
    'meditation'			=> 'Meditation',
    'medium-sized business'		=> 'Small business',
    'medium-sized businesses'		=> 'Small business',
    'memorabilia'			=> 'Collectibles',
    'memory management (computer science)' => 'Memory management (Computer science)',
    'memory management'			=> 'Memory management (Computer science)',
    'mental healing'			=> 'Mental healing',
    'mental prayer'			=> 'Meditation',
    'mental telepathy'			=> 'Telepathy',
    'mercantile law'			=> 'Commercial law',
    'merchandise'			=> 'Commercial products',
    'merger of corporations'		=> 'Consolidation and merger of corporations',
    'mergers & acquisitions'		=> 'Consolidation and merger of corporations',
    'mergers and acquisitions'		=> 'Consolidation and merger of corporations',
    'mergers and acquisitions of corporations' => 'Consolidation and merger of corporations',
    'mergers of corporations'		=> 'Consolidation and merger of corporations',
    'mergers, corporate'		=> 'Consolidation and merger of corporations',
    'merging, data (Statistics)'	=> 'Statistical matching',
    'merging, data'			=> 'Statistical matching',
    'metamorphosis--folklore'		=> 'Shapeshifting',
    'micro computers'			=> 'Microcomputers',
    'micro-businesses'			=> 'Small business',
    'micro-enterprises'			=> 'Small business',
    'microbusinesses'			=> 'Small business',
    'microcomputers'			=> 'Microcomputers',
    'microeconomics'			=> 'Microeconomics',
    'microenterprises'			=> 'Small business',
    'microprocessors'			=> 'Microprocessors',
    'micros (microcomputers)'		=> 'Microcomputers',
    'microsimulation modeling (statistics)' => 'Statistical matching',
    'microwave cookery'			=> 'Microwave cooking',
    'microwave cooking'			=> 'Microwave cooking',
    'mid-atlantic states'		=> 'Middle Atlantic States',
    'middle atlantic region'		=> 'Middle Atlantic States',
    'middle atlantic states'		=> 'Middle Atlantic States',
    'middle colonies'			=> 'Middle Atlantic States',
    'middle states'			=> 'Middle Atlantic States',
    'middle-atlantic states'		=> 'Middle Atlantic States',
    'midwestern cooking'		=> 'Cooking, American--Midwestern style',
    'military biography'		=> 'Military biography',
    'military history'			=> 'History, Military',
    'mind reading'			=> 'Telepathy',
    'mind-cure'				=> 'Mental healing',
    'mind-distorting drugs'		=> 'Hallucinogenic drugs',
    'mind-reading'			=> 'Telepathy',
    'mindreading'			=> 'Telepathy',
    'mining data'			=> 'Data mining',
    'mining, data'			=> 'Data mining',
    'mixed media (art)'			=> 'Mixed media (Art)',
    'mixed media crafts'		=> 'Mixed media (Art)',
    'mixology'				=> 'Bartending',
    'mobile computing'			=> 'Mobile computing',
    'modeling, computer'		=> 'Computer simulation',
    'modeling, microsimulation (statistics)' => 'Statistical matching',
    'models, computer'			=> 'Computer simulation',
    'modems'				=> 'Modems',
    'modern & contemporary fiction (post c 1945)' => 'Fiction--1945-',
    'modern art'			=> 'Art, Modern',
    'modula-2 (computer program language)' => 'Modula-2 (Computer program language)',
    'modula-2'				=> 'Modula-2 (Computer program language)',
    'molesting children'		=> 'Child sexual abuse',
    'molesting of children'		=> 'Child sexual abuse',
    'monetary management'		=> 'Monetary policy',
    'monetary policy'			=> 'Monetary policy',
    'money'				=> 'Money',
    'monitoring, election'		=> 'Election monitoring',
    'mongolians'			=> 'Mongols',
    'mongols'				=> 'Mongols',
    'mongols--fiction'			=> 'Mongols--Fiction',
    'mongols--history'			=> 'Mongols--History',
    'monitoring, environmental'		=> 'Environmental monitoring',
    'moral philosophy'			=> 'Ethics',
    'morality'				=> 'Ethics',
    'morals'				=> 'Ethics',
    'mortgages'				=> 'Mortgages',
    'motivational speakers'		=> 'Motivational speakers',
    'motorcars (automobiles)'		=> 'Automobiles',
    'motorcars'				=> 'Automobiles',
    'mount sneffels wilderness (colo.)' => 'Mount Sneffels Wilderness (Colo.)',
    'mourning'				=> 'Grief',
    'mui tsai'				=> 'Slavery',
    'multilevel distributorship'	=> 'Multilevel marketing',
    'multilevel marketing'		=> 'Multilevel marketing',
    'multilevel sales companies'	=> 'Multilevel marketing',
    'multinational corporations'	=> 'International business enterprises',
    'multinational enterprises'		=> 'International business enterprises',
    'municipalities'			=> 'Cities and towns',
    'murder'				=> 'Murder',
    'murder mystery'			=> 'Mystery and detective stories',
    'museology'				=> 'Museum techniques',
    'museum administration'		=> 'Museum techniques',
    'museum techniques'			=> 'Museum techniques',
    'museums'				=> 'Museums',
    'museums--technique'		=> 'Museum techniques',
    'musical instruments'		=> 'Musical instruments',
    'mutual funds'			=> 'Mutual funds',
    'mutual insurance'			=> 'Insurance',
    'mystery and detective stories'	=> 'Mystery and detective stories',
    'mystery'				=> 'Mystery and detective stories',
    'mystical theology'			=> 'Mysticism',
    'mysticism'				=> 'Mysticism',
    'mythology, greek'			=> 'Mythology, Greek',
    'national resources'		=> 'Natural resources',
    'natural language processing'	=> 'Computational linguistics',
    'natural resources'			=> 'Natural resources',
    'natural science'			=> 'Science',
    'naval tactics'			=> 'Naval tactics',
    'navigators'			=> 'Explorers',
    'near-death experiences'		=> 'Near-death experiences',
    'negotiating'			=> 'Negotiation',
    'negotiation'			=> 'Negotiation',
    'negotiations'			=> 'Negotiation',
    'neglect of children'		=> 'Child abuse',
    'negro art'				=> 'African American art',
    'netcasting'			=> 'Webcasting',
    'nets, neural (computer science)'	=> 'Neural networks (Computer science)',
    'nets, neural (neurobiology)'	=> 'Neural networks (Neurobiology)',
    'network marketing'			=> 'Multilevel marketing',
    'network security'			=> 'Computer networks--Security measures',
    'network security, computer'	=> 'Computer networks--Security measures',
    'networks, computer'		=> 'Computer networks',
    'networks, neural (computer science)' => 'Neural networks (Computer science)',
    'networks, neural (neurobiology)'	=> 'Neural networks (Neurobiology)',
    'neural nets (computer science)'	=> 'Neural networks (Computer science)',
    'neural nets (neurobiology)'	=> 'Neural networks (Neurobiology)',
    'neural networks (computer science)' => 'Neural networks (Computer science)',
    'neural networks (neurobiology)'	=> 'Neural networks (Neurobiology)',
    'new business enterprises'		=> 'New business enterprises',
    'new companies'			=> 'New business enterprises',
    'new england cooking'		=> 'Cooking, American--New England style',
    'new thought'			=> 'New Thought',
    'newspapers'			=> 'Newspapers',
    'non-alcoholic beverages'		=> 'Non-alcoholic beverages',
    'non-alcoholic drinks'		=> 'Non-alcoholic beverages',
    'non-profit organizations'		=> 'Nonprofit organizations',
    'non-profit sector'			=> 'Nonprofit organizations',
    'non-profits'			=> 'Nonprofit organizations',
    'non-resistance to government'	=> 'Government, Resistance to',
    'nonprofit organizations'		=> 'Nonprofit organizations',
    'nonprofit sector'			=> 'Nonprofit organizations',
    'nonprofits'			=> 'Nonprofit organizations',
    'norman cooking'			=> 'Cooking, French--Normandy style',
    'normandy cooking'			=> 'Cooking, French--Normandy style',
    'northwestern states'		=> 'Northwestern States',
    'northwestern united states'	=> 'Northwestern States',
    'not-for-profit organizations'	=> 'Nonprofit organizations',
    'npos'				=> 'Nonprofit organizations',
    'numerology'			=> 'Numerology',
    'nutrition',			=> 'Nutrition',
    'nutrition--health aspects',	=> 'Nutrition',
    'oacet (imaginary organization)'	=> 'OACET (Imaginary organization)',
    'oacet'		 		=> 'OACET (Imaginary organization)',
    'obe (parapsychology)'		=> 'Astral projection',
    'obesity'				=> 'Obesity',
    'obesity--control'			=> 'Weight loss',
    'obesity--etiology'			=> 'Obesity--Etiology',
    'object-oriented programming (computer science)' => 'Object-oriented programming (Computer science)',
    'object-oriented programming'	=> 'Object-oriented programming (Computer science)',
    'objective c'			=> 'Objective-C (Computer program language)',
    'objective-c (Computer program language)'	=> 'Objective-C (Computer program language)',
    'objective-c'			=> 'Objective-C (Computer program language)',
    'objects, art'			=> 'Art objects',
    "objets d'art"			=> 'Art objects',
    'occultism'				=> 'Occultism',
    'occupations'			=> 'Occupations',
    'office administration'		=> 'Office management',
    'office automation'			=> 'Office practice--Automation',
    'office equipment and supplies'	=> 'Office equipment and supplies',
    'office equipment'			=> 'Office equipment and supplies',
    'office etiquette'			=> 'Business etiquette',
    'office machines'			=> 'Office equipment and supplies',
    'office management'			=> 'Office management',
    'office practice--automation'	=> 'Office practice--Automation',
    'office products'			=> 'Office equipment and supplies',
    'office supplies'			=> 'Office equipment and supplies',
    'office systems'			=> 'Office equipment and supplies',
    'oil painting'			=> 'Painting',
    'old age'				=> 'Old age',
    'on-line information services'	=> 'Online information services',
    'online auctions'			=> 'Internet auctions',
    'online commerce'			=> 'Electronic commerce',
    'online information services'	=> 'Online information services',
    'online investing'			=> 'Electronic trading of securities',
    'online publishing'			=> 'Electronic publishing',
    'online services (information services)'	=> 'Online information services',
    'online social networks'		=> 'Online social networks',
    'online trading'			=> 'Electronic trading of securities',
    'online trading of securities'	=> 'Electronic trading of securities',
    'open spaces'			=> 'Open spaces',
    'open spaces, fear of'		=> 'Agoraphobia',
    'open-end mutual funds'		=> 'Mutual funds',
    'operational analysis'		=> 'Operations research',
    'operational research'		=> 'Operations research',
    'operations management'		=> 'Production management',
    'operations research'		=> 'Operations research',
    'operating systems (computers)'	=> 'Operating systems (Computers)',
    'operating systems'			=> 'Operating systems (Computers)',
    'operators, tour (industry)'	=> 'Tourism',
    'operators, tour'			=> 'Tourism',
    'optical computing'			=> 'Optical data processing',
    'optical data processing'		=> 'Optical data processing',
    'optical discs'			=> 'Optical disks',
    'optical disks'			=> 'Optical disks',
    'optical illusions'			=> 'Optical illusions',
    'options (finance)'			=> 'Options (Finance)',
    'options exchange'			=> 'Options (Finance)',
    'options market'			=> 'Options (Finance)',
    'options trading'			=> 'Options (Finance)',
    'ordnance'				=> 'Ordnance',
    'organization development'		=> 'Organizational change',
    'organizational behavior'		=> 'Organizational behavior',
    'organizational change'		=> 'Organizational change',
    'organizational development'	=> 'Organizational change',
    'organizational innovation'		=> 'Organizational change',
    'organizations, business'		=> 'Business enterprises',
    'organizations, nonprofit'		=> 'Nonprofit organizations',
    'oriental art'			=> 'Art, Asian',
    'oriental cooking'			=> 'Cooking, Asian',
    'out-of-body experiences'		=> 'Astral projection',
    'outdoor cookery'			=> 'Outdoor cooking',
    'outdoor cooking'			=> 'Outdoor cooking',
    'outsourcing'			=> 'Contracting out',
    'overseas marketing'		=> 'Export marketing',
    'ownership of slaves'		=> 'Slavery',
    'pad computers'			=> 'Tablet computers',
    'pages, web'			=> 'Web sites',
    'painting'				=> 'Painting',
    'painting, primitive'		=> 'Painting',
    'painting--technique'		=> 'Painting--Technique',
    'paintings'				=> 'Painting',
    'palm reading'			=> 'Palmistry',
    'palmistry'				=> 'Palmistry',
    'palmreading'			=> 'Palmistry',
    'papooses'				=> 'Infants',
    'parallel programming (computer science)' => 'Parallel programming (Computer science)',
    'parallel programming'		=> 'Parallel programming (Computer science)',
    'paranormal'			=> 'Paranormal fiction',
    'paranormal fiction'		=> 'Paranormal fiction',
    'paranormal phenomena'		=> 'Parapsychology',
    'paraphernalia, political'		=> 'Political collectibles',
    'parapsychology'			=> 'Parapsychology',
    'park administration'		=> 'Parks--Management',
    'park management'			=> 'Parks--Management',
    'parklands'				=> 'Parks',
    'parks'				=> 'Parks',
    'parks--management'			=> 'Parks--Management',
    'parody'				=> 'Parody',
    'pascal (computer program language)' => 'Pascal (Computer program language)',
    'pastel drawing'			=> 'Pastel drawing',
    'pastel drawing--technique'		=> 'Pastel drawing--Technique',
    'pastel painting'			=> 'Pastel drawing',
    'pastels'				=> 'Pastel drawing',
    'pastry'				=> 'Pastry',
    'pathfinder (game)'			=> 'Pathfinder (Game)',
    'pathfinder game'			=> 'Pathfinder (Game)',
    'pathfinder novel'			=> 'Pathfinder (Game)',
    'pathfinder roleplaying game'	=> 'Pathfinder (Game)',
    'pathfinder story'			=> 'Pathfinder (Game)',
    'pathologically eclectic rubbish lister'	=> 'Perl (Computer program language)',
    'pattern classification systems'	=> 'Pattern recognition systems',
    'pattern recognition computers'	=> 'Pattern recognition systems',
    'pattern recognition systems'	=> 'Pattern recognition systems',
    'pcs (microcomputers)'		=> 'Microcomputers',
    'pcs'				=> 'Microcomputers',
    'peasant art'			=> 'Folk art',
    'pedagogy'				=> 'Education',
    'pedology (child study)'		=> 'Children',
    'pen drawing'			=> 'Pen drawing',
    'pen drawing--technique'		=> 'Pen drawing--Technique',
    'pen and ink drawing'		=> 'Pen drawing',
    'pencil drawing'			=> 'Pencil drawing',
    'pencil drawing--technique'		=> 'Pencil drawing--Technique',
    'peng, rachel (fictitious character)' => 'Peng, Rachel (Fictitious character)',
    'peng, rachel'			=> 'Peng, Rachel (Fictitious character)',
    'perceptual isolation'		=> 'Sensory deprivation',
    'performance art'			=> 'Performance art',
    'performance motivation'		=> 'Achievement motivation',
    'performance pieces'		=> 'Performance art',
    'periodicals'			=> 'Periodicals',
    'peripheral computer devices'	=> 'Computer peripherals',
    'peripheral computer equipment'	=> 'Computer peripherals',
    'peripheral devices (computers)'	=> 'Computer peripherals',
    'peripheral equipment (computers)'	=> 'Computer peripherals',
    'peripherals, computer'		=> 'Computer peripherals',
    'perl (computer program language)'	=> 'Perl (Computer program language)',
    'perl'				=> 'Perl (Computer program language)',
    'perl/tk (computer program language)' => 'Perl/Tk (Computer program language)',
    'perl/tk'				=> 'Perl/Tk (Computer program language)',
    'persian gulf war, 1991'		=> 'Persian Gulf War, 1991',
    'persistent cookies (computer science)'	=> 'Cookies (Computer science)',
    'persistent cookies'		=> 'Cookies (Computer science)',
    'personal body care'		=> 'Hygiene',
    'personal budgets'			=> 'Budgets, Personal',
    'personal cleanliness'		=> 'Hygiene',
    'personal computers'		=> 'Microcomputers',
    'personal finance software'		=> 'Finance, Personal--Software',
    'personal finance'			=> 'Finance, Personal',
    'personal financial management'	=> 'Finance, Personal',
    'personal financial planning'	=> 'Finance, Personal',
    'personal health services'		=> 'Medical care',
    'personal hygiene'			=> 'Hygiene',
    'personal identity'			=> 'Personality',
    'personal income tax'		=> 'Income tax',
    'personal time management'		=> 'Time management',
    'personality'			=> 'Personality',
    'personality traits'		=> 'Personality',
    'personification of death'		=> 'Death (Personification)',
    'personnel administration'		=> 'Personnel management',
    'personnel management'		=> 'Personnel management',
    'personology'			=> 'Personality',
    'pharmaceuticals'			=> 'Drugs',
    'philosophers'			=> 'Philosophers',
    'philosophers--biography'		=> 'Philosophers--Biography',
    'philosophy, moral'			=> 'Ethics',
    'php (computer program language)'	=> 'PHP (Computer program language)',
    'php'				=> 'PHP (Computer program language)',
    'physical endurance'		=> 'Physical fitness',
    'physical fitness'			=> 'Physical fitness',
    'physical stamina'			=> 'Physical fitness',
    'physical-appearance-based bias'	=> 'Physical-appearance-based bias',
    'physique'				=> 'Bodybuilding',
    'pickling'				=> 'Canning and preserving',
    'pictorial data processing'		=> 'Image processing',
    'picture processing'		=> 'Image processing',
    'pies'				=> 'Pies',
    'pies, pizza'			=> 'Pizza',
    'pizza pies'			=> 'Pizza',
    'pizza'				=> 'Pizza',
    'pizzas'				=> 'Pizza',
    'pl/c (computer program language)'	=> 'PL/C (Computer program language)',
    'pl/c'				=> 'PL/C (Computer program language)',
    'pl/sql (computer program language)' => 'PL/SQL (Computer program language)',
    'pl/sql'				=> 'PL/SQL (Computer program language)',
    'places, imaginary'			=> 'Imaginary places',
    'planning'				=> 'Planning',
    'planning, strategic'		=> 'Strategic planning',
    'plants in art'			=> 'Plants in art',
    'plants, psychoactive'		=> 'Psychotropic plants',
    'plants, psychotropic'		=> 'Psychotropic plants',
    'pocket calculators'		=> 'Calculators',
    'poems'				=> 'Poetry',
    'poetry'				=> 'Poetry',
    'police'			        => 'Police',
    'police--england'			=> 'Police--England',
    'police--england--london'		=> 'Police--England--London',
    'police--england--london--fiction'	=> 'Police--England--London--Fiction',
    'political collectibles'		=> 'Political collectibles',
    'political ethics'			=> 'Political ethics',
    'political paraphernalia'		=> 'Political collectibles',
    'political science--moral and ethical aspects' => 'Political ethics',
    'political terrorism'		=> 'Terrorism',
    'politics and culture'		=> 'Politics and culture',
    'politics and culture--united states' => 'Politics and culture--United States',
    'politics in art'			=> 'Politics in art',
    'politics, practical'		=> 'Politics, Practical',
    'politics, practical--moral and ethical aspects' => 'Political ethics',
    'poll watching'			=> 'Election monitoring',
    'pop culture'			=> 'Popular culture',
    'popular arts'			=> 'Popular culture',
    'popular culture'			=> 'Popular culture',
    'portraits'				=> 'Portraits',
    'possible art'			=> 'Conceptual art',
    'post-object art'			=> 'Conceptual art',
    'potable liquids'			=> 'Beverages',
    'potable water'			=> 'Drinking water',
    'potables'				=> 'Beverages',
    'pottery'				=> 'Pottery',
    'pottery, primitive'		=> 'Pottery',
    'poverty'				=> 'Poverty',
    'pr'				=> 'Public relations',
    'practical extraction and report language'	=> 'Perl (Computer program language)',
    'practical politics'		=> 'Politics, Practical',
    'prayer, mental'			=> 'Meditation',
    'precog'				=> 'Precognition',
    'precognition'			=> 'Precognition',
    'prepaid health plans'		=> 'Health insurance',
    'prepaid medical care'		=> 'Health insurance',
    'prescription drugs'		=> 'Drugs',
    'presentation graphics software'	=> 'Presentation graphics software',
    'presentation software'		=> 'Presentation graphics software',
    'preservation of art objects'       => 'Art objects--Conservation and restoration',
    'price theory'			=> 'Microeconomics',
    'princesses'			=> 'Princesses',
    'print making'			=> 'Prints--Technique',
    'printmaking'			=> 'Prints--Technique',
    'prints'				=> 'Prints',
    'prints--technique'			=> 'Prints--Technique',
    'privacy'				=> 'Privacy',
    'private enterprise'		=> 'Free enterprise',
    'procedural language/sql'		=> 'PL/SQL (Computer program language)',
    'processing, image'			=> 'Image processing',
    'processing, word'			=> 'Word processing',
    'processing, words'			=> 'Word processing',
    'produce exchanges'			=> 'Commodity exchanges',
    'product design'			=> 'Product design',
    'production management'		=> 'Production management',
    'products, commercial'		=> 'Commercial products',
    'professions'			=> 'Occupations',
    'profit-sharing trusts'		=> 'Mutual funds',
    'programming (electronic computers)' => 'Computer programming',
    'programming (mathematics)'		=> 'Programming (Mathematics)',
    'programming languages (computers)' => 'Programming languages (Electronic computers)',
    'programming languages (electronic computers)' => 'Programming languages (Electronic computers)',
    'programming languages'		=> 'Programming languages (Electronic computers)',
    'programming search engines'	=> 'Search engines--Programming',
    'programming'			=> 'Computer programming',
    'project management software'	=> 'Project management--Software',
    'project management'		=> 'Project management',
    'project management--software'	=> 'Project management--Software',
    'projection, astral'		=> 'Astral projection',
    'prolog (computer program language)' => 'Prolog (Computer program language)',
    'prolog'				=> 'Prolog (Computer program language)',
    'prolog++ (computer program language)' => 'Prolog++ (Computer program language)',
    'prolog++'				=> 'Prolog++ (Computer program language)',
    'property insurance'		=> 'Property insurance',
    'prophecy'				=> 'Prophecy',
    'prose poems'			=> 'Prose poems',
    'prose poetry'			=> 'Prose poems',
    'prostitution'			=> 'Prostitution',
    'protocols, computer network'	=> 'Computer network protocols',
    'provencal cooking'			=> 'Cooking, French--Provençal style',
    'provençal cooking'			=> 'Cooking, French--Provençal style',
    'provincial parks'			=> 'Parks',
    'psychedelic drugs'			=> 'Hallucinogenic drugs',
    'psychic healing'			=> 'Mental healing',
    'psychic phenomena'			=> 'Parapsychology',
    'psychic research'			=> 'Parapsychology',
    'psychical research'		=> 'Parapsychology',
    'psycho-kinesis'			=> 'Psychokinesis',
    'psychoactive plants'		=> 'Psychotropic plants',
    'psychokinesis'			=> 'Psychokinesis',
    'psychology'			=> 'Psychology',
    'psychology, cognitive'		=> 'Cognitive psychology',
    'psychotomimetic drugs'		=> 'Hallucinogenic drugs',
    'psychotropic plants'		=> 'Psychotropic plants',
    'public finance'			=> 'Finance, Public',
    'public health services'		=> 'Public health',
    'public health'			=> 'Public health',
    'public health--planning'		=> 'Health planning',
    'public hygiene'			=> 'Public health',
    'public markets'			=> 'Markets',
    'public relations'			=> 'Public relations',
    'public transportation'		=> 'Transportation',
    'punctuation'			=> 'Punctuation',
    'purchasing and buying'		=> 'Purchasing',
    'purchasing'			=> 'Purchasing',
    'purchasing--law and legislation'	=> 'Sales',
    'purchasing--law'			=> 'Sales',
    'put and call transactions'		=> 'Options (Finance)',
    'put options'			=> 'Options (Finance)',
    'puts (finance)'			=> 'Options (Finance)',
    'pyramid marketing'			=> 'Multilevel marketing',
    'pyramid sales clubs'		=> 'Multilevel marketing',
    'python (computer program language)' => 'Python (Computer program language)',
    'python'				=> 'Python (Computer program language)',
    'pythonidae'			=> 'Pythons',
    'pythoninae'			=> 'Pythons',
    'pythons'				=> 'Pythons',
    'q-boats'				=> 'Q-ships',
    'q-ships'				=> 'Q-ships',
    'quality assurance'			=> 'Quality assurance',
    'quality control'			=> 'Quality control',
    'quality management, total'		=> 'Total quality management',
    'quality of environment'		=> 'Environmental quality',
    'quantitative business analysis'	=> 'Management science',
    'quantity cookery'			=> 'Quantity cooking',
    'quantity cooking'			=> 'Quantity cooking',
    'quick and easy cookery'		=> 'Quick and easy cooking',
    'quick and easy cooking'		=> 'Quick and easy cooking',
    'quick-meal cookery'		=> 'Quick and easy cooking',
    'quick-meal cooking'		=> 'Quick and easy cooking',
    'r & d projects'			=> 'Research and development projects',
    'r & d'				=> 'Research and development projects',
    'r and d projects'			=> 'Research and development projects',
    'r&d projects'			=> 'Research and development projects',
    'r&d'				=> 'Research and development projects',
    'rabbits'				=> 'Rabbits',
    'rachel peng'			=> 'Peng, Rachel (Fictitious character)',
    'radio vision'			=> 'Television',
    'rationalization of industry'	=> 'Industrial management',
    'raw food'				=> 'Raw foods',
    'raw foods'				=> 'Raw foods',
    'real estate investment'		=> 'Real estate investment',
    'real estate'			=> 'Real property',
    'real property investment'		=> 'Real estate investment',
    'real property'			=> 'Real property',
    'reducing diets'			=> 'Reducing diets',
    'reduction of weight'		=> 'Weight loss',
    'realty'				=> 'Real property',
    'recreational shooting'		=> 'Shooting',
    'regional economics'		=> 'Regional economics',
    'regional parks'			=> 'Parks',
    'reicki (healing system)'		=> 'Reiki (Healing system)',
    'reicki'				=> 'Reiki (Healing system)',
    'reiki (healing system)'		=> 'Reiki (Healing system)',
    'reiki'				=> 'Reiki (Healing system)',
    'reincarnation'			=> 'Reincarnation',
    'religion and politics'		=> 'Religion and politics',
    'religion and politics--united states' => 'Religion and politics--United States',
    'religion and social problems'	=> 'Religion and social problems',
    'religious art'			=> 'Religious art',
    'religious corporations'		=> 'Corporations, Religious',
    'report program generator'		=> 'RPG (Computer program language)',
    'research & development projects'	=> 'Research and development projects',
    'research and development projects'	=> 'Research and development projects',
    'resistance to government'		=> 'Government, Resistance to',
    'resources, natural'		=> 'Natural resources',
    'restoration of art objects'	=> 'Art objects--Conservation and restoration',
    'resumes'				=> 'Résumés (Employment)',
    'resumes (employment)'		=> 'Résumés (Employment)',
    'résumés'				=> 'Résumés (Employment)',
    'résumés (employment)'		=> 'Résumés (Employment)',
    'retail industry'			=> 'Retail trade',
    'retail marketing'			=> 'Marketing',
    'retail trade'			=> 'Retail trade',
    'retail trade--marketing'		=> 'Marketing',
    'retail franchises'			=> 'Franchises (Retail trade)',
    'retailing'				=> 'Retail trade',
    'retirement planning'		=> 'Retirement--Planning',
    "rights of women"			=> "Women's rights",
    'retirement'			=> 'Retirement',
    'retirement--planning'		=> 'Retirement--Planning',
    'ridgway reservoir (colo.)'  	=> 'Ridgway Reservoir (Colo.)',
    'ridgway'				=> 'Ridgway Reservoir (Colo.)',
    'right and left (political science)' => 'Right and left (Political science)',
    'rights, civil'			=> 'Civil rights',
    'risk (insurance)'			=> 'Risk (Insurance)',
    'risk'				=> 'Risk',
    'rivers'				=> 'Rivers',
    'rivers--england'			=> 'Rivers--England',
    'romance'				=> 'Love stories',
    'routines, utility'			=> 'Utilities (Computer programs)',
    'rpg (computer program language)'	=> 'RPG (Computer program language)',
    'rubies'				=> 'Rubies',
    'ruby (computer program language)'	=> 'Ruby (Computer program language)',
    'rulers'				=> 'Heads of state',
    'russian art'			=> 'Art, Russian',
    'safety measures'			=> 'Safety measures',
    'salads'				=> 'Salads',
    'sales management'			=> 'Sales management',
    'sales'				=> 'Sales',
    'sales--law and legislation'	=> 'Sales',
    'sales--law'			=> 'Sales',
    'salesmanship'			=> 'Selling',
    'salesmen and salesmanship'		=> 'Selling',
    'salt-free diet'			=> 'Salt-free diet',
    'sam vimes (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
    'samuel vimes (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
    'sanitary affairs'			=> 'Public health',
    'satire'				=> 'Satire',
    'sauces'				=> 'Sauces',
    'schleswig-holstein cooking'	=> 'Cooking, German--Schleswig-Holstein style',
    'schooling'				=> 'Education',
    'sci fi'	 			=> 'Science fiction',
    'sci-fi'	 			=> 'Science fiction',
    'sci/fi'	 			=> 'Science fiction',
    'science fiction & fantasy'		=> 'Speculative fiction',
    'science fiction and fantasy'	=> 'Speculative fiction',
    'science fiction' 			=> 'Science fiction',
    'science fiction--authorship'	=> 'Science fiction--Authorship',
    'science fiction--general'		=> 'Science fiction',
    'science fiction--high tech'	=> 'High technology',
    'science fiction--technique'	=> 'Science fiction--Technique',
    'science fiction/fantasy'		=> 'Speculative fiction',
    'science of language'		=> 'Linguistics',
    'science of science'		=> 'Science',
    'science stories'			=> 'Science fiction',
    'science'				=> 'Science',
    'science, moral'			=> 'Ethics',
    'science--authorship'		=> 'Technical writing',
    'science--fiction'			=> 'Science fiction',
    'sciences'				=> 'Science',
    'scientific writing'		=> 'Technical writing',
    'scifi'	 			=> 'Science fiction',
    'scotland'				=> 'Scotland',
    'scottish cooking'			=> 'Cooking, Scottish',
    'screen trading (securities)'	=> 'Electronic trading of securities',
    'screen trading'			=> 'Electronic trading of securities',
    'sculpting'				=> 'Sculpture--Technique',
    'sculpture'				=> 'Sculpture',
    'sculpture--technique'		=> 'Sculpture--Technique',
    'search agents'			=> 'Search engines',
    'search engines'			=> 'Search engines',
    'search engines, web'		=> 'Web search engines',
    'search engines--programming'	=> 'Search engines--Programming',
    'second sight'			=> 'Precognition',
    'second world war'			=> 'World War, 1939-1945',
    'secret writing'			=> 'Cryptography',
    'secretarial aids & training'	=> 'Secretaries--Training',
    'secretarial aids and training'	=> 'Secretaries--Training',
    'secretaries'			=> 'Secretaries',
    'secretaries--training'		=> 'Secretaries--Training',
    'security of computer systems'	=> 'Computer security',
    'security of computer networks'	=> 'Computer networks--Security measures',
    'self-improvement'			=> 'Success',
    'selling homes'			=> 'House selling',
    'selling houses'			=> 'House selling',
    'selling'				=> 'Selling',
    'selling--law and legislation'	=> 'Sales',
    'selling--law'			=> 'Sales',
    'sensory deprivation therapy'	=> 'Sensory deprivation--Therapeutic use',
    'sensory deprivation'		=> 'Sensory deprivation',
    'sensory deprivation--therapeutic use' => 'Sensory deprivation--Therapeutic use',
    'sensory isolation'			=> 'Sensory deprivation',
    'seraphim'				=> 'Angels',
    'serial picture books'		=> 'Comic books, strips, etc',
    'service industries'		=> 'Service industries',
    'services, contracting for'		=> 'Contracting out',
    'services, financial'		=> 'Financial services industry',
    'sex'				=> 'Sex',
    'sex--religious aspects'		=> 'Sex--Religious aspects',
    'sexual abuse of children'		=> 'Child sexual abuse',
    'sexual behavior'			=> 'Sex',
    'sexual child abuse'		=> 'Child sexual abuse',
    'sexual practices'			=> 'Sex',
    'sexuality'				=> 'Sex',
    'shamanism'				=> 'Shamanism',
    'shape-shifters'			=> 'Shapeshifting',
    'shape-shifting'			=> 'Shapeshifting',
    'shapeshifters'			=> 'Shapeshifting',
    'shapeshifting'			=> 'Shapeshifting',
    'shapeshifting--fiction'		=> 'Shapeshifting',
    'shares of stock'			=> 'Stocks',
    'shooting sport'			=> 'Shooting',
    'shooting sports'			=> 'Shooting',
    'shooting'				=> 'Shooting',
    'short stories'			=> 'Short stories',
    'sickness insurance'		=> 'Health insurance',
    'simulation, computer'		=> 'Computer simulation',
    'sisters'				=> 'Sisters',
    'sisters--fiction'			=> 'Sisters--Fiction',
    'sites, web'			=> 'Web sites',
    'sixth sense'			=> 'Extrasensory perception',
    'sizeism'				=> 'Physical-appearance-based bias',
    'sizism'				=> 'Physical-appearance-based bias',
    'skills training'			=> 'Training',
    'slate computers'			=> 'Tablet computers',
    'slave holders'			=> 'Slaveholders',
    'slave keeping'			=> 'Slavery',
    'slave master'			=> 'Slaveholders',
    'slave masters'			=> 'Slaveholders',
    'slave owner'			=> 'Slaveholders',
    'slave owners'			=> 'Slaveholders',
    'slave ownership'			=> 'Slavery',
    'slave system'			=> 'Slavery',
    'slaveholders'			=> 'Slaveholders',
    'slaveholding'			=> 'Slavery',
    'slavemasters'			=> 'Slaveholders',
    'slaveowners'			=> 'Slaveholders',
    'slavery'				=> 'Slavery',
    'sleep deprivation'			=> 'Sleep deprivation',
    'sleep'				=> 'Sleep',
    'sleeping'				=> 'Sleep',
    'slimming'				=> 'Weight loss',
    'slumber'				=> 'Sleep',
    'small and medium-sized business'	=> 'Small business',
    'small and medium-sized businesses'	=> 'Small business',
    'small and medium-sized enterprise'	=> 'Small business',
    'small and medium-sized enterprises' => 'Small business',
    'small arms'			=> 'Firearms',
    'small business tax'		=> 'Small business--Taxation',
    'small business taxation'		=> 'Small business--Taxation',
    'small business taxes'		=> 'Small business--Taxation',
    'small business'			=> 'Small business',
    'small business--Taxation'		=> 'Small business--Taxation',
    'small businesses'			=> 'Small business',
    'smalltalk (computer program language)' => 'Smalltalk (Computer program language)',
    'smart growth'			=> 'Sustainable development',
    'sneffels' 				=> 'Mount Sneffels Wilderness (Colo.)',
    'social action'			=> 'Social action',
    'social action--research'		=> 'Action research',
    'social action--religious aspects'	=> 'Religion and social problems',
    'social classes'			=> 'Social classes',
    'social classes--fiction'		=> 'Social classes--Fiction',
    'social exclusion'			=> 'Social isolation',
    'social hygiene'			=> 'Public health',
    'social infrastructure'		=> 'Infrastructure (Economics)',
    'social isolation'			=> 'Social isolation',
    'social medicine'			=> 'Social medicine',
    'social networking web sites'	=> 'Online social networks',
    'social networking'			=> 'Social networks',
    'social networks'			=> 'Social networks',
    'social overhead capital'		=> 'Infrastructure (Economics)',
    'social problems and religion'	=> 'Religion and social problems',
    'social support systems'		=> 'Social networks',
    'socioeconomic status'		=> 'Economic conditions',
    'sodium-restricted diet'		=> 'Salt-free diet',
    'software development'		=> 'Computer software--Development',
    'software engineering'		=> 'Software engineering',
    'software engineering--project management' => 'Software engineering--Project management',
    'software'				=> 'Computer software',
    'software, computer'		=> 'Computer software',
    'software, malevolent'		=> 'Malware (Computer software)',
    'software, malicious'		=> 'Malware (Computer software)',
    'soho (london, england)'		=> 'Soho (London, England)',
    'soothsaying'			=> 'Soothsaying',
    'sorcerers'				=> 'Wizards',
    'sorrow'				=> 'Grief',
    'soup'				=> 'Soups',
    'soups'				=> 'Soups',
    'south german cooking'		=> 'Cooking, German--Southern style',
    'southern cooking (united states)'	=> 'Cooking, American--Southern style',
    'southern cooking (germany)'	=> 'Cooking, German--Southern style',
    'southwestern cooking (united states)'	=> 'Cooking, American--Southwestern style',
    'space colonies'			=> 'Space colonies',
    'space opera'			=> 'Speculative fiction',
    'space rockets'			=> 'Space vehicles',
    'space ships'			=> 'Space vehicles',
    'space vehicles'			=> 'Space vehicles',
    'spacecraft'			=> 'Space vehicles',
    'spaceships'			=> 'Space vehicles',
    'spam (electronic mail)'		=> 'Spam (Electronic mail)',
    'spam'				=> 'Spam (Electronic mail)',
    'specfic'				=> 'Speculative fiction',
    'specie'				=> 'Coins',
    'speculative fantasy'		=> 'Speculative fiction',
    'speculative fiction'		=> 'Speculative fiction',
    'speech processing systems'		=> 'Speech processing systems',
    'speech processing'			=> 'Speech processing systems',
    'spices--use in cooking'		=> 'Cooking (Spices)',
    'spies'     			=> 'Spy stories',
    'spirit channeling'			=> 'Channeling (Spiritualism)',
    'spirit guides'			=> 'Guides (Spiritualism)',
    'spiritism'				=> 'Spiritualism',
    'spirits, alcoholic'		=> 'Liquors',
    'spiritual healing'			=> 'Spiritual healing',
    'spiritual therapies'		=> 'Spiritual healing',
    'spiritual-mindedness'		=> 'Spirituality',
    'spiritualism'			=> 'Spiritualism',
    'spirituality'			=> 'Spirituality',
    'spirituous liquors'		=> 'Liquors',
    'sport shooting'			=> 'Shooting',
    'sports stories'			=> 'Sports stories',
    'sports'				=> 'Sports',
    'sports--fiction'			=> 'Sports stories',
    'sports--juvenile fiction'		=> 'Sports stories',
    'spread sheets'			=> 'Electronic spreadsheets',
    'spread sheets, electronic'		=> 'Electronic spreadsheets',
    'spreadsheet software'		=> 'Electronic spreadsheets--Software',
    'spreadsheeting, electronic'	=> 'Electronic spreadsheets',
    'spreadsheets'			=> 'Electronic spreadsheets',
    'spreadsheets, electronic'		=> 'Electronic spreadsheets',
    'spy stories'     			=> 'Spy stories',
    'sql (computer program language)'	=> 'SQL (Computer program language)',
    'sql'				=> 'SQL (Computer program language)',
    'standard of value'			=> 'Money',
    'star trek fiction'			=> 'Star Trek fiction',
    'star trek'				=> 'Star Trek fiction',
    'star wars fiction'			=> 'Star Wars fiction',
    'star wars'				=> 'Star Wars fiction',
    'start-up business enterprises'	=> 'New business enterprises',
    'start-up businesses enterprises'	=> 'New business enterprises',
    'start-up companies'		=> 'New business enterprises',
    'start-up enterprises'		=> 'New business enterprises',
    'start-ups (business enterprises)'	=> 'New business enterprises',
    'start-ups'				=> 'New business enterprises',
    'starting a business'		=> 'New business enterprises',
    'startups (business enterprises)'	=> 'New business enterprises',
    'startups'				=> 'New business enterprises',
    'state parks'			=> 'Parks',
    'statistical matching'		=> 'Statistical matching',
    'statues'				=> 'Sculpture',
    'statuettes'			=> 'Figurines',
    'statutes'				=> 'Statutes',
    'steampunk fiction'			=> 'Steampunk fiction',
    'steampunk'				=> 'Steampunk fiction',
    'steganography'			=> 'Cryptography',
    'stereograms'			=> 'Stereograms',
    'stew'				=> 'Stews',
    'stews'				=> 'Stews',
    'stimulus deprivation'		=> 'Sensory deprivation',
    'stock issues'			=> 'Stocks',
    'stock offerings'			=> 'Stocks',
    'stock trading'			=> 'Stocks',
    'stocks'				=> 'Stocks',
    'stonework, decorative'		=> 'Sculpture',
    'strategic management'		=> 'Strategic planning',
    'strategic planning'		=> 'Strategic planning',
    'street art'			=> 'Street art',
    'structural adjustment (economic policy)' => 'Structural adjustment (Economic policy)',
    'structural adjustment'		=> 'Structural adjustment (Economic policy)',
    'structured query language'		=> 'SQL (Computer program language)',
    'study guides'			=> 'Study guides',
    'submarine boats'			=> 'Submarines (Ships)',
    'submarines (ships)'		=> 'Submarines (Ships)',
    'submarines'			=> 'Submarines (Ships)',
    'success'				=> 'Success',
    'super heroes'			=> 'Superheroes',
    'superannuation'			=> 'Retirement',
    'superheroes'			=> 'Superheroes',
    'supernatural'			=> 'Supernatural',
    'survival'				=> 'Survival',
    'survival skills'			=> 'Survival',
    'suspense fiction'			=> 'Suspense fiction',
    'suspense tales'			=> 'Suspense fiction',
    'suspense'				=> 'Suspense fiction',
    'sustainable development'		=> 'Sustainable development',
    'sustainable economic development'	=> 'Sustainable development',
    'sweets'				=> 'Confectionery',
    'system administration'		=> 'Computers--Administration',
    'system analysis'			=> 'System analysis',
    'systems administration'		=> 'Computers--Administration',
    'systems analysis'			=> 'System analysis',
    'systems architecture'		=> 'Computer architecture',
    'systems, expert (computer science)' => 'Expert systems (Computer science)',
    'systems, expert'			=> 'Expert systems (Computer science)',
    'tablet computers'			=> 'Tablet computers',
    'tablets (computers)'		=> 'Tablet computers',
    'takeovers, corporate'		=> 'Consolidation and merger of corporations',
    'tap water'				=> 'Drinking water',
    'tarot'				=> 'Tarot',
    'tattooing'				=> 'Tattooing',
    'tattoos (body markings)'		=> 'Tattooing',
    'tattoos'				=> 'Tattooing',
    'tax policy'			=> 'Taxation',
    'tax reform'			=> 'Taxation',
    'tax-exempt organizations'		=> 'Nonprofit organizations',
    'taxable income'			=> 'Income tax',
    'taxation of franchises'		=> 'Corporations--Taxation',
    'taxation of income'		=> 'Income tax',
    'taxation'				=> 'Taxation',
    'taxation, incidence of'		=> 'Taxation',
    'taxes'				=> 'Taxation',
    'tea'				=> 'Tea',
    'tea--use in cooking'		=> 'Cooking (Tea)',
    'teachers'				=> 'Teachers',
    'technical writing'			=> 'Technical writing',
    'technology'			=> 'Technology',
    'technology--authorship'		=> 'Technical writing',
    'technomancers'			=> 'Technomancers',
    'technomancy'			=> 'Technomancy',
    'telekinesis'			=> 'Psychokinesis',
    'telemarketing'			=> 'Telemarketing',
    'telepathy'				=> 'Telepathy',
    'telephone marketing'		=> 'Telemarketing',
    'telephone service'			=> 'Telephone',
    'telephone'				=> 'Telephone',
    'telephones'			=> 'Telephone',
    'teleportation'			=> 'Teleportation',
    'teleprocessing networks'		=> 'Computer networks',
    'television'			=> 'Television',
    'territorial parks'			=> 'Parks',
    'terror attacks'			=> 'Terrorism',
    'terrorism'				=> 'Terrorism',
    'terrorism, acts of'		=> 'Terrorism',
    'terrorist acts'			=> 'Terrorism',
    'terrorist attacks'			=> 'Terrorism',
    'textile industry and fabrics'	=> 'Textile industry',
    'textile industry'			=> 'Textile industry',
    'textiles industry'			=> 'Textile industry',
    'thames'                 		=> 'Thames River (England)',
    'thames river'           		=> 'Thames River (England)',
    'thames river (england)' 		=> 'Thames River (England)',
    'theology'				=> 'Theology',
    'theology, mystical'		=> 'Mysticism',
    'thieves'				=> 'Thieves',
    'thinking, artificial'		=> 'Artificial intelligence',
    'thought-transference'		=> 'Telepathy',
    'thralldom'				=> 'Slavery',
    'thriller'				=> 'Suspense fiction',
    'thrillers (fiction)'		=> 'Suspense fiction',
    'thrillers'				=> 'Suspense fiction',
    'tiffany aching (fictitious character)' => 'Aching, Tiffany (Fictitious character)',
    'tiffany aching'			=> 'Aching, Tiffany (Fictitious character)',
    'time allocation'			=> 'Time management',
    'time budgets'			=> 'Time management',
    'time management'			=> 'Time management',
    'time use'				=> 'Time management',
    'time'				=> 'Time',
    'time--management'			=> 'Time management',
    'time--organization'		=> 'Time management',
    'time--use of'			=> 'Time management',
    'time-saving cooking'		=> 'Quick and easy cooking',
    'timepieces'			=> 'Clocks and watches',
    'total quality management'		=> 'Total quality management',
    'tour operators'			=> 'Tourism',
    'tourism industry'			=> 'Tourism',
    'tourism operators'			=> 'Tourism',
    'tourism'				=> 'Tourism',
    'tourist industry'			=> 'Tourism',
    'tourist trade'			=> 'Tourism',
    'tourist traffic'			=> 'Tourism',
    'towns'				=> 'Cities and towns',
    'tqm (total quality management)'	=> 'Total quality management',
    'tqm'				=> 'Total quality management',
    'trade policy'			=> 'Commercial policy',
    'trade'				=> 'Commerce',
    'trade, international'		=> 'International trade',
    'trades'				=> 'Occupations',
    'trading of securities, electronic'	=> 'Electronic trading of securities',
    'trading, futures'			=> 'Futures',
    'trading, stock'			=> 'Stocks',
    'training'				=> 'Training',
    'transmission of data'		=> 'Data transmission systems',
    'transnational corporations'	=> 'International business enterprises',
    'transnational enterprises'		=> 'International business enterprises',
    'transport industry'		=> 'Transportation',
    'transport'				=> 'Transportation',
    'transportation companies'		=> 'Transportation',
    'transportation industry'		=> 'Transportation',
    'transportation'			=> 'Transportation',
    'travel industry'			=> 'Tourism',
    'travel, astral'			=> 'Astral projection',
    'tv'				=> 'Television',
    'u.s. government'			=> 'United States--Politics and government',
    'u.s. political culture'		=> 'Politics and culture--United States',
    'u.s. politics'			=> 'United States--Politics and government',
    'u.s.a.'				=> 'United States',
    'ufo phenomena'			=> 'Unidentified flying objects',
    'ufo'				=> 'Unidentified flying objects',
    'ufology'				=> 'Unidentified flying objects',
    'ufos'				=> 'Unidentified flying objects',
    'uits'				=> 'Mutual funds',
    'uml (computer science)'		=> 'UML (Computer science)',
    'uml'				=> 'UML (Computer science)',
    'uncooked food'			=> 'Raw foods',
    'underwater basket weaving'		=> 'Underwater construction--Baskets',
    'underwater basket-weaving'		=> 'Underwater construction--Baskets',
    'underwater construction'		=> 'Underwater construction',
    'underwater construction--baskets'	=> 'Underwater construction--Baskets',
    'underwriting'			=> 'Insurance',
    'unemployment'			=> 'Unemployment',
    'unfired food'			=> 'Raw foods',
    'unidentified flying objects'	=> 'Unidentified flying objects',
    'unified modeling language'		=> 'Unified Modeling Language',
    'unit investment trusts'		=> 'Mutual funds',
    'unit trusts'			=> 'Mutual funds',
    'united states political culture'	=> 'Politics and culture--United States',
    'united states'			=> 'United States',
    'united states, northwestern'	=> 'Northwestern States',
    'united states--collectibles'	=> 'Americana',
    'united states--politics and culture' => 'Politics and culture--United States',
    'united states--politics and government' => 'United States--Politics and government',
    'urban economics'			=> 'Urban economics',
    'us government'			=> 'United States--Politics and government',
    'us political culture'		=> 'Politics and culture--United States',
    'us politics'			=> 'United States--Politics and government',
    'us'				=> 'United States',
    'usa'				=> 'United States',
    'use of time'			=> 'Time management',
    'user created content'		=> 'User-generated content',
    'user generated content'		=> 'User-generated content',
    'user interfaces (computer systems)' => 'User interfaces (Computer systems)',
    'user interfaces'			 => 'User interfaces (Computer systems)',
    'user-created content'		=> 'User-generated content',
    'user-generated content'		=> 'User-generated content',
    'usury'				=> 'Interest',
    'utensils'				=> 'Implements, utensils, etc',
    'utilities (computer programs)'	=> 'Utilities (Computer programs)',
    'utility programs'			=> 'Utilities (Computer programs)',
    'utility routines'			=> 'Utilities (Computer programs)',
    'valdemar'			        => 'Valdemar (Imaginary place)',
    'valdemar (imaginary place)'        => 'Valdemar (Imaginary place)',
    'vampires'				=> 'Vampires',
    'vbscript (computer program language)' => 'VBScript (Computer program language)',
    'vbscript'				=> 'VBScript (Computer program language)',
    'vessels (utensils)'		=> 'Implements, utensils, etc',
    'video art'				=> 'Video art',
    'video games'			=> 'Computer games',
    'vietnam'				=> 'Vietnam',
    'vietnam war'			=> 'Vietnam War, 1961-1975',
    'vietnam war, 1961-1975'		=> 'Vietnam War, 1961-1975',
    'vimes, samuel (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
    'vintage automobiles'		=> 'Antique and classic cars',
    'vintage cars'			=> 'Antique and classic cars',
    'virtual computer systems'		=> 'Virtual computer systems',
    'virtual corporations'		=> 'Virtual corporations',
    'virtual environments'		=> 'Virtual reality',
    'virtual machine systems'		=> 'Virtual computer systems',
    'virtual machines'			=> 'Virtual computer systems',
    'virtual private networks'		=> 'Extranets (Computer networks)',
    'virtual reality'			=> 'Virtual reality',
    'virtual worlds'			=> 'Virtual reality',
    'vision, computer'			=> 'Computer vision',
    'vision, machine'			=> 'Computer vision',
    'visitor industry'			=> 'Tourism',
    'visual data processing'		=> 'Optical data processing',
    'voyagers'				=> 'Explorers',
    'vpns'				=> 'Extranets (Computer networks)',
    'vr'				=> 'Virtual reality',
    'w3 (world wide web)'		=> 'World Wide Web',
    'w3'				=> 'World Wide Web',
    'wall art'				=> 'Street art',
    'war stories'			=> 'War stories',
    'war'				=> 'War stories',
    'warehousing data'			=> 'Data warehousing',
    'warehousing information'		=> 'Data warehousing',
    'warehousing, data'			=> 'Data warehousing',
    'warehousing, information'		=> 'Data warehousing',
    'warlocks'				=> 'Warlocks',
    'warships'				=> 'Warships',
    'watches'				=> 'Clocks and watches',
    'water-color painting'		=> 'Watercolor painting',
    'water-color paintings'		=> 'Watercolor painting',
    'water-colors'			=> 'Watercolor painting',
    'watercolor painting'		=> 'Watercolor painting',
    'watercolor painting--technique'	=> 'Watercolor painting--Technique',
    'watercolor paintings'		=> 'Watercolor painting',
    'watercolors'			=> 'Watercolor painting',
    'weather'				=> 'Weather',
    'weatherwax, esme (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'weatherwax, esmerelda (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'weatherwax, granny (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
    'web (world wide web)'		=> 'World Wide Web',
    'web broadcasting'			=> 'Webcasting',
    'web browsers'			=> 'Browsers (Computer programs)',
    'web browsing software'		=> 'Browsers (Computer programs)',
    'web casting'			=> 'Webcasting',
    'web drugstores'			=> 'Internet pharmacies',
    'web logs'				=> 'Blogs',
    'web pages'				=> 'Web sites',
    'web pharmacies'			=> 'Internet pharmacies',
    'web programming'			=> 'Web site development',
    'web retailing'			=> 'Electronic commerce',
    'web search engines'		=> 'Web search engines',
    'web searching'			=> 'Web search engines',
    'web services'			=> 'Web services',
    'web site development'		=> 'Web site development',
    'web site directories'		=> 'Web sites--Directories',
    'web site programming'		=> 'Web site development',
    'web sites'				=> 'Web sites',
    'web sites--computer programming'	=> 'Web site development',
    'web sites--design'			=> 'Web sites--Design',
    'web sites--development'		=> 'Web site development',
    'web sites--directories'		=> 'Web sites--Directories',
    'web sites--programming'		=> 'Web site development',
    'webcasting'			=> 'Webcasting',
    'webliographies'			=> 'Web sites--Directories',
    'weblogs'				=> 'Blogs',
    'webpage design'			=> 'Web sites--Design',
    'webpages'				=> 'Web sites',
    'website design'			=> 'Web sites--Design',
    'website development'		=> 'Web site development',
    'website programming'		=> 'Web site development',
    'websites'				=> 'Web sites',
    'weight control of obesity'		=> 'Weight loss',
    'weight control'			=> 'Weight loss',
    'weight loss'			=> 'Weight loss',
    'weight reducing'			=> 'Weight loss',
    'weight reduction'			=> 'Weight loss',
    'welsh cooking'			=> 'Cooking, Welsh',
    'were-wolf'				=> 'Werewolves',
    'were-wolves'			=> 'Werewolves',
    'werewolf'				=> 'Werewolves',
    'werewolves'			=> 'Werewolves',
    'werewolves--fiction'		=> 'Werewolves',
    'werwolf'				=> 'Werewolves',
    'werwolves'				=> 'Werewolves',
    'west indian cooking'		=> 'Cooking, West Indian',
    'western cooking (united states)'	=> 'Cooking, American--Western style',
    'westphalian cooking'		=> 'Cooking, German--Westphalian style',
    'wideband communication systems'	=> 'Broadband communication systems',
    'wimmin'				=> 'Women',
    'wireless communication systems'	=> 'Wireless communication systems',
    'wireless data communication systems' => 'Wireless communication systems',
    'wireless data communication'	=> 'Wireless communication systems',
    'wireless data transmission systems' => 'Wireless communication systems',
    'wireless data transmission'	=> 'Wireless communication systems',
    'wireless information systems'	=> 'Wireless communication systems',
    'wireless telecommunication systems' => 'Wireless communication systems',
    'wireless telecommunication'	=> 'Wireless communication systems',
    'witchcraft'			=> 'Witchcraft',
    'wizard of london'			=> 'Wizards--England--London--Fiction',
    'wizards'				=> 'Wizards',
    'wizards--england--london--fiction'	=> 'Wizards--England--London--Fiction',
    'wok cookery'			=> 'Wok cooking',
    'wok cooking'			=> 'Wok cooking',
    'woman'				=> 'Women',
    'women'				=> 'Women',
    'women detectives'			=> 'Women detectives',
    'women entrepreneurs'		=> 'Businesswomen',
    'women heroes'			=> 'Women heroes',
    'women heroes--fiction'		=> 'Women heroes--Fiction',
    'women in business'			=> 'Businesswomen',
    'women--biography'			=> 'Women--Biography',
    "women--civil rights"		=> "Women's rights",
    "women--rights of women"		=> "Women's rights",
    'women--suffrage'			=> 'Women--Suffrage',
    "women's rights"			=> "Women's rights",
    'womon'				=> 'Women',
    'womyn'				=> 'Women',
    'wood furniture'			=> 'Furniture',
    'wooden furniture'			=> 'Furniture',
    'wooing'				=> 'Courtship',
    'word processing software'		=> 'Word processing--Software',
    'word processing'			=> 'Word processing',
    'word processing--software'		=> 'Word processing--Software',
    'work environment'			=> 'Work environment',
    'working conditions'		=> 'Work environment',
    'working conditions, physical'	=> 'Work environment',
    'working environment'		=> 'Work environment',
    'workplace climate'			=> 'Work environment',
    'workplace culture'			=> 'Work environment',
    'workplace environment'		=> 'Work environment',
    'workplace'				=> 'Work environment',
    'worksite environment'		=> 'Work environment',
    'world economics'			=> 'Competition, International',
    'world terrorism'			=> 'Terrorism',
    'world trade'			=> 'International trade',
    'world war, 1939-1945'		=> 'World War, 1939-1945',
    'world war 2'			=> 'World War, 1939-1945',
    'world war ii'			=> 'World War, 1939-1945',
    'world war two'			=> 'World War, 1939-1945',
    'world wide web'			=> 'World Wide Web',
    'world wide web pages'		=> 'Web sites',
    'world wide web searching'		=> 'Web search engines',
    'world wide web sites'		=> 'Web sites',
    'world wide web sites--directories'	=> 'Web sites--Directories',
    'world wide web--directories'	=> 'Web sites--Directories',
    'writing'				=> 'Writing',
    'writing letters'			=> 'Letter writing',
    'writing of letters'		=> 'Letter writing',
    'www (world wide web)'		=> 'World Wide Web',
    'www pages'				=> 'Web sites',
    'www sites'				=> 'Web sites',
    'www'				=> 'World Wide Web',
    'xml (document markup language)'	=> 'XML (Document markup language)',
    'xml'				=> 'XML (Document markup language)',
    'xml-based user interface language'	=> 'XUL (Document markup language)',
    'xul (document markup language)'	=> 'XUL (Document markup language)',
    'xul'				=> 'XUL (Document markup language)',
    'young women'			=> 'Young women',
    'young women--england--fiction'	=> 'Young women--England--Fiction',
    'young women--fiction'		=> 'Young women--Fiction',
    'youngsters'			=> 'Children',
    'zombies'				=> 'Zombies',
    'zombis'				=> 'Zombies',
   );


our %mobibooktypes = (
    'Default' => undef,
    'eBook' => 'text/x-oeb1-document',
    'eNews' => 'application/x-mobipocket-subscription',
    'News feed' => 'application/x-mobipocket-subscription-feed',
    'News magazine' => 'application/x-mobipocket-subscription-magazine',
    'Images' => 'image/gif',
    'Microsoft Word document' => 'application/msword',
    'Microsoft Excel sheet' => 'application/vnd.ms-excel',
    'Microsoft Powerpoint presentation' => 'application/vnd.ms-powerpoint',
    'Plain text' => 'text/plain',
    'HTML' => 'text/html',
    'Mobipocket game' => 'application/vnd.mobipocket-game',
    'Franklin game' => 'application/vnd.mobipocket-franklin-ua-game'
    );

our %publishermap = (
    'a.s.s.t.r'				=> 'ASSTR',
    'a.s.s.t.r.'			=> 'ASSTR',
    'ace books'             		=> 'Ace Books',
    'ace'                   		=> 'Ace Books',
    'acebooks'              		=> 'Ace Books',
    'alt sex stories text repository' 	=> 'ASSTR',
    'alt.sex.stories text repository' 	=> 'ASSTR',
    'alt.sex.stories'			=> 'ASSTR',
    'alt.sex.stories.moderated'		=> 'ASSTR',
    'anchor books'			=> 'Anchor Books',
    'anchor books a division of random house, inc.' => 'Anchor Books',
    'anchor booksa division of random house, inc.' => 'Anchor Books',
    'asstr'				=> 'ASSTR',
    'baen books'               		=> 'Baen Publishing Enterprises',
    'baen publishing'       		=> 'Baen Publishing Enterprises',
    'baen'                  		=> 'Baen Publishing Enterprises',
    'ballantine books'      		=> 'Ballantine Books',
    'ballantine'            		=> 'Ballantine Books',
    'barnes & noble'      		=> 'Barnes and Noble Publishing',
    'barnes &amp; noble'      		=> 'Barnes and Noble Publishing',
    'barnes and noble'      		=> 'Barnes and Noble Publishing',
    'barnes&amp;noble'      		=> 'Barnes and Noble Publishing',
    'barnes&noble'      		=> 'Barnes and Noble Publishing',
    'barnesandnoble.com'    		=> 'Barnes and Noble Publishing',
    'blackmask online'			=> 'Blackmask Online',
    'cpan'                  		=> 'CPAN',
    'del rey books'         		=> 'Del Rey Books',
    'del rey'               		=> 'Del Rey Books',
    'delrey'                		=> 'Del Rey Books',
    'e-reads'               		=> 'E-Reads',
    'electronic text center. university of virginia library.' => 'University of Virginia Library',
    'ereads'                		=> 'E-Reads',
    'ereads.com'            		=> 'E-Reads',
    'feedbooks (www.feedbooks.com)' 	=> 'Feedbooks',
    'feedbooks'             		=> 'Feedbooks',
    'fictionwise'           		=> 'Fictionwise',
    'fictionwise, inc.'       		=> 'Fictionwise',
    'fictionwise.com'       		=> 'Fictionwise',
    'gutenberg'             		=> 'Project Gutenberg',
    'gutenberg.org'         		=> 'Project Gutenberg',
    'harmony books'         		=> 'Harmony Books',
    'harmony'               		=> 'Harmony Books',
    'harper collins'        		=> 'HarperCollins',
    'harper-collins'        		=> 'HarperCollins',
    'harpercollins'         		=> 'HarperCollins',
    'http://www.blackmask.com'		=> 'Blackmask Online',
    'http://www.blackmask.com/'		=> 'Blackmask Online',
    'manybooks'             		=> 'ManyBooks',
    'manybooks.net'         		=> 'ManyBooks',
    'marion zimmer bradley literary works trust' => 'Marion Zimmer Bradley Literary Works Trust',
    'mzb literary works trust'		=> 'Marion Zimmer Bradley Literary Works Trust',
    'penguin group'         		=> 'Penguin Group',
    'penguin group, usa'    		=> 'Penguin Group',
    'penguin'               		=> 'Penguin Group',
    'project gutenberg'     		=> 'Project Gutenberg',
    'random-house.com'      		=> 'Random House',
    'randomhouse'           		=> 'Random House',
    'randomhouse.co.uk'     		=> 'Random House',
    'rosetta books'         		=> 'Rosetta Books',
    'rosetta'               		=> 'Rosetta Books',
    'rosettabooks'          		=> 'Rosetta Books',
    'siren publishing'      		=> 'Siren Publishing',
    'siren'                 		=> 'Siren Publishing',
    'smashwords'			=> 'Smashwords',
    'smashwords, inc.'			=> 'Smashwords',
    'stonehouse press'			=> 'Stonehouse Press',
    'stories online'			=> 'World Literature Company',
    'storiesonline'			=> 'World Literature Company',
    'the random house publishing group' => 'Random House',
    'tom doherty associates'		=> 'Tom Doherty Associates',
    'university of virginia library'	=> 'University of Virginia Library',
    'university of virginia library.'	=> 'University of Virginia Library',
    'wildside press'        		=> 'Wildside Press',
    'wildside'              		=> 'Wildside Press',
    'world literature company'          => 'World Literature Company',
    'www.blackmask.com'			=> 'Blackmask Online',
    'www.ereads.com'        		=> 'E-Reads',
    'www.feedbooks.com'     		=> 'Feedbooks',
    'www.fictionwise.com'   		=> 'Fictionwise',
    'www.gutenberg.org'     		=> 'Project Gutenberg',
    'www.random-house.com'  		=> 'Random House',
    'www.randomhouse.co.uk' 		=> 'Random House',
    );


our %nonxmlentity2char = %entity2char;
delete($nonxmlentity2char{'amp'});
delete($nonxmlentity2char{'gt'});
delete($nonxmlentity2char{'lt'});
delete($nonxmlentity2char{'quot'});
delete($nonxmlentity2char{'apos'});

our %referencetypes = (
    # standard types
    'acknowledgements'   => 'acknowledgements',
    'bibliography'       => 'bibliography',
    'colophon'           => 'colophon',
    'copyright-page'     => 'copyright-page',
    'cover'              => 'cover',
    'dedication'         => 'dedication',
    'epigraph'           => 'epigraph',
    'foreword'           => 'foreword',
    'glossary'           => 'glossary',
    'index'              => 'index',
    'loi'                => 'loi',
    'lot'                => 'lot',
    'notes'              => 'notes',
    'preface'            => 'preface',
    'text'               => 'text',
    'title-page'         => 'title-page',
    'toc'                => 'toc',
    # common nonstandard types
    'start'              => 'text',
    'coverimage'         => 'other.ms-coverimage',
    'coverimagestandard' => 'other.ms-coverimage-standard',
    'other.copyright'    => 'copyright-page',
    'other.ms-firstpage' => 'text',
    'thumbimage'         => 'other.ms-thumbimage',
    'thumbimagestandard' => 'other.ms-thumbimage-standard',
   );

our %relatorcodes = (
    'act' => 'Actor',
    'adp' => 'Adapter',
    'ann' => 'Annotator',
    'ant' => 'Bibliographic antecedent',
    'app' => 'Applicant',
    'arc' => 'Architect',
    'arr' => 'Arranger',
    'art' => 'Artist',
    'asg' => 'Assignee',
    'asn' => 'Associated name',
    'att' => 'Attributed name',
    'aui' => 'Author of introduction',
    'aus' => 'Author of screenplay',
    'aut' => 'Author',
    'bdd' => 'Binding designer',
    'bjd' => 'Bookjacket designer',
    'bkd' => 'Book designer',
    'bkp' => 'Book producer',
    'bnd' => 'Binder',
    'bpd' => 'Bookplate designer',
    'bsl' => 'Bookseller',
    'chr' => 'Choreographer',
    'cli' => 'Client',
    'cll' => 'Calligrapher',
    'clt' => 'Collotyper',
    'cmm' => 'Commentator',
    'cmp' => 'Composer',
    'cmt' => 'Compositor',
    'cnd' => 'Conductor',
    'cns' => 'Censor',
    'coe' => 'Contestant-appellee',
    'col' => 'Collector',
    'com' => 'Compiler',
    'cos' => 'Contestant',
    'cot' => 'Contestant-appellant',
    'cpe' => 'Complainant-appellee',
    'cph' => 'Copyright holder',
    'cpl' => 'Complainant',
    'cpt' => 'Complainant-appellant',
    'crp' => 'Correspondent',
    'crr' => 'Corrector',
    'cst' => 'Costume designer',
    'cte' => 'Contestee-appellee',
    'ctg' => 'Cartographer',
    'cts' => 'Contestee',
    'ctt' => 'Contestee-appellant',
    'dfd' => 'Defendant',
    'dfe' => 'Defendant-appellee',
    'dft' => 'Defendant-appellant',
    'dln' => 'Delineator',
    'dnc' => 'Dancer',
    'dnr' => 'Donor',
    'dpt' => 'Depositor',
    'drt' => 'Director',
    'dsr' => 'Designer',
    'dst' => 'Distributor',
    'dte' => 'Dedicatee',
    'dto' => 'Dedicator',
    'dub' => 'Dubious author',
    'edt' => 'Editor',
    'egr' => 'Engraver',
    'elt' => 'Electrotyper',
    'eng' => 'Engineer',
    'etr' => 'Etcher',
    'flm' => 'Film editor',
    'fmo' => 'Former owner',
    'fnd' => 'Funder/Sponsor',
    'frg' => 'Forger',
    'grt' => 'Graphic technician (discontinued code)',
    'hnr' => 'Honoree',
    'ill' => 'Illustrator',
    'ilu' => 'Illuminator',
    'ins' => 'Inscriber',
    'inv' => 'Inventor',
    'itr' => 'Instrumentalist',
    'ive' => 'Interviewee',
    'ivr' => 'Interviewer',
    'lbt' => 'Librettist',
    'lee' => 'Libelee-appellee',
    'lel' => 'Libelee',
    'len' => 'Lender',
    'let' => 'Libelee-appellant',
    'lie' => 'Libelant-appellee',
    'lil' => 'Libelant',
    'lit' => 'Libelant-appellant',
    'lse' => 'Licensee',
    'lso' => 'Licensor',
    'ltg' => 'Lithographer',
    'lyr' => 'Lyricist',
    'mon' => 'Monitor/Contractor',
    'mte' => 'Metal-engraver',
    'nrt' => 'Narrator',
    'org' => 'Originator',
    'oth' => 'Other',
    'pbl' => 'Publisher',
    'pfr' => 'Proofreader',
    'pht' => 'Photographer',
    'plt' => 'Platemaker',
    'pop' => 'Printer of plates',
    'ppm' => 'Papermaker',
    'prd' => 'Production personnel',
    'prf' => 'Performer',
    'pro' => 'Producer',
    'prt' => 'Printer',
    'pte' => 'Plaintiff-appellee',
    'ptf' => 'Plaintiff',
    'pth' => 'Patent holder',
    'ptt' => 'Plaintiff-appellant',
    'rbr' => 'Rubricator',
    'rce' => 'Recording engineer',
    'rcp' => 'Recipient',
    'rse' => 'Respondent-appellee',
    'rsp' => 'Respondent',
    'rst' => 'Respondent-appellant',
    'sce' => 'Scenarist',
    'scr' => 'Scribe',
    'scl' => 'Sculptor',
    'sec' => 'Secretary',
    'sgn' => 'Signer',
    'srv' => 'Surveyor',
    'str' => 'Stereotyper',
    'trc' => 'Transcriber',
    'trl' => 'Translator',
    'tyd' => 'Type designer',
    'tyg' => 'Typographer',
    'voc' => 'Vocalist',
    'wam' => 'Writer of accompanying material',
    'wde' => 'Wood-engraver',
    'wit' => 'Witness',
    );

our %sexcodes = (
    # Age/Gender
    'MF'      => 'Ma/Fa',	# Adult Male over 18, Adult Female over 18
    'mf'      => 'mt/ft',	# Teen Male under 18, Teen Female under 18
    'Mf'      => 'Ma/ft',	# Adult Male over 18, Teen Female under 18
    'mF'      => 'mt/Fa',	# Teen Male under 18, Adult Female
    'FF'      => 'Fa/Fa',	# 2 or more Females over 18
    'ff'      => 'ft/ft',	# 2 or more Teen Females under 18
    'Ff'      => 'Fa/ft',	# Adult Female, Teen female under 18
    'boy'     => 'boy',		# Boy 12 years old or younger
    'Boy'     => 'boy',
    'girl'    => 'gi',		# Girl 12 years old or younger
    'Girl'    => 'gi',
    'MM'      => 'Ma/Ma',	# 2 or more Adult Males over 18
    'Mm'      => 'Ma/mt',	# Adult Male, Teen Male under 18
    'mm'      => 'mt/mt',	# 2 or more Teen Males under 18
    'mult'    => 'Mult',	# Multiple Partner. ie MFF or mmmF
    'teen'    => 'Teenagers',	# Cast of story is mostly teenagers.
    'Teen'    => 'Teenagers',
    'teenagers' => 'Teenagers',

    # Level of Consent
    'cons'       => 'Consensual', # All parties are consenting to the act
    'consensual' => 'Consensual',
    'rom'      => 'Romantic',	  #  Mushy love story
    'romance'  => 'Romantic',
    'romantic' => 'Romantic',
    'non-con'  => 'NonConsensual', # At least one of the parties is not participating willfully
    'nonconsensual' => 'NonConsensual',
    'pedo'     => 'Pedophilia',	  # An adult initiating sexual contact with a
				  # pre-pubescent child (boy or girl, 12 or under)
    'pedophilia' => 'Pedophilia',
    'lolita'  => 'Lolita',	  # A pre-pubescent child (boy or girl, 12 or
				  # younger) initiates sexual contact with an adult
    'reluc'   => 'Reluctant',	  # Start as non consentual and then the non
				  # consenting party participates willingly
    'reluctant' => 'Reluctant',
    'rape'    => 'Rape',	  # Stories with violent rape
    'coer'    => 'Coercion',	  # Not exactly blackmail, but applying different
				  # kinds of pressure to force into the act.
    'coercion' => 'Coercion',
    'Blkm'    => 'Blackmail',	  # Blackmailing somebody to force them into the sex act depicted
    'blackmail' => 'Blackmail',
    'mc'      => 'Mind Control',  # Stories where one party somehow controls another's mind
    'mind control' => 'Mind Control',
    'MindControl'  => 'Mind Control',
    'Mindcontrol'  => 'Mind Control',
    'mindcontrol'  => 'Mind Control',
    'hypno'   => 'Hypnosis',	  # One party using hypnosis for sexual purposes
    'hypnosis' => 'Hypnosis',
    'dru'     => 'Drunk/Drugged', # One party at least is drunk or drugged and
				  # participates while under the influence without really knowing
    'drunk/drugged' => 'Drunk/Drugged',
    'Drunk'         => 'Drunk/Drugged',
    'drunk'         => 'Drunk/Drugged',
    'Drugged'       => 'Drunk/Drugged',
    'drugged'       => 'Drunk/Drugged',
    'mag'     => 'Magic', 	  # Contains magic and supernatural phenomenons
    'slave'   => 'Slavery',	  # Slavery, sexual or otherwise
    'Slave'   => 'Slavery',
    'slavery' => 'Slavery',

    # Sexual Orientations
    'gay'     => 'Gay',		  # Self Explanatory
    'les'     => 'Lesbian',	  # Self Explanatory
    'Les'     => 'Lesbian',
    'lesbian' => 'Lesbian',
    'lesbian sex' => 'Lesbian',
    'bi'      => 'BiSexual',	  # Self Explanatory
    'Bisexual' => 'BiSexual',
    'bisexual' => 'BiSexual',
    'het'     => 'Heterosexual',  # Self Explanatory
    'hetero'  => 'Heterosexual',
    'heterosexual' => 'Heterosexual',
    'tg'      => 'TransGender',   # Gender crossing (note that code 'trans' refers to transformation)
    'Transgender' => 'TransGender',
    'transgender' => 'TransGender',
    'cd'      => 'CrossDressing', # Men dressing as women and in women clothing, also vice-versa
    'Crossdressing' => 'CrossDressing',
    'crossdressing' => 'CrossDressing',
    'Crossdress' => 'CrossDressing',
    'crossdress' => 'CrossDressing',
    'herm'    => 'Hermaphrodite', # Person having both male and female genitalia
    'hermaphrodite' => 'Hermaphrodite',
    'shem'    => 'Shemale',	  # Trans women with male genitalia and female
    'shemale' => 'Shemale',       # breasts from breast augmentation and/or use of hormones

    # Story Types
    'fict'     => 'Fiction',	# Fictitious story, not true
    'fiction'  => 'Fiction',
    'true'    => 'True Story',	# A true story, according to the author.
    'hiFant'  => 'High Fantasy', # High Fantasy, sword and sorcery and such
    'fanfic'  => 'Fan Fiction', # A story using characters from movies, TV shows or Comics
    'celeb'   => 'Celebrity',	# A story about a real life celebrity, the
				# story is fictional of course.
    'ScFi'    => 'Science Fiction', # Science Fiction
    'Scifi'   => 'Science Fiction',
    'scifi'   => 'Science Fiction',
    'TimeTr'  => 'Time Travel',	# Science Fiction involving travel through time
    'robot'   => 'Robot',	# Humans having relations, sexual or otherwise,
				# with non-biological entities (mechanical
				# robots or full blown androids)
    'PostApoc' => 'Post Apocalypse', # Post Apocalyptic world.
    'hist'    => 'Historical',	# Story that is set in past history, mostly
				# related to famous people and events.
    'horror'  => 'Horror',	# Horror Elements in the story, erotic or otherwise.
    'humor'   => 'Humor',	# Contains humor or the story is a spoof
    'tears'   => 'Tear Jerker', # Sad story or containing sad moments
    'superhero' => 'Superhero', # Story contains Super Hero/Heroine
    'ESP'     => 'Extra Sensory Perception', # Perceiving things by means other
				# than the known senses, e.g. by telepathy or clairvoyance.
    'Zoo'     => 'Zoophilia ',	# Different from bestiality, it's about
				# relationships between humans and animals, not just sex.
    'space'   => 'Space',	# Story set in outer space
    'myst'    => 'Mystery',	# A suspensful story about a mystery
    'sport'   => 'Sports',	# A story with a sports theme
    'west'    => 'Western',	# Set in the old US west
    'doover'  => 'DoOver',	# Getting to do one's life all over again

    # Paranormal
    'paranormal' => 'Paranormal', # Ghosts, angels, spirits, poltergeists, etc
    'vamp'    => 'Vampires',	# Vampires in the story
    'vampire' => 'Vampires',
    'vampires' => 'Vampires',
    'furry'   => 'Furry',	# Anthropomorphic animals or 'taurs' (animal
				# bodies with anthropomorphic torsos) it also
				# includes other forms such as dragons.
    'non-anthro' => 'non-anthro', # Animal in form with human level
				# intelligence, e.g. Werewolves
    'were'    => 'Were animal', # A human that can turn into an animal temporarily.
    'zom'     => 'Zombies',	# Zombies in the story

    # Couples
    'cheat'   => 'Cheating',	# One party is cheating on their spouse
    'cheating' => 'Cheating',
    'wife'    => 'Slut Wife',	# Slutty wife
    'wimp'    => 'Wimp Husband', # Husband does not stand up for himself when
				# faced with a cheating wife
    'cuckold' => 'Cuckold',	# When man is being forced to accept
				# his wife's cheating against his will.
    'watch'   => 'Wife Watching', # Man enjoys watching his wife getting fucked
    'reven'   => 'Revenge',	# One party gets revenge for spouse's cheating

    # Incest
    'inc'     => 'Incest',	# sex between the members of the same family
    'incest'  => 'Incest',
    'mother'  => 'Mother',	# Self Explanatory
    'son'     => 'Son',		# Self Explanatory
    'bro'     => 'Brother',	# Self Explanatory
    'brother' => 'Brother',
    'sis'     => 'Sister',	# Self Explanatory
    'fath'    => 'Father',	# Self Explanatory
    'father'  => 'Father',
    'dau'     => 'Daughter',	# Self Explanatory
    'daughter' => 'Daughter',
    'cous'    => 'Cousins',	# Self Explanatory
    'cousin'  => 'Cousins',
    'cousins' => 'Cousins',
    'grand'   => 'Grand Parent', # Self Explanatory
    'Grandfather' => 'Grand Parent',
    'grandfather' => 'Grand Parent',
    'Grandmother' => 'Grand Parent',
    'grandmother' => 'Grand Parent',
    'Grandparent' => 'Grand Parent',
    'grandparent' => 'Grand Parent',
    'grand parent' => 'Grand Parent',
    'unc'     => 'Uncle',	# Self Explanatory
    'uncle'   => 'Uncle',
    'niece'   => 'Niece',	# Self Explanatory
    'aunt'    => 'Aunt',	# Self Explanatory
    'neph'    => 'Nephew',	# Self Explanatory
    'nephew'  => 'Nephew',
    'in-law'  => 'InLaws',	# Self Explanatory
    'inlaw'   => 'InLaws',
    'inlaws'   => 'InLaws',
    'Inlaws'   => 'InLaws',

    # BDSM Elements
    'BDSM'    => 'BDSM',	# Bondage and/or SadoMasochism
    'bdsm'    => 'BDSM',
    'bd/sm'   => 'BDSM',
    'Bondage' => 'BDSM',
    'bondage' => 'BDSM',
    'Sadomasochism' => 'BDSM',
    'sadomasochism' => 'BDSM',
    'D/S'     => 'DomSub',	# Story about domination, being sexual or otherwise
    'd/s'     => 'DomSub',
    'Domsub'  => 'DomSub',
    'domsub'  => 'DomSub',
    'Domination' => 'DomSub',
    'domination' => 'DomSub',
    'Lesdom'  => 'DomSub',
    'lesdom'  => 'DomSub',
    'Mdom'    => 'MaleDom',	# Male Dominant
    'mdom'    => 'MaleDom',
    'Fdom'    => 'FemaleDom',	# Female Dominant
    'fdom'    => 'FemaleDom',
    'span'    => 'Spanking',	# Somebody gets spanked in the story, willingly or unwillingly
    'spank'   => 'Spanking',
    'spanking' => 'Spanking',
    'rough'   => 'Rough',	# Rough sex
    'lght'    => 'Light Bond',  # Light Bondage, usually consensual and for
				# experimentation at the request of the one of the parties
    'humil'   => 'Humiliation', # Degradation of somebody in the story and/or
    'Humil'   => 'Humiliation',	# humiliating them publicly
    'sad'     => 'Sadistic',	# Somebody inflicting pain, physical or mental, for the hell of it
    'sadism'  => 'Sadistic',
    'Sadism'  => 'Sadistic',
    'tort'    => 'Torture',	# Self Explanatory
    'torture' => 'Torture',
    'snuff'   => 'Snuff',	# Killing being done during the act of sex.
				# (Non-sexual murder is in violent)

    # Groups
    'swing'   => 'Swinging',	# Trading Sexual Partners
    'swingers' => 'Swinging',
    'swinging' => 'Swinging',
    'gang'    => 'Gang Bang',   # Multiple men fucking same woman
    'GangBang' => 'Gang Bang',
    'Gangbang' => 'Gang Bang',
    'gangbang' => 'Gang Bang',
    'group'   => 'Group Sex',   # Multiple couples in the same place or a threesome
    'orgy'    => 'Orgy',	# Everybody is Fucking everybody in a group
    'harem'   => 'Harem',	# There is a harem involved in the story.
    'poly'    => 'Polygamy/Polyamory', # Multiple spouses or partners (multiple men or multiple women)
    'polygamy' => 'Polygamy/Polyamory',
    'Polygamy' => 'Polygamy/Polyamory',
    'polyamory' => 'Polygamy/Polyamory',
    'Polyamory' => 'Polygamy/Polyamory',

    # Interracial Elements
    'interr'  => 'Interracial',   # Sexual partners of different races
    'Interr'  => 'Interracial',
    'interrracial'  => 'Interracial',
    'WC'      => 'White Couple',  # Self Explanatory
    'BC'      => 'Black Couple',  # Self Explanatory
    'BF'      => 'Black Female',  # Self Explanatory
    'BM'      => 'Black Male',    # Self Explanatory
    'WM'      => 'White Male',    # Self Explanatory
    'WF'      => 'White Female',  # Self Explanatory
    'OM'      => 'Oriental Male', # Self Explanatory
    'OF'      => 'Oriental Female', # Self Explanatory
    'HM'      => 'Hispanic Male', # Self Explanatory
    'HF'      => 'Hispanic Female', # Self Explanatory

    # Sexual Activities
    '1st'     => 'First',	# One of the parties having sex is a virgin
    'first'   => 'First',	# One of the parties having sex is a virgin
    'safe'    => 'Safe Sex',    # Sex with proper protection
    'Safe'    => 'Safe Sex',
    'oral'    => 'Oral Sex',    # Self Explanatory
    'oral sex' => 'Oral Sex',
    'fellatio' => 'Oral Sex',
    'cunnilingus' => 'Oral Sex',
    'anal'    => 'Anal Sex',    # Self Explanatory
    'mastrb'  => 'Masturbation', # Self pleasuring, could be alone or while
				# with somebody else but no intercourse
    'masturbation' => 'Masturbation',
    'pett'    => 'Petting',     # Feeling up and such
    'petting' => 'Petting',
    'fingering' => 'Petting',
    'fist'    => 'Fisting',	# Hand and arm or foot insertion
    'Fist'    => 'Fisting',
    'toys'    => 'Sex Toys',	# Sex with the help of sex toys, such as dildos and vibrators
    'Toys'    => 'Sex Toys',
    'beast'   => 'Bestiality',  # Sex with animals
    'bestiality' => 'Bestiality',
    'squirt'  => 'Squirting',	# Female Ejaculation
    'squirting' => 'Squirting',
    'food'    => 'Food',        # Sex where food is involved, like doing it to
				# a pie or shoving a carrot
    'lac'     => 'Lactation',   # Drinking and playing with human milk
    'lact'    => 'Lactation',
    'lactation' => 'Lactation',
    'ws'      => 'Water Sports', # Peeing on each other
    'golden shower' => 'Water Sports',
    'enem'    => 'Enema',       # Getting an enema as a sexual thing
    'scat'    => 'Scatology',   # Playing with feces
    'preg'    => 'Pregnancy',   # Somebody gets pregnant in the story
    'Preg'    => 'Pregnancy',
    'creampie' => 'Cream Pie',  # Somebody licks a pussy full of cum
    'necro'   => 'Necrophilia', # Sex with a corpse
    'Necro'   => 'Necrophilia',
    'fart'    => 'Flatulence',  # Flatulence as a sexual fetish
    'spit'    => 'Spitting',    # Spitting as a sexual fetish
    'exhib'   => 'Exhibitionism', # Enjoyment of self exposure
    'Exhib'   => 'Exhibitionism',
    'voy'     => 'Voyeurism',   # Enjoyment of watching other people without their knowledge
    'Voy'     => 'Voyeurism',
    'voyeur'  => 'Voyeurism',
    'Voyeur'  => 'Voyeurism',
    'DP'      => 'Double Penetration', # Simultaneous Anal and Vaginal Penetration
    'dp'      => 'Double Penetration',

    # Fetishes
    'size'    => 'Size',        # Where the size of the sex parts (penis,
				# breasts, toys) plays a major role in the story
    'doct'    => 'Doctor/Nurse', # Medical Fetish, Doctor or Nurse Play a prominent role in the story
    'BBW'     => 'BBW',		# Big Beautiful Woman. Loving Big Women
    'feet'    => 'Foot Fetish', # Foot fetish
    'legs'    => 'Leg Fetish',  # Leg fetish, often with stockings, garters and heels.
    'hairy'   => 'Hairy Fetish', # with hairy men/women
    'mod'     => 'Body Modification', # Erotic body modification, including
				# piercings and tattoos, plays a prominent role.
    'amput'   => 'Amputee',	# Sex with an amputee
    'needles' => 'Needles',	# Includes injections and play or permanent piercings.
    'teach'   => 'Teacher/Student', # Teacher/student relations
    'teacher/student' => 'Teacher/Student',
    'teacher student' => 'Teacher/Student',
    'bbsit'   => 'Babysitter',	# Sex with babysitters fetish
    'BBr'     => 'Big Breasts', # Women with Big Breasts
    'clergy'  => 'Clergy',      # Involving members of the clergy (Priest, Bishops, nuns)
    'menses'  => 'Menstrual Play', # Sex play with a woman who's on menstrual cycle
    'Menses'  => 'Menstrual Play',
    'menstruation' => 'Menstrual Play',
    'Menstruation' => 'Menstrual Play',
    'public'  => 'Public Sex',	# Sexual events taking place in public
    'Public'  => 'Public Sex',

    # Other
    'slow'    => 'Slow',	# There is a story and plot development before
				# any sex occurs. Not a stroke story.
    'clas'    => 'Novel-Classic', # An old classic novel that is now in the public domain
    'bknk'    => 'Novel-Pocketbook', # An old published pocketbook novel that
				# is not in circulation any more and the
				# publisher has gone under, so it's not going to be published again
    'caution' => 'Caution',	# Story contains something that can't be
				# classified but requires caution on the reader's part
    '2nd'     => '2nd POV',	# Story written from the 2nd person POV
    'violent' => 'Violent',	# Violence in the story, not necessarily of sexual nature
    'work'    => 'Workplace',   # Story centres around a workplace setting
    'workplace' => 'Workplace',
    'sch'     => 'School',	# Mostly in an educational setting (high-school, college, University)
    'school'  => 'School',
    'schoolgirl' => 'School',
    'student' => 'School',
    'college' => 'School',
    'professor' => 'School',
    'university' => 'School',
    'secret'  => 'Secret',      # Secret or hidden or forbidden relationship
    'Secret'  => 'Secret',
    'Secret Love' => 'Secret',
    'Secret love' => 'Secret',
    'secret love' => 'Secret',
    'Forbidden Love' => 'Secret',
    'Forbidden love' => 'Secret',
    'forbidden love' => 'Secret',
    'trans'   => 'Transformation', # Un-natural physical transformation
    'prost'   => 'Prostitution', # Prostitution elements
    'prostitute' => 'Prostitution',
    'prostitution' => 'Prostitution',
    'nud'     => 'Nudism',      # Features scenes set in a naturist/nudist environment
    'nudism'  => 'Nudism',
    'nudist'  => 'Nudism',
    'Mil'     => 'Military',    # Story in a Military setting
    'military' => 'Military',
    'pornThea' => 'Porn Theatre', # Story has events in a porn theatre or Adult bookstore
    'cf'      => 'Cat-Fighting', # Women having a cat fight
    'catfight' => 'Cat-Fighting',
    'cannib'  => 'Cannibalism',	# Self explanatory
    'cannibalism' => 'Cannibalism',
);
hashvalue_key_self(\%sexcodes);

our %strangenames = (
    'brian d foy'	=> 'brian d foy',
    'brian d. foy'	=> 'brian d foy',
   );

our %strangefileas = (
    'foy, brian d'	=> 'd foy, brian',
    'foy, brian d.'	=> 'd foy, brian',
   );

our %validspecs = (
    'OEB12' => 'OEB12',
    'OPF20' => 'OPF20',
    'MOBI12' => 'MOBI12',
    );


#####################################################
########## CONSTRUCTORS AND INITIALIZATION ##########
#####################################################

my %rwfields = (
    'opffile'   => 'string', # OPF filename (with no path)
    'opfsubdir' => 'string', # Subdirectory name where opffile is found
    'erotic'    => 'scalar', # Enable special handling for erotic fiction
    'spec'      => 'string',
    );
my %rofields = (
    'topdir'   => 'string', # Top-level directory of the unpacked book
    'twig'     => 'scalar',
    'twig_unmodified' => 'scalar',
    'twigroot' => 'scalar',
    'errors'   => 'arrayref',
    'warnings' => 'arrayref',
    );
my %privatefields = (
);

# A simple 'use fields' will not work here: use takes place inside
# BEGIN {}, so the @...fields variables won't exist.
require fields;
fields->import(
    keys(%rwfields),keys(%rofields),keys(%privatefields)
    );


=head1 CONSTRUCTORS AND INITIALIZATION

=head2 C<new($filename)>

Instantiates a new EBook::Tools object.  If C<$filename> is specified,
it will also immediately initialize itself via the C<init> method.

=cut

sub new {
    my ($self,$filename) = @_;
    my $class = ref($self) || $self;
    my $subname = (caller(0))[3];
    debug(2,"DEBUG[",$subname,"]");
    my $bisg = EBook::Tools::BISG->new();

    $self = fields::new($class);

    if($filename) {
	debug(2,"DEBUG: new object from '",$filename,"'");
	$self->init($filename);
    }
    return $self;
}


=head2 C<init($filename)>

Initializes the object from an existing OPF file.  If C<$filename> is
specified and exists, the OEB object will be set to read and write to
that file before attempting to initialize.  Otherwise, if the object
currently points to an OPF file it will use that name.  If there is no
OPF filename data, and C<$filename> was not specified, it will make a
last-ditch attempt to find an OPF file first by looking in
META-INF/container.xml, and if nothing is found there, by looking in
the current directory for a single OPF file.

If no such files or found (or more than one is found), the
initialization croaks.

=cut

sub init :method {
    my ($self,$filename) = @_;
    my $fh_opffile;
    my $opfstring;
    my $subname = (caller(0))[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    $self->{topdir} = getcwd();

    if($filename) { $self->{opffile} = $filename; }

    if(!$self->{opffile})
    {
        $opfstring = find_opffile();
        $self->{opffile} = $opfstring if($opfstring);
    }

    if(!$self->{opffile})
    {
	croak($subname,"(): Unable to find an OPF file to work with!\n");
    }

    if(! -f $self->{opffile})
    {
	croak($subname,"(): '",$self->{opffile},
              "' does not exist or is not a regular file!")
    }

    if(-z $self->{opffile})
    {
	croak("OPF file '",$self->{opffile},"' has zero size!");
    }

    # At this point, we have definitely found an OPF file to work
    # with, but it might not be in the top level directory, and with
    # the exception of final book construction and EPUB metadata, we
    # want to be in the directory of the OPF.
    $self->{opfsubdir} = dirname($self->{opffile});
    $self->{opffile} = basename($self->{opffile});
    usedir($self->opfdir);

    debug(2,"DEBUG: init using '",$self->{opfsubdir},"/",$self->{opffile},"'");

    # Initialize the twig before use
    $self->{twig} = XML::Twig->new(
	keep_atts_order => 1,
	output_encoding => 'utf-8',
	pretty_print => 'record'
	);

    # Initialize a second copy to be used to check for changes
    $self->{twig_unmodified} = XML::Twig->new(
	keep_atts_order => 1,
	output_encoding => 'utf-8',
	pretty_print => 'record'
	);

    # Read and decode entities before parsing to avoid parsing errors
    open($fh_opffile,'<:encoding(:UTF-8)',$self->{opffile})
        or croak($subname,"(): failed to open '",$self->{opffile},
                 "' for reading!");
    read($fh_opffile,$opfstring,-s $self->opffile)
        or croak($subname,"(): failed to read from '",$self->{opffile},"'!");
    close($fh_opffile)
        or croak($subname,"(): failed to close '",$self->{opffile},"'!");

    # We use _decode_entities and the custom hash to decode, but also
    # see below for the regexp
    _decode_entities($opfstring,\%nonxmlentity2char);

    # This runs decode_entities on the substring containing just the
    # entity for every entity *except* the 5 predefined internal
    # entities.  It seems to be about the same speed as running
    # _decode_entities on a small file, but having the hash map as a
    # package variable has additional utility, so that technique gets
    # used instead.
#    $opfstring =~
#        s/(&
#            (?! (?:lt|gt|quot|apos|amp);
#            )\w+;
#          )
#         / decode_entities($1) /gex;

    $self->{twig}->parse($opfstring);
    $self->{twig_unmodified}->parse($opfstring);
    $self->{twigroot} = $self->{twig}->root;
    $self->opf_namespace;
    $self->twigcheck;
    debug(2,"DEBUG[/",$subname,"]");
    return $self;
}


=head2 C<init_blank(%args)>

Initializes an object containing nothing more than the basic OPF
framework, suitable for adding new documents when creating an e-book
from scratch.

=head3 Arguments

C<init_blank> takes up to three optional named arguments:

=over

=item C<opffile>

This specifies the OPF filename to use.  If not specified, defaults to
'content.opf'

=item C<author>

This specifies the content of the initial dc:creator element.  If not
specified, defaults to "Unknown Author".

=item C<title>

This specifies the content of the initial dc:title element. If not
specified, defaults to "Unknown Title".

=back

=head3 Example

 init_blank('opffile' => 'newfile.opf',
            'title' => 'The Great Unknown');

=cut

sub init_blank :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");


    my %valid_args = (
        'opffile' => 1,
        'author' => 1,
        'title' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    $args{opffile} ||= "content.opf";

    my $author = $args{author} || 'Unknown Author';
    my $title = $args{title} || 'Unknown Title';
    my $metadata;
    my $element;

    $self->{topdir} = getcwd();
    $self->{opfsubdir} = '.';
    $self->{opffile} = $args{opffile};
    $self->{twig} = XML::Twig->new(
	keep_atts_order => 1,
	output_encoding => 'utf-8',
	pretty_print => 'record'
        );
    $self->{twig_unmodified} = XML::Twig->new(
	keep_atts_order => 1,
	output_encoding => 'utf-8',
	pretty_print => 'record'
       );

    $element = XML::Twig::Elt->new('package');
    $self->{twig}->set_root($element);
    $self->{twig_unmodified}->set_root($element);
    $self->{twigroot} = $self->{twig}->root;
    $metadata = $self->{twigroot}->insert_new_elt('first_child','metadata');

    # dc:identifier
    $self->fix_packageid;

    # dc:title
    $element = $metadata->insert_new_elt('last_child','dc:title');
    $element->set_text($title);

    # dc:creator (author)
    $element = $metadata->insert_new_elt('last_child','dc:creator');
    $element->set_att('opf:role','aut');
    $element->set_text($author);

    $self->fix_opf20;
    return 1;
}


######################################
########## ACCESSOR METHODS ##########
######################################

=head1 ACCESSOR METHODS

The following methods return data deeper in the structure than the
auto-accessors, but still do not modify any object data or files.


=head2 C<adult()>

Returns the text of the Mobipocket-specific <Adult> element, if it
exists.  Expected values are 'yes' and undef.

=cut

sub adult :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $twigroot = $self->{twigroot};

    my $element = $twigroot->first_descendant(qr/^adult$/ix);
    return unless($element);

    if($element->text) { return $element->text; }
    else { return; }
}


=head2 C<contributor_list()>

Returns a list containing the text of all dc:contributor elements
(case-insensitive) or undef if none are found.

In scalar context, returns the first contributor, not the last.

=cut

sub contributor_list :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my @retval = ();
    my $twigroot = $self->{twigroot};

    my @elements = $twigroot->descendants(qr/dc:contributor/ix);
    return unless(@elements);

    foreach my $el (@elements)
    {
        push(@retval,$el->text) if($el->text);
    }
    return unless(@retval);
    if(wantarray) { return @retval; }
    else { return $retval[0]; }
}


=head2 C<coverimage()>

Returns the href to the cover image, or undef if none is found.

Checks the following in order:

=over

=item <reference type="other.ms-coverimage-standard">

=item <EmbeddedCover>

=item <meta name="cover"> (as href)

=item <meta name="cover"> (as item id)

=back

=cut

sub coverimage :method {
    my ($self) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $element;
    my $href;
    my $id;

    $element = $twigroot->first_descendant('reference[@type="other.ms-coverimage-standard"]');
    $href = $element->att('href') if $element;
    return $href if $href;

    $element = $twigroot->first_descendant('EmbeddedCover');
    $href = $element->text if $element;
    return $href if $href;

    $element = $twigroot->first_descendant('meta[@name="cover"]');
    if($element) {
        if(-f $element->att('content')) {
            return $element->att('content');
        }
        $id = $element->att('content');
        $element = $twigroot->first_descendant("item[\@id='${id}']");
        $href = $element->att('href') if $element;
        return $href if $href;
    }
    return;
}


=head2 C<date_list(%args)>

Returns the text of all dc:date elements (case-insensitive) matching
the specified attributes.

In scalar context, returns the first match, not the last.

Returns undef if no matches are found.

=head3 Arguments

=over

=item * C<id> - 'id' attribute that must be matched exactly for the
result to be added to the list

=item * C<event> 'opf:event' or 'event' attribute that must be
matched exactly for the result to be added to the list

=back

If both arguments are specified a value is added to the list if it
matches either one (i.e. the logic is OR).

=cut

sub date_list :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'id' => 1,
        'event' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my @elements = $self->{twigroot}->descendants(qr/^ dc:date $/ix);
    my @list = ();
    my $id;
    my $scheme;
    foreach my $el (@elements) {
        if($args{id}) {
            $id = $el->att('id') || '';
            if($id eq $args{id}) {
                push(@list,$el->text);
                next;
            }
        }
        if($args{event}) {
            $scheme = $el->att('opf:event') || $el->att('event') || '';
            if($scheme eq $args{event}) {
                push(@list,$el->text);
                next;
            }
        }
        next if($args{id} || $args{event});
        push(@list,$el->text);
    }
    return unless(@list);
    if(wantarray) { return @list; }
    else { return $list[0]; }
}


=head2 C<description()>

Returns the description of the e-book, if set, or undef otherwise.

=cut

sub description :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $twigroot = $self->{twigroot};

    my $element = $twigroot->first_descendant(qr/^dc:description$/ix);
    return unless($element);

    if($element->text) { return $element->text; }
    else { return; }
}


=head2 C<element_list(%args)>

Returns a list containing the text values of all elements matching the
specified criteria.

=head3 Arguments

=over

=item * C<cond>

The L<XML::Twig> search condition used to find the elements.
Typically this is just the GI (tag) of the element you wish to find,
but it can also be a C<qr//> expression, coderef, or anything else
that XML::Twig can work with.  See the XML::Twig documentation for
details.

If this is not specified, an error is added and the method returns
undef.

=item * C<id> (optional)

'id' attribute that must be matched exactly for the
result to be added to the list

=item * C<scheme> (optional)

'opf:scheme' or 'scheme' attribute that must be
matched exactly for the result to be added to the list

=item * C<event> (optional)

'opf:event' or 'event' attribute that must be matched exactly for the
result to be added to the list

=back

If more than one of the arguments C<id>, C<scheme>, or C<event> are
specified a value is added to the list if it matches any one (i.e. the
logic is OR).

=cut

sub element_list :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'cond' => 1,
        'id' => 1,
        'scheme' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    unless($args{cond}) {
        $self->add_error($subname,"(): no search condition specified");
        return;
    }

    my @elements = $self->{twigroot}->descendants($args{cond});
    my @list = ();
    my $id;
    my $scheme;
    foreach my $el (@elements) {
        if($args{id}) {
            $id = $el->att('id') || '';
            if($id eq $args{id}) {
                push(@list,$el->text);
                next;
            }
        }
        if($args{event}) {
            $scheme = $el->att('opf:event') || $el->att('event') || '';
            if($scheme eq $args{event}) {
                push(@list,$el->text);
                next;
            }
        }
        if($args{scheme}) {
            $scheme = $el->att('opf:scheme') || $el->att('scheme') || '';
            if($scheme eq $args{scheme}) {
                push(@list,$el->text);
                next;
            }
        }
        next if($args{id} || $args{event} || $args{scheme});
        push(@list,$el->text);
    }
    return unless(@list);
    if(wantarray) { return @list; }
    else { return $list[0]; }
}


=head2 C<errors()>

Returns an arrayref containing any generated error messages.

=cut

sub errors :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    return $self->{errors};
}


=head2 C<identifier()>

Returns the text of the dc:identifier element pointed to by the
'unique-identifier' attribute of the root 'package' element, or undef
if it could not be located.

=cut

sub identifier :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $identid = $self->{twigroot}->att('unique-identifier');
    return unless($identid);

    my $identifier = $self->{twig}->first_elt("*[\@id='$identid']");
    return unless($identifier);

    my $idtext = $identifier->text;
    return unless($idtext);

    return($idtext);
}


=head2 C<isbn_list(%args)>

Returns a list of all ISBNs matching the specified attributes.  See
L</twigelt_is_isbn()> for a detailed description of how the ISBN
elements are found.

Returns undef if no matches are found.

In scalar context returns the first match, not the last.

See also L</isbns(%args)>.

=head3 Arguments

=over

=item * C<id> (optional)

'id' attribute that must be matched exactly for the
result to be added to the list

=item * C<scheme> (optional)

'opf:scheme' or 'scheme' attribute that must be
matched exactly for the result to be added to the list

=back

If both arguments are specified a value is added to the list if it
matches either one (i.e. the logic is OR).

=cut

sub isbn_list :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'id' => 1,
        'scheme' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my @list = $self->element_list(cond => \&twigelt_is_isbn,
                                   %args);
    return unless(@list);
    if(wantarray) { return @list; }
    else { return $list[0]; }
}


=head2 C<isbns(%args)>

Returns all of the ISBN identifiers matching the specificied
attributes as a list of hashrefs, with one hash per ISBN identifier
presented in the order that the identifiers are found.  The hash keys
are 'id' (containing the value of the 'id' attribute), 'scheme'
(containing the value of either the 'opf:scheme' or 'scheme'
attribute, whichever is found first), and 'isbn' (containing the text
of the element).

If no entries are found, returns undef.

In scalar context returns the first match, not the last.

See also L</isbn_list(%args)>.

=head3 Arguments

C<isbns()> takes two optional named arguments:

=over

=item * C<id> - 'id' attribute that must be matched exactly for the
result to be added to the list

=item * C<scheme> - 'opf:scheme' or 'scheme' attribute that must be
matched exactly for the result to be added to the list

=back

If both arguments are specified a value is added to the list if it
matches either one (i.e. the logic is OR).

=cut

sub isbns :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'id' => 1,
        'scheme' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my @elements = $self->{twigroot}->descendants(\&twigelt_is_isbn);
    my @list = ();
    my $id;
    my $scheme;
    foreach my $el (@elements) {
        if($args{id}) {
            $id = $el->att('id') || '';
            if($id eq $args{id}) {
                push(@list,
                     {
                         'isbn' => $el->text,
                         'id'   => $el->att('id'),
                         'scheme'   => $el->att('scheme'),
                     });
                next;
            }
        }
        if($args{scheme}) {
            $scheme = $el->att('opf:scheme') || $el->att('scheme') || '';
            if($scheme eq $args{scheme}) {
                push(@list,
                     {
                         'isbn' => $el->text,
                         'id'   => $el->att('id'),
                         'scheme'   => $el->att('scheme'),
                     });
                next;
            }
        }
        next if($args{id} || $args{scheme});
        $scheme = $el->att('opf:scheme') || $el->att('scheme');
        push(@list,
             {
                 'isbn'   => $el->text,
                 'id'     => $el->att('id'),
                 'scheme' => $scheme,
             });
    }
    return unless(@list);
    if(wantarray) { return @list; }
    else { return $list[0]; }
}


=head2 C<languages()>

Returns a list containing the text of all dc:language
(case-insensitive) entries, or undef if none are found.

In scalar context returns the first match, not the last.

=cut

sub languages :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my @retval = ();
    my @elements = $self->{twigroot}->descendants(qr/^dc:language$/ix);
    foreach my $el (@elements) {
        push(@retval,$el->text) if($el->text);
    }
    return unless(@retval);
    if(wantarray) { return @retval; }
    else { return $retval[0]; }
}


=head2 C<manifest(%args)>

Returns all of the items in the manifest as a list of hashrefs, with
one hash per manifest item in the order that they appear, where the
hash keys are 'id', 'href', and 'media-type', each returning the
appropriate attribute, if any.

In scalar context, returns the first match, not the last.

=head3 Arguments

C<manifest()> takes four optional named arguments:

=over

=item * C<id> - 'id' attribute to match

=item * C<href> - 'href' attribute to match

=item * C<mtype> - 'media-type' attribute to match

=item * C<logic> - logic to use (valid values are 'and' or 'or', default: 'and')

=back

If any of the named arguments are specified, C<manifest()> will return
only items matching the specified criteria.  This is an exact
case-sensitive match, but it can (especially in the case of mtype)
still return multiple elements.

=head3 Return values

Returns undef if there is no <manifest> element directly underneath
<package>, or if <manifest> contains no items.

=head3 See also

L</manifest_hrefs()>, L</spine()>

=head3 Example

 @manifest = $ebook->manifest(id => 'ncx',
                              mtype => 'text/xml',
                              logic => 'or');

=cut

sub manifest :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'id' => 1,
        'href' => 1,
        'mtype' => 1,
        'logic' => 1
        );
    my %valid_logic = (
        'and' => 1,
        'or' => 1
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    if($args{logic}) {
        croak($subname,
              "(): logic must be 'and' or 'or' (got '",$args{logic},"')")
            if(!$valid_logic{$args{logic}});
    }
    else { $args{logic} = 'and'; }

    debug(1,"DEBUG: manifest() called with '",join(" ",keys(%args)),"'");

    my @elements;
    my @retarray;
    my $cond;

    if($args{id})
    {
        debug(1,"DEBUG: manifest searching on id");
        $cond = "item[\@id='$args{id}'";
    }
    if($args{href})
    {
        debug(1,"DEBUG: manifest searching on href");
        if($cond) { $cond .= " $args{logic} \@href='$args{href}'"; }
        else { $cond = "item[\@href='$args{href}'"; }
    }
    if($args{mtype})
    {
        debug(1,"DEBUG: manifest searching on mtype");
        if($cond) { $cond .= " $args{logic} \@media-type='$args{mtype}'"; }
        else { $cond = "item[\@media-type='$args{mtype}'"; }
    }
    if($cond) { $cond .= "]"; }
    else { $cond = "item"; }

    my $manifest = $self->{twigroot}->first_child('manifest');
    return unless($manifest);

    debug(1,"DEBUG: manifest search condition = '",$cond,"'");
    @elements = $manifest->children($cond);
    return unless(@elements);

    foreach my $el (@elements)
    {
        push(@retarray,
             {
                 'id' => $el->id,
                 'href' => $el->att('href'),
                 'media-type' => $el->att('media-type')
             });
    }
    if(wantarray) { return @retarray; }
    else { return $retarray[0]; }
}


=head2 C<manifest_hrefs()>

Returns a list of all of the hrefs in the current OPF manifest, or the
empty list if none are found.

In scalar context returns the first href, not the last.

See also: C<manifest()>, C<spine_idrefs()>

=cut

sub manifest_hrefs :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my @items;
    my $href;
    my $manifest;
    my $mimetype;
    my @retval = ();

    $manifest = $self->{twigroot}->first_child('manifest');
    if(! $manifest) { return @retval; }

    @items = $manifest->descendants('item');
    foreach my $item (@items) {
	$href = $item->att('href');
        if($href) {
            $mimetype = mimetype($href) || "UNKNOWN";
            debug(3,"DEBUG: '",$href,"' has mime-type '",$mimetype,"'");
            push(@retval,$href);
        }
    }
    debug(2,"DEBUG[/",$subname,"]");
    if(wantarray) { return @retval; }
    else { return $retval[0]; }
}


=head2 C<opf_namespace()>

Some OPF generators explicity assign 'opf:' in the gi as a prefix on
OPF elements.  This makes later parsing more complex and is
unnecessary, so this is stripped before any parsing takes place.

=cut

sub opf_namespace :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");

    my @elements = $self->{twig}->descendants(qr/^opf:/ix);
    foreach my $el (@elements)
    {
        my $gi = $el->gi;
        $gi =~ s/^opf:(.*)/$1/ix;
        $el->set_gi($gi);
    }
    return;
}


=head2 C<opfdir()>

Returns the full filesystem path to the directory where the OPF
metadata file will be stored, or undef if either the top-level
directory or the OPF subdirectory is not found.

=cut

sub opfdir :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    return unless($self->{topdir});
    return unless($self->{opfsubdir});

    if($self->{opfsubdir} eq '.') {
        return $self->{topdir};
    }
    return $self->{topdir} . '/' . $self->{opfsubdir};
}


=head2 C<opffile()>

Returns the name of the file where the OPF metadata will be stored or
undef if no value is found..

=cut

sub opffile :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    return unless($self->{opffile});
    return $self->{opffile};
}


=head2 C<opfpath()>

Returns the full filesystem path to the file where the OPF metadata
will be stored or undef if either the top level directory or the OPF
filename is not found.

=cut

sub opfpath :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    return unless($self->{topdir});
    return unless($self->{opfsubdir});
    return unless($self->{opffile});

    return $self->{topdir} . '/' . $self->{opfsubdir} . '/' . $self->{opffile};
}


=head2 C<primary_author()>

Finds the primary author of the book, defined as the first
'dc:creator' entry (case-insensitive) where either the attribute
opf:role="aut" or role="aut", or the first 'dc:creator' entry if no
entries with either attribute can be found.  Entries must actually
have text to be considered.

In list context, returns a two-item list, the first of which is the
text of the entry (the author name), and the second element of which
is the value of the 'opf:file-as' or 'file-as' attribute (where
'opf:file-as' is given precedence if both are present).

In scalar context, returns the text of the entry (the author name).

If no entries are found, returns undef.

Uses L</twigelt_is_author()> in the first half of the search.

=head3 Example

 my ($fileas, $author) = $ebook->primary_author;
 my $author = $ebook->primary_author;

=cut

sub primary_author :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my $twigroot = $self->{twigroot};
    my $element;
    my $fileas;

    $element = $twigroot->first_descendant(\&twigelt_is_author);
    $element = $twigroot->first_descendant(qr/dc:creator/ix) if(!$element);
    if(! $element) {
        carp("## WARNING: no dc:creator elements found!");
        return;
    }
    if(! $element->text) {
        carp("## WARNING: dc:creator element is empty!");
        return;
    }
    $fileas = $element->att('opf:file-as');
    $fileas = $element->att('file-as') unless($fileas);
    if(wantarray) { return ($element->text, $fileas); }
    else { return $element->text; }
}


=head2 C<print_errors()>

Prints the current list of errors to STDERR.

=cut

sub print_errors :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my $errorref = $self->{errors};

    if(!$self->errors) {
	debug(1,"DEBUG: no errors found!");
	return 1;
    }


    foreach my $error (@$errorref) {
	print "ERROR: ",$error,"\n";
    }
    return 1;
}


=head2 C<print_warnings()>

Prints the current list of warnings to STDERR.

=cut

sub print_warnings :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my $warningref = $self->{warnings};

    if(!$self->warnings) {
	debug(2,"DEBUG: no warnings found!");
	return 1;
    }

    foreach my $warning (@$warningref) {
	print "WARNING: ",$warning,"\n";
    }
    return 1;
}


=head2 C<print_opf()>

Prints the OPF file to the default filehandle

=cut

sub print_opf :method {
    my $self = shift;
    my $filehandle = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    if(defined $filehandle) { $self->{twig}->print($filehandle); }
    else { $self->{twig}->print; }
    return 1;
}


=head2 C<publishers()>

Returns a list containing the text of all dc:publisher
(case-insensitive) entries, or undef if none are found.

In scalar context returns the first match, not the last.

=cut

sub publishers :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my @pubs = ();
    my @elements = $self->{twigroot}->descendants(qr/^dc:publisher$/ix);
    foreach my $el (@elements) {
        push(@pubs,$el->text) if($el->text);
    }
    return unless(@pubs);
    if(wantarray) { return @pubs; }
    else { return $pubs[0]; }
}


=head2 C<retailprice()>

Returns a two-scalar list, the first scalar being the text of the
Mobipocket-specific <SRP> element, if it exists, and the second being
the 'Currency' attribute of that element, if it exists.

In scalar context, returns just the text (price).

Returns undef if the SRP element is not found.

=cut

sub retailprice :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $twigroot = $self->{twigroot};

    my $element = $twigroot->first_descendant(qr/^ SRP $/ix);
    return unless($element);
    if(wantarray) { return ($element->text,$element->att('Currency')); }
    else { return $element->text };
}


=head2 C<review()>

Returns the text of the Mobipocket-specific <Review> element, if it
exists.  Returns undef if one is not found.

=cut

sub review :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $twigroot = $self->{twigroot};

    my $element = $twigroot->first_descendant(qr/^review$/ix);
    return unless($element);
    return $element->text;
}


=head2 C<< rights('id' => 'identifier') >>

Returns a list containing the text of all of dc:rights or
dc:copyrights (case-insensitive) entries in the e-book, or undef if
none are found.

In scalar context returns the first match, not the last.

If the optional named argument 'id' is specified, it will only return
entries where the id attribute matches the specified identifier.
Although this still returns a list, if more than one entry is found, a
warning is logged.

Note that dc:copyrights is not a valid Dublin Core element -- it is
included only because some broken Mobipocket books use it.

=cut

sub rights :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my %valid_args = (
        'id' => 1,
        );
    foreach my $arg (keys %args)
    {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }


    my @rights = ();
    my $id = $args{id};
    my @elements = $self->{twigroot}->descendants(qr/^dc:(copy)?rights$/ix);

    foreach my $element (@elements)
    {
        if($id)
        {
            next if($element->att('id') ne $id);
            push @rights,$element->text if($element->text);
        }
        else { push @rights,$element->text if($element->text); }
    }

    if($id)
    {
        add_warning($subname
                     . "(): More than one rights entry found with id '"
                     . $id ."'" )
            if(scalar(@rights) > 1);
    }
    return unless(@rights);
    if(wantarray) { return @rights; }
    else { return $rights[0]; }
}


=head2 C<search_knownuids()>

Searches the OPF twig for the first dc:identifier (case-insensitive)
element with an ID matching known UID IDs.

Returns the ID if a match is found, undef otherwise

=cut

sub search_knownuids :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my @elements;

    @elements = $self->{twigroot}->descendants(\&twigelt_is_knownuid);
    return unless(@elements);
    debug(1,"DEBUG: found known UID '",$elements[0]->id,"'");
    return $elements[0]->id;
}


=head2 C<search_knownuidschemes()>

Searches descendants of the OPF twig element for the first
<dc:identifier> or <dc:Identifier> subelement with the attribute
'scheme' or 'opf:scheme' matching a known list of schemes for unique
IDs

NOTE: this is NOT a case-insensitive search!  If you have to deal with
really bizarre input, make sure that you run L</fix_oeb12()> or
L</fix_opf20()> before calling L</fix_packageid()> or L</fix_misc()>.

Returns the ID if a match is found, undef otherwise.

=cut

sub search_knownuidschemes :method {
    my ($self,$gi) = @_;
    if(!$gi) { $gi = 'dc:identifier'; }
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $topelement = $self->{twigroot};

    my @knownuidschemes = (
        'GUID',
	'UUID',
	'FWID',
	);

    # Creating a regexp to search on works, but doesn't let you
    # specify priority.  For that, you really do have to loop.
#    my $schemeregexp = "(" . join('|',@knownuidschemes) . ")";

    my @elems;
    my $id;

    my $retval = undef;

    foreach my $scheme (@knownuidschemes) {
	debug(2,"DEBUG: searching for scheme='",$scheme,"'");
	@elems = $topelement->descendants(
            "dc:identifier[\@opf:scheme=~/$scheme/ix or \@scheme=~/$scheme/ix]"
            );
        push @elems, $topelement->descendants(
            "dc:Identifier[\@opf:scheme=~/$scheme/ix or \@scheme=~/$scheme/ix]"
            );
	foreach my $elem (@elems) {
	    debug(2,"DEBUG: working on scheme '",$scheme,"'");
	    if(defined $elem) {
		if($scheme eq 'FWID') {
		    # Fictionwise has a screwy output that sets the ID
		    # equal to the text.  Fix the ID to just be 'FWID'
		    debug(1,"DEBUG: fixing FWID");
		    $elem->set_id('FWID');
		}

		$id = $elem->id;
                unless(defined $id) {
		    debug(1,"DEBUG: assigning ID from scheme '",$scheme,"'");
		    $id = uc($scheme);
		    $elem->set_id($id);
                }
                $retval = $id;
		debug(1,"DEBUG: found Package ID: ",$id);
		last;
	    } # if(defined $elem)
	} # foreach my $elem (@elems)
	last if(defined $retval);
    }
    debug(2,"[/",$subname,"]");
    return $retval;
}


=head2 C<spec()>

Returns the version of the OEB specification currently in use.  Valid
values are C<OEB12> and C<OPF20>.  This value will default to undef
until C<fix_oeb12> or C<fix_opf20> is called, as there is no way for
the object to know what specification is being conformed to (if any)
until it attempts to enforce it.

=cut

sub spec :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    return $self->{spec};
}


=head2 C<spine()>

Returns all of the manifest items referenced in the spine as a list of
hashrefs, with one hash per manifest item in the order that they
appear, where the hash keys are 'id', 'href', and 'media-type', each
returning the appropriate attribute, if any.

In scalar context, returns the first item, not the last.

Returns undef if there is no <spine> element directly underneath
<package>, or if <spine> contains no itemrefs.  If <spine> exists, but
<manifest> does not, or a spine itemref exists but points an ID not
found in the manifest, spine() logs an error and returns undef.

See also: L</spine_idrefs()>, L</manifest()>

=cut

sub spine :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $spine = $self->{twigroot}->first_child('spine');
    return unless($spine);
    my @spinerefs;
    my $idref;
    my $element;
    my @retarray;

    my $manifest = $self->{twigroot}->first_child('manifest');
    if(!$manifest)
    {
        $self->add_error(
            $subname . "(): <spine> found without <manifest>"
            );
        debug(1,"DEBUG: <spine> found without <manifest>!");
        return;
    }

    @spinerefs = $spine->children('itemref');
    return unless(@spinerefs);

    foreach my $spineref (@spinerefs)
    {
        $idref = $spineref->att('idref');
        if(!$idref)
        {
            $self->add_warning(
                $subname . "(): <itemref> found with no idref -- skipping"
                );
            debug(1,"DEBUG: <itemref> found with no idref -- skipping");
            next;
        }
        $element = $manifest->first_child("item[\@id='$idref']");
        if(!$element)
        {
            $self->add_error(
                $subname ."(): id '" . $idref . "' not found in manifest!"
                );
            debug(1,"DEBUG: id '",$idref," not found in manifest!");
            return;
        }
        push(@retarray,
             {
                 'id' => $element->id,
                 'href' => $element->att('href'),
                 'media-type' => $element->att('media-type')
             });
    }
    if(wantarray) { return @retarray; }
    else { return $retarray[0]; }
}


=head2 C<spine_idrefs()>

Returns a list of all of the idrefs in the current OPF spine, or the
empty list if none are found.

In scalar context, returns the first idref, not the last.

See also: L</spine()>, L</manifest_hrefs()>

=cut

sub spine_idrefs :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $spine = $self->{twigroot}->first_child('spine');;
    my @retval = ();
    my @itemrefs;
    my $idref;

    if(! $spine) { return @retval; }

    @itemrefs = $spine->children('itemref');
    foreach my $item (@itemrefs) {
	$idref = $item->att('idref');
	push(@retval,$idref) if($idref);
    }
    if(wantarray) { return @retval; }
    else { return $retval[0]; }
}


=head2 C<subject_list()>

Returns a list containing the text of all dc:subject elements or undef
if none are found.

In scalar context, returns the first subject, not the last.

=cut

sub subject_list :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my @retval = ();
    my $twigroot = $self->{twigroot};

    my @subjects = $twigroot->descendants(qr/dc:subject/ix);
    return unless(@subjects);

    foreach my $subject (@subjects) {
        push(@retval,$subject->text) if($subject->text);
    }
    return unless(@retval);
    if(wantarray) { return @retval; }
    else { return $retval[0]; }
}


=head2 C<title()>

Returns the title of the e-book, or undef if no dc:title element
(case-insensitive) exists.  If a dc:title element exists, but contains
no text, returns an empty string.

=cut

sub title :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my $twigroot = $self->{twigroot};
    my $retval;

    my $element = $twigroot->first_descendant(qr/^dc:title$/ix);
    return unless($element);
    $retval = $element->text || '';
    $retval = trim($retval);
    return $retval;
}


=head2 C<twig()>

Returns the raw L<XML::Twig> object used to store the OPF metadata.

Although this twig can be manipulated via the standard XML::Twig
methods, doing so requires caution and is not recommended.  In
particular, changing the root element from here will cause the
EBook::Tools internal twig and twigroot attributes to become unlinked
and the result of any subsequent action is not defined.

=cut

sub twig :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    return $self->{twig};
}


=head2 C<twigcheck()>

Croaks showing the calling location unless C<$self> has both a twig and a
twigroot, and the twigroot is <package>.  Used as a sanity check for
methods that use twig or twigroot.

=cut

sub twigcheck :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");

    my @calledfrom = caller(1);
    croak("twigcheck called from unknown location") if(!@calledfrom);

    croak($calledfrom[3],"(): undefined twig")
        if(!$self->{twig});
    croak($calledfrom[3],"(): twig isn't a XML::Twig")
        if( (ref $self->{twig}) ne 'XML::Twig' );
    croak($calledfrom[3],"(): twig root missing")
        if(!$self->{twigroot});
    croak($calledfrom[3],"(): twig root isn't a XML::Twig::Elt")
        if( (ref $self->{twigroot}) ne 'XML::Twig::Elt' );
    croak($calledfrom[3],"(): twig root is '" . $self->{twigroot}->gi
          . "' (needs to be 'package')")
        if($self->{twigroot}->gi ne 'package');
    debug(3,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<twigroot()>

Returns the raw L<XML::Twig> root element used to store the OPF
metadata.

This twig element can be manipulated via the standard XML::Twig::Elt
methods, but care should be taken not to attempt to cut this element
from its twig as doing so will cause the EBook::Tools internal twig
and twigroot attributes to become unlinked and the result of any
subsequent action is not defined.

=cut

sub twigroot :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    return $self->{twigroot};
}


=head2 C<warnings()>

Returns an arrayref containing any generated warning messages.

=cut

sub warnings :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    return $self->{warnings};
}


######################################
########## MODIFIER METHODS ##########
######################################

=head1 MODIFIER METHODS

Unless otherwise specified, all modifier methods return undef if an
error was added to the error list, and true otherwise (even if a
warning was added to the warning list).


=head2 C<add_document($href,$id,$mediatype)>

Adds a document to the OPF manifest and spine, creating <manifest> and
<spine> if necessary.  To add an item only to the OPF manifest, see
add_item().

=head3 Arguments

=over

=item C<$href>

The href to the document in question.  Usually, this is just a
filename (or relative path and filename) of a file in the current
working directory.  If you are planning to eventually generate a .epub
book, all hrefs MUST be in or below the current working directory.

The method returns undef if $href is not defined or empty.

=item C<$id>

The XML ID to use.  If not specified, defaults to the href with
invalid characters removed.

This must be unique not only to the manifest list, but to every
element in the OPF file.  If a duplicate ID exists, the method sets an
error and returns undef.

=item C<$mediatype> (optional)

The mime type of the document.  If not specified, will attempt to
autodetect the mime type, and if that fails, will default to
'application/xhtml+xml'.

=back

=cut

sub add_document :method {
    my ($self,$href,$id,$mediatype) = @_;
    my $subname = (caller(0))[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    $href = trim($href);
    return unless($href);

    my $twig = $self->{twig};
    my $topelement = $self->{twigroot};
    my $element;

    $id = $href unless($id);
    $id =~ s/[^\w.-]//gx; # Delete all nonvalid XML 1.0 namechars
    $id =~ s/^[.\d -]+//gx; # Delete all nonvalid XML 1.0 namestartchars

    $element = $twig->first_elt("*[\@id='$id']");
    if($element)
    {
        $self->add_error(
            $subname . "(): ID '" . $id . "' already exists"
            . " (in a '" . $element->gi ."' tag)"
            );
        return;
    }

    if(!$mediatype)
    {
	my $mimetype = mimetype($href);
	if($mimetype) { $mediatype = $mimetype; }
	else { $mediatype = "application/xhtml+xml"; }
	debug(2,"DEBUG: '",$href,"' has mimetype '",$mimetype,"'");
    }

    my $manifest = $topelement->first_child('manifest');
    $manifest = $topelement->insert_new_elt('last_child','manifest')
        if(!$manifest);
    my $spine = $topelement->first_child('spine');
    $spine = $topelement->insert_new_elt('last_child','spine')
        if(!$spine);

    my $item = $manifest->insert_new_elt('last_child','item');
    $item->set_id($id);
    $item->set_att(
	'href' => $href,
	'media-type' => $mediatype
	);

    my $itemref = $spine->insert_new_elt('last_child','itemref');
    $itemref->set_att('idref' => $id);

    return 1;
}


=head2 C<add_error(@errors)>

Adds @errors to the list of object errors.  Each member of
@errors should be a string containing the entire text of the
error, with no ending newline.

SEE ALSO: L</add_warning()>, L</clear_errors()>, L</clear_warnerr()>

=cut

sub add_error :method {
    my ($self,@newerror) = @_;
    my $subname = (caller(0))[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");

    my $currenterrors;
    $currenterrors = $self->{errors} if($self->{errors});

    if(@newerror) {
        my $error = join('',@newerror);
	debug(1,"ERROR: ",$error);
	push(@$currenterrors,$error);
    }
    $self->{errors} = $currenterrors;
    return 1;
}


=head2 C<add_identifier(%args)>

Creates a new dc:identifier element containing the specified text, id,
and scheme.

If a <dc-metadata> element exists underneath <metadata>, the
identifier element will be created underneath the <dc-metadata> in OEB
1.2 format, otherwise the title element is created underneath
<metadata> in OPF 2.0 format.

Returns the twig element containing the new identifier.

=head3 Arguments

C<add_identifier()> takes three named arguments, one mandatory, two
optional.

=over

=item * C<text> - the text of the identifier.  This is mandatory, and
the method croaks if it is not present.

=item * C<scheme> - 'opf:scheme' or 'scheme' attribute to be added (optional)

=item * C<id> - 'id' attribute to be added.  If this is specified, and
the id is already in use, a warning will be added but the method will
continue, removing the id attribute from the element that previously
contained it.

=back

=cut

sub add_identifier :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        'scheme' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    croak($subname,"(): identifier text not specified")
        unless($args{text});

    $self->fix_metastructure_basic();
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $element;
    my $idelem;
    my $newid = $args{id};
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    if($dcmeta) {
        $element = $dcmeta->insert_new_elt('last_child','dc:Identifier');
        $element->set_att('scheme' => $args{scheme}) if($args{scheme});
    }
    else {
        $element = $meta->insert_new_elt('last_child','dc:identifier');
        $element->set_att('opf:scheme' => $args{scheme}) if($args{scheme});
    }
    $element->set_text($args{text});

    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);

    return $element;
}


=head2 C<add_item($href,$id,$mediatype)>

Adds a document to the OPF manifest (but not spine), creating
<manifest> if necessary.  To add an item only to both the OPF manifest
and spine, see add_document().

=head3 Arguments

=over

=item C<$href>

The href to the document in question.  Usually, this is just a
filename (or relative path and filename) of a file in the current
working directory.  If you are planning to eventually generate a .epub
book, all hrefs MUST be in or below the current working directory.

=item C<$id>

The XML ID to use.  If not specified, defaults to the href with all
nonword characters removed.

This must be unique not only to the manifest list, but to every
element in the OPF file.  If a duplicate ID exists, the method sets an
error and returns undef.

=item C<$mediatype> (optional)

The mime type of the document.  If not specified, will attempt to
autodetect the mime type, and if that fails, will set an error and
return undef.

=back

=cut

sub add_item :method {
    my ($self,$href,$id,$mediatype) = @_;
    my $subname = (caller(0))[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    $href = trim($href);
    return unless($href);

    my $twig = $self->{twig};
    my $topelement = $self->{twigroot};
    my $element;

    $id = $href unless($id);
    $id =~ s/[^\w.-]//gx; # Delete all nonvalid XML 1.0 namechars
    if($id =~ /^[.\d -]+/x) {
       # We're starting with a nonvalid XML 1.0 namestartchar, so
       # prefix with id_
        $id = 'id_' . $id;
    }

    $element = $twig->first_elt("*[\@id='$id']");
    if($element) {
        $self->add_error(
            $subname . "(): ID '" . $id . "' already exists"
            . " (in a '" . $element->gi ."' tag)"
            );
        debug(2,"DEBUG[/",$subname,"]");
        return;
    }

    if(!$mediatype) {
	my $mimetype = mimetype($href);
	if($mimetype) { $mediatype = $mimetype; }
	else { $mediatype = "application/xhtml+xml"; }
	debug(2,"DEBUG: '",$href,"' has mimetype '",$mediatype,"'");
    }

    my $manifest = $self->{twigroot}->first_child('manifest');
    $manifest = $topelement->insert_new_elt('last_child','manifest')
        if(!$manifest);

    debug(2,"DEBUG: adding item '",$id,"': '",$href,"'");
    my $item = $manifest->insert_new_elt('last_child','item');
    $item->set_id($id);
    $item->set_att(
	'href' => $href,
	'media-type' => $mediatype
	);

    debug(3,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 add_metadata(%args)

Creates a metadata element with the specified text, attributes, and parent.

If a <dc-metadata> element exists underneath <metadata>, the language
element will be created underneath the <dc-metadata> and any standard
attributes will be created in OEB 1.2 format, otherwise the element is
created underneath <metadata> in OPF 2.0 format.

Returns 1 on success, returns undef if no gi or if no text was specified.

=cut

=head3 Arguments

=over

=item C<gi>

The generic identifier (tag) of the metadata element to alter or
create.  If not specified, the method sets an error and returns undef.

=item C<parent>

The generic identifier (tag) of the parent to use for any newly
created element.  If not specified, defaults to 'dc-metadata' if
'dc-metadata' exists underneath 'metadata', and 'metadata' otherwise.

A newly created element will be created under the first element found
with this gi.  A modified element will be moved under the first
element found with this gi.

Newly created elements will use OPF 2.0 attribute names if the parent
is 'metadata' and OEB 1.2 attribute names otherwise.

=item C<text>

This specifies the element text to set.  If not specified, the method
sets an error and returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=item C<fileas> (optional)

This specifies the file-as attribute to set on the element.

=item C<role> (optional)

This specifies the role attribute to set on the element.

=item C<scheme> (optional)

This specifies the scheme attribute to set on the element.

=back

=head3 Example

 $retval = $ebook->add_metadata(gi => 'AuthorNonstandard',
                                text => 'Element Text',
                                id => 'customid',
                                fileas => 'Text, Element',
                                role => 'xxx',
                                scheme => 'code');

=cut

sub add_metadata :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    my %valid_args = (
        'gi' => 1,
        'parent' => 1,
        'text' => 1,
        'id' => 1,
        'fileas' => 1,
        'role' => 1,
        'scheme' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $gi = $args{gi};
    unless($gi) {
        $self->add_error($subname,"(): no gi specified");
        return;
    }

    my $text = $args{text};
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    my $newid = $args{id};
    my $idelem;
    my $element;
    my $meta;
    my $dcmeta;
    my $parent;
    my %dcatts;

    $self->fix_metastructure_basic();
    $parent =  $self->{twigroot}->first_descendant(qr/^ $args{parent} $/ix)
        if($args{parent});
    $meta = $self->{twigroot}->first_child('metadata');
    $dcmeta = $meta->first_child('dc-metadata');
    $parent = $parent || $dcmeta || $meta;
    if($parent->gi eq 'metadata') {
        %dcatts = (
            'file-as' => 'opf:file-as',
            'role' => 'opf:role',
            'scheme' => 'opf:scheme',
            );
    }
    else {
        %dcatts = (
            'file-as' => 'file-as',
            'role' => 'role',
            'scheme' => 'scheme'
        );
    }

    debug(2,"DEBUG: creating '",$gi,"' under <",$parent->gi,">");
    $element = $parent->insert_new_elt('last_child',$gi);
    $element->set_att($dcatts{'file-as'},$args{fileas})
        if($args{fileas});
    $element->set_att($dcatts{'role'},$args{role})
        if($args{role});
    $element->set_att($dcatts{'scheme'},$args{scheme})
        if($args{scheme});
    $element->set_text($text);

    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);
    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' to a '",$element->gi,"'!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 C<add_subject(%args)>

Creates a new dc:subject element containing the specified text, code,
and id.

If a <dc-metadata> element exists underneath <metadata>, the
subject element will be created underneath the <dc-metadata> in OEB
1.2 format, otherwise the title element is created underneath
<metadata> in OPF 2.0 format.

Returns the twig element containing the new subject.

=head3 Arguments

C<add_subject()> takes four named arguments, one mandatory, three
optional.

=over

=item * C<text> - the text of the subject.  This is mandatory, and
the method croaks if it is not present.

=item * C<scheme> (optional) - 'opf:scheme' or 'scheme' attribute to
be added.  Be warned that neither the OEB 1.2 nor the OPF 2.0
specifications allow a scheme to be added to this element, so if this
is specified, the resulting OPF file will fail to validate against
either standard.

=item * C<basiccode> (optional) - 'BASICCode' attribute to be added.
Be warned that this is a Mobipocket-specific attribute that does not
exist in either the OEB 1.2 nor the OPF 2.0 specifications, so if this
is specified, the resulting OPF file will fail to validate against
either standard.

=item * C<id> (optional) - 'id' attribute to be added.  If this is
specified, and the id is already in use, a warning will be added but
the method will continue, removing the id attribute from the element
that previously contained it.

=back

=cut

sub add_subject :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        'scheme' => 1,
        'basiccode' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    croak($subname,"(): subject text not specified")
        unless($args{text});

    $self->fix_metastructure_basic();
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $element;
    my $idelem;
    my $newid = $args{id};
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    if($dcmeta) {
        $element = $dcmeta->first_child('dc:Subject[string()="' . $args{text} .  '"]');
        if(! $element) {
            $element = $dcmeta->insert_new_elt('last_child','dc:Subject');
        }
        $element->set_att('scheme' => $args{scheme}) if($args{scheme});
    }
    else {
        $element = $meta->first_child('dc:subject[string()="' . $args{text} .  '"]');
        if(! $element) {
            $element = $meta->insert_new_elt('last_child','dc:subject');
        }
        $element->set_att('opf:scheme' => $args{scheme}) if($args{scheme});
    }
    $element->set_text($args{text});
    $element->set_att('BASICCode' => $args{basiccode}) if($args{basiccode});

    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);

    return $element;
}


=head2 C<add_warning(@newwarning)>

Joins @newwarning to a single string and adds it to the list of object
warnings.  The warning should not end with a newline newline.

SEE ALSO: L</add_error()>, L</clear_warnings()>, L</clear_warnerr()>

=cut

sub add_warning :method {
    my ($self,@newwarning) = @_;
    my $subname = (caller(0))[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");

    my @currentwarnings;
    @currentwarnings = @{$self->{warnings}} if($self->{warnings});

    if (@newwarning) {
        my $warning = join('',@newwarning);
	debug(1,"WARNING: ",$warning);
	push(@currentwarnings,$warning);
    }
    $self->{warnings} = \@currentwarnings;

    debug(3,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<clear_errors()>

Clear the current list of errors

=cut

sub clear_errors :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    $self->{errors} = ();
    return 1;
}


=head2 C<clear_warnerr()>

Clear both the error and warning lists

=cut

sub clear_warnerr :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    $self->{errors} = ();
    $self->{warnings} = ();
    return 1;
}


=head2 C<clear_warnings()>

Clear the current list of warnings

=cut

sub clear_warnings :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    $self->{warnings} = ();
    return 1;
}


=head2 C<delete_meta_filepos()>

Deletes metadata elements with the attribute 'filepos' underneath
the given parent element

These are secondary metadata elements included in the output from
mobi2html may that are not used.

=cut

sub delete_meta_filepos :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my @elements = $self->{twigroot}->descendants('metadata[@filepos]');
    foreach my $el (@elements) {
	$el->delete;
    }
    return 1;
}


=head2 C<delete_subject(%args)>

Deletes dc:subject and dc:Subject elements based on text content or
the id, scheme, or basiccode attributes.  Matches are case-sensitive.

Specifying multiple arguments will delete subject matching any of them.

This has the same potential arguments as add_subject.

Returns the count of elements deleted.

=cut

sub delete_subject :method {
    my ($self,%args) = @_;
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        'scheme' => 1,
        'basiccode' => 1,
        );
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my @elements;
    my $count = 0;

    if(defined $args{text}) {
        @elements = $self->{twigroot}->descendants('dc:subject[text()="' . $args{text} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:Subject[text()="' . $args{text} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
    }

    if($args{id}) {
        @elements = $self->{twigroot}->descendants('dc:subject[@id="' . $args{id} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:Subject[@id="' . $args{id} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
    }

    if($args{scheme}) {
        @elements = $self->{twigroot}->descendants('dc:subject[@scheme="' . $args{scheme} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:subject[@opf:scheme="' . $args{scheme} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:Subject[@scheme="' . $args{scheme} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:Subject[@opf:scheme="' . $args{scheme} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
    }

    if($args{basiccode}) {
        @elements = $self->{twigroot}->descendants('dc:subject[@BASICCode="' . $args{id} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
        @elements = $self->{twigroot}->descendants('dc:Subject[@BASICCode="' . $args{id} . '"]');
        foreach my $el (@elements) {
            $el->delete;
            $count++;
        }
    }
    debug(2,"DEBUG: Deleted ",$count," elements");
    return $count;
}


=head2 C<fix_creators()>

Normalizes creator and contributor names and file-as attributes

Names are normalized to 'First Last' format, while file-as attributes
are normalized to 'Last, First' format.

This can damage some unusual names that do not match standard
capitalization formats, so it is not made part of L</fix_misc()>.

=cut

sub fix_creators :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my @elements = $twigroot->descendants(qr/dc:(creator|contributor)/ix);
    my $nameparse = Lingua::EN::NameParse->new(
        allow_reversed  => 1,
        extended_titles => 1,
        force_case      => 1,
        lc_prefix       => 1,
       );
    foreach my $el (@elements) {
        my $fileas = $el->att('opf:file-as') || '';
        my $name = $el->text || '';
        my $fixed;
        $name = trim($name);

        if ( $nameparse->parse($name) ) {
	    $self->add_warning(
                "failure while parsing name: ",
                {$nameparse->properties}->{non_matching});
        }
        else {
            $fixed = $nameparse->case_all;
            if($fixed and $strangenames{lc $fixed}) {
                $fixed = $strangenames{lc $fixed};
            }
            debug(2,"DEBUG: creator name '",$name,"' -> '",
                  $fixed,"'");
            $el->set_text($fixed);

            $fixed = $nameparse->case_all_reversed;
            if($fixed and $strangefileas{lc $fixed}) {
                $fixed = $strangefileas{lc $fixed};
            }
            if($fixed) {
                debug(2,"DEBUG: creator file-as '",$fileas,"' -> '",
                      $fixed,"'");
                $el->set_att('opf:file-as',$fixed);
            }
            else {
                debug(2,"DEBUG: removing empty creator file-as from creator '",
                      $name,"'");
                $el->del_att('opf:file-as');
            }
        }
    }
    return;
}


=head2 C<fix_dates()>

Standardizes all <dc:date> elements via fix_datestring().  Adds a
warning to the object for each date that could not be fixed.

Called from L</fix_misc()>.

=cut

sub fix_dates :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my @dates;
    my $newdate;

    @dates = $self->{twigroot}->descendants('dc:date');
    push(@dates,$self->{twigroot}->descendants('dc:Date'));

    foreach my $dcdate (@dates) {
	if(!$dcdate->text) {
	    $self->add_warning(
                "WARNING: found dc:date with no value -- skipping");
	}
	else {
	    $newdate = fix_datestring($dcdate->text);
	    if(!$newdate) {
		$self->add_warning(
		    sprintf("fixmisc(): can't deal with date '%s' -- skipping",
                            $dcdate->text)
		    );
	    }
	    elsif($dcdate->text ne $newdate) {
		debug(2,"DEBUG: setting date from '",$dcdate->text,
                      "' to '",$newdate,"'");
		$dcdate->set_text($newdate);
	    }
	}
    }
    return 1;
}


=head2 C<fix_guide()>

Fixes problems related to the OPF guide elements, specifically:

=over

=item * Ensures the guide element exists

=item * Moves all reference elements directly underneath the guide element

=item * Finds nonstandard reference types and either converts them to
standard or prefaces them with 'other.'

=item * Finds reference elements with a href with only an anchor
portion and assigns them to the first spine href.  This only works if
the spine is in working condition, so it may be wise to run
L</fix_spine()> before C<fix_guide()> if the input is expected to be
very badly broken.

=back

Logs a warning if a reference href is found that does not appear in
the manifest.

=cut

sub fix_guide :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $guide = $twigroot->first_descendant('guide');
    my $parent;
    my $href;
    my $type;
    my @spine;

    # If <guide> doesn't exist, create it
    unless($guide) {
        debug(1,"DEBUG: creating <guide>");
        $guide = $twigroot->insert_new_elt('last_child','guide');
    }

    # Make sure that the guide is a child of the twigroot,
    $parent = $guide->parent;
    if( $parent->cmp($twigroot) ) {
        debug(1,"DEBUG: moving <guide>");
        $guide->move('last_child',$twigroot);
    }


    my @elements = $twigroot->descendants(qr/^reference$/ix);
    foreach my $el (@elements) {
        $type = $el->att('type');
        if($referencetypes{$type}) {
            $el->set_att('type',$referencetypes{$type});
        }
        elsif($type !~ /^other./x) {
            $type = 'other.' . $type;
            $el->set_att('type',$type);
        }

        $href = $el->att('href');
        if (!$href) {
            # No href means it is broken.
            # Leave it alone, but log a warning
            $self->add_warning(
                "fix_guide(): <reference> with no href -- skipping");
            next;
        }
        if ($href =~ /^#/) {
            # Anchor-only href.  Attempt to fix from the first
            # spine entry
            @spine = $self->spine;
            if (!@spine) {
                $self->add_warning(
                    "fix_guide(): Cannot correct reference href '",$href,
                    "', spine is empty");
            }
            elsif (!$spine[0]->{href}) {
                $self->add_warning(
                    "fix_guide(): Cannot correct reference href '",$href,
                    "', cannot find href for first spine entry");
            }
            else {
                debug(1,"DEBUG: correcting reference href from '",$href,
                      "' to '",$spine[0]->{href} . $href,"'");
                $el->set_att('href',$spine[0]->{href} . $href);
            }
        }
        debug(3,"DEBUG: processing reference '",$href,"')");
        $el->move('last_child',$guide);
    } # foreach my $el (@elements)

    return 1;
}


=head2 C<fix_languages(%args)>

Checks through the <dc:language> elements (case-insensitive) and
removes any duplicates.  If no <dc:language> elements are found, one
is created.

TODO: Also convert language names to IANA language and region codes.

=head3 Arguments

=over

=item * C<default>

The default language string to use when creating a new language
element.  If not specified, defaults to 'en'.

=back

=cut

sub fix_languages :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my %valid_args = (
        'default' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $twigroot = $self->{twigroot};
    my $defaultlang = $args{default} || 'en';
    my $langel;
    my @elements = $twigroot->descendants(qr/dc:language/ix);
    while($langel = shift(@elements) ) {
        foreach my $el (@elements) {
            $el->delete if(twigelt_detect_duplicate($el,$langel) );
        }
    }

    @elements = $self->languages;
    if(!@elements) {
        $self->set_language(text => $defaultlang);
    }
    return 1;
}


=head2 C<fix_links()>

Checks through the links in the manifest and checks them for anything
they might link to, adding anything missing to the manifest.

A warning is added for every manifest item missing a href.

If no <manifest> element exists directly underneath the <package>
root, or <manifest> contains no items, the method logs a warning and
returns undef.  Otherwise, it returns 1.

=cut

sub fix_links :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};

    my $manifest = $twigroot->first_child('manifest');
    my @unchecked;

    # The %links hash points a href to one of three values:
    # * undef : the link has not been checked at all
    # * 0 : the link has been checked and is not reachable
    # * 1 : the link has been checked and exists
    my %links;
    my @newlinks;
    my $href;
    my $mimetype;

    my %linking_mimetypes = (
        'text/html' => 1,
        'text/xhtml' => 1,
        'text/xml' => 1,
        'text/x-oeb1-document' => 1,
        'application/atom+xml' => 1,
        'application/xhtml+xml' => 1,
        'application/xml' => 1
        );

    if(!$manifest) {
        $self->add_warning(
            "fix_links(): no manifest found!"
            );
        return;
    }

    @unchecked = $self->manifest_hrefs;
    if(!@unchecked) {
        $self->add_warning(
            "fix_links(): empty manifest found!"
            );
        return;
    }

    # Initialize %links, so we don't try to add something already in
    # the manifest
    foreach my $mhref (@unchecked) {
        $mhref = uri_unescape($mhref);
        $links{$mhref} = undef unless(exists $links{$mhref});
    }

    while(@unchecked) {
        debug(3,"DEBUG: ",scalar(@unchecked),
              " items left to check at start of loop");
        $href = shift(@unchecked);
        $href = trim($href);
        $href = uri_unescape($href);
        debug(3,"DEBUG: checking '",$href,"'");
        next if(defined $links{$href});

        # Skip mailto: and news: links
        if($href =~ m#(mailto|news):#ix) {
            debug(1,"DEBUG: mailto link '",$href,"' skipped");
            $links{$href} = 0;
            next;
        }

        # Skip URIs for now
        if($href =~ m#^ \w+://#ix) {
            debug(1,"DEBUG: URI '",$href,"' skipped");
            $links{href} = 0;
            next;
        }
        if(! -f $href) {
            $self->add_warning(
                "fix_links(): '" . $href . "' not found"
                );
            $links{$href} = 0;
            next;
        }

        $mimetype = mimetype($href);

        if(!$linking_mimetypes{$mimetype}) {
            debug(2,"DEBUG: '",$href,"' has mimetype '",$mimetype,
                "' -- not checking");
            $links{$href} = 1;
            next;
        }

        debug(1,"DEBUG: finding links in '",$href,"'");
        @newlinks = find_links($href);
        trim(@newlinks) if(@newlinks);
        $links{$href} = 1;
        foreach my $newlink (sort @newlinks) {
            # Skip mailto: and news: links
            if($newlink =~ m#(mailto|news):#ix) {
                debug(1,"DEBUG: mailto link '",$href,"' skipped");
                next;
            }
            # Skip URIs for now
            elsif($newlink =~ m#^ \w+://#ix) {
                debug(1,"DEBUG: URI '",$newlink,"' skipped");
                next;
            }
            elsif(!exists $links{$newlink}) {
                debug(2,"DEBUG: adding '",$newlink,"' to the manifest");
                push(@unchecked,$newlink);
                $self->add_item($newlink);
                $links{$newlink} = 1;
            }
        }
        debug(2,"DEBUG: ",scalar(@unchecked),
            " items left to check at end of loop");
    } # while(@unchecked)
    debug(2,"DEBUG[/",$subname,"]");
    return 1;
}

=head2 C<fix_manifest()>

Finds all <item> elements and moves them underneath <manifest>,
creating <manifest> if necessary.

Logs a warning but continues if it finds an <item> with a missing id
or href attribute.  If both id and href attributes are missing, logs a
warning, skips moving the item entirely (unless it was already
underneath <manifest>, in which case it is moved to preserve its sort
order along all other items under <manifest>), but otherwise
continues.

=cut

sub fix_manifest :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};

    my $spec = $self->spec || '';
    my $manifest = $twigroot->first_descendant('manifest');
    my @elements;
    my @extras;
    my $parent;

    my $href;
    my $id;
    my $type;

    # If <manifest> doesn't exist, create it
    if(! $manifest) {
	debug(1,"DEBUG: creating <manifest>");
	$manifest = $twigroot->insert_new_elt('last_child','manifest');
    }

    # Make sure that the manifest is the first child of the twigroot,
    # which should be <package>
    $parent = $manifest->parent;
    if($parent != $twigroot) {
	debug(1,"DEBUG: moving <manifest>");
	$manifest->move('first_child',$twigroot);
    }

    # Find and merge any other manifests
    @extras = $twigroot->descendants('manifest');
    shift @extras;
    foreach my $extra (@extras) {
        my @elements = $extra->children;
        foreach my $el (@elements) {
            debug(2,"DEBUG: moving <",$el->gi,"> into primary manifest");
            $el->move('last_child',$manifest);
        }
        $extra->delete;
    }

    @elements = $twigroot->descendants(qr/^item$/ix);
    foreach my $el (@elements) {
        $href = $el->att('href');
        $id = $el->id;
        $type = $el->att('media-type');

        # Convert to OPF2.0 document types if necessary
        if($spec eq 'OPF20') {
            if($type eq "text/x-oeb1-document") {
                $type = "application/xhtml+xml";
                $el->set_att('media-type',$type);
            }
            if($id eq 'ncx') {
                $type = 'application/x-dtbncx+xml';
                $el->set_att('media-type',$type);
            }

        }

        if(!$id) {
            if(!$href) {
                # No ID, no href, there's something very fishy here,
                # so log a warning.
                # If it is already underneath <manifest>, move it to
                # preserve sort order, but otherwise leave it alone
                $self->add_warning(
                    "fix_manifest(): found item with no id or href"
                    );
                debug(1,"fix_manifest(): found item with no id or href");
                if($el->parent == $manifest) {
                    $el->move('last_child',$manifest);
                }
                else {
                    print "DEBUG: skipping item with no id or href\n"
                        if($debug);
                }
                next;
            } # if(!$href)
            # We have a href, but no ID.  Log a warning, but move it anyway.
            $self->add_warning(
                'fix_manifest(): handling item with no ID! '
                . sprintf "(href='%s')",$href
                );
            debug(1,"DEBUG: processing item with no id (href='",$href,"')");
            $el->move('last_child',$manifest);
        } # if(!$id)
        if(!$href) {
            # We have an ID, but no href.  Log a warning, but move it anyway.
            $self->add_warning(
                "fix_manifest(): item with id '" . $id . "' has no href!"
                );
            debug(1,"fix_manifest(): item with id '",$id,"' has no href!");
            $el->move('last_child',$manifest);
        }
        else {
            # We have an ID and a href
            debug(3,"DEBUG: processing item '",$id,"' (href='",$href,"')");
            $el->move('last_child',$manifest);
        }
    }
    return 1;
}

=head2 C<fix_metastructure_basic()>

Verifies that <metadata> exists (creating it if necessary), and moves
it to be the first child of <package>.  If additional <metadata>
elements exist, their children are moved into the first one found and
then the extras are deleted.

Used in L</fix_metastructure_oeb12()>, L</fix_packageid()>, and
L</set_primary_author(%args)>.

=cut

sub fix_metastructure_basic :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $metadata = $twigroot->first_descendant('metadata');

    my @extras = $twigroot->descendants('metadata');
    shift @extras;

    if(! $metadata) {
	debug(1,"DEBUG: creating <metadata>");
	$metadata = $twigroot->insert_new_elt('first_child','metadata');
    }

    foreach my $extra (@extras) {
        my @elements = $extra->children;
        foreach my $el (@elements) {
            debug(2,"DEBUG: moving <",$el->gi,"> into primary metadata");
            $el->move('last_child',$metadata);
        }
        $extra->delete;
    }
    debug(3,"DEBUG: moving <metadata> to be the first child of <package>");
    $metadata->move('first_child',$twigroot);

    if($metadata->att('xmlns:calibre')) {
        # xmlns:calibre isn't a real namespace; it's just there for
        # advertising, so remove it
        $metadata->del_att('xmlns:calibre');
    }
    return 1;
}


=head2 C<fix_metastructure_oeb12()>

Verifies the existence of <metadata>, <dc-metadata>, and <x-metadata>,
creating them as needed, and making sure that <metadata> is a child of
<package>, while <dc-metadata> and <x-metadata> are children of
<metadata>.

Used in L</fix_oeb12()> and L</fix_mobi()>.

=cut

sub fix_metastructure_oeb12 :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};

    my $metadata;
    my $dcmeta;
    my $xmeta;
    my $parent;

    # Start by forcing the basic <package><metadata> structure
    $self->fix_metastructure_basic;
    $metadata = $twigroot->first_child('metadata');

    # If <dc-metadata> doesn't exist, we'll have to create it.
    $dcmeta = $twigroot->first_descendant('dc-metadata');
    if(! $dcmeta) {
	debug(2,"DEBUG: creating <dc-metadata>");
	$dcmeta = $metadata->insert_new_elt('first_child','dc-metadata');
    }

    # Make sure that $dcmeta is a child of $metadata
    $parent = $dcmeta->parent;
    if($parent != $metadata) {
	debug(2,"DEBUG: moving <dc-metadata>");
	$dcmeta->move('first_child',$metadata);
    }

    # If <x-metadata> doesn't exist, create it
    $xmeta = $metadata->first_descendant('x-metadata');
    if(! $xmeta) {
        debug(2,"DEBUG: creating <x-metadata>");
        $xmeta = $metadata->insert_new_elt('last_child','x-metadata');
    }

    # Make sure that x-metadata is a child of metadata
    $parent = $xmeta->parent;
    if($parent != $metadata) {
        debug(2,"DEBUG: moving <x-metadata>");
        $xmeta->move('after',$dcmeta);
    }
    return 1;
}


=head2 C<fix_misc()>

Fixes miscellaneous potential problems in OPF data.  Specifically,
this is a shortcut to calling L</delete_meta_filepos()>,
L</fix_packageid()>, L</fix_dates()>, L</fix_languages()>,
L</fix_publisher()>, L</fix_manifest()>, L</fix_spine()>,
L</fix_subjects()>, L</fix_type()>, L</fix_guide()>, and
L</fix_links()>.

L</fix_creators()> is not run from this, as it carries a risk of taking
a correct name and making it incorrect.

The objective here is that you can run either C<fix_misc()> and either
L</fix_oeb12()> or L</fix_opf20()> and a perfectly valid OPF file will
result from only two calls.

=cut

sub fix_misc :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    $self->delete_meta_filepos();
    $self->fix_packageid();
    $self->fix_dates();
    $self->fix_languages();
    $self->fix_publisher();
    $self->fix_manifest();
    $self->fix_spine();
    $self->fix_subjects();
    $self->fix_type();
    $self->fix_guide();
    $self->fix_links();

    debug(2,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<fix_mobi()>

Manipulates the twig to fix Mobipocket-specific issues

=over

=item * Force the OEB 1.2 structure (although not the namespace, DTD,
or capitalization), so that <dc-metadata> and <x-metadata> are
guaranteed to exist.

=item * Find and move all Mobi-specific elements to <x-metadata>

=item * If no <output> element exists, creates one for a utf-8 ebook

=back

Note that the forced creation of <output> will cause the OPF file to
become noncompliant with IDPF specifications.

=cut

sub fix_mobi :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};

    my %mobicontenttypes = (
	'text/x-oeb1-document' => 'text/x-oeb1-document',
	'application/x-mobipocket-subscription'
	=> 'application/x-mobipocket-subscription',
	'application/x-mobipocket-subscription-feed'
	=> 'application/x-mobipocket-subscription-feed',
	'application/x-mobipocket-subscription-magazine'
	=> 'application/x-mobipocket-subscription-magazine',
	'image/gif' => 'image/gif',
	'application/msword' => 'application/msword',
	'application/vnd.ms-excel' => 'application/vnd.ms-excel',
	'application/vnd.ms-powerpoint' => 'application/vnd.ms-powerpoint',
	'text/plain' => 'text/plain',
	'text/html' => 'text/html',
	'application/vnd.mobipocket-game' => 'application/vnd.mobipocket-game',
	'application/vnd.mobipocket-franklin-ua-game'
	=> 'application/vnd.mobipocket-franklin-ua-game'
	);

    my %mobiencodings = (
	'Windows-1252' => 'Windows-1252',
	'utf-8' => 'utf-8'
	);

    my @mobitags = (
        'output',
        'Adult',
        'Demo',
        'DefaultLookupIndex',
        'DictionaryInLanguage',
        'DictionaryOutLanguage',
        'DictionaryVeryShortName',
        'DatabaseName',
        'EmbeddedCover',
        'Review',
        'SRP',
        'Territory'
        );

    my $dcmeta;
    my $xmeta;
    my @elements;
    my $output;


    # Mobipocket currently requires that its custom elements be found
    # underneath <x-metadata>.  Since the presence of <x-metadata>
    # requires that the Dublin Core tags be under <dc-metadata>, we
    # have to use at least the OEB1.2 structure (deprecated, but still
    # allowed in OPF2.0), though we don't have to convert everything.
    $self->fix_metastructure_oeb12();
    $dcmeta = $twigroot->first_descendant('dc-metadata');
    $xmeta = $twigroot->first_descendant('x-metadata');

    # If <x-metadata> doesn't exist, create it.  Even if there are no
    # mobi-specific tags, this method will create at least one
    # (<output>) which will need it.
    if(!$xmeta) {
        debug(2,"DEBUG: creating <x-metadata>");
        $xmeta = $dcmeta->insert_new_elt('after','x-metadata')
    }

    foreach my $tag (@mobitags) {
        @elements = $twigroot->descendants($tag);
        next unless (@elements);

        # In theory, only one Mobipocket-specific element should ever
        # be present in a document.  We'll deal with multiples anyway,
        # but send a warning.
        if(scalar(@elements) > 1) {
            $self->add_warning(
                'fix_mobi(): Found ' . scalar(@elements) . " '" . $tag .
                "' elements, but only one should exist."
                );
        }

        foreach my $el (@elements) {
            $el->move('last_child',$xmeta);
        }
    }

    $output = $xmeta->first_child('output');
    if($output) {
	my $encoding = $mobiencodings{$output->att('encoding')};
	my $contenttype = $mobicontenttypes{$output->att('content-type')};

	if($contenttype) {
	    $output->set_att('encoding','utf-8') if(!$encoding);
	    debug(2,"DEBUG: setting encoding only and returning");
	    return 1;
	}
    }
    else {
        debug(1,"DEBUG: creating <output> under <x-metadata>");
        $output = $xmeta->insert_new_elt('last_child','output');
    }


    # At this stage, we definitely have <output> in the right place.
    # Set the attributes and return.
    $output->set_att('encoding' => 'utf-8',
		     'content-type' => 'text/x-oeb1-document');
    debug(2,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<fix_oeb12()>

Modifies the OPF data to conform to the OEB 1.2 standard

Specifically, this involves:

=over

=item * adding the OEB 1.2 doctype

=item * removing OPF 2.0 version and namespace attributes

=item * setting the OEB 1.2 namespace on <package>

=item * moving all of the dc-metadata elements underneath an element
with tag <dc-metadata>, which itself is forced to be underneath
<metadata>, which is created if it doesn't exist.

=item * moving any remaining tags underneath <x-metadata>, again
forced to be under <metadata>

=item * making the dc-metadata tags conform to the OEB v1.2 capitalization

=back

=cut

sub fix_oeb12 :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $metadata;
    my $dcmeta;
    my $xmeta;
    my @elements;

    # Verify and correct locations for <metadata>, <dc-metadata>, and
    # <x-metadata>, creating them as needed.
    $self->fix_metastructure_oeb12;
    $metadata = $twigroot->first_descendant('metadata');
    $dcmeta = $metadata->first_descendant('dc-metadata');
    $xmeta = $metadata->first_descendant('x-metadata');

    # Clobber metadata attributes 'xmlns:dc' and 'xmlns:opf'
    # used only in OPF2.0
    $metadata->del_atts('xmlns:dc','xmlns:opf');

    # Assign the DC namespace attribute to dc-metadata for OEB 1.2
    $dcmeta->set_att('xmlns:dc',"http://purl.org/dc/elements/1.1/");

    # Set the correct tag name and move it into <dc-metadata> in the
    # right order
    foreach my $dcel (keys %dcelements12) {
        @elements = $twigroot->descendants(qr/^$dcel$/ix);
        foreach my $el (@elements) {
            debug(3,"DEBUG: processing '",$el->gi,"'");
            croak("Found invalid DC element '",$el->gi,"'!")
                if(!$dcelements12{lc $el->gi});
            $el->set_gi($dcelements12{lc $el->gi});
            $el = twigelt_fix_oeb12_atts($el);
            $el->move('last_child',$dcmeta);
        }
    }

    # Handle non-DC metadata, deleting <x-metadata> if it isn't
    # needed.
    @elements = $metadata->children(qr/^(?!(?s:.*)-metadata)/x);
    if(@elements) {
	if($debug) {
	    print {*STDERR} "DEBUG: extra metadata elements found: ";
	    foreach my $el (@elements) { print {*STDERR} $el->gi," "; }
	    print {*STDERR} "\n";
	}
	foreach my $el (@elements) {
	    $el->move('last_child',$xmeta);
	}
    }
    @elements = $twigroot->children(qr/^meta$/ix);
    foreach my $el (@elements) {
        $el->set_gi(lc $el->gi);
        $el->move('last_child',$xmeta);
    }
    @elements = $xmeta->children;
    $xmeta->delete unless(@elements);

    # Delete Calibre advertisements in dc:Contributor elements
    @elements = $twigroot->descendants('dc:Contributor[string() =~ /calibre-ebook.com|calibre.kovidgoyal.net/]');
    foreach my $el (@elements) {
	$el->delete;
    }

    # Fix <manifest> and <spine>
    $self->fix_manifest;
    $self->fix_spine;

    # Set the OEB 1.2 doctype
    $self->{twig}->set_doctype('package',
                              "http://openebook.org/dtds/oeb-1.2/oebpkg12.dtd",
                              "+//ISBN 0-9673008-1-9//DTD OEB 1.2 Package//EN");

    # Clean up <package>
    $twigroot->del_att('version');
    $twigroot->set_att(
        'xmlns' => 'http://openebook.org/namespaces/oeb-package/1.0/');
    $self->fix_packageid;

    $self->{spec} = $validspecs{'OEB12'};
    debug(2,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<fix_oeb12_dcmetatags()>

Makes a case-insensitive search for tags matching a known list of DC
metadata elements and corrects the capitalization to the OEB 1.2
standard.  Also corrects 'dc:Copyrights' to 'dc:Rights'.  See global
variable $dcelements12.

The L</fix_oeb12()> method does this also, but fix_oeb12_dcmetatags()
is usable separately for the case where you want DC metadata elements
with consistent tag names, but don't want them moved from wherever
they are.

=cut

sub fix_oeb12_dcmetatags :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $topelement = $self->{twigroot};

    my @elements;

    foreach my $dcmetatag (keys %dcelements12) {
	@elements = $topelement->descendants(qr/^$dcmetatag$/ix);
	foreach my $el (@elements) {
	    $el->set_tag($dcelements12{lc $el->tag})
		if($dcelements12{lc $el->tag});
	}
    }
    return 1;
}


=head2 C<fix_opf20()>

Modifies the OPF data to conform to the OPF 2.0 standard

Specifically, this involves:

=over

=item * moving all of the dc-metadata and x-metadata elements directly
underneath <metadata>

=item * removing the <dc-metadata> and <x-metadata> elements themselves

=item * lowercasing the dc-metadata tags (and fixing dc:copyrights to
dc:rights)

=item * setting namespaces on dc-metata OPF attributes

=item * setting version and xmlns attributes on <package>

=item * setting xmlns:dc and xmlns:opf on <metadata>

=back

=cut

sub fix_opf20 :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    # Ensure a sane structure
    $self->fix_metastructure_basic();

    # If there is an existing cover image, ensure it hits all standards
    my $coverimage = $self->coverimage;
    $self->set_cover('href' => $coverimage) if $coverimage;

    my $twigroot = $self->{twigroot};
    my $metadata = $twigroot->first_descendant('metadata');
    my @elements;

    # If <dc-metadata> exists, make sure that it is directly
    # underneath <metadata> so that its children will collapse to the
    # correct position, then erase it.
    @elements = $twigroot->descendants('dc-metadata');
    if(@elements) {
	foreach my $dcmeta (@elements) {
	    debug(1,"DEBUG: moving <dc-metadata>");
	    $dcmeta->move('first_child',$metadata);
	    $dcmeta->erase;
	}
    }

    # If <x-metadata> exists, make sure that it is directly underneath
    # <metadata> so that its children will collapse to the correct
    # position, then erase it.
    @elements = $twigroot->descendants('x-metadata');
    if(@elements) {
	foreach my $xmeta (@elements) {
	    debug(1,"DEBUG: moving <x-metadata>");
	    $xmeta->move('last_child',$metadata);
	    $xmeta->erase;
	}
    }

    # Delete any old OEB12 output elements
    @elements = $twigroot->descendants('output');
    foreach my $el (@elements) {
        $el->delete;
    }

    # For all DC elements at any location, set the correct tag name
    # and attribute namespace and move it directly under <metadata>
    foreach my $dcmetatag (keys %dcelements20) {
	@elements = $twigroot->descendants(qr/$dcmetatag/ix);
	foreach my $el (@elements) {
	    debug(2,"DEBUG: checking DC element <",$el->gi,">");
	    $el->set_gi($dcelements20{$dcmetatag});
            $el = twigelt_fix_opf20_atts($el);
	    $el->move('last_child',$metadata);
	}
    }

    # Find any <meta> elements anywhere in the package and move them
    # under <metadata>.  Force the tag to lowercase.

    @elements = $twigroot->descendants(qr/^meta$/ix);
    foreach my $el (@elements) {
        debug(2,'DEBUG: checking meta element <',$el->gi,
              ' name="',$el->att('name'),'">');
        $el->set_gi(lc $el->gi);
        $el->move('last_child',$metadata);
    }

    # Delete Calibre advertisements in dc:contributor elements
    @elements = $twigroot->descendants('dc:contributor[string() =~ /calibre-ebook.com|calibre.kovidgoyal.net/]');
    foreach my $el (@elements) {
	$el->delete;
    }
    # Delete Aspose.Words advertisements in dc:contributor elements
    @elements = $twigroot->descendants('dc:contributor[string() =~ /Aspose.Words/]');
    foreach my $el (@elements) {
	$el->delete;
    }

    # Fix the <package> attributes
    $twigroot->set_att('version' => '2.0',
                       'xmlns' => 'http://www.idpf.org/2007/opf');
    $self->fix_packageid;

    # Fix the <metadata> attributes
    $metadata->set_att('xmlns:dc' => "http://purl.org/dc/elements/1.1/",
                       'xmlns:opf' => "http://www.idpf.org/2007/opf",
                       'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance");

    # Fix <manifest> and <spine>
    $self->fix_manifest;
    $self->fix_spine;

    # Clobber the doctype, if present
    $self->{twig}->set_doctype(0,0,0,0);

    # Set the specification
    $self->{spec} = $validspecs{'OPF20'};

    debug(2,"DEBUG[/",$subname,"]");
    return 1;
}


=head2 C<fix_opf20_dcmetatags()>

Makes a case-insensitive search for tags matching a known list of DC
metadata elements and corrects the capitalization to the OPF 2.0
standard.  Also corrects 'dc:copyrights' to 'dc:rights'.  See package
variable %dcelements20.

The L</fix_opf20()> method does this also, but
C<fix_opf20_dcmetatags()> is usable separately for the case where you
want DC metadata elements with consistent tag names, but don't want
them moved from wherever they are.

=cut

sub fix_opf20_dcmetatags :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $topelement = $self->{twigroot};
    my @elements;

    foreach my $dcmetatag (keys %dcelements20) {
	@elements = $topelement->descendants(qr/^$dcmetatag$/ix);
	foreach my $el (@elements) {
	    $el->set_tag($dcelements20{lc $el->tag})
		if($dcelements20{lc $el->tag});
	}
    }
    return;
}


=head2 C<fix_packageid()>

Checks the <package> element for the attribute 'unique-identifier',
makes sure that it is mapped to a valid dc:identifier subelement, and
if not, searches those subelements for an identifier to assign, or
creates one if nothing can be found.

Requires that <metadata> exist.  Croaks if it doesn't.  Run
L</fix_oeb12()> or L</fix_opf20()> before calling this if the input
might be very broken.

=cut

sub fix_packageid :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    # Start by enforcing the basic structure needed
    $self->fix_metastructure_basic();
    my $twigroot = $self->{twigroot};
    my $packageid = $twigroot->att('unique-identifier');

    my $meta = $twigroot->first_child('metadata')
        or croak($subname,"(): metadata not found");
    my $element;

    if($packageid) {
        # Check that the ID maps to a valid identifier
	# If not, undefine it
	debug(2,"DEBUG: checking existing packageid '",$packageid,"'");

	# The twig ID handling system is unreliable, especially when
	# multiple twigs may be existing simultaneously.  Use
	# XML::Twig->first_elt instead of XML::Twig->elt_id, even
	# though it is slower.
        #
        # As of Twig 3.32, this will cause 'uninitialized value'
        # warnings to be spewed for each time no descendants are
        # found.
	#$element = $self->{twig}->elt_id($packageid);
	$element = $self->{twig}->first_elt("*[\@id='$packageid']");

	if($element) {
	    if(lc($element->tag) ne 'dc:identifier') {
		debug(1,"DEBUG: packageid '",$packageid,
                      "' points to a non-identifier element ('",
                      $element->tag,"')");
                debug(1,"DEBUG: undefining existing packageid '",
                      $packageid,"'");
		undef($packageid);
	    }
	    elsif(!$element->text) {
		debug(1,"DEBUG: packageid '",$packageid,
                      "' points to an empty identifier.");
		debug(1,"DEBUG: undefining existing packageid '",
                      $packageid,"'");
		undef($packageid);
	    }
	}
	else { undef($packageid); };
    }

    if(!$packageid) {
	# Search known IDs for a unique Package ID
	$packageid = $self->search_knownuids;
    }

    # If no unique ID found so far, start searching known schemes
    if(!$packageid) {
	$packageid = $self->search_knownuidschemes;
    }

    # And if we still don't have anything, we have to make one from
    # scratch using Data::UUID
    if(!$packageid) {
	debug(1,"DEBUG: creating new UUID");
	$element = twigelt_create_uuid();
	$element->paste('first_child',$meta);
	$packageid = 'UUID';
    }

    # At this point, we have a unique ID.  Assign it to package
    $twigroot->set_att('unique-identifier',$packageid);
    debug(2,"[/",$subname,"]");
    return 1;
}


=head2 C<fix_publisher()>

Standardizes publisher names in all dc:publisher entities, mapping
known variants of a publisher's name to a canonical form via package
variable %publishermap.

Publisher entries with no text are deleted.

=cut

sub fix_publisher :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my @publishers = $self->twigroot->descendants(qr/^dc:publisher$/ix);
    foreach my $pub (@publishers) {
        debug(3,"Examining publisher entry in element '",$pub->gi,"'");
        if(!$pub->text) {
            debug(1,'Deleting empty publisher entry');
            $pub->delete;
            next;
        }
        elsif( $publishermap{lc $pub->text} &&
               ($publishermap{lc $pub->text} ne $pub->text) )
        {
            debug(1,"DEBUG: Changing publisher from '",$pub->text,"' to '",
                  $publishermap{lc $pub->text},"'");
            $pub->set_text($publishermap{lc $pub->text});
        }
    }
    return 1;
}


=head2 C<fix_spine()>

Fixes problems with the OPF spine, specifically:

=over

=item Moves all <itemref> elements underneath <spine>, creating
<spine> if necessary.

=back

=cut

sub fix_spine :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $manifest = $twigroot->first_descendant('manifest');
    my $spine = $twigroot->first_descendant('spine');
    my @elements;
    my $parent;

    @elements = $twigroot->descendants(qr/^itemref$/ix);
    if(@elements) {
        # If <spine> doesn't exist, create it
        if(! $spine) {
            debug(1,"DEBUG: creating <spine>");
            $spine = $twigroot->insert_new_elt('last_child','spine');
        }

        # Make sure that the spine is a child of the twigroot,
        $parent = $spine->parent;
        if($parent != $twigroot) {
            debug(1,"DEBUG: moving <spine>");
            $spine->move('last_child',$twigroot);
        }

        # If an NCX item exists in the manifest, reference it as a
        # spine attribute
        if($manifest->has_child('@id="ncx"')) {
            $spine->set_att('toc' => 'ncx');
        }

        foreach my $el (@elements) {
            if(!$el->att('idref')) {
                # No idref means it is broken.
                # Leave it alone, but log a warning
                $self->add_warning(
                    "fix_spine(): <itemref> with no idref -- skipping");
                next;
            }
            debug(3,"DEBUG: processing itemref '",$el->att('idref'),"')");
            $el->move('last_child',$spine);
        }
    }
    else {
        # No elements, delete spine if it exists
        $spine->delete if($spine);
    }

    return 1;
}


=head2 C<fix_subjects()>

Deletes empty and duplicate subject elements and normalizes existing
subject text against the known Library of Congress mappings.

If $self->{erotic} is true, then the book will be treated as a
work of erotic fiction and the subjects will go through preprocessing
against the C<%sexcodes> package variable, normalizing matches and
prepending 'FICTION / Erotica / ' (with a trailing space).

This method is called as a component of C<fix_misc()>.

=cut

sub fix_subjects :method {
    my ($self) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $eroticaprefix = 'FICTION / Erotica / ';

    my $twigroot = $self->{twigroot};
    my $manifest = $twigroot->first_descendant('manifest');
    my $bisac;
    my $newsubject;
    my $text;
    my $newtext;

    $self->delete_subject('text' => '');
    my @elements = $self->{twigroot}->descendants(qr/^dc:subject$/ix);
    my %subjects_seen;
    foreach my $el (@elements) {
        # Matches to the list are done after stripping leading and
        # trailing whitespace, trailing periods, and removing
        # whitespace around '--', then converting ' - ' to '--' and
        # decoding entities.
        $text = trim $el->text;
        $text =~ s|\.\z||x;
        $text =~ s|\s* -- \s*|--|gx;
        $text =~ s|\s* - \s*|--|gx;
        $text =~ s|\s*—\s*|--|gx;
        # We can't normalize spaces around slashes -- they have
        # potentially distinct meanings
        #$text =~ s|\s* / \s*|/|gx;
        $text = decode_entities($text);

        # Sex code matches are case-sensitive
        if($self->{erotic}) {
            if($sexcodes{$text}) {
                $newtext = $eroticaprefix . $sexcodes{$text};
                debug(1,"DEBUG: normalizing ${text} to " . $newtext);
                if($subjects_seen{$newtext}) {
                    $el->delete;
                }
                else {
                    $el->set_text($newtext);
                    $subjects_seen{$newtext} = 1;
                }
            }
        }

        # ... but all other normalizations are not.
        $text = lc $text;
        debug(1,"DEBUG: normalizing subject '",$text,"'");

        # TODO: check for BISACCODE property, convert it to both a
        # BISAC descriptor and a LoC subject, then remove it.
        if($el->att('BASICCode')) {
            $newsubject = $self->add_subject(text => $el->att('BASICCode'));
            $el->del_att('BASICCode');
            push(@elements,$newsubject);
        }
        if($bisacsubjects{$text}) {
            debug(1,"DEBUG: found BISAC: ",$text);
            $bisac = $bisacsubjects{$text};
            if($subjects_seen{$bisac}) {
                $el->delete;
            }
            else {
                $el->set_text($bisac);
                $subjects_seen{$bisac} = 1;
            }
        }

        # TODO: check for a BISAC code or descriptor in attribute as
        # well as text, normalize it to standard descriptor format, as
        # well as inserting an additional matching LoC subject if
        # present
        if($bisactolc{$text}) {
            $bisac = $bisactolc{$text};
            if(! $subjects_seen{$bisac}) {
                $self->add_subject(text => $bisac);
            }
        }
        elsif($lcsubjects{$text}) {
            $text = $lcsubjects{$text};
            $el->set_text($text);
        }

        if($subjects_seen{$text}) {
            $el->delete;
        }
        else {
            $subjects_seen{$text} = 1;
        }
    }
    return;
}


=head2 C<fix_type()>

Normalizes <dc:type> elements against a limited list based on book types listed in Wikipedia.

=cut

sub fix_type :method {
    my ($self) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck();

    my $twigroot = $self->{twigroot};
    my $manifest = $twigroot->first_descendant('manifest');
    my $text;
    my $type;

    my @elements = $self->{twigroot}->descendants(qr/^dc:type$/ix);
    foreach my $el (@elements) {
        # Matches to the list are done after stripping leading and
        # trailing whitespace, trailing periods, and removing
        # whitespace around '--' and '/'
        $text = lc trim $el->text;
        $text =~ s|\.\z||x;
        debug(2,"DEBUG: normalizing type '",$text,"'");

        if($booktypes{$text}) {
            $type = $booktypes{$text};
            $el->set_text($type);
        }
        else {
            debug(1,"WARNING: unknown book type '",$text,"'");
        }
    }
    return;
}


=head2 C<gen_epub(%args)>

Creates a .epub format e-book.  This will create (or overwrite) the
files 'mimetype' and 'META-INF/container.xml' in the current
directory, creating the subdirectory META-INF as needed.

A NCX file will also be created if missing.

=head3 Arguments

This method can take two optional named arguments.

=over

=item C<filename>

The filename of the .epub output file.  If not specified, takes the
base name of the opf file and adds a .epub extension.

=item C<dir>

The directory to output the .epub file.  If not specified, uses the
current working directory.  If a specified directory does not exist,
it will be created, or the method will croak.

=back

=head3 Example

 gen_epub(filename => 'mybook.epub',
          dir => '../epub_books');

=cut

sub gen_epub :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my %valid_args = (
        'filename' => 1,
        'dir' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $filename = $args{filename};
    my $dir = $args{dir};
    my $zip = Archive::Zip->new();
    my $member;
    my $cwd = usedir($self->{topdir});

    $self->gen_epub_files();
    if(! $self->{opffile} ) {
	$self->add_error(
	    "Cannot create epub without an OPF (did you forget to init?)");
        debug(1,"Cannot create epub without an OPF");
	return;
    }
    if(! -f $self->opfpath) {
	$self->add_error(
	    sprintf("OPF '%s' does not exist (did you forget to save?)",
		    $self->opfpath)
	    );
        debug(1,"OPF '",$self->opfpath,"' does not exist");
	return;
    }

    debug(3,"DEBUG: adding core metadata to zip archive");
    $member = $zip->addFile('mimetype');
    $member->desiredCompressionMethod(COMPRESSION_STORED);

    $member = $zip->addFile('META-INF/container.xml');
    $member->desiredCompressionLevel(9);

    $member = $zip->addFile($self->{opfsubdir} . '/' . $self->{opffile});
    $member->desiredCompressionLevel(9);

    debug(3,"DEBUG: adding manifest files to zip archive");
    foreach my $file ($self->manifest_hrefs()) {
        $file = uri_unescape($file);
	if(-f $self->{opfsubdir} . '/' . $file) {
	    $member = $zip->addFile($self->{opfsubdir} . '/' . $file);
	    $member->desiredCompressionLevel(9);
	}
	else { print STDERR "WARNING: ",$self->{opfsubdir} . '/' . $file," not found, skipping.\n"; }
    }

    if(! $filename) {
	$filename = basename($self->{topdir}) . '.epub';
    }

    if($dir) {
        unless(-d $dir) {
            mkpath($dir)
                or croak("Unable to create working directory '",$dir,"'!");
        }
        $filename = "$dir/$filename";
    }

    unless ( $zip->writeToFileNamed($filename) == AZ_OK ) {
	$self->add_error(
            sprintf("Failed to create epub as '%s'",$filename));
        debug(1,"Failed to create epub as '",$filename,"'");
	return;
    }

    usedir($cwd);
    return 1;
}


=head2 C<gen_epub_files()>

Generates the C<mimetype> and C<META-INF/container.xml> files expected
by a .epub container, but does not actually generate the .epub file
itself.  This will be called automatically by C<gen_epub>.

The OPF will be normalized to the OPF 2.0 format.

If no NCX element exists, it will also be created.

=cut

sub gen_epub_files :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my $manifest = $self->{twigroot}->first_descendant('manifest');

    $self->fix_opf20();
    if( ! $manifest->first_child('item[@id="ncx"]') ) {
        $self->gen_ncx();
    }
    $self->save();

    # These two functions must happen from the top-level directory, not the OPF directory
    if($self->{opfsubdir} ne '.') {
        debug(3,"DEBUG: switching to ",$self->{topdir}," to generate EPUB metadata");
        my $cwd = usedir($self->{topdir});
        create_epub_mimetype();
        create_epub_container($self->{opfsubdir} . '/' . $self->{opffile});
        usedir($cwd);
    }
    else {
        create_epub_mimetype();
        create_epub_container($self->{opffile});
    }

    return 1;
}


=head2 C<gen_ncx($filename)>

Creates a NCX-format table of contents from the package
unique-identifier, the dc:title, dc:creator, and spine elements, and
then add the NCX entry to the manifest if it is not already
referenced.

Adds an error and fails if any of those cannot be found.  The first
available dc:title is taken, but will prioritize dc:creator elements
with opf:role="aut" over those with no role attribute (see
twigelt_is_author() for details).

WARNING: This method REQUIRES that the e-book be in OPF 2.0 format to
function correctly.  Call fix_opf20() before calling gen_ncx().
gen_ncx() will log an error and fail if $self{spec} is not set to
OPF20.

=head3 Arguments

=over

=item $filename : The filename to save to.  If not specified, will use
'toc.ncx'.

=back

This method will overwrite any existing file.

Returns a twig containing the NCX XML, or undef on failure.

=cut

sub gen_ncx :method {
    my ($self,$filename) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    $filename = 'toc.ncx' if(!$filename);

    my $cwd = usedir($self->opfdir);
    my $twigroot = $self->{twigroot};
    my $identifier = $self->identifier;
    my $element;            # Generic element container
    my $parent;             # Generic parent element container
    my $ncx;                # NCX twig
    my $ncxroot;            # NCX twig root <ncx>
    my $ncxitem;            # manifest item pointing to the NCX document
    my $navmap;             # NCX element <navMap>
    my $navpoint;           # NCX element <navPoint>
    my %navpointorder;      # Hash mapping playOrder to id
    my $navpointindex = 1;  # playOrder number starting at 1
    my $title;              # E-book title
    my $author;             # E-book primary author
    my @spinelist;          # List of hashrefs containing spine data
    my $manifest;           # OPF manifest element
    my $spine;		    # OPF spine element

    if($self->{spec} ne 'OPF20') {
        $self->add_error(
            $subname . "(): specification is currently set to '"
            . $self->{spec} . "' -- need 'OPF20'"
            );
        debug(1,"DEBUG: gen_ncx() FAILED: wrong specification ('",
              $self->{spec},"')!");
        return;
    }

    if(!$identifier) {
        $self->add_error( $subname . "(): no unique-identifier found" );
        debug(1,"DEBUG: gen_ncx() FAILED: no unique-identifier!");
        return;
    }

    # Get the title
    $title = $self->title();
    if(!$title) {
        $self->add_error( $subname . "(): no title found" );
        debug(1,"DEBUG: gen_ncx() FAILED: no title!");
        return;
    }

    # Get the author
    $author = $self->primary_author();
    if(!$author) {
        $self->add_error( $subname . "(): no title found" );
        debug(1,"DEBUG: gen_ncx() FAILED: no title!");
        return;
    }

    # Get the spine list
    @spinelist = $self->spine();
    if(!@spinelist) {
        $self->add_error( $subname . "(): no spine found" );
        debug(1,"DEBUG: gen_ncx() FAILED: no spine!");
        return;
    }

    # Make sure the manifest element exists
    # (This should in theory never fail, since it is also checked by
    # spine() above)
    $manifest = $twigroot->first_descendant('manifest');
    if(!$manifest) {
        $self->add_error( $subname . "(): no manifest found" );
        debug(1,"DEBUG: gen_ncx() FAILED: no manifest!");
        return;
    }

    $ncx = XML::Twig->new(
	output_encoding => 'utf-8',
	pretty_print => 'record'
	);

    # <ncx>
    $element = XML::Twig::Elt->new('ncx');
    $element->set_att('xmlns' => 'http://www.daisy.org/z3986/2005/ncx/');
    $element->set_att('version' => '2005-1');
    $ncx->set_root($element);
    $ncxroot = $ncx->root;

    # <head>
    $parent = $ncxroot->insert_new_elt('first_child','head');
    $element = $parent->insert_new_elt('last_child','meta');
    $element->set_att(
        'name' => 'dtb:uid',
        'content' => $identifier
        );

    $element = $parent->insert_new_elt('last_child','meta');
    $element->set_att(
        'name'    => 'dtb:depth',
        'content' => '1'
        );
    $element = $parent->insert_new_elt('last_child','meta');
    $element->set_att(
        'name' => 'dtb:totalPageCount',
        'content' => '0'
        );

    $element = $parent->insert_new_elt('last_child','meta');
    $element->set_att(
        'name' => 'dtb:maxPageNumber',
        'content' => '0'
        );

    # <docTitle>
    $parent = $parent->insert_new_elt('after','docTitle');
    $element = $parent->insert_new_elt('first_child','text');
    $element->set_text($title);

    # <navMap>
    $navmap = $parent->insert_new_elt('after','navMap');

    foreach my $spineitem (@spinelist) {
        # <navPoint>
        $navpoint = $navmap->insert_new_elt('last_child','navPoint');
        $navpoint->set_att('id' => $spineitem->{'id'},
                           'playOrder' => $navpointindex);
        $navpointindex++;

        # <navLabel>
        $parent = $navpoint->insert_new_elt('last_child','navLabel');
        $element = $parent->insert_new_elt('last_child','text');
        $element->set_text($spineitem->{'id'});

        # <content>
        $element = $navpoint->insert_new_elt('last_child','content');
        $element->set_att('src' => $spineitem->{'href'});
    }

    # Backup existing file
    if(-e $filename) {
        rename($filename,"$filename.backup")
            or croak($subname,"(): could not backup ",$filename,"!");
    }

    # Twig handles utf-8 on its own.  Setting binmode :utf8 here will
    # cause double-conversion.
    open(my $fh_ncx,'>',$filename)
        or croak($subname,"(): failed to open '",$filename,"' for writing!");
    $ncx->print(\*$fh_ncx);
    close($fh_ncx)
        or croak($subname,"(): failed to close '",$filename,"'!");

    # Search for existing NCX entries and modify the first one found,
    # creating a new one if there are no matches.
    $ncxitem = $manifest->first_child('item[@id="ncx"]');
    $ncxitem = $manifest->first_child("item[\@href='$filename']")
        if(!$ncxitem);
    $ncxitem = $manifest->first_child('item[@media-type="application/x-dtbncx+xml"]')
        if(!$ncxitem);
    $ncxitem = $manifest->insert_new_elt('first_child','item')
        if(!$ncxitem);

    $ncxitem->set_att(
        'id' => 'ncx',
        'href' => $filename,
        'media-type' => 'application/x-dtbncx+xml'
        );

    # Move the NCX item to the top of the manifest
    $ncxitem->move('first_child',$manifest);

    # Ensure that the spine references the NCX
    $spine = $twigroot->first_descendant('spine');
    $spine->set_att('toc' => 'ncx');

    usedir($cwd);
    return $ncx;
}


=head2 C<save()>

Saves the OPF file to disk.  Existing files are backed up to
filename.backup.

=cut

sub save :method {
    my $self = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    croak($subname,"(): no opffile specified (did you forget to init?)")
        if(!$self->{opffile});

    my $fh_opf;
    my $cwd = usedir($self->opfdir);
    my $filename = $self->{opffile};

    # Backup existing file
    if(-e $filename) {
        rename($filename,"$filename.backup")
            or croak($subname,"(): could not backup ",$filename,"!");
    }

    # Update the last-modified timestamp as the very last thing before
    # saving if the output changed at all
    if($self->{twig}->sprint ne $self->{twig_unmodified}->sprint) {
        $self->set_timestamp();
    }

    if(!open($fh_opf,">:encoding(UTF-8)",$self->{opffile})) {
	add_error(sprintf("Could not open '%s' to save to!",$self->{opffile}));
	return;
    }
    $self->{twig}->print(\*$fh_opf);

    if(!close($fh_opf)) {
	add_error(sprintf("Failure while closing '%s'!",$self->{opffile}));
	return;
    }

    usedir($cwd);
    return 1;
}


=head2 C<set_adult($bool)>

Sets the Mobipocket-specific <Adult> element, creating or deleting it
as necessary.  If C<$bool> is true, the text is set to 'yes'.  If it
is defined but false, any existing elements are deleted.  If it is
undefined, the method immediately returns.

If a new element has to be created, L</fix_metastructure_oeb12> is
called to ensure that <x-metadata> exists and the element is created
under <x-metadata>, as Mobipocket elements are not recognized by
Mobipocket's software when placed directly under <metadata>

=cut

sub set_adult :method {
    my $self = shift;
    my $adult = shift;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    return 1 unless(defined $adult);

    my $xmeta;
    my $element;
    my @elements;

    if($adult) {
        $element = $self->{twigroot}->first_descendant(qr/^adult$/ix);
        unless($element) {
            $self->fix_metastructure_oeb12();
            $xmeta = $self->{twigroot}->first_descendant('x-metadata');
            $element = $xmeta->insert_new_elt('last_child','Adult');
        }
        $element->set_text('yes');
    }
    else {
        @elements = $self->{twigroot}->descendants(qr/^adult$/ix);
        foreach my $el (@elements) {
            debug(2,"DEBUG: deleting <Adult> flag");
            $el->delete;
        }
    }
    return 1;
}


=head2 C<set_cover(%args)>

Sets a cover image

In OPF 2.0, this is done by setting both a <meta name="cover"> element
and a guide <reference type="other.ms-coverimage-standard"> element
(though some readers will also extract the first image found in the
HTML of the <reference type="cover"> element, which this method will
not handle).

In OEB 1.2, this is done by setting the <EmbeddedCover> tag.

If the filename is not currently listed as an item in the manifest, it
is added.

=head3 Arguments

=over

=item C<href>

The filename of the image file to use.  This is mandatory.

=item C<id>

The id attribute to assign to its item element

=item C<spec>

The specification to use, either OEB12 or OPF20.  If this is left
undefined, the current spec state will be checked, and if that is
undefined, it will default to OPF20.

=back

=cut

sub set_cover :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'href' => 1,
        'id' => 1,
        'spec' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $href = $args{href};
    my $newid = $args{id};
    my $spec = $args{spec};
    my $id;
    my $mimetype;
    my $manifest;
    my $guide;
    my $dcmeta;
    my $element;

    if(! $href) {
        $self->add_error($subname,"(): no href specified");
        return;
    }
    $mimetype = mimetype($href);
    if($mimetype !~ m#^image/#ix) {
        $self->add_warning(
            $subname,"(): ",$href,
            " does not appear to be an image (detected: ",$mimetype,")"
           );
    }

    if(! $spec) {
        $spec = $self->spec || 'OPF20';
    }

    # Ensure that there is a matching manifest item
    $manifest = $self->{twigroot}->first_child('manifest');
    $element = $manifest->first_child('item[@href="' . $href . '"]');
    if($element) {
        if($newid) {
            $element->set_id($newid);
        }
    }
    else {
        $element = $manifest->insert_new_elt('first_child','item');
        if ($newid) {
            $element->set_id($newid);
        }
        elsif ($self->{twig}->first_elt('*[@id="coverimage"]')) {
            $element->set_id(basename($href));
        }
        else {
            $element->set_id('coverimage');
        }
        $element->set_att('href',$href);
        $element->set_att('media-type',$mimetype);
    }
    $id = $element->id;

    if ($spec eq 'OPF20') {
        $self->fix_metastructure_basic;
        $self->fix_guide;
        $guide = $self->{twigroot}->first_child('guide');
        $element = $guide->first_child('reference[@type="other.ms-coverimage-standard"]');
        if ($element) {
            $element->set_att('href',$href);
            $element->set_att('title','Cover');
        }
        else {
            $element = $guide->insert_new_elt('last_child','reference');
            $element->set_att('href',$href);
            $element->set_att('title','Cover');
            $element->set_att('type','other.ms-coverimage-standard');
        }
        $self->set_meta('name' => 'cover',
                        'content' => $href);

        # Now that the OPF 2.0 elements are set, we can delete the
        # OEB12 EmbeddedCover element
        $element = $self->{twigroot}->first_descendant('EmbeddedCover');
        if ($element) {
            debug(1,"deleting cover");
            $element->delete;
        }
    }
    elsif ($spec eq 'OEB12') {
        $self->fix_metastructure_oeb12;
        $dcmeta = $self->{twigroot}->first_child('dc-metadata');
        $element = $dcmeta->first_child('EmbeddedCover');
        if ($element) {
            $element->set_text($href);
        }
        else {
            $element = $dcmeta->insert_new_elt('last_child','EmbeddedCover');
            $element->set_text($href);
        }
    }
    else {
        self->add_error($subname,"(): unknown specification type: '",$spec,"'");
    }
    return;
}


=head2 C<set_date(%args)>

Sets the date metadata for a given event.  If more than one dc:date or
dc:Date element is present with the specified event attribute, sets
the first.  If no dc:date element is present with the specified event
attribute, a new element is created.

If a <dc-metadata> element exists underneath <metadata>, the date
element will be created underneath the <dc-metadata> in OEB 1.2
format, otherwise the title element is created underneath <metadata>
in OPF 2.0 format.

Returns 1 on success, logs an error and returns undef if no text or
event was specified.

=head3 Arguments

=over

=item C<text>

This specifies the description to use as the text of the element.  If
not specified, the method sets an error and returns undef.

=item C<event>

This optionally specifies the event attribute for the date.  This
attribute is not valid in OPF 3.0 (which only allows publication date
in this element) and should no longer be used.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=cut

sub set_date :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'event' => 1,
        'id' => 1,
       );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
          if (!$valid_args{$arg});
    }

    my $text = $args{text};
    my $event = $args{event};
    my $newid = $args{id};
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $self->fix_metastructure_basic();

    my $element;
    if ($event) {
        $element = $self->{twigroot}->first_descendant(
            "dc:date[\@opf:event=~/$event/ix or \@event=~/$event/ix]");
        $element = $self->{twigroot}->first_descendant(
            "dc:Date[\@opf:event=~/$event/ix or \@event=~/$event/ix]")
          unless($element);
    }
    else {
        $element = $self->{twigroot}->first_descendant("dc:date");
        $element = $self->{twigroot}->first_descendant("dc:Date")
          unless($element);
    }

    if ($element) {
        $element->set_text($text);
    }
    elsif ($dcmeta) {
        $element = $dcmeta->insert_new_elt('last_child','dc:Date');
        $element->set_text($text);
        if ($event) {
            $element->set_att('event',$event);
        }
    }
    else {
        $element = $meta->insert_new_elt('last_child','dc:date');
        $element->set_text($text);
        if ($event) {
            $element->set_att('opf:event',$event);
        }
    }

    if ($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
           );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 set_description(%args)

Sets the text and optionally ID of the first dc:description element
found (case-insensitive).  Creates the element if one did not exist.
If a <dc-metadata> element exists underneath <metadata>, the
description element will be created underneath the <dc-metadata> in
OEB 1.2 format, otherwise the title element is created underneath
<metadata> in OPF 2.0 format.

Returns 1 on success, returns undef if no publisher was specified.

=head3 Arguments

C<set_description()> takes one required and one optional named argument

=over

=item C<text>

This specifies the description to use as the text of the element.  If
not specified, the method returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=head3 Example

 $retval = $ebook->set_description('text' => 'A really good book',
                                   'id' => 'mydescid');

=cut

sub set_description :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
       );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
          if (!$valid_args{$arg});
    }

    my $text = $args{text};
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:description$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');

    my $gi = ($dcmeta) ? 'dc:Description' : 'dc:description';
    $self->set_metadata(gi => $gi,
                        text => $text,
                        id => $args{id});

    return 1;
}


=head2 C<set_erotic($bool)>

If C<$bool> is true, C<$self->{erotic}> is set to 1, otherwise this is
set to 0.

This will enable or disable special handling for erotic books, most
notably in subject normalization.

This is not related in any way to C<set_adult> which is a
Mobipocket-specific flag.

Returns 1 if no argument is given, 0 otherwise.

=cut

sub set_erotic :method {
    my ($self,$flag) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    return 1 unless(defined $flag);

    if($flag) {
        $self->{erotic} = 1;
    }
    else {
        $self->{erotic} = 0;
    }
    return 0;
}


=head2 C<set_language(%args)>

Sets the text and optionally the ID of the first dc:language element
found (case-insensitive).  Creates the element if one did not exist.
If a <dc-metadata> element exists underneath <metadata>, the language
element will be created underneath the <dc-metadata> in OEB 1.2
format, otherwise the title element is created underneath <metadata>
in OPF 2.0 format.

Returns 1 on success, returns undef if no text was specified.

=head3 Arguments

=over

=item C<text>

This specifies the language set as the text of the element.  If not
specified, the method sets an error and returns undef.  This should be
an IANA language code, and it will be lowercased before it is set.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=head3 Example

 $retval = $ebook->set_language('text' => 'en-us',
                                'id' => 'langid');

=cut

sub set_language :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
       );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
          if (!$valid_args{$arg});
    }

    my $text = lc($args{text});
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:language$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');

    my $gi = ($dcmeta) ? 'dc:Language' : 'dc:language';
    $self->set_metadata(gi => $gi,
                        text => $text,
                        id => $args{id});
    return 1;
}


=head2 set_meta(%args)

Sets a <meta> element in the <metadata> element area.

=head3 Arguments

=over

=item C<name>

The name attribute to use when finding or creating OPF 2.0 <meta>
elements.  Either this or the property attribute (below) must be
specified, but specifying both is an error.

=item C<content>

The value of the content attribute to set on OPF 2.0 elements.  If
this value is empty or undefined, but C<name> is provided and matches
an existing element, that element will be deleted.

=item C<property>

The property attribute to use when finding or creating OPF 3.0 <meta>
elements.  Either this or C<name> (above) must be specified, but
specifying both is an error.

=item C<refines>

The refines attribute to use when finding or creating OPF 3.0 <meta>
elements.

=item C<scheme>

The scheme attribute to use when creating or updating OPF 3.0 <meta>
elements.

=item C<text>

The text set on OPF 3.0 <meta> elements.  If this value is empty or
undefined, but C<property> is provided and the combination of
C<property> and C<refines> matches an existing element, that element
will be deleted.

=item C<lang>

The xml:lang attribute to set.  This is valid on both OPF 2 and OPF 3
<meta> elements.

=back

=cut

sub set_meta :method {
    my ($self, %args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    my %valid_args = (
        'name'     => 1,
        'content'  => 1,
        'property' => 1,
        'refines'  => 1,
        'scheme'   => 1,
        'text'     => 1,
        'lang'     => 1,
       );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
          if (!$valid_args{$arg});
    }

    my $name = $args{name};
    my $content = $args{content};
    my $property = $args{property};
    my $refines = $args{refines};
    my $scheme = $args{scheme};
    my $text = $args{text};
    my $lang = $args{lang};
    my $standard;

    if ($name) {
        if ($property) {
            $self->add_error($subname,"(): both name (OPF2) and property (OPF3) attributes specified for a meta tag");
            return;
        }
        $standard = 'OPF2';
    }
    else {
        if ($property) {
            $standard = 'OPF3';
        }
        else {
            $self->add_error($subname,"(): neither name (OPF2) nor property (OPF3) attributes specified for a meta tag");
            return;
        }
    }

    $self->fix_metastructure_basic();
    my $metadata = $self->{twigroot}->first_child('metadata');
    my $element;

    if ($standard eq 'OPF2') {
        $element = $metadata->first_descendant('meta[@name="' . $name . '"]');

        if ($element) {
            if (! $content) {
                debug(2,"DEBUG: deleting <meta name='",$name,"'>");
                $element->delete;
            }
            else {
                debug(2,"DEBUG: updating <meta name='",$name,"'>");
                $element->set_att('content',$content);
                if ($lang) {
                    $element->set_att('xml:lang',$lang);
                }
            }
        }
        else {
            if ($content) {
                debug(2,"DEBUG: creating <meta name='",$name,"'>");
                $element = $metadata->insert_new_elt('last_child','meta');
                $element->set_att('name',$name);
                $element->set_att('content',$content);
            }
        }
    }
    elsif ($standard eq 'OPF3') {
        if ($refines) {
            $element = $metadata->first_descendant(
                'meta[@property="' . $property . '" and @refines="' . $refines . '"]');
        }
        else {
            $element = $metadata->first_descendant(
                'meta[@property="' . $property . '"]');
        }
        if ($element) {
            if (! $text) {
                debug(2,"DEBUG: deleting meta property='",$property,"'>");
                $element->delete;
            }
            else {
                debug(2,"DEBUG: updating <meta property='",$property,"'>");
                $element->set_text($text);
            }
            if ($scheme) {
                $element->set_att('scheme',$scheme);
            }
            if ($lang) {
                $element->set_att('xml:lang',$lang);
            }
        }
        else {
            if ($text) {
                debug(2,"DEBUG: creating <meta property='",$property,"'>");
                $element = $metadata->insert_new_elt('last_child','meta');
                $element->set_att('property',$property);
                if ($refines) {
                    $element->set_att('refines',$refines);
                }
                if ($scheme) {
                    $element->set_att('scheme',$scheme);
                }
                if ($lang) {
                    $element->set_att('xml:lang',$lang);
                }
                $element->set_text($text);
            }
        }
    }
    else {
        croak($subname,"(): unknown standard '${standard}'! (This should be impossible!)");
    }

    return;
}


=head2 set_metadata(%args)

Sets the text and optionally the ID of the first specified element
type found (case-insensitive).  Creates the element if one did not
exist (with the exact capitalization specified).

If a <dc-metadata> element exists underneath <metadata>, the language
element will be created underneath the <dc-metadata> and any standard
attributes will be created in OEB 1.2 format, otherwise the element is
created underneath <metadata> in OPF 2.0 format.

Returns 1 on success, returns undef if no gi or if no text was specified.

=cut

=head3 Arguments

=over

=item C<gi>

The generic identifier (tag) of the metadata element to alter or
create.  If not specified, the method sets an error and returns undef.

=item C<parent>

The generic identifier (tag) of the parent to use for any newly
created element.  If not specified, defaults to 'dc-metadata' if
'dc-metadata' exists underneath 'metadata', and 'metadata' otherwise.

A newly created element will be created under the first element found
with this gi.  A modified element will be moved under the first
element found with this gi.

Newly created elements will use OPF 2.0 attribute names if the parent
is 'metadata' and OEB 1.2 attribute names otherwise.

=item C<text>

This specifies the element text to set.  If not specified, the method
sets an error and returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=item C<fileas> (optional)

This specifies the file-as attribute to set on the element.

=item C<role> (optional)

This specifies the role attribute to set on the element.

=item C<scheme> (optional)

This specifies the scheme attribute to set on the element.

=back

=head3 Example

 $retval = $ebook->set_metadata(gi => 'AuthorNonstandard',
                                text => 'Element Text',
                                id => 'customid',
                                fileas => 'Text, Element',
                                role => 'xxx',
                                scheme => 'code');

=cut

sub set_metadata :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(3,"DEBUG[",$subname,"]");
    my %valid_args = (
        'gi' => 1,
        'parent' => 1,
        'text' => 1,
        'id' => 1,
        'fileas' => 1,
        'role' => 1,
        'scheme' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $gi = $args{gi};
    unless($gi) {
        $self->add_error($subname,"(): no gi specified");
        return;
    }

    my $text = $args{text};
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    my $element = $self->{twigroot}->first_descendant(qr/^ $gi $/ix);
    my $meta;
    my $dcmeta;
    my $parent;
    my %dcatts;

    $self->fix_metastructure_basic();
    $parent =  $self->{twigroot}->first_descendant(qr/^ $args{parent} $/ix)
        if($args{parent});
    $meta = $self->{twigroot}->first_child('metadata');
    $dcmeta = $meta->first_child('dc-metadata');
    $parent = $parent || $dcmeta || $meta;
    if($parent->gi eq 'metadata') {
        %dcatts = (
            'file-as' => 'opf:file-as',
            'role' => 'opf:role',
            'scheme' => 'opf:scheme',
            );
    }
    else {
        %dcatts = (
            'file-as' => 'file-as',
            'role' => 'role',
            'scheme' => 'scheme'
        );
    }


    if($element) {
        debug(2,"DEBUG: updating '",$gi,"'");
        if($element->att('opf:file-as') && $args{fileas}) {
            debug(3,"DEBUG:   setting opf:file-as '",$args{fileas},"'");
            $element->set_att('opf:file-as',$args{fileas});
        }
        elsif($args{fileas}) {
            debug(3,"DEBUG:   setting file-as '",$args{fileas},"'");
            $element->set_att('file-as',$args{fileas});
        }
        if($element->att('opf:role') && $args{role}) {
            debug(3,"DEBUG:   setting opf:role '",$args{role},"'");
            $element->set_att('opf:role',$args{role});
        }
        elsif($args{role}) {
            debug(3,"DEBUG:   setting role '",$args{role},"'");
            $element->set_att('role',$args{role});
        }
        if($element->att('opf:scheme') && $args{scheme}) {
            debug(3,"DEBUG:   setting opf:scheme '",$args{scheme},"'");
            $element->set_att('opf:scheme',$args{scheme});
        }
        elsif($args{scheme}) {
            debug(3,"DEBUG:   setting scheme '",$args{scheme},"'");
            $element->set_att('scheme',$args{scheme});
        }
        debug(3,"DEBUG:   setting text");
        $element->set_text($text);

        unless($element->parent->gi eq $parent->gi) {
            debug(2,"DEBUG: moving <",$element->gi,"> under <",
                  $parent->gi,">");
            $element->move('last_child',$parent);
        }
    }
    else {
        debug(2,"DEBUG: creating '",$gi,"' under <",$parent->gi,">");
        $element = $parent->insert_new_elt('last_child',$gi);
        $element->set_att($dcatts{'file-as'},$args{fileas})
            if($args{fileas});
        $element->set_att($dcatts{'role'},$args{role})
            if($args{role});
        $element->set_att($dcatts{'scheme'},$args{scheme})
            if($args{scheme});
        $element->set_text($text);
    }

    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' to a '",$element->gi,"'!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 set_opffile($filename)

Sets the filename used to store the OPF metadata.

Returns 1 on success; sets an error message and returns undef if no
filename was specified.

=cut

sub set_opffile :method {
    my ($self,$filename) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    unless($filename) {
        debug(1,$subname,"(): no filename specified!");
        $self->add_warning($subname,"(): no filename specified!");
        return;
    }
    $self->{opffile} = $filename;
    return 1;
}


=head2 set_retailprice(%args)

Sets the Mobipocket-specific <SRP> element (Suggested Retail Price),
creating or deleting it as necessary.

If a new element has to be created, L</fix_metastructure_oeb12> is
called to ensure that <x-metadata> exists and the element is created
under <x-metadata>, as Mobipocket elements are not recognized by
Mobipocket's software when placed directly under <metadata>

=head3 Arguments

=over

=item * C<text>

The price to set as the text of the element.  If this is undefined,
the method sets an error and returns undef.  If it is set but false,
any existing <SRP> element is deleted.

=item * C<currency> (optional)

The value to set on the 'Currency' attribute.  If not provided,
defaults to 'USD' (US Dollars)

=back

=cut

sub set_retailprice :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    $self->twigcheck;

    my %valid_args = (
        'text' => 1,
        'currency' => 1,
        );

    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    unless(defined $args{text}) {
        $self->add_error($subname,"(): text not defined");
        return;
    }

    my $xmeta;
    my $element;
    my @elements;

    if($args{text}) {
        $element = $self->{twigroot}->first_descendant(qr/^ SRP $/ix);
        unless($element) {
            $self->fix_metastructure_oeb12();
            $xmeta = $self->{twigroot}->first_descendant('x-metadata');
            $element = $xmeta->insert_new_elt('last_child','SRP');
        }
        $element->set_text($args{text});
        $element->set_att('Currency',$args{currency}) if($args{currency});
    }
    else {
        @elements = $self->{twigroot}->descendants(qr/^ SRP $/ix);
        foreach my $el (@elements) {
            debug(2,"DEBUG: deleting <SRP>");
            $el->delete;
        }
    }
    return 1;
}


=head2 set_primary_author(%args)

Sets the text, id, file-as, and role attributes of the primary author
element (see L</primary_author()> for details on how this is found),
or if no primary author exists, creates a new element containing the
information.

This method calls L</fix_metastructure_basic()> to enforce the
presence of the <metadata> element.  When creating a new element, the
method will use the OEB 1.2 element name and create the element
underneath <dc-metadata> if an existing <dc-metadata> element is found
underneath <metadata>.  If no existing <dc-metadata> element is found,
the new element will be created with the OPF 2.0 element name directly
underneath <metadata>.  Regardless, it is probably a good idea to call
L</fix_oeb12()> or L</fix_opf20()> after calling this method to ensure
a consistent scheme.

=head3 Arguments

Three optional named arguments can be passed:

=over

=item * C<text>

Specifies the author text to set.  If omitted and a primary author
element exists, the text will be left as is; if omitted and a primary
author element cannot be found, an error message will be generated and
the method will return undef.

=item * C<fileas>

Specifies the 'file-as' attribute to set.  If omitted and a primary
author element exists, any existing attribute will be left untouched;
if omitted and a primary author element cannot be found, the newly
created element will not have this attribute.

=item * C<id>

Specifies the 'id' attribute to set.  If this is specified, and the id
is already in use, a warning will be added but the method will
continue, removing the id attribute from the element that previously
contained it.

If this is omitted and a primary author element exists, any existing
id will be left untouched; if omitted and a primary author element
cannot be found, the newly created element will not have an id set.

=back

If called with no arguments, the only effect this method has is to
enforce that either an 'opf:role' or 'role' attribute is set to 'aut'
on the primary author element.

=head3 Return values

Returns 1 if successful, returns undef and sets an error message if
the author argument is missing and no primary author element was
found.

=cut

sub set_primary_author :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'fileas' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $twigroot = $self->{twigroot};
    $self->fix_metastructure_basic();
    my $meta = $twigroot->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $element;
    my $newauthor = $args{text};
    my $newfileas = $args{fileas};
    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $element = $twigroot->first_descendant(\&twigelt_is_author);
    $element = $twigroot->first_descendant(qr/dc:creator/ix) if(!$element);

    unless($element) {
        unless($newauthor) {
            add_error(
                $subname,
                "(): cannot create a new author element when the author is not specified");
            return;
        }
        if($dcmeta) {
            $element = $dcmeta->insert_new_elt('last_child','dc:Creator');
            $element->set_att('role' => 'aut');
            $element->set_att('file-as' => $newfileas) if($newfileas);
        }
        else {
            $element = $meta->insert_new_elt('last_child','dc:creator');
            $element->set_att('opf:role' => 'aut');
            $element->set_att('opf:file-as' => $newfileas) if($newfileas);
        }
    } # unless($element)
    $element->set_text($newauthor);

    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 C<set_publisher(%args)>

Sets the text and optionally the ID of the first dc:publisher element
found (case-insensitive).  Creates the element if one did not exist.
If a <dc-metadata> element exists underneath <metadata>, the publisher
element will be created underneath the <dc-metadata> in OEB 1.2
format, otherwise the title element is created underneath <metadata>
in OPF 2.0 format.

Returns 1 on success, returns undef if no publisher was specified.

=head3 Arguments

C<set_publisher()> takes one required and one optional named argument

=over

=item C<text>

This specifies the publisher name to set as the text of the element.
If not specified, the method returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=head3 Example

 $retval = $ebook->set_publisher('text' => 'My Publishing House',
                                 'id' => 'mypubid');

=cut

sub set_publisher :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $publisher = $args{text};
    return unless($publisher);

    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:publisher$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');

    if(!$element && $dcmeta) {
        $element = $dcmeta->insert_new_elt('last_child','dc:Publisher');
    }
    elsif(!$element) {
        $element = $meta->insert_new_elt('last_child','dc:publisher');
    }
    $element->set_text($publisher);
    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 set_review(%args)

Sets the text and optionally ID of the first <Review> element found
(case-insensitive), creating the element if one did not exist.

This is a Mobipocket-specific element and if it needs to be created it
will always be created under <x-metadata> with
L</fix_metastructure_oeb12()> called to ensure that <x-metadata>
exists.

Returns 1 on success, returns undef if no review text was specified

=head3 Arguments

=over

=item C<text>

This specifies the description to use as the text of the element.  If
not specified, the method returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=head3 Example

 $retval = $ebook->set_review('text' => 'This book is perfect!',
                              'id' => 'revid');

=cut

sub set_review :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $text = $args{text};
    unless($text) {
        $self->add_error($subname,"(): no text specified");
        return;
    }

    $self->fix_metastructure_oeb12();
    $self->set_metadata(gi => 'Review',
                        parent => 'x-metadata',
                        text => $args{text},
                        id => $args{id});

    return 1;
}


=head2 C<set_rights(%args)>

Sets the text of the first dc:rights or dc:copyrights element found
(case-insensitive).  If the element found has the gi of dc:copyrights,
it will be changed to dc:rights.  This is to correct certain
noncompliant Mobipocket files.

Creates the element if one did not exist.  If a <dc-metadata> element
exists underneath <metadata>, the title element will be created
underneath the <dc-metadata> in OEB 1.2 format, otherwise the title
element is created underneath <metadata> in OPF 2.0 format.

Returns 1 on success, returns undef if no rights string was specified.

=head3 Arguments

=over

=item * C<text>

This specifies the text of the element.  If not specified, the method
returns undef.

=item * C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged but the method continues anyway.

=back

=cut

sub set_rights :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    my $rights = $args{text};
    return unless($rights);
    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:(copy)?rights$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $parent = $dcmeta || $meta;

    $element ||= $parent->insert_new_elt('last_child','dc:rights');
    $element->set_text($rights);
    $element->set_gi('dc:Rights') if($element->gi eq 'dc:Copyrights');
    $element->set_gi('dc:rights') if($element->gi eq 'dc:copyrights');
    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 C<set_spec($spec)>

Sets the OEB specification to match when modifying OPF data.
Allowable values are 'OEB12', 'OPF20', and 'MOBI12'.

Returns 1 if successful; returns undef and sets an error message if an
unknown specification was set.

=cut

sub set_spec :method {
    my ($self,$spec) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    unless($validspecs{$spec}) {
        $self->add_error($subname,"(): invalid specification '",$spec,"'");
        return;
    }
    $self->{spec} = $validspecs{$spec};
    return 1;
}


=head2 C<set_timestamp()>

Sets the <meta property="dcterms:modified"> element to the current
timestamp and removes duplicate or nonstandard timestamps.

=cut

sub set_timestamp :method {
    my ($self) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my @timestamps;
    my $twigroot = $self->{twigroot};
    my $time = Date::Manip::Date->new();

    @timestamps = $twigroot->descendants(
        'meta[@property="dcterms:modified" or @name="calibre:timestamp"]');
    foreach my $timestamp (@timestamps) {
        $timestamp->delete;
    }
    @timestamps = $twigroot->descendants(
        'dc:date[@opf:event="modification" or @event="modification"]');
    foreach my $timestamp (@timestamps) {
        $timestamp->delete;
    }
    $time->parse('now');
    $self->set_meta('property' => 'dcterms:modified',
                    'text' => $time->printf('%O') );
    return;
}


=head2 C<set_title(%args)>

Sets the text or id of the first dc:title element found
(case-insensitive).  Creates the element if one did not exist.  If a
<dc-metadata> element exists underneath <metadata>, the title element
will be created underneath the <dc-metadata> in OEB 1.2 format,
otherwise the title element is created underneath <metadata> in OPF
2.0 format.

=head3 Arguments

set_title() takes two optional named arguments.  If neither is
specified, the method will do nothing.

=over

=item * C<text>

This specifies the text of the element.  If not specified, and no
title element is found, an error will be set and the method will
return undef -- set_title() will refuse to create a dc:title element
with no text.

=item * C<id>

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged but the method continues anyway.

=back

=cut

sub set_title :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");

    my %valid_args = (
        'text' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }
    my $title = $args{text};
    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:title$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');
    my $parent = $dcmeta || $meta;
    unless($element) {
        unless($title) {
            add_error($subname,
                      "(): no title specified, but no existing title found");
            return;
        }
        $element = $parent->insert_new_elt('last_child','dc:title');
    }
    $element->set_text($title) if($title);

    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


=head2 C<set_type(%args)>

Sets the text and optionally the ID of the first dc:type element
found (case-insensitive).  Creates the element if one did not exist.
If a <dc-metadata> element exists underneath <metadata>, the publisher
element will be created underneath the <dc-metadata> in OEB 1.2
format, otherwise the title element is created underneath <metadata>
in OPF 2.0 format.

Returns 1 on success, returns undef if no publisher was specified.

=head3 Arguments

C<set_type()> takes one required and one optional named argument

=over

=item C<text>

This specifies the publisher name to set as the text of the element.
If not specified, the method returns undef.

=item C<id> (optional)

This specifies the ID to set on the element.  If set and the ID is
already in use, a warning is logged and the ID is removed from the
other location and assigned to the element.

=back

=head3 Example

 $retval = $ebook->set_type('text' => 'Short Story',
                            'id' => 'mytypeid');

=cut

sub set_type :method {
    my ($self,%args) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname . "() called as a procedure") unless(ref $self);
    debug(2,"DEBUG[",$subname,"]");
    my %valid_args = (
        'text' => 1,
        'id' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $text = $args{text};
    return unless($text);

    my $newid = $args{id};
    my $idelem;
    $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);

    $self->fix_metastructure_basic();
    my $element = $self->{twigroot}->first_descendant(qr/^dc:type$/ix);
    my $meta = $self->{twigroot}->first_child('metadata');
    my $dcmeta = $meta->first_child('dc-metadata');

    if(!$element && $dcmeta) {
        $element = $dcmeta->insert_new_elt('last_child','dc:Type');
    }
    elsif(!$element) {
        $element = $meta->insert_new_elt('last_child','dc:type');
    }
    $element->set_text($text);
    if($idelem && $idelem->cmp($element) ) {
        $self->add_warning(
            $subname,"(): reassigning id '",$newid,
            "' from a '",$idelem->gi,"' element!"
            );
        $idelem->del_att('id');
    }
    $element->set_att('id' => $newid) if($newid);
    return 1;
}


################################
########## PROCEDURES ##########
################################

=head1 PROCEDURES

All procedures are exportable, but none are exported by default.  All
procedures can be exported by using the ":all" tag.


=head2 C<_lc>

Wrapper for CORE::lc to get around the fact that builtins can't be
used in dispatch tables prior to Perl 5.16.

WARNING: this procedure may disappear once Perl 5.16 is standard on
all systems in common use!  For that reason, this is not exportable.

=cut

sub _lc {
    my ($string) = @_;
    return lc $string;
}


=head2 C<_uc>

Wrapper for CORE::uc to get around the fact that builtins can't be
used in dispatch tables prior to Perl 5.16.

WARNING: this procedure may disappear once Perl 5.16 is standard on
all systems in common use!  For that reason, this is not exportable.

=cut

sub _uc {
    my ($string) = @_;
    return uc $string;
}


=head2 C<capitalize($string)>

Capitalizes the first letter of each word in $string.

Returns the corrected string.

=cut

sub capitalize {
    my ($string) = @_;
    $string =~ s/(?<=\w)(.)/\l$1/gx;
    return $string;
}


=head2 C<clean_filename($string)>

Takes an input string and cleans out any characters that would not be
valid in a filename.

Returns the cleaned string.

=cut

sub clean_filename {
    my ($string) = @_;
    $string =~ s/[^A-Za-z0-9_ \-\.]//g;
    return $string;
}


=head2 C<create_epub_container($opffile)>

Creates the XML file META-INF/container.xml pointing to the
specified OPF file.

Creates the META-INF directory if necessary.  Will destroy any
non-directory file named 'META-INF' in the current directory.  If
META-INF/container.xml already exists, it will rename that file to
META-INF/container.xml.backup.

=head3 Arguments

=over

=item $opffile

The OPF filename (and path, if necessary) to use in the container.  If
not specified, looks for a sole OPF file in the current working
directory.  Fails if more than one is found.

=back

=head3 Return values

=over

=item Returns a twig representing the container data if successful, undef
otherwise

=back

=cut

sub create_epub_container {
    my ($opffile) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my $twig;
    my $twigroot;
    my $rootfiles;
    my $element;
    my $fh_container;

    if($opffile eq '') { return; }

    if(-e 'META-INF') {
	if(! -d 'META-INF') {
	    unlink('META-INF') or return;
	    mkdir('META-INF') or return;
	}
    }
    else { mkdir('META-INF') or return; }

    $twig = XML::Twig->new(
	output_encoding => 'utf-8',
	pretty_print => 'record'
	);

    $element = XML::Twig::Elt->new('container');
    $element->set_att('version' => '1.0',
		      'xmlns' => 'urn:oasis:names:tc:opendocument:xmlns:container');
    $twig->set_root($element);
    $twigroot = $twig->root;

    $rootfiles = $twigroot->insert_new_elt('first_child','rootfiles');
    $element = $rootfiles->insert_new_elt('first_child','rootfile');
    $element->set_att('full-path',$opffile);
    $element->set_att('media-type','application/oebps-package+xml');


    # Backup existing file
    if(-e 'META-INF/container.xml') {
        rename('META-INF/container.xml','META-INF/container.xml.backup')
            or croak($subname,"(): could not backup container.xml!");
    }

    open($fh_container,'>:encoding(UTF-8)','META-INF/container.xml')
        or croak($subname,"(): could not write to 'META-INF/container.xml'\n");
    $twig->print(\*$fh_container);
    close($fh_container)
        or croak($subname,"(): could not close 'META-INF/container.xml'\n");
    return $twig;
}


=head2 C<create_epub_mimetype()>

Creates a file named 'mimetype' in the current working directory
containing 'application/epub+zip' (no trailing newline)

Destroys and overwrites that file if it exists.

Returns the mimetype string if successful, undef otherwise.

=cut

sub create_epub_mimetype {
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my $mimetype = "application/epub+zip";
    my $fh_mimetype;

    open($fh_mimetype,">",'mimetype') or return;
    print {*$fh_mimetype} $mimetype;
    close($fh_mimetype) or croak($subname,"(): failed to close filehandle [$!]");

    return $mimetype;
}


=head2 C<debug($level,@message)>

Prints a debugging message to C<STDERR> if package variable C<$debug>
is greater than or equal to C<$level>.  A trailing newline is
appended, and should not be part of @message.

Returns true or dies.

=cut

sub debug {
    my ($level,@message) = @_;
    my $subname = ( caller(0) )[3];
    croak($subname,"(): no debugging level specified") unless($level);
    croak($subname,"(): invalid debugging level '",$level,"'")
        unless( $level =~ /^\d$/ );
    croak($subname,"(): no message specified") unless(@message);
    print {*STDERR} @message,"\n" if($debug >= $level);
    return 1;
}


=head2 C<excerpt_line($text)>

Takes as an argument a list of text pieces that will be joined.  If
the joined length is less than 70, all of the joined text is returned.

If the joined length is greater than 70, the return string is the
first 30 characters followed by C<' [...] '> followed by the last 30
characters.

=cut

sub excerpt_line {
    my @parts = @_;
    my $subname = ( caller(0) )[3];
    my $text = join('',@parts);
    if(length($text) > 70)
    {
        $text =~ /^ (.{30}) .*? (.{30}) $/sx;
        return ($1 . ' [...] ' . $2);
    }
    else { return $text; }
}


=head2 C<find_in_path($pattern,@extradirs)>

Searches through C<$ENV{PATH}> (and optionally any additional
directories specified in C<@extradirs>) for the first regular file
matching C<$pattern>.  C<$pattern> itself can take two forms: if
passed a C<qr//> regular expression, that expression is used directly.
If passed any other string, that string will be used for a
case-insensitive exact match where the extension '.bat', '.com', or
'.exe' is optional (i.e. the final pattern will be
C<qr/^ $pattern (\.bat|\.com|\.exe)? $/ix>).

Returns the first match found, or undef if there were no matches or if
no pattern was specified.

=cut

sub find_in_path {
    my ($pattern,@extradirs) = @_;
    return unless($pattern);
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    my $regexp;
    my @dirs;
    my $fh_dir;
    my @filelist;
    my $envsep = ':';
    my $filesep = '/';
    if($OSNAME eq 'MSWin32') {
        $envsep = ';';
        $filesep = "\\";
    }

    if(ref($pattern) eq 'Regexp') { $regexp = $pattern; }
    else { $regexp = qr/^ $pattern (\.bat|\.com|\.exe)? $/ix; }

    @dirs = split(/$envsep/,$ENV{PATH});
    unshift(@dirs,@extradirs) if(@extradirs);
    foreach my $dir (@dirs) {
        if(-d $dir) {
            if(opendir($fh_dir,$dir)) {
                @filelist = grep { /$regexp/ } readdir($fh_dir);
                @filelist = grep { -f "$dir/$_" } @filelist;
                closedir($fh_dir);

                if(@filelist) { return $dir . $filesep . $filelist[0]; }
            }
        }
    }
    return;
}

=head2 C<find_links($filename)>

Searches through a file for href and src attributes, and returns a
list of unique links with any named anchors removed
(e.g. 'myfile.html#part7' returns as just 'myfile.html').  If no links
are found, or the file does not exist, returns undef.

Does not check to see if the links are local.  Requires that links be
surrounded by double quotes, not single or left bare.  Assumes that
any link will not be broken across multiple lines, so it will (for
example) fail to find:

 <img src=
 "myfile.jpg">

though it can find:

 <img
  src="myfile.jpg">

This also does not distinguish between local files and remote links.

=cut

sub find_links {
    my ($filename) = @_;
    return unless(-f $filename);
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    my $fh;
    my %linkhash;
    my @links;

    my $subdir = dirname($filename);
    if($subdir eq '.') {
        $subdir = '';
    }
    else {
        $subdir = $subdir . '/';
    }

    open($fh,'<:raw',$filename)
        or croak($subname,"(): unable to open '",$filename,"'\n");

    while(<$fh>) {
        @links = /(?:href|src) \s* = \s* "
                  ([^">]+)/gix;
        foreach my $link (@links) {
            # Perform URI decoding
            $link = uri_unescape($link);

            # Strip off any named anchors
            $link =~ s/#.*$//;
            next unless $link;

            # Skip links that begin with backwards directory traversal
            next if $link =~ m#^\.\./.*$#;

            # Strip javascript: hrefs
            next if $link =~ m#javascript:#;

            # Baen HTML sources in particular may contain javascript
            # href generators
            next if $link =~ /\+ /;

            # If the link is not a URI and we are in a subdirectory
            # relative to the OPF, ensure that subdirectory is placed
            # as a prefix.
            if($subdir and $link !~ m#^ \w+://#ix) {
                $link = $subdir . $link;
            }
            debug(2, "DEBUG: found link '",$link,"'");
            $linkhash{$link}++;
        }
    }
    if(%linkhash) { return keys(%linkhash); }
    else { return; }
}


=head2 C<find_opffile()>

Attempts to locate an OPF file, first by calling
L</get_container_rootfile()> to check the contents of
C<META-INF/container.xml>, and then by looking for a single file with
the extension C<.opf> in the current working directory.

Returns the filename of the OPF file, or undef if nothing was found.

=cut

sub find_opffile {
    my $subname = ( caller(0) )[3];
    my $opffile = get_container_rootfile();

    if(!$opffile) {
	my @candidates = glob("*.opf");
        if(scalar(@candidates) > 1) {
            debug(1,"DEBUG: Multiple OPF files found, but no container",
                  " to specify which one to choose!");
            return;
        }
        if(scalar(@candidates) < 1) {
            debug(1,"DEBUG: No OPF files found!");
            return;
        }
        $opffile = $candidates[0];
    }
    return $opffile;
}


=head2 C<fix_datestring($datestring)>

Takes a date string and attempts to convert it to the limited subset
of ISO8601 allowed by the OPF standard (YYYY, YYYY-MM, or YYYY-MM-DD).

In the special case of finding MM/DD/YYYY, it assumes that it was a
Mobipocket-mangled date, and not only converts it, but will strip the
day information if the day is '01', and both the month and day
information if both month and day are '01'.  This is because
Mobipocket Creator enforces a complete MM/DD/YYYY even if the month
and day aren't known, and it is common practice to use 01 for an
unknown value.

=head3 Arguments

=over

=item $datestring

A date string in a format recognizable by Date::Manip

=back

=head3 Returns $fixeddate

=over

=item $fixeddate : the corrected string, or undef on failure

=back

=cut

sub fix_datestring {
    my ($datestring) = @_;
    return unless($datestring);
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my $date;
    my ($year,$month,$day);
    my $fixeddate;

    $_ = $datestring;

    debug(3,"DEBUG: checking M(M)/D(D)/YYYY");
    if(( ($month,$day,$year) = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/x ) == 3) {
	# We have a XX/XX/XXXX datestring
	debug(3,"DEBUG: found '",$month,"/",$day,"/",$year,"'");
	($year,$month,$day) = ymd_validate($year,$month,$day);
	if($year) {
	    $fixeddate = $year;
	    $fixeddate .= sprintf("-%02u",$month)
		unless( ($month == 1) && ($day == 1) );
	    $fixeddate .= sprintf("-%02u",$day) unless($day == 1);
	    debug(3,"DEBUG: returning '",$fixeddate,"'");
	    return $fixeddate;
	}
    }

    debug(3,"DEBUG: checking M(M)/YYYY");
    if(( ($month,$year) = /^(\d{1,2})\/(\d{4})$/x ) == 2) {
	# We have a XX/XXXX datestring
	debug(3,"DEBUG: found '",$month,"/",$year,"'");
	if($month <= 12) {
	    # We probably have MM/YYYY
	    $fixeddate = sprintf("%04u-%02u",$year,$month);
	    debug(3,"DEBUG: returning '",$fixeddate,"'");
	    return $fixeddate;
	}
    }

    # These regexps will reduce '2009-xx-01' to just 2009
    # We don't want this, so don't use them.
#    ($year,$month,$day) = /(\d{4})-?(\d{2})?-?(\d{2})?/;
#    ($year,$month,$day) = /(\d{4})(?:-?(\d{2})-?(\d{2}))?/;

    # Force exact match)
    debug(3,"DEBUG: checking YYYY-MM-DD");
    ($year,$month,$day) = /^(\d{4})-(\d{2})-(\d{2})$/x;
    ($year,$month,$day) = ymd_validate($year,$month,$day);

    if(!$year) {
	debug(3,"DEBUG: checking YYYYMMDD");
	($year,$month,$day) = /^(\d{4})(\d{2})(\d{2})$/x;
	($year,$month,$day) = ymd_validate($year,$month,$day);
    }

    if(!$year) {
	debug(3,"DEBUG: checking YYYY-M(M)");
	($year,$month) = /^(\d{4})-(\d{1,2})$/x;
	($year,$month) = ymd_validate($year,$month,undef);
    }

    if(!$year) {
	debug(3,"DEBUG: checking YYYY");
	($year) = /^(\d{4})$/x;
    }

    # At this point, we've exhausted all of the common cases.  We use
    # Date::Manip to hit all of the unlikely ones as well.  This comes
    # with a drawback: Date::Manip doesn't distinguish between 2008
    # and 2008-01-01, but we should have covered that above.
    #
    # Note that Date::Manip will die on MS Windows system unless the
    # TZ environment variable is set in a specific manner.
    # See:
    # http://search.cpan.org/perldoc?Date::Manip#TIME_ZONES

    if(!$year) {
	$date = ParseDate($datestring);
	$year = UnixDate($date,"%Y");
	$month = UnixDate($date,"%m");
	$day = UnixDate($date,"%d");
	debug(2,"DEBUG: Date::Manip found '",UnixDate($date,"%Y-%m-%d"),"'");
    }

    if($year) {
	# If we still have a $year, $month and $day either don't exist
	# or are plausibly valid.
	print {*STDERR} "DEBUG: found year=",$year," " if($debug >= 2);
	$fixeddate = sprintf("%04u",$year);
	if($month) {
	    print {*STDERR} "month=",$month," " if($debug >= 2);
	    $fixeddate .= sprintf("-%02u",$month);
	    if($day) {
		print {*STDERR} "day=",$day if($debug >= 2);
		$fixeddate .= sprintf("-%02u",$day);
	    }
	}
	print {*STDERR} "\n" if($debug >= 2);
	debug(2,"DEBUG: returning '",$fixeddate,"'");
	return $fixeddate if($fixeddate);
    }

    if(!$year) {
	debug(3,"fix_date: didn't find a valid date in '",$datestring,"'!");
	return;
    }
    elsif($debug) {
	print {*STDERR} "DEBUG: found ",sprintf("04u",$year);
	print {*STDERR} sprintf("-02u",$month) if($month);
	print {*STDERR} sprintf("-02u",$day),"\n" if($day);
    }

    $fixeddate = sprintf("%04u",$year);
    $fixeddate .= sprintf("-%02u-%02u",$month,$day);
    return $fixeddate;
}


=head2 C<get_container_rootfile($container)>

Opens and parses an OPS/epub container, extracting the 'full-path'
attribute of element 'rootfile'

=head3 Arguments

=over

=item $container

The OPS container to parse.  Defaults to 'META-INF/container.xml'

=back

=head3 Return values

=over

=item Returns a string containing the rootfile on success, undef on failure.

=back

=cut

sub get_container_rootfile {
    my ($container) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my $twig = XML::Twig->new();
    my $rootfile;
    my $retval = undef;

    $container = 'META-INF/container.xml' if(! $container);

    if(-f $container) {
	$twig->parsefile($container) or return;
	$rootfile = $twig->root->first_descendant('rootfile');
	return unless($rootfile);
	$retval = $rootfile->att('full-path');
    }
    return $retval;
}


=head2 C<hashvalue_key_self(\%hash, $modifier)>

Takes as an argument a hash reference and an optional modifier and
inserts a new key for every value in that hash if no such key already
exists.

If the modifer is set to 'lc' or 'uc', the value is either lowercased
or uppercased respectively before it is used as a key.

Croaks if the first argument is not a hashref, or if an invalid
modifier string is used.

=cut

sub hashvalue_key_self {
    my ($hashref, $modifier) = @_;
    my $subname = ( caller(0) )[3];
    debug(4,"DEBUG[",$subname,"]");

    if (not $hashref or not ref($hashref)) {
        croak($subname,"(): first argument is not a reference");
    }
    elsif (!UNIVERSAL::isa($hashref,'HASH')) {
        croak($subname,"(): first argument is not a hashref");
    }

    my %modifier_dispatch = (
        'lc' => \&_lc,
        'uc' => \&_uc,
       );

    if ($modifier and not $modifier_dispatch{$modifier}) {
        croak($subname,"(): ",$modifier," is not a valid modifier string");
    }

    foreach my $value (values %{$hashref}) {
        my $key;
        if($modifier) {
            $key = $modifier_dispatch{$modifier}($value);
        }
        else {
            $key = $value;
        }
        if (! $hashref->{$key}) {
            $hashref->{$key} = $value;
        }
    }
    return;
}


=head2 C<hexstring($bindata)>

Takes as an argument a scalar containing a sequence of binary bytes.
Returns a string converting each octet of the data to its two-digit
hexadecimal equivalent.  There is no leading "0x" on the string.

=cut

sub hexstring {
    my $data = shift;
    my $subname = ( caller(0) )[3];
    debug(4,"DEBUG[",$subname,"]");

    croak($subname,"(): no data provided")
        unless($data);

    my $byte;
    my $retval = '';
    my $pos = 0;

    while($pos < length($data)) {
        $byte = unpack("C",substr($data,$pos,1));
        $retval .= sprintf("%02x",$byte);
        $pos++;
    }
    return $retval;
}


=head2 C<print_memory($label)>

Checks /proc/$PID/statm and prints out a line to STDERR showing the
current memory usage.  This is a debugging tool that will likely fail
to do anything useful on a system without a /proc system compatible
with Linux.

=head3 Arguments

=over

=item $label

If defined, will be output along with the memory usage.

=back

Returns 1 on success, undef otherwise.

=cut

sub print_memory {
    my ($label) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    my @mem;
    my $fh_procstatm;

    if(!open($fh_procstatm,"<","/proc/$$/statm")) {
	print "[",$label,"]: " if(defined $label);
	print "Couldn't open /proc/$$/statm [$!]\n";
        return;
    }

    @mem = split(/\s+/,<$fh_procstatm>);
    close($fh_procstatm);

    # @mem[0]*4 = size (kb)
    # @mem[1]*4 = resident (kb)
    # @mem[2]*4 = shared (kb)
    # @mem[3] = trs
    # @mem[4] = lrs
    # @mem[5] = drs
    # @mem[6] = dt

    print "Current memory usage";
    print " [".$label."]" if(defined $label);
    print ":  size=",$mem[0]*4,"k";
    print "  resident=",$mem[1]*4,"k";
    print "  shared=",$mem[2]*4,"k\n";
    return 1;
}



=head2 C<split_metadata($metahtmlfile, $metafile)>

Takes a psuedo-HTML containing one or more <metadata>...</metadata>
blocks and splits out the metadata blocks into an XML file ready to be
used as an OPF document.  The input HTML file is rewritten without the
metadata.

If $metafile (or the temporary HTML-only file created during the
split) already exists, it will be moved to filename.backup.

=head3 Arguments

=over

=item C<$metahtmlfile>

The filename of the pseudo-HTML file

=item C<$metafile> (optional)

The filename to write out any extracted metadata.  If not specified,
will default to the basename of $metahtmlfile with '.opf' appended.

=back

Returns the filename the metadata was written to, or undef if no
metadata was found.

=cut

sub split_metadata {
    my ($metahtmlfile,$metafile) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    croak($subname,"(): no input file specified")
        if(!$metahtmlfile);

    croak($subname,"(): input file has zero size")
        if(-z $metahtmlfile);

    my @metablocks;
    my @guideblocks;
    my $htmlfile;

    my ($filebase,$filedir,$fileext);
    my ($fh_metahtml,$fh_meta,$fh_html);

    ($filebase,$filedir,$fileext) = fileparse($metahtmlfile,'\.\w+$');
    $metafile = $filedir . $filebase . ".opf" if(!$metafile);
    $htmlfile = $filedir . $filebase . "-html.html";

    debug(2,"DEBUG: metahtml='",$metahtmlfile,"'  meta='",$metafile,
          "  html='",$htmlfile,"'");

    # Move existing output files to avoid overwriting them
    if(-f $metafile) {
        debug(3, "DEBUG: moving metafile '",$metafile,"'");
        croak ($subname,"(): output file '",$metafile,
               "' exists and could not be moved!")
            if(! rename($metafile,"$metafile.backup") );
    }
    if(-f $htmlfile) {
        debug(3, "DEBUG: moving htmlfile '",$htmlfile,"'");
        croak ($subname,"(): output file '",$htmlfile,
               "' exists and could not be moved!")
            if(! rename($htmlfile,"$metafile.backup") );
    }

    debug(2,"  splitting '",$metahtmlfile,"'");
    open($fh_metahtml,"<:raw",$metahtmlfile)
	or croak($subname,"(): Failed to open '",$metahtmlfile,"' for reading!");
    open($fh_meta,">:raw",$metafile)
	or croak($subname,"(): Failed to open '",$metafile,"' for writing!");
    open($fh_html,">:raw",$htmlfile)
	or croak($subname,"(): Failed to open '",$htmlfile,"' for writing!");


    # Preload the return value with the OPF headers
    print $fh_meta $utf8xmldec,$oeb12doctype,"<package>\n";
    #print $fh_meta $utf8xmldec,$opf20package;

    # Finding and removing all of the metadata requires that the
    # entire thing be handled as one slurped string, so temporarily
    # undefine the perl delimiter
    #
    # Since multiple <metadata> sections may be present, cannot use
    # </metadata> as a delimiter.
    local $/;
    while(<$fh_metahtml>) {
        s/\sfilepos=\d+//gix;
	(@metablocks) = m#(<metadata>.*</metadata>)#gisx;
	(@guideblocks) = m#(<guide>.*</guide>)#gisx;
	last unless(@metablocks || @guideblocks);
	print {*$fh_meta} @metablocks,"\n" if(@metablocks);
	print {*$fh_meta} @guideblocks,"\n" if(@guideblocks);
	s#<metadata>.*</metadata>##gisx;
	s#<guide>.*</guide>##gisx;
	print {*$fh_html} $_,"\n";
    }
    print $fh_meta "</package>\n";

    close($fh_html)
        or croak($subname,"(): Failed to close '",$htmlfile,"'!");
    close($fh_meta)
        or croak($subname,"(): Failed to close '",$metafile,"'!");
    close($fh_metahtml)
        or croak($subname,"(): Failed to close '",$metahtmlfile,"'!");

    if( (-z $htmlfile) && (-z $metafile) ) {
        croak($subname,"(): ended up with no text in any output file",
              " -- bailing out!");
    }

    # It is very unlikely that split_metadata will be called twice
    # from the same program, so undef all capture variables reclaim
    # the memory.  Just going out of scope will not necessarily do
    # this.
    undef(@metablocks);
    undef(@guideblocks);
    undef($_);

    if(-z $htmlfile) {
        debug(1,"split_metadata(): HTML has zero size.",
             "  Not replacing original.");
        unlink($htmlfile);
    }
    else {
        rename($htmlfile,$metahtmlfile)
            or croak("split_metadata(): Failed to rename ",$htmlfile,
                     " to ",$metahtmlfile,"!\n");
    }

    if(-z $metafile) {
        croak($subname,
              "(): unable to remove empty output file '",$metafile,"'!")
            if(! unlink($metafile) );
        return;
    }
    return $metafile;
}


=head2 C<split_pre($htmlfile,$outfilebase)>

Splits <pre>...</pre> blocks out of a source HTML file into their own
separate HTML files including required headers.  Each block will be
written to its own file following the naming format
C<$outfilebase-###.html>, where ### is a three-digit number beginning
at 001 and incrementing for each block found.  If C<$outfilebase> is
not specified, it defaults to the basename of C<$htmlfile> with
"-pre-###.html" appended.  The

Returns a list containing all filenames created.

=cut

sub split_pre {
    my ($htmlfile,$outfilebase) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    croak($subname,"(): no input file specified")
        if(!$htmlfile);

    my ($filebase,$filedir,$fileext);
    my ($fh_html,$fh_htmlout,$fh_pre);
    my $htmloutfile;
    my @preblocks;
    my @prefiles = ();
    my $prefile;
    my $count = 0;

    my $htmlheader = <<'END';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
</head>
<body>
END

    ($filebase,$filedir,$fileext) = fileparse($htmlfile,'\.\w+$');
    $outfilebase = "$filebase-pre" if(!$outfilebase);
    $htmloutfile = "$filebase-nopre.html";

    open($fh_html,"<:raw",$htmlfile)
	or croak($subname,"(): Failed to open '",$htmlfile,"' for reading!");
    open($fh_htmlout,">:raw",$htmloutfile)
        or croak($subname,"(): Failed to open '",$htmloutfile,"' for writing!");

    local $/;
    while(<$fh_html>)
    {
	(@preblocks) = /(<pre>.*?<\/pre>)/gisx;
	last unless(@preblocks);

        foreach my $pre (@preblocks)
        {
            $count++;
            debug(1,"DEBUG: split_pre() splitting block ",
                  sprintf("%03d",$count));
            $prefile = sprintf("%s-%03d.html",$outfilebase,$count);
            if(-f $prefile)
            {
                rename($prefile,"$prefile.backup")
                    or croak("Unable to rename '",$prefile,
                             "' to '",$prefile,".backup'");
            }
            open($fh_pre,">:raw",$prefile)
                or croak("Unable to open '",$prefile,"' for writing!");
            print {*$fh_pre} $utf8xmldec;
            print {*$fh_pre} $htmlheader,"\n";
            print {*$fh_pre} $pre,"\n";
            print {*$fh_pre} "</body>\n</html>\n";
            close($fh_pre) or croak("Unable to close '",$prefile,"'!");
            push @prefiles,$prefile;
        }
	s/(<pre>.*?<\/pre>)//gisx;
	print {*$fh_htmlout} $_,"\n";
        close($fh_htmlout)
            or croak($subname,"(): Failed to close '",$htmloutfile,"'!");
        rename($htmloutfile,$htmlfile)
            or croak($subname,"(): Failed to rename '",$htmloutfile,"' to '",
                     $htmlfile,"'!");
    }
    return @prefiles;
}


=head2 C<strip_script(%args)>

Strips any <script>...</script> blocks out of a HTML file.

=head3 Arguments

=over

=item C<infile>

Specifies the input file.  If not specified, the sub croaks.

=item C<outfile>

Specifies the output file.  If not specified, it defaults to C<infile>
(i.e. the input file is overwritten).

=item C<noscript>

If set to true, the sub will strip <noscript>...</noscript> blocks as
well.

=back

=cut

sub strip_script {
    my %args = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");

    croak($subname,"(): no input file specified")
        if(!$args{infile});

    my %valid_args = (
        'infile'  => 1,
        'outfile' => 1,
        'noscript' => 1,
        );
    foreach my $arg (keys %args) {
        croak($subname,"(): invalid argument '",$arg,"'")
            if(!$valid_args{$arg});
    }

    my $infile = $args{infile};
    my $outfile = $args{outfile};
    $outfile = $infile unless($outfile);

    my ($fh_in,$fh_out);
    my $html;
    local $/;

    open($fh_in,"<:raw",$infile)
	or croak($subname,"(): Failed to open '",$infile,"' for reading!\n");
    $html = <$fh_in>;
    close($fh_in)
	or croak($subname,"(): Failed to close '",$infile,"'!\n");

    $html =~ s#<script>.*?</script>\n?##gix;
    $html =~ s#<noscript>.*?</noscript>\n?##gix
        if($args{noscript});

    open($fh_out,">:raw",$outfile)
	or croak($subname,"(): Failed to open '",$outfile,"' for writing!\n");
    print {*$fh_out} $html;
    close($fh_out)
	or croak($subname,"(): Failed to close '",$outfile,"'!\n");

    return 1;
}


=head2 C<system_result($caller,$retval,@syscmd)>

Checks the result of a system call and croak on failure with an
appropriate message.  For this to work, it MUST be used as the line
immediately following the system command.

=head3 Arguments

=over

=item $caller

The calling function (used in output message)

=item $retval

The return value of the system command

=item @syscmd

The array passed to the system call

=back

=head3 Return Values

Returns 0 on success

Croaks on failure.

=cut

sub system_result {
    my ($caller,$retval,@syscmd) = @_;

    if ( ($CHILD_ERROR >> 8) == 0 ) {
        return 0;
    }
    elsif ($CHILD_ERROR == -1) {
        croak($caller," child failed to execute (ERRNO=",$ERRNO,"):\n ",
              join(' ',@syscmd),"\n")
    }
    elsif ($CHILD_ERROR & 127) {
        my $withcoredump = ($CHILD_ERROR & 128) ? 'with' : 'without';
        croak($caller," child died with signal ",($CHILD_ERROR & 127)," ",
              $withcoredump," coredump:\n ",join(' ',@syscmd),"\n");
    }
    else {
        croak($caller," child exited with value ",$CHILD_ERROR >> 8,":\n ",
              join(' ',@syscmd),"\n")
    }
}


=head2 C<system_tidy_xhtml($infile,$outfile)>

Runs tidy on a XHTML file semi-safely (using a secondary file)

Converts HTML to XHTML if necessary

=head3 Arguments

=over

=item $infile

The filename to tidy

=item $outfile

The filename to use for tidy output if the safety condition to
overwrite the input file isn't met.

Defaults to C<infile-tidy.ext> if not specified.

=back

=head3 Global variables used

=over

=item $tidycmd

the location of the tidy executable

=item $tidyxhtmlerrors

the filename to use to output errors

=item $tidysafety

the safety factor to use (see CONFIGURABLE GLOBAL VARIABLES, above)

=back

=head3 Return Values

Returns the return value from tidy

=over

=item 0 - no errors

=item 1 - warnings only

=item 2 - errors

=item Dies horribly if the return value is unexpected

=back

=cut

sub system_tidy_xhtml {
    my ($infile,$outfile) = @_;
    my $retval;

    croak("system_tidy_xhtml called with no input file") if(!$infile);
    if(!$outfile)
    {
        my ($filebase,$filedir,$fileext) = fileparse($infile,'\.\w+$');
        $outfile = $filebase . "-tidy" . $fileext;
    }
    croak("system_tidy_xhtml called with no output file") if(!$outfile);

    $retval = system($tidycmd,
		     '-q','-utf8','--tidy-mark','no',
                     '--wrap','0',
                     '--clean','yes',
		     '-asxhtml',
                     '--output-xhtml','yes',
                     '--add-xml-decl','yes',
		     '--doctype','auto',
		     '-f',$tidyxhtmlerrors,
		     '-o',$outfile,
		     $infile);

    # Some systems may return a two-byte code, so deal with that first
    if($retval >= 256) { $retval = $retval >> 8 };
    if($retval == 0)
    {
	rename($outfile,$infile) if($tidysafety < 4);
	unlink($tidyxhtmlerrors);
    }
    elsif($retval == 1)
    {
	rename($outfile,$infile) if($tidysafety < 3);
	unlink($tidyxhtmlerrors) if($tidysafety < 2);
    }
    elsif($retval == 2)
    {
	print {*STDERR} "WARNING: Tidy errors encountered.  Check ",$tidyxhtmlerrors,"\n"
	    if($tidysafety > 0);
	unlink($tidyxhtmlerrors) if($tidysafety < 1);
    }
    else
    {
	# Something unexpected happened (program crash, sigint, other)
	croak("Tidy did something unexpected (return value=",$retval,
              ").  Check all output.");
    }
    return $retval;
}


=head2 C<system_tidy_xml($infile,$outfile)>

Runs tidy on an XML file semi-safely (using a secondary file)

=head3 Arguments

=over

=item C<$infile>

The filename to tidy

=item C<$outfile> (optional)

The filename to use for tidy output if the safety condition to
overwrite the input file isn't met.

Defaults to C<infile-tidy.ext> if not specified.

=back

=head3 Global variables used

=over

=item C<$tidycmd>

the name of the tidy executable

=item C<$tidyxmlerrors>

the filename to use to output errors

=item C<$tidysafety>

the safety factor to use (see CONFIGURABLE GLOBAL VARIABLES, above)

=back

=head3 Return values

Returns the return value from tidy

=over

=item 0 - no errors

=item 1 - warnings only

=item 2 - errors

=item Dies horribly if the return value is unexpected

=back

=cut

  sub system_tidy_xml {
      my ($infile,$outfile) = @_;
      my $retval;

      croak("system_tidy_xml called with no input file") if(!$infile);

      if (!$outfile) {
          my ($filebase,$filedir,$fileext) = fileparse($infile,'\.\w+$');
          $outfile = $filebase . "-tidy" . $fileext;
      }
      croak("system_tidy_xml called with no output file") if(!$outfile);

      $retval = system($tidycmd,
                       '-q','-utf8','--tidy-mark','no',
                       '--wrap','0',
                       '-xml',
                       '--add-xml-decl','yes',
                       '-f',$tidyxmlerrors,
                       '-o',$outfile,
                       $infile);

      # Some systems may return a two-byte code, so deal with that first
      if ($retval >= 256) {
          $retval = $retval >> 8;
      }
      ;
      if ($retval == 0) {
          rename($outfile,$infile) if($tidysafety < 4);
          unlink($tidyxmlerrors);
      }
      elsif ($retval == 1) {
          rename($outfile,$infile) if($tidysafety < 3);
          unlink($tidyxmlerrors) if($tidysafety < 2);
      }
      elsif ($retval == 2) {
          print STDERR "WARNING: Tidy errors encountered.  Check ",$tidyxmlerrors,"\n"
	    if ($tidysafety > 0);
          unlink($tidyxmlerrors) if($tidysafety < 1);
      }
      else {
          # Something unexpected happened (program crash, sigint, other)
          croak("Tidy did something unexpected (return value=",$retval,
                ").  Check all output.");
      }
      return $retval;
  }


=head2 C<trim>

Removes any whitespace characters from the beginning or end of every
string in @list (also works on scalars).

 trim;               # trims $_ inplace
 $new = trim;        # trims (and returns) a copy of $_
 trim $str;          # trims $str inplace
 $new = trim $str;   # trims (and returns) a copy of $str
 trim @list;         # trims @list inplace
 @new = trim @list;  # trims (and returns) a copy of @list

This was shamelessly copied from japhy's example at perlmonks.org:

http://www.perlmonks.org/?node_id=36684

If needed for large lists, it would probably be better to use
String::Strip.

=cut

sub trim { ## no critic
    ## no critic
    # PerlCritic is turned off here, as this is unusual code
    # deliberately bending rules.
    @_ = $_ if not @_ and defined wantarray;
    @_ = @_ if defined wantarray;
    for ( @_ ? @_ : $_ ) { s/^\s+//, s/\s+$// }
    return wantarray ? @_ : $_[ 0 ] if defined wantarray;
}


=head2 C<twigelt_create_uuid($gi)>

Creates an unlinked element with the specified gi (tag), and then
assigns it the id and scheme attributes 'UUID'.

=head3 Arguments

=over

=item  $gi : The gi (tag) to use for the element

Default: 'dc:identifier'

=back

Returns the element.

=cut

sub twigelt_create_uuid {
    my ($gi) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]");
    my $element;

    if(!$gi) { $gi = 'dc:identifier'; }

    my $uuidgen = Data::UUID->new();
    $element = XML::Twig::Elt->new($gi);
    $element->set_id('UUID');
    $element->set_att('scheme' => 'UUID');
    $element->set_text($uuidgen->create_str());
    return $element;
}


=head2 C<twigelt_detect_duplicate($element1, $element2)>

Takes two twig elements and returns 1 if they have the same GI (tag),
text, and attributes, but are not actually the same element.  The GI
comparison is case-insensitive.  The others are case-sensitive.

Returns 0 otherwise.

Croaks if passed anything but twig elements.

=cut

sub twigelt_detect_duplicate {
    my ($element1,$element2) = @_;
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    croak($subname,"(): arguments must be XML::Twig::Elt objects")
      unless( $element1->isa('XML::Twig::Elt')
                && $element2->isa('XML::Twig::Elt') );

    my (%atts1, %atts2);

    unless($element1->cmp($element2)) {
        debug(3,"  both elements have the same position");
        return 0;
    }

    unless( lc($element1->gi) eq lc($element2->gi) ) {
        debug(3,"  elements have different GIs");
        return 0;
    }

    unless($element1->text eq $element2->text) {
        debug(3,"  elements have different text");
        return 0;
    }

    %atts1 = %{$element1->atts};
    %atts2 = %{$element2->atts};

    my $attkeys1 = join('',sort keys %atts1);
    my $attkeys2 = join('',sort keys %atts2);

    unless($attkeys1 eq $attkeys2) {
        debug(3,"  elements have different attributes");
        return 0;
    }

    foreach my $att (keys %atts1) {
        unless($element1->att($att) eq $element2->att($att)) {
            debug(3,"  elements have different values for attribute '",
                  $att,"'");
            return 0;
        }
    }
    debug(3,"  elements are duplicates of each other!");
    return 1;
}

=head2 C<twigelt_fix_oeb12_atts($element)>

Checks the attributes in a twig element to see if they match OPF names
with an opf: namespace, and if so, removes the namespace.  Used by the
fix_oeb12() method.

Takes as a sole argument a twig element.

Returns that element with the modified attributes, or undef if the
element didn't exist.  Returns an unmodified element if both att and
opf:att exist.

=cut

sub twigelt_fix_oeb12_atts {
    my ($element) = @_;
    return unless($element);
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    my %opfatts_no_ns = (
        "opf:role" => "role",
        "opf:file-as" => "file-as",
        "opf:scheme" => "scheme",
        "opf:event" => "event"
        );

    foreach my $att ($element->att_names)
    {
        debug(3,"DEBUG:   checking attribute '",$att,"'");
        if($opfatts_no_ns{$att})
        {
            # If the opf:att attribute properly exists already, do nothing.
            if($element->att($opfatts_no_ns{$att}))
            {
                debug(1,"DEBUG:   found both '",$att,"' and '",
                      $opfatts_no_ns{$att},"' -- skipping.");
                next;
            }
            debug(1,"DEBUG:   changing attribute '",$att,"' => '",
                  $opfatts_no_ns{$att},"'");
            $element->change_att_name($att,$opfatts_no_ns{$att});
        }
    }
    return $element;
}


=head2 C<twigelt_fix_opf20_atts($element)>

Checks the attributes in a twig element to see if they match OPF
names, and if so, prepends the OPF namespace.  Used by the fix_opf20()
method.

Takes as a sole argument a twig element.

Returns that element with the modified attributes, or undef if the
element didn't exist.

=cut

sub twigelt_fix_opf20_atts {
    my ($element) = @_;
    return unless($element);
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    my %opfatts_ns = (
        "role" => "opf:role",
        "file-as" => "opf:file-as",
        "scheme" => "opf:scheme",
        "event" => "opf:event"
        );

    foreach my $att ($element->att_names)
    {
        debug(2,"DEBUG:   checking attribute '",$att,"'");
        if($opfatts_ns{$att})
        {
            # If the opf:att attribute properly exists already, do nothing.
            if($element->att($opfatts_ns{$att}))
            {
                debug(1,"DEBUG:   found both '",$att,"' and '",
                      $opfatts_ns{$att},"' -- skipping.");
                next;
            }
            debug(1,"DEBUG:   changing attribute '",$att,"' => '",
                  $opfatts_ns{$att},"'");
            $element->change_att_name($att,$opfatts_ns{$att});
        }
    }
    return $element;
}


=head2 C<twigelt_is_author($element)>

Takes as an argument a twig element.  Returns true if the element is a
dc:creator (case-insensitive) with either a opf:role="aut" or
role="aut" attribute defined.  Returns undef otherwise, and also if
the element has no text.

Croaks if fed no argument, or fed an argument that isn't a twig
element.

Intended to be used as a twig search condition.

=head3 Example

 my @elements = $ebook->twigroot->descendants(\&twigelt_is_author);

=cut

sub twigelt_is_author {
    my ($element) = @_;
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    croak($subname,"(): no element provided") unless($element);

    my $ref = ref($element) || '';

    croak($subname,"(): argument was of type '",$ref,
          "', needs to be 'XML::Twig::Elt' or a subclass")
        unless($element->isa('XML::Twig::Elt'));

    return if( (lc $element->gi) ne 'dc:creator');
    return unless($element->text);

    my $role = $element->att('opf:role') || $element->att('role');
    return unless($role);

    return 1 if($role eq 'aut');
    return;
}


=head2 C<twigelt_is_isbn($element)>

Takes as an argument a twig element.  Returns true if the element is a
dc:identifier (case-insensitive) where any of the id, opf:scheme, or
scheme attributes start with 'isbn', '-isbn', 'eisbn', or 'e-isbn'
(again case-insensitive).

Returns undef otherwise, and also if the element has no text.

Croaks if fed no argument, or fed an argument that isn't a twig
element.

Intended to be used as a twig search condition.

=head3 Example

 my @elements = $ebook->twigroot->descendants(\&twigelt_is_isbn);

=cut

sub twigelt_is_isbn {
    my ($element) = @_;
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    croak($subname,"(): no element provided") unless($element);

    my $ref = ref($element) || '';
    my $id;
    my $scheme;

    croak($subname,"(): argument was of type '",$ref,
          "', needs to be 'XML::Twig::Elt' or a subclass")
        unless($element->isa('XML::Twig::Elt'));

    return if( (lc $element->gi) ne 'dc:identifier');
    return unless($element->text);

    $id = $element->id || '';
    return 1 if($id =~ /^e?-?isbn/ix);

    $scheme = $element->att('opf:scheme') || '';
    return 1 if($scheme =~ /^e?-?isbn/ix);
    $scheme = $element->att('scheme') || '';
    return 1 if($scheme =~ /^e?-?isbn/ix);
    return;
}


=head2 C<twigelt_is_knownuid($element)>

Takes as an argument a twig element.  Returns true if the element is a
dc:identifier (case-insensitive) element with an C<id> attribute
matching the known IDs of proper unique identifiers suitable for a
package-id (also case-insensitive).  Returns undef otherwise.

Croaks if fed no argument, or fed an argument that isn't a twig element.

Intended to be used as a twig search condition.

=head3 Example

 my @elements = $ebook->twigroot->descendants(\&twigelt_is_knownuid);

=cut

sub twigelt_is_knownuid {
    my ($element) = @_;
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    croak($subname,"(): no element provided") unless($element);

    my $ref = ref($element) || '';

    croak($subname,"(): argument was of type '",$ref,
          "', needs to be 'XML::Twig::Elt' or a subclass")
        unless($element->isa('XML::Twig::Elt'));

    return if( (lc $element->gi) ne 'dc:identifier');
    my $id = $element->id;
    return unless($id);

    my %knownuids = (
        'package-id' => 56,
        'overdriveguid' => 48,
        'guid' => 40,
        'uuid' => 32,
        'uid'  => 24,
        'calibre_id' => 16,
        'fwid' => 8,
        );

    if($knownuids{lc $id})
    {
#        debug(2,"DEBUG: '",$element->gi,"' has known UID '",$id,"'");
        return 1;
    }
    return;
}


=head2 C<usedir($dir)>

Changes the current working directory to the one specified, creating
it if necessary.

Returns the current working directory before the change.  If no
directory is specified, returns the current working directory without
changing anything.

Croaks on any failure.

=cut

sub usedir {
    my ($dir) = @_;
    my $subname = ( caller(0) )[3];
    debug(2,"DEBUG[",$subname,"]: $dir");

    my $cwd = getcwd();
    return $cwd unless($dir);

    unless(-d $dir) {
        debug(2,"  Creating directory '",$dir,"'");
        mkpath($dir)
            or croak("Unable to create output directory '",$dir,"'!\n");
    }
    chdir($dir)
        or croak("Unable to change working directory to '",$dir,"'!\n");
    return $cwd;
}


=head2 C<userconfigdir()>

Returns the directory in which user configuration files and helper
programs are expected to be found, creating that directory if it does
not exist.  Typically, this directory is C<"$ENV{HOME}/.ebooktools">,
but on MSWin32 systems if that directory does not already exist,
C<"$ENV{USERPROFILE}/ApplicationData/EBook-Tools"> is returned (and
potentially created) instead.

If C<$ENV{HOME}> (and C<$ENV{USERPROFILE}> on MSWin32) are either not
set or do not point to a directory, the sub returns undef.

=cut

sub userconfigdir {
    my $subname = ( caller(0) )[3];
    debug(3,"DEBUG[",$subname,"]");

    my $dir;

    if(-d $ENV{HOME}) {
        $dir = $ENV{HOME} . '/.ebooktools';
    }

    if($OSNAME eq 'MSWin32') {
        if(not -d $dir and -d $ENV{USERPROFILE}) {
            $dir = $ENV{USERPROFILE} . '\Application Data\EBook-Tools';
        }
    }
    if($dir) {
        if(! -d $dir) {
            mkpath($dir)
                or croak($subname,
                         "(): unable to create configuration directory '",
                         $dir,"'!\n");
        }
        return $dir;
    }
    else { return; }
}


=head2 C<ymd_validate($year,$month,$day)>

Make sure month and day have valid values.  Return the passed values
if they are, return 3 undefs if not.  Testing of month or day can be
skipped by passing undef in that spot.

=cut

sub ymd_validate {
    my ($year,$month,$day) = @_;

    return (undef,undef,undef) unless($year);

    if($month) {
	return (undef,undef,undef) if($month > 12);
	if($day) {
	    if(!eval { timelocal(0,0,0,$day,$month-1,$year); }) {
		debug(1,"DEBUG: timelocal validation failed for year=",
                      $year," month=",$month," day=",$day);
		return (undef,undef,undef);
	    }
	}
	return ($year,$month,$day);
    }

    # We don't have a month.  If we *do* have a day, the result is
    # broken, so send back the undefs.
    return (undef,undef,undef) if($day);
    return ($year,undef,undef);
}


########## END CODE ##########

=head1 BUGS AND LIMITATIONS

=over

=item * fix_links() could be improved to download remote URIs instead
of ignoring them.

=item * fix_links() needs to check the <reference> links under <guide>

=item * fix_links() needs to be redone with HTML::TreeBuilder or
Mojo::DOM to avoid the weakness with newlines between attribute names
and values

=item * Need to implement fix_tours() that should collect the related
elements and delete the parent if none are found.  Empty <tours>
elements aren't allowed.

=item * fix_languages() needs to convert language names into IANA
language codes.

=item * set_language() should add a warning if the text isn't a valid
IANA language code.

=item * NCX generation only generates from the spine.  It should be
possible to use a TOC html file for generation instead.  In the long
term, it should be possible to generate one from the headers and
anchors in arbitrary HTML files.

=item * It might be better to use sysread / index / substr / syswrite in
&split_metadata to handle the split in 10k chunks, to avoid massive
memory usage on large files.

This may not be worth the effort, since the average size for most
books is less than 500k, and the largest books are rarely over 10M.

=item * The only generator is currently for .epub books.  PDF,
PalmDoc, Mobipocket, Plucker, and iSiloX are eventually planned.

=item * Although I like keeping warnings associated with the ebook
object, it may be better to throw exceptions on errors and catch them
later.  This probably won't be implemented until it bites someone who
complains, though.

=item * Unit tests are incomplete

=back

=head1 AUTHOR

Zed Pobre <zed@debian.org>

=head1 LICENSE AND COPYRIGHT

Copyright 2008-2013 Zed Pobre

Licensed to the public under the terms of the GNU GPL, version 2

=cut

1;