forked from Bookworm-project/BookwormDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_metadata.awk
287 lines (284 loc) · 7.27 KB
/
parse_metadata.awk
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
## Script for parsing arxiv metadata. Call with a catalog file
## that lists the bookids and file location (with _ for /) of the
## abstracts:
## awk -f parse_metadata.awk texts/catalog.txt
##
## The code in the BEGIN statement sets up hashes for convenience. "a"
## is used to make the date into proper datetime SQL format. "b" is to
## translate the raw categories into ones that we will use in our database
BEGIN{
a["Jan"] = "01";
a["Feb"] = "02";
a["Mar"] = "03";
a["Apr"] = "04";
a["May"] = "05";
a["Jun"] = "06";
a["Jul"] = "07";
a["Aug"] = "08";
a["Sep"] = "09";
a["Oct"] = "10";
a["Nov"] = "11";
a["Dec"] = "12";
OFS = "\t";
where=0;
b["acc-phys"]="physics"
b["adap-org"]="nlin"
b["alg-geom"]="math"
b["ao-sci"]="physics"
b["astro-ph"]="physics.astro-ph"
b["astro-ph.CO"]="physics.astro-ph"
b["astro-ph.EP"]="physics.astro-ph"
b["astro-ph.GA"]="physics.astro-ph"
b["astro-ph.HE"]="physics.astro-ph"
b["astro-ph.IM"]="physics.astro-ph"
b["astro-ph.SR"]="physics.astro-ph"
b["atom-ph"]="physics"
b["bayes-an"]="physics"
b["chao-dyn"]="nlin"
b["chem-ph"]="physics"
b["cmp-lg"]="cs"
b["comp-gas"]="nlin"
b["cond-mat"]="physics.cond-mat"
b["cond-mat.dis-nn"]="physics.cond-mat"
b["cond-mat.mes-hall"]="physics.cond-mat"
b["cond-mat.mtrl-sci"]="physics.cond-mat"
b["cond-mat.other"]="physics.cond-mat"
b["cond-mat.quant-gas"]="physics.cond-mat"
b["cond-mat.soft"]="physics.cond-mat"
b["cond-mat.stat-mech"]="physics.cond-mat"
b["cond-mat.str-el"]="physics.cond-mat"
b["cond-mat.supr-con"]="physics.cond-mat"
b["cs.AI"]="cs"
b["cs.AR"]="cs"
b["cs.CC"]="cs"
b["cs.CE"]="cs"
b["cs.CG"]="cs"
b["cs.CL"]="cs"
b["cs.CR"]="cs"
b["cs.CV"]="cs"
b["cs.CY"]="cs"
b["cs.DB"]="cs"
b["cs.DC"]="cs"
b["cs.DL"]="cs"
b["cs.DM"]="cs"
b["cs.DS"]="cs"
b["cs.ET"]="cs"
b["cs.FL"]="cs"
b["cs.GL"]="cs"
b["cs.GR"]="cs"
b["cs.GT"]="cs"
b["cs.HC"]="cs"
b["cs.IR"]="cs"
b["cs.IT"]="cs"
b["cs.LG"]="cs"
b["cs.LO"]="cs"
b["cs.MA"]="cs"
b["cs.MM"]="cs"
b["cs.MS"]="cs"
b["cs.NA"]="cs"
b["cs.NE"]="cs"
b["cs.NI"]="cs"
b["cs.OH"]="cs"
b["cs.OS"]="cs"
b["cs.PF"]="cs"
b["cs.PL"]="cs"
b["cs.RO"]="cs"
b["cs.SC"]="cs"
b["cs.SD"]="cs"
b["cs.SE"]="cs"
b["cs.SI"]="cs"
b["cs.SY"]="cs"
b["dg-ga"]="math"
b["funct-an"]="math"
b["gr-qc"]="physics.gr-qc"
b["hep-ex"]="physics.hep-ex"
b["hep-lat"]="physics.hep-lat"
b["hep-ph"]="physics.hep-ph"
b["hep-th"]="physics.hep-th"
b["math.AC"]="math"
b["math.AG"]="math"
b["math.AP"]="math"
b["math.AT"]="math"
b["math.CA"]="math"
b["math.CO"]="math"
b["math.CT"]="math"
b["math.CV"]="math"
b["math.DG"]="math"
b["math.DS"]="math"
b["math.FA"]="math"
b["math.GM"]="math"
b["math.GN"]="math"
b["math.GR"]="math"
b["math.GT"]="math"
b["math.HO"]="math"
b["math.IT"]="math"
b["math.KT"]="math"
b["math.LO"]="math"
b["math.MG"]="math"
b["math.MP"]="math"
b["math.NA"]="math"
b["math.NT"]="math"
b["math.OA"]="math"
b["math.OC"]="math"
b["math-ph"]="math.math-ph"
b["math.PR"]="math"
b["math.QA"]="math"
b["math.RA"]="math"
b["math.RT"]="math"
b["math.SG"]="math"
b["math.SP"]="math"
b["math.ST"]="math"
b["mtrl-th"]="physics.cond-mat"
b["nlin.AO"]="nlin"
b["nlin.CD"]="nlin"
b["nlin.CG"]="nlin"
b["nlin.PS"]="nlin"
b["nlin.SI"]="nlin"
b["nucl-ex"]="physics.nucl-ex"
b["nucl-th"]="physics.nucl-th"
b["patt-sol"]="nlin"
b["physics.acc-ph"]="physics"
b["physics.ao-ph"]="physics"
b["physics.atm-clus"]="physics"
b["physics.atom-ph"]="physics"
b["physics.bio-ph"]="physics"
b["physics.chem-ph"]="physics"
b["physics.class-ph"]="physics"
b["physics.comp-ph"]="physics"
b["physics.data-an"]="physics"
b["physics.ed-ph"]="physics"
b["physics.flu-dyn"]="physics"
b["physics.gen-ph"]="physics"
b["physics.geo-ph"]="physics"
b["physics.hist-ph"]="physics"
b["physics.ins-det"]="physics"
b["physics.med-ph"]="physics"
b["physics.optics"]="physics"
b["physics.plasm-ph"]="physics"
b["physics.pop-ph"]="physics"
b["physics.soc-ph"]="physics"
b["physics.space-ph"]="physics"
b["plasm-ph"]="physics"
b["q-alg"]="math"
b["q-bio.BM"]="q-bio"
b["q-bio.CB"]="q-bio"
b["q-bio.GN"]="q-bio"
b["q-bio.MN"]="q-bio"
b["q-bio.NC"]="q-bio"
b["q-bio.OT"]="q-bio"
b["q-bio.PE"]="q-bio"
b["q-bio.QM"]="q-bio"
b["q-bio"]="q-bio"
b["q-bio.SC"]="q-bio"
b["q-bio.TO"]="q-bio"
b["q-fin.CP"]="q-fin"
b["q-fin.GN"]="q-fin"
b["q-fin.PM"]="q-fin"
b["q-fin.PR"]="q-fin"
b["q-fin.RM"]="q-fin"
b["q-fin.ST"]="q-fin"
b["q-fin.TR"]="q-fin"
b["quant-ph"]="physics.quant-ph"
b["solv-int"]="nlin"
b["stat.AP"]="stat"
b["stat.CO"]="stat"
b["stat.ME"]="stat"
b["stat.ML"]="stat"
b["stat.OT"]="stat"
b["stat.TH"]="stat"
b["supr-con"]="physics.cond-mat"
}
{
# x is the book id
x = $1;
# fn is the filename
fn = $2;
gsub(/_/,"/", fn);
fn = "abs/"fn".abs"
# where is used to deal with multiline author and title fields
# t is title, au is author, c is the categories, fields is the line
# we read in
where = 0;
t = "";
au = "";
delete c;
delete fields;
while ((getline line < fn) > 0) {
len=split(line, fields);
# arxiv id
if (line ~ /^arXiv:/) {
id=substr(line,7);
}
# email address
if (line ~ /^From:/) {
from=substr(line,7);
# take only the email address, not user name
st=index(from, "<");
ed=index(from, ">");
from2=substr(from,st+1,ed-st-1);
# take only the domain from the email address
at=index(from2, "@");
from=substr(from2, at+1);
num=split(from, s, "\\.");
final=s[num];
# write out the domain levels separated by tabs.
# the first level is "edu", then "harvard.edu", then "seas.harvard.edu"
for (i=num-1; i>=1; i--) {
dl=sprintf("%s", s[i]);
for (j=i+1; j<=num; j++) {
dl=sprintf("%s.%s", dl,s[j]);
}
final=sprintf("%s\t%s",final,dl);
}
}
# date, convert to datetime format
if (line ~ /^Date:/) {
ds=sprintf("%s-%s-%02d %s",fields[5],a[fields[4]],fields[3],fields[6])
}
# where is for dealing with multiline title
if (line ~ /^Title:/) {
t=substr(line, index(line,fields[2]));
where=1;
}
# where is for dealing with multiline author
if (line ~ /^Authors:/) {
au=substr(line, index(line,fields[2]));
where=2;
}
# if line starts with an indentation, choose appropriate field to add to
if (line ~ /^ /) {
if (where==1)
t=t""line;
else if (where==2)
au=au""line;
}
# reset where if we get to end of metadata
if (line ~ /^\\/) {
where=0;
}
# reset where if we get to Categories, which is always after title/author
if (line ~ /^Categories:/) {
where = 0;
# loop over all categories
for (i=2; i<=len; i++) {
# check if we already saw it; want to preserve original order
if (!(fields[i] in c)) {
# check if it's a funky one
if (fields[i] in b) {
# b-mapped category not seen yet, add
if (!(b[fields[i]] in c)) {
c[b[fields[i]]]=i;
print x,id,ds,t,au,b[fields[i]], final;
}
}
else {
# regular category not seen yet, add
c[fields[i]] = i;
print x,id,ds,t,au,fields[i], final;
}
}
}
}
}
close (fn);
}