Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

loc.py 4.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. import logging
  2. from invoke import task
  3. import support as s
  4. _log = logging.getLogger(__name__)
  5. @task(s.init)
  6. def init(c, force=False):
  7. if s.start('loc-mds-init', force=force, fail=False):
  8. _log.info('initializing LOC schema')
  9. s.psql(c, 'loc-mds-schema.sql')
  10. s.finish('loc-mds-init')
  11. else:
  12. _log.info('LOC schema initialized')
  13. @task(s.init)
  14. def init_id(c, force=False):
  15. if s.start('loc-id-init', force=force, fail=False):
  16. _log.info('initializing LOC schema')
  17. s.psql(c, 'loc-id-schema.sql')
  18. s.finish('loc-id-init')
  19. else:
  20. _log.info('LOC schema initialized')
  21. @task(s.build, s.init, init)
  22. def import_books(c, force=False):
  23. "Import the LOC MDS data"
  24. s.start('loc-mds-books', force=force)
  25. loc = s.data_dir / 'LOC'
  26. files = list(loc.glob('BooksAll.2014.part*.xml.gz'))
  27. _log.info('importing LOC data from %d files', len(files))
  28. s.pipeline([
  29. [s.bdtool, 'parse-marc', '--db-schema', 'locmds', '-t', 'book_marc_field', '--truncate'] + files
  30. ])
  31. s.finish('loc-mds-books')
  32. @task(s.build, s.init, init)
  33. def import_names(c, force=False):
  34. "Import the LOC MDS name data"
  35. s.start('loc-mds-names', force=force)
  36. loc = s.data_dir / 'LOC'
  37. names = loc / 'Names.2014.combined.xml.gz'
  38. _log.info('importing LOC data from %s', loc)
  39. s.pipeline([
  40. [s.bin_dir / 'parse-marc', '--db-schema', 'locmds', '-t', 'name_marc_field', '--truncate', names]
  41. ])
  42. s.finish('loc-mds-names')
  43. @task(s.init)
  44. def index_books(c, force=False):
  45. "Index LOC MDS books data"
  46. s.check_prereq('loc-mds-books')
  47. s.start('loc-mds-book-index', force=force)
  48. _log.info('building LOC indexes')
  49. s.psql(c, 'loc-mds-index-books.sql', staged=True)
  50. s.finish('loc-mds-book-index')
  51. @task(s.init)
  52. def index_names(c, force=False):
  53. "Index LOC MDS name data"
  54. s.check_prereq('loc-mds-names')
  55. s.start('loc-mds-name-index', force=force)
  56. _log.info('building LOC name indexes')
  57. s.psql(c, 'loc-mds-index-names.sql', True)
  58. s.finish('loc-mds-name-index')
  59. @task(s.build, s.init, init_id)
  60. def import_id_auth(c, force=False, convert_only=False, convert=True):
  61. s.start('loc-id-names', force=force)
  62. loc = s.data_dir / 'LOC'
  63. auth = loc / 'authoritiesnames.nt.both.zip'
  64. if convert:
  65. _log.info('converting authority ntriples to PSQL')
  66. s.pipeline([
  67. [s.bdtool, 'import-ntriples', '--db-schema', 'locid', '--prefix', 'auth', '--truncate', auth]
  68. ])
  69. s.finish('loc-id-names')
  70. @task(s.build, s.init, init_id)
  71. def import_id_work(c, force=False, convert_only=False, convert=True):
  72. s.start('loc-id-works', force=force)
  73. loc = s.data_dir / 'LOC'
  74. auth = loc / 'bibframeworks.nt.zip'
  75. if convert:
  76. _log.info('converting BIBFRAME ntriples to PSQL')
  77. s.pipeline([
  78. [s.bdtool, 'import-ntriples', '--db-schema', 'locid', '--prefix', 'work', '--truncate', auth]
  79. ])
  80. s.finish('loc-id-works')
  81. @task(s.build, s.init, init_id)
  82. def import_id_instance(c, force=False, convert_only=False, convert=True):
  83. s.start('loc-id-instances', force=force)
  84. loc = s.data_dir / 'LOC'
  85. auth = loc / 'bibframeinstances.nt.zip'
  86. if convert:
  87. _log.info('converting BIBFRAME ntriples to PSQL')
  88. s.pipeline([
  89. [s.bdtool, 'import-ntriples', '--db-schema', 'locid', '--prefix', 'instance', '--truncate', auth]
  90. ])
  91. s.finish('loc-id-instances')
  92. @task(s.init)
  93. def index_id_triples(c, force=False):
  94. "Create basic indexes on LOC ID data"
  95. s.check_prereq('loc-id-init')
  96. s.check_prereq('loc-id-names')
  97. s.check_prereq('loc-id-instances')
  98. s.check_prereq('loc-id-works')
  99. s.check_prereq('loc-id-instances')
  100. s.start('loc-id-triple-index', force=force)
  101. _log.info('building LOC name indexes')
  102. s.psql(c, 'loc-id-triple-index.sql', staged=True)
  103. s.finish('loc-id-triple-index')
  104. @task(s.init)
  105. def index_id_books(c, force=False):
  106. "Create book indexes on LOC ID data"
  107. s.check_prereq('loc-id-init')
  108. s.check_prereq('loc-id-works')
  109. s.check_prereq('loc-id-instances')
  110. s.check_prereq('loc-id-triple-index')
  111. s.start('loc-id-book-index', force=force)
  112. _log.info('building LOC name indexes')
  113. s.psql(c, 'loc-id-book-index.sql', staged=True)
  114. s.finish('loc-id-book-index')
  115. @task(s.init, s.build)
  116. def record_mds_files(c):
  117. files = list((s.data_dir / 'LOC').glob('BooksAll.2014.*.xml.gz'))
  118. files.append(s.data_dir / 'LOC' / 'Names.2014.combined.xml.gz')
  119. s.booktool(c, 'hash', *files)
  120. @task(s.init, s.build)
  121. def record_id_files(c):
  122. fns = [
  123. 'authoritiesnames.nt.both.zip',
  124. 'bibframeinstances.nt.zip',
  125. 'bibframeworks.nt.zip'
  126. ]
  127. files = [s.data_dir / 'LOC' / fn for fn in fns]
  128. s.booktool(c, 'hash', *files)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...