-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoTEI.js
254 lines (220 loc) · 8.25 KB
/
toTEI.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
const fs = require('fs')
const xmldom = require('xmldom')
const csvparse = require('csv-parse')
var request = require('request')
const uuidv4 = require('uuid/v4')
const exist = "http://localhost:8081/exist"
function getTEIDoc(block) {
return new xmldom.DOMParser().parseFromString(`<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>Mishnah - Tosefta Alignment - ${block}</title>
</titleStmt>
<publicationStmt>
<p></p>
</publicationStmt>
<sourceDesc>
<p>Generated</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text><body><ab></ab></body></text></TEI>`, 'text/xml')
}
function writeTEIFile(teiDoc, block) {
// Write TEI
const teiString = new xmldom.XMLSerializer().serializeToString(teiDoc)
// TODO create output folder if it doesn't exist
fs.writeFileSync(`output/mtalignment-${block}.xml`, teiString)
console.log("The TEI file was created.");
}
function generateTEI(refData, refTData, alignData) {
let teiDoc
let linkGrp
let curBlock = ''
let curBook = 0
let curChapter = 0
let curSection = 0
// Using intermediate pointers to link ranges of words
// See http://www.tei-c.org/release/doc/tei-p5-doc/en/html/SA.html#SAPTIP
for (const unit of alignData) {
// Skip header
if (unit[0] === '') continue
// Determine if this is a new section
const groups = unit[1].match(/ref\.((\d+)\.(\d+)\.(\d+))\./)
const newBlock = groups[1]
const newBook = parseInt(groups[2])
const newChapter = parseInt(groups[3])
const newSection = parseInt(groups[4])
// console.log(curBook, newBook, '.', curChapter, newChapter, '.', curSection, newSection)
if ( curBook < newBook ||
(curBook === newBook && curChapter < newChapter) ||
(curBook === newBook && curChapter === newChapter && curSection < newSection) ) {
// If this is not the very first block, write out TEI
if (curBook > 0) {
writeTEIFile(teiDoc, curBlock)
}
// Get new TEI doc
teiDoc = getTEIDoc(newBlock)
// Create tei:linkGrp for this alignment block
const containerAb = teiDoc.getElementsByTagName('ab')[0]
linkGrp = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'linkGrp')
linkGrp.setAttribute('xml:id', `mt.${newBlock}`)
containerAb.appendChild(linkGrp)
// Update current data
curBlock = newBlock
curBook = newBook
curChapter = newChapter
curSection = newSection
}
let baseRangeId = ''
let compRangeId = ''
for (const i in unit) {
const index = parseInt(i)
const field = unit[i]
const isBase = index === 1
const isComp = index === 3
if (!isBase && !isComp) continue
const startId = field
const endId = unit[index + 1]
// go find in XML
const lookupData = isBase ? refData : refTData
const rangeIds = []
let inRange = false
for (const w of Array.from(lookupData.getElementsByTagName('w'))) {
const id = w.getAttribute('xml:id')
if (id === startId) {
rangeIds.push(id)
inRange = true
} else if (id === endId) {
rangeIds.push(id)
inRange = false
} else if (inRange) {
rangeIds.push(id)
}
}
const uid = uuidv4().slice(0, 6)
if (isBase) baseRangeId = startId + '-' + endId + '-' + uid
if (isComp) compRangeId = startId + '-' + endId + '-' + uid
// Create intermediary pointer
const ptr = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'ptr')
ptr.setAttribute('xml:id', startId + '-' + endId + '-' + uid)
ptr.setAttribute('target', '#' + rangeIds.join(' #'))
linkGrp.appendChild(ptr)
console.log('Created pointer ' + startId + '-' + endId + '-' + uid)
}
if (baseRangeId && compRangeId) {
// Create link between intermediary pointers
const link = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'link')
link.setAttribute('target', '#' + baseRangeId + ' ' + '#' + compRangeId)
linkGrp.appendChild(link)
}
}
// for (const unit of alignData.split('----------------')) {
// const lines = unit.split(/[\n\r]/)
// if (lines.length < 2) continue
// // Determine if this is a new section
// const groups = unit.match(/ref\.((\d+)\.(\d+)\.(\d+))\./)
// const newBlock = groups[1]
// const newBook = parseInt(groups[2])
// const newChapter = parseInt(groups[3])
// const newSection = parseInt(groups[4])
// console.log(curBook, newBook, '.', curChapter, newChapter, '.', curSection, newSection)
// if ( curBook < newBook ||
// (curBook === newBook && curChapter < newChapter) ||
// (curBook === newBook && curChapter === newChapter && curSection < newSection) ) {
// // If this is not the very first block, write out TEI
// if (curBook > 0) {
// writeTEIFile(teiDoc, curBlock)
// }
// // Get new TEI doc
// teiDoc = getTEIDoc(newBlock)
// // Create tei:linkGrp for this alignment block
// const containerAb = teiDoc.getElementsByTagName('ab')[0]
// linkGrp = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'linkGrp')
// linkGrp.setAttribute('xml:id', `mt.${newBlock}`)
// containerAb.appendChild(linkGrp)
// // Update current data
// curBlock = newBlock
// curBook = newBook
// curChapter = newChapter
// curSection = newSection
// }
// let baseRangeId = ''
// let compRangeId = ''
// for (const line of lines) {
// const isBase = !(line.match(/Base:/) === null)
// const isComp = !(line.match(/Comp:/) === null)
// if (isBase || isComp) {
// const match = line.match(/\[(ref(-t)?.*?)-(ref(-t)?.*?)\]/)
// const startId = match[1]
// const endId = match[3]
// // go find in XML
// const lookupData = isBase ? refData : refTData
// const rangeIds = []
// let inRange = false
// for (const w of Array.from(lookupData.getElementsByTagName('w'))) {
// const id = w.getAttribute('xml:id')
// if (id === startId) {
// rangeIds.push(id)
// inRange = true
// } else if (id === endId) {
// rangeIds.push(id)
// inRange = false
// } else if (inRange) {
// rangeIds.push(id)
// }
// }
// if (isBase) baseRangeId = startId + '-' + endId
// if (isComp) compRangeId = startId + '-' + endId
// // Create intermediary pointer
// const ptr = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'ptr')
// ptr.setAttribute('xml:id', startId + '-' + endId)
// ptr.setAttribute('target', '#' + rangeIds.join(' #'))
// linkGrp.appendChild(ptr)
// console.log('Created pointer ' + startId + '-' + endId)
// }
// }
// if (baseRangeId && compRangeId) {
// // Create link between intermediary pointers
// const link = teiDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'link')
// link.setAttribute('target', '#' + baseRangeId + ' ' + '#' + compRangeId)
// linkGrp.appendChild(link)
// }
// }
}
const ref = new Promise((res, rej) => {
request.get(`${exist}/rest/db/digitalmishnah-tei/mishnah/ref.xml`, function (error, response, body) {
if (!error && response.statusCode == 200) {
res(new xmldom.DOMParser().parseFromString(body, 'text/xml'))
} else {
console.log(error)
rej()
}
})
})
const reft = new Promise((res, rej) => {
request.get(`${exist}/rest/db/digitalmishnah-tei/tosefta/ref-t.xml`, function (error, response, body) {
if (!error && response.statusCode == 200) {
res(new xmldom.DOMParser().parseFromString(body, 'text/xml'))
} else {
console.log(error)
rej()
}
})
})
const align = new Promise((res, rej) => {
fs.readFile('data.csv', 'utf8', function(err, contents) {
if (err) {
console.log(err)
rej()
} else {
csvparse(contents, {}, function(err, output) {
res(output)
})
}
})
})
Promise.all([ref, reft, align]).then((res) => {
generateTEI(...res)
})