-
Notifications
You must be signed in to change notification settings - Fork 1
/
makecorpora.py
executable file
·251 lines (224 loc) · 7.73 KB
/
makecorpora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
#!../bin/python
"""
HipparchiaBuilder: compile a database of Greek and Latin texts
Copyright: E Gunderson 2016-23
License: GNU GENERAL PUBLIC LICENSE 3
(see LICENSE in the top level directory of the distribution)
"""
import configparser
import multiprocessing
import time
from multiprocessing import freeze_support
from pathlib import Path
try:
import psutil
except ModuleNotFoundError:
psutil = None
from builder import corpusbuilder
from builder.configureatlaunch import getcommandlineargs, tobuildaccordingtoconfigfile
from builder.dbinteraction.versioning import timestampthebuild
from builder.lexica.buildlexica import analysisloader, formatgklexicon, formatlatlexicon, grammarloader, fixmorphologytranslations
from builder.postbuild.postbuildmetadata import noblankauthorcolumns, noblankworkdata
from builder.lexica.fixmorphologydefs import fixgreeklemmatacapitalization
from builder.wordcounting.databasewordcounts import monowordcounter
from builder.sql.loadarchivedtablesfromsql import archivedsqlloader
from builder.wordcounting.wordcountsbyheadword import headwordcounts
mpmethod = str()
try:
# macOS now wants 'spawn' by default, but 'fork' is a lot faster...
# mpmethod = 'forkserver'
# TypeError: can't pickle psycopg2.extensions.connection objects
mpmethod = 'fork'
multiprocessing.set_start_method(mpmethod)
except RuntimeError:
# File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/context.py", line 242, in set_start_method
# raise RuntimeError('context has already been set')
# RuntimeError: context has already been set
pass
except:
mpmethod = 'spawn'
multiprocessing.set_start_method(mpmethod)
finally:
print('multiprocessing method set to: {m}'.format(m=mpmethod))
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf8')
tobuild = tobuildaccordingtoconfigfile()
commandlineargs = getcommandlineargs()
additionalchecks = [commandlineargs.all, commandlineargs.allbutwordcounts, commandlineargs.allcorpora, commandlineargs.sqlloadwordcounts]
ovverride = {getattr(commandlineargs, k) for k in tobuild.keys() if getattr(commandlineargs, k)}.union({a for a in additionalchecks if a})
if ovverride:
if commandlineargs.all:
tobuild = {k: True for k in tobuild.keys()}
elif commandlineargs.allbutwordcounts:
tobuild = {k: True for k in tobuild.keys()}
tobuild['wordcounts'] = False
elif commandlineargs.allcorpora:
tobuild = {k: True for k in tobuild.keys()}
tobuild['wordcounts'] = False
tobuild['lex'] = False
tobuild['gram'] = False
elif commandlineargs.sqlloadwordcounts:
tobuild = {k: False for k in tobuild.keys()}
tobuild['wordcounts'] = True
else:
tobuild = {k: getattr(commandlineargs, k) for k in tobuild.keys()}
# print([a for a in tobuild if tobuild[a]])
start = time.time()
corpusvars = {
'latin':
{'dataprefix': 'LAT',
'datapath': config['io']['phi'],
'tmpprefix': None,
'corpusabbrev': 'lt',
'maxfilenumber': 9999, # canon at 9999
'minfilenumber': 0,
'exclusionlist': list(),
'languagevalue': 'L'
},
'greek':
{'dataprefix': 'TLG',
'datapath': config['io']['tlg'],
'tmpprefix': None,
'corpusabbrev': 'gr',
'maxfilenumber': 9999,
'minfilenumber': 0,
'exclusionlist': list(),
'languagevalue': 'G'
},
'inscriptions':
{'dataprefix': 'INS',
'datapath': config['io']['ins'],
'tmpprefix': 'xx',
'corpusabbrev': 'in',
'maxfilenumber': 8000, # 8000+ are bibliographies
'minfilenumber': 0,
'exclusionlist': list(),
'languagevalue': 'B'
},
'papyri':
{'dataprefix': 'DDP',
'datapath': config['io']['ddp'],
'tmpprefix': 'yy',
'corpusabbrev': 'dp',
'maxfilenumber': 5000, # maxval is 213; checklist at 9999
'minfilenumber': 0,
'exclusionlist': list(),
'languagevalue': 'B'
},
'christians':
{'dataprefix': 'CHR',
'datapath': config['io']['chr'],
'tmpprefix': 'zz',
'corpusabbrev': 'ch',
'maxfilenumber': 5000, # maxval is 140; bibliographies at 9900 and 9910
'minfilenumber': 0,
'exclusionlist': [21], # CHR0021 Judaica [Hebrew/Aramaic]; don't know how to read either language
'languagevalue': 'B'
}
}
#
# corpora
#
if __name__ == '__main__':
freeze_support()
corporatobuild = list()
if tobuild['latinauthors']:
corporatobuild.append('latin')
if tobuild['greekauthors']:
corporatobuild.append('greek')
if tobuild['inscriptions']:
corporatobuild.append('inscriptions')
if tobuild['papyri']:
corporatobuild.append('papyri')
if tobuild['christians']:
corporatobuild.append('christians')
for corpusname in corporatobuild:
corpusbuilder.buildcorpusdbs(corpusname, corpusvars)
corpusbuilder.remaptables(corpusname, corpusvars)
corpusbuilder.buildcorpusmetadata(corpusname, corpusvars)
print('regularizing author and work tables')
noblankauthorcolumns()
noblankworkdata()
#
# lexica, etc
#
if tobuild['lex']:
print('building lexical dbs')
formatgklexicon()
formatlatlexicon()
timestampthebuild('lx')
if tobuild['gram']:
sqlgrammar = False
try:
if config['corporatobuild']['loadprebuiltgrammarifpresent'] == 'y':
sqlgrammar = True
except KeyError:
print('"loadprebuiltgrammarifpresent" not set in config file; will try to load anyway')
sqlgrammar = True
try:
p = Path(config['lexica']['grammarsqldir'])
except KeyError:
print('"grammarsqldir" not set in config file; cannot load grammar via sql')
p = None
sqlgrammar = False
if not sqlgrammar:
print('building grammar dbs')
grammarloader('greek')
analysisloader('greek')
grammarloader('latin')
analysisloader('latin')
fixmorphologytranslations('greek')
fixgreeklemmatacapitalization()
fixmorphologytranslations('latin')
timestampthebuild('lm')
else:
print('loading grammar tables from sql dumps')
archivedsqlloader(p.resolve())
if tobuild['wordcounts']:
sqlcounts = False
try:
if config['corporatobuild']['loadwordcountsviasql'] == 'y':
sqlcounts = True
except KeyError:
# you have an old config.ini that does not include this option
print('please set "loadwordcountsviasql" to "y" or "n" under "corporatobuild" in "config.ini"')
sqlcounts = False
if commandlineargs.sqlloadwordcounts:
print('loading wordcounts from sql dumps')
p = Path(config['wordcounts']['wordcountdir'])
archivedsqlloader(p.resolve())
elif sqlcounts:
print('loading wordcounts from sql dumps')
p = Path(config['wordcounts']['wordcountdir'])
archivedsqlloader(p.resolve())
else:
print('building wordcounts by (repeatedly) examining every line of every text in all available dbs: this might take a minute or two...')
if psutil:
installedmem = psutil.virtual_memory().total / 1024 / 1024 / 1024
requiredmem = 12
if installedmem < requiredmem:
badnews = """
WARNING:
c. {r}G RAM is required to build the wordcounts.
You have {i}G of RAM installed.
The counts might fail.
If they do not fail, the count might be quite slow (because of "swapping")
[only the very first set of counts requires the 12G of RAM]
WARNING
"""
print(badnews.format(r=requiredmem, i=installedmem))
# this can be dangerous if the number of workers is high and the RAM available is not substantial; not the most likely configuration?
# mpwordcounter() is the hazardous one; if your survive it headwordcounts() will never get you near the same level of resource use
# mpwordcounter(): Build took 8.69 minutes
if 0 > 1:
# does not return counts properly: see notes
mpwordcounter()
else:
# see note on rediswordcounter(): do not use...
# rediswordcounter()
monowordcounter()
headwordcounts()
# if you do genres, brace yourself: Build took 84.11 minutes
stop = time.time()
took = round((stop-start)/60, 2)
print('\nBuild took', str(took), 'minutes')