-
Notifications
You must be signed in to change notification settings - Fork 1
/
oracc_text_reader.js
87 lines (81 loc) · 2.66 KB
/
oracc_text_reader.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
export class ORACCReader {
constructor(jsonString) {
this.data = jsonString;
this.pnum = this.data['textid'];
}
getText(outputType) {
let lines = [];
let line = [];
for (let k in this.data) {
for (let x of this.walkLine(this.data[k], outputType)) {
if (x === "new line") {
if (line.length > 0) {
lines.push(line);
}
line = [];
} else {
line.push(x);
}
}
lines.push(line);
}
return lines;
}
parseLemma(lemma, outputType) {
if (lemma["inst"] === "u") {
return "x";
} else {
if (outputType === "cuneiform") {
let sign = '';
for (let k in lemma) {
for (let value of this.pullCuneiform(lemma[k])) {
sign += value;
}
}
return sign;
} else if (outputType === "sense") {
return lemma["f"]["cf"] + ": " + lemma["f"]["sense"];
} else {
if (outputType === 'form') {
return lemma['frag'];
} else if (outputType === 'norm') {
return lemma["f"]['norm'];
}
}
}
}
* pullCuneiform(lemma) {
if (Object.prototype.toString.call(lemma) === '[object Object]') {
if ("gdl_utf8" in lemma) {
yield lemma["gdl_utf8"];
} else {
for (let k in lemma) {
yield* this.pullCuneiform(lemma[k]);
}
}
} else if (Object.prototype.toString.call(lemma) === "[object Array]") {
for (let i = 0; i < lemma.length; i++) {
yield* this.pullCuneiform(lemma[i]);
}
}
}
* walkLine(node, outputType) {
if (Object.prototype.toString.call(node) === '[object Object]') {
if (node["node"] === "l") {
yield this.parseLemma(node, outputType);
} else if (node["node"] === "d") {
if (node["type"] === "line-start") {
yield "new line";
}
} else {
for (let k in node) {
yield* this.walkLine(node[k], outputType)
}
}
} else if (Object.prototype.toString.call(node) === "[object Array]") {
for (let i = 0; i < node.length; i++) {
yield* this.walkLine(node[i], outputType);
}
}
}
}