-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
368 lines (330 loc) · 12.9 KB
/
index.js
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
const { Collector, generateArcpId } = require("oni-ocfl");
const { languageProfileURI, Languages, Vocab } = require("language-data-commons-vocabs");
const XLSX = require('xlsx');
const { DataPack } = require('@ldac/data-packs');
const extraContext = {
"register": "http://w3id.org/meta-share/meta-share/register",
"TextType": "http://w3id.org/meta-share/meta-share/TextType",
"period": "http://purl.org/dc/terms/Period",
}
const classes = [
{
"@id": "#class_I",
"name": "Upper Class",
"description": " Nobility, university education, government service; Parliaments and Committees",
"@type": "DefinedTerm"
},
{
"@id": "#class_II",
"name": "Upper Middle Class",
"description": " educated citizens, gentlemen",
"@type": "DefinedTerm"
},
{
"@id": "#class_III",
"name": "Lower Middle Class",
"description": " free settlers with little education",
"@type": "DefinedTerm"
},
{
"@id": "#class_IIII",
"name": "Lower Class",
"description": " convicts, labourers, uneducated people, servants",
"@type": "DefinedTerm"
}
]
const periods = [
{ "@id": "#period_1", "name": "Period 1 (1788-1825)", "@type": "DefinedTerm", "start": "1788", "end": "1825" },
{ "@id": "#period_2", "name": "Period 2 (1826-1850)", "@type": "DefinedTerm", "start": "1826", "end": "1850" },
{ "@id": "#period_3", "name": "Period 3 (1851-1875)", "@type": "DefinedTerm", "start": "1851", "end": "1875" },
{ "@id": "#period_4", "name": "Period 4 (1876-1900)", "@type": "DefinedTerm", "start": "1876", "end": "1900" }
]
const registers = [
{ "@id": "#register_SB", "name": "Speech Based", "@type": "DefinedTerm" },
{ "@id": "#register_PrW", "name": "Private Written", "@type": "DefinedTerm" },
{ "@id": "#register_PcW", "name": "Public Written", "@type": "DefinedTerm" },
{ "@id": "#register_GE", "name": "Government English", "@type": "DefinedTerm" }
]
const textTypes = [
{ "@id": "#texttype_MI", "name": "Minutes", "@type": "DefinedTerm" },
{ "@id": "#texttype_PL", "name": "Play", "@type": "DefinedTerm" },
{ "@id": "#texttype_SP", "name": "Speeches", "@type": "DefinedTerm" },
{ "@id": "#texttype_DI", "name": "Diaries", "@type": "DefinedTerm" },
{ "@id": "#texttype_PC", "name": "Private Correspondence", "@type": "DefinedTerm" },
{ "@id": "#texttype_MM", "name": "Memoirs", "@type": "DefinedTerm" },
{ "@id": "#texttype_NB", "name": "Newspapers & Broadsides", "@type": "DefinedTerm" },
{ "@id": "#texttype_NV", "name": "Narratives", "@type": "DefinedTerm" },
{ "@id": "#texttype_OC", "name": "Official Correspondence", "@type": "DefinedTerm" },
{ "@id": "#texttype_RP", "name": "Reports", "@type": "DefinedTerm" },
{ "@id": "#texttype_VE", "name": "Verse", "@type": "DefinedTerm" },
{ "@id": "#texttype_IC", "name": "Imperial Correspondence", "@type": "DefinedTerm" },
{ "@id": "#texttype_LG", "name": "Legal English", "@type": "DefinedTerm" },
{ "@id": "#texttype_PP", "name": "Petitions & Proclamations", "@type": "DefinedTerm" }
]
const places = [
{ "@id": "#place_A", "name": "Australia", "@type": "DefinedTerm" },
{ "@id": "#place_A-NSW", "name": "New South Wales", "@type": "DefinedTerm" },
{ "@id": "#place_A-QLD", "name": "Queensland", "@type": "DefinedTerm" },
{ "@id": "#place_A-NT", "name": "Northern Territory", "@type": "DefinedTerm" },
{ "@id": "#place_A-SA", "name": "South Australia", "@type": "DefinedTerm" },
{ "@id": "#place_A-VDL", "name": "Van Diemen's Land", "@type": "DefinedTerm" },
{ "@id": "#place_A-VIC", "name": "Victoria", "@type": "DefinedTerm" },
{ "@id": "#place_A-WA", "name": "Western Australia", "@type": "DefinedTerm" },
{ "@id": "#place_CAN", "name": "Canada", "@type": "DefinedTerm" },
{ "@id": "#place_GB", "name": "Great Britain", "@type": "DefinedTerm" },
{ "@id": "#place_GB-E", "name": "England", "@type": "DefinedTerm" },
{ "@id": "#place_GB-SC", "name": "Scotland", "@type": "DefinedTerm" },
{ "@id": "#place_GB-W", "name": "Wales", "@type": "DefinedTerm" },
{ "@id": "#place_India", "name": "India", "@type": "DefinedTerm" },
{ "@id": "#place_NI", "name": "Northern Ireland", "@type": "DefinedTerm" },
{ "@id": "#place_NZ", "name": "New Zealand", "@type": "DefinedTerm" },
{ "@id": "#place_SA", "name": "South Africa", "@type": "DefinedTerm" },
{ "@id": "#place_SI", "name": "Southern Ireland", "@type": "DefinedTerm" },
{ "@id": "#place_USA", "name": "USA", "@type": "DefinedTerm" }
]
const lingGenreMap = {
MI: "Informational",
PL: "Drama",
SP: "Oratory",
DI: "Narrative",
PC: "Informational",
MM: "Narrative",
NB: "Informational",
NV: "Narrative",
OC: "Informational",
RP: "Report",
VE: "Forulaic",
IC: "Informational",
LG: "Informational",
PP: "Informational"
}
async function main() {
const vocab = new Vocab;
await vocab.load();
let datapack = new DataPack({ dataPacks: ['Glottolog'], indexFields: ['name'] });
await datapack.load();
let engLang = datapack.get({
field: "name",
value: "English",
});
const coll = new Collector(); // Get all the paths etc from commandline
await coll.connect();
// Make a base corpus using template
const corpus = coll.newObject(coll.templateCrateDir);
const corpusCrate = corpus.crate;
// TODO need some tools for all this
corpusCrate.addContext(vocab.getContext());
corpusCrate.addContext(extraContext);
dataDir = corpusCrate.getItem("data/");
corpusCrate.addProfile(languageProfileURI("Collection"));
const corpusRoot = corpus.rootDataset;
corpusRoot["@type"] = ["Dataset", "RepositoryCollection"];
corpus.mintArcpId();
for (let register of registers) {
corpusCrate.addEntity(register);
}
for (let texttype of textTypes) {
corpusCrate.addEntity(texttype);
}
for (let place of places) {
corpusCrate.addEntity(place);
}
for (let cl of classes) {
corpusCrate.addEntity(cl);
}
for (let period of periods) {
corpusCrate.addEntity(period);
}
var workbook = await XLSX.readFile(coll.excelPath, { cellDates: true });
var bibsheet = workbook.Sheets[workbook.SheetNames[1]];
const bibData = XLSX.utils.sheet_to_json(bibsheet, { raw: false });
corpusCrate.inLanguage = engLang;
corpusCrate.subjectLanguage = engLang;
// Decode publications
const citedNames = {};
for (const pub of bibData) {
//console.log(pub)
if (pub.Author) {
const authorName = pub.Author.replace(/,.*/, "").replace(/ /g, "_");
const work = {
"@type": "CreativeWork",
author: pub.Author,
datePublished: pub.Date,
name: pub.Title,
publisher: pub.Source,
wordCount: pub["Words CEEA"],
"@id": generateArcpId(coll.namespace, "work", `${authorName}${pub.Date}`)
}
work.inLanguage = engLang;
work.subjectLanguage = engLang;
corpusCrate.addEntity(work);
citedNames[authorName] = work;
// console.log(work["@id"], corpusCrate.getItem(work["@id"]))
}
}
//console.log(citedNames);
var worksheet = workbook.Sheets[workbook.SheetNames[0]];
const data = XLSX.utils.sheet_to_json(worksheet, { raw: false, range: 1 });
//console.log(data)
for (let input of data) {
//console.log(input)
//const interviewDate = new Date(input["Date of interview"]).toISOString().replace(/T.*/, "");
/*
{
Nr: '1-001',
Name: 'Phillip, Arthur',
Birth: '1738',
Gender: 'm',
Origin: 'GB',
Age: '50',
Status: 'I',
Arrival: '1788',
Abode: '0',
'Year Writing': '1788',
'Place Writing': 'A-NSW',
Register: 'PrW',
TextT: 'PC',
'# of words': '951',
Gender_1: 'm',
Status_1: 'II',
Place: 'GB-E',
Source: 'Niall, 1998',
Pages: '10-11'
}
*/
const date = input["Year Writing"];
const id = generateArcpId(coll.namespace, "item", input["Nr"]);
const authorID = `${input.Name.replace(/[, ]+/, "_")}`;
const author = {
"@id": generateArcpId(coll.namespace, "author", authorID),
"@type": "Person",
"name": input.Name,
"birthDate": input.Birth,
"birthPlace": { "@id": `#place_${input.Origin}` },
"gender": input["Gender_1"],
"immigration": input.Arrival
}
const authorProxy = JSON.parse(JSON.stringify(author));
authorProxy["@type"] = ["Person"];
authorProxy["@id"] = `${authorProxy["@id"]}-${input.Nr}-status`;
authorProxy.name = `${input.Name} - status ${date} text #${input.Nr}`;
authorProxy["age"] = input.Age;
authorProxy.class = { "@id": `#class_${input["Status"]}` };
authorProxy["prov:specializationOf"] = author["@id"];
// TODO - Addressees
// TODO - sort out citations for federation debates
var citedId = generateArcpId(coll.namespace, "work", input.Source.replace(", ", "").replace(/ /g, "_"))
var cited = corpusCrate.getItem(citedId)
if (!cited) {
//Not an exact match - lets try jsut by name
const authorName = input.Source.replace(/,.*/, "").replace(/ /g, "_").replace(/\d+/, "");
cited = citedNames[authorName];
if (!cited) {
console.log("CANNOT FIND REFERENCE", authorName);
}
}
const citationStubId = `${citedId}p${input.Pages}`;
//console.log(input.Source)
const citationStub = {
"@type": "CreativeWork",
"materialType": vocab.getVocabItem("PrimaryMaterial"),
"isPartOf": { "@id": citedId },
"name": input.Source,
"@id": citationStubId,
"wordCount": input["# of words"]
};
const recipient = {
"@id": `${id.replace("item", "recipient")}`,
"@type": ["Person"],
"name": `${input.Nr} Recipient`,
"gender": input.AdresseeGender,
"class": { "@id": `#class_${input.AdresseeStatus}` },
"place": { "@id": `#place_${input.AdresseePlace}` }
}
const item = {
"@id": id,
"@type": ["RepositoryObject"],
"conformsTo": { "@id": languageProfileURI("Object") },
"name": `Text ${input.Nr} ${date} ${author.name}`,
"author": authorProxy,
"description": `Text ${input.Nr} ${date} ${author.name}`,
"dateCreated": date,
"register": { "@id": `#register_${input.Register}` },
"TextType": { "@id": `#texttype_${input.TextT}` },
"period": { "@id": `#period_${input.Nr.replace(/^(\d).+/, "$1")}` },
"linguisticGenre": vocab.getVocabItem(lingGenreMap[input.TextT]),
"citation": citationStub
};
if (recipient.gender !== "x") {
console
item.recipient = recipient;
}
item.datePublished = input.Source.match(/.+(\d{4})/) ? input.Source.replace(/.+(\d{4})/, "$1") : date;
if (item.register["@id"] === "#register_SB") {
item.communicationMode = vocab.getVocabItem("SpokenLanguage")
} else {
item.communicationMode = vocab.getVocabItem("WrittenLanguage")
}
if (input.Pages !== "x") {
const pages = input.Pages.split("-");
const start = pages[0];
citationStub.pageStart = start;
const end = pages[1];
if (pages[1]) {
if (end.length < start.length) {
citationStub.pageEnd = start.slice(0, start.length - end.length) + end;
} else {
citationStub.pageEnd = end;
}
}
citationStub.name += ` p${input.Pages}`;
}
const file = {
"name": `${item.name} - text with metadata codes`,
"@id": `data/${input.Nr}.txt`,
"@type": ["File"],
"materialType": vocab.getVocabItem("DerivedMaterial"),
"communicationMode": vocab.getVocabItem("WrittenLanguage"),
"annotationOf": citationStub,
"inLanguage": engLang,
"encodingFormat": "text/plain"
}
const plain = {
"name": `${item.name} - text`,
"@id": `data/${input.Nr}-plain.txt`,
"@type": ["File"],
"materialType": vocab.getVocabItem("DerivedMaterial"),
"annotationOf": citationStub,
"communicationMode": vocab.getVocabItem("WrittenLanguage"),
"inLanguage": engLang,
"encodingFormat": "text/plain"
}
/*if (item.register["@id"] === "#register_SB") {
file.communicationMode = vocab.getVocabItem("SpokenLanguage");
plain.communicationMode = vocab.getVocabItem("SpokenLanguage");
} else {
file.communicationMode = vocab.getVocabItem("WrittenLanguage")
plain.communicationMode = vocab.getVocabItem("WrittenLanguage")
}*/
item.inLanguage = engLang;
item.indexableText = plain;
item.hasPart = [plain, file];
corpusCrate.pushValue(corpusRoot, "hasPart", file);
corpusCrate.pushValue(corpusRoot, "hasPart", plain);
//corpusCrate.addItem(author);
//corpusCrate.addItem(authorProxy);
//corpusRoot.hasMember.push({"@id": item["@id"]});
corpusCrate.pushValue(corpusRoot, 'hasMember', item)
}
corpusRoot.hasMember.sort((a, b) => (
a["@id"].localeCompare(b["@id"]))
)
for (let item of corpusCrate.getGraph()) {
/// TODO - change to a new getItemsOfType() when available
if (corpusCrate.utils.asArray(item["@type"]).includes("File")) {
await corpus.addFile(item, coll.templateCrateDir, null, false);
}
}
await corpus.addToRepo();
}
main();