-
Notifications
You must be signed in to change notification settings - Fork 0
/
loaders.js
185 lines (156 loc) · 5.54 KB
/
loaders.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
const fs = require('fs').promises;
const { arrayOfLines } = require('./utils');
const {
LONGFORM, SHORTFORM
} = require('./mapping/shortforms');
const { ASCII_GLYPH, BRAILLE_MEANING } = require('./mapping/braille-ascii-columns.js');
const {
LOWERCASE_LETTER, STANDING_ALONE, WITH_DOTS_5, WITH_DOTS_45, WITH_DOTS_456, WITH_DOTS_46, WITH_DOTS_56
} = require('./mapping/alphabetic-contractions-columns.js');
const {
LOWER_ASCII, LOWER_ALONE, LOWER_START, LOWER_MIDDLE, LOWER_END
} = require('./mapping/lower-contractions-columns.js');
const {
BOC_ASCII_GLYPH, BOC_BRAILLE_MEANING
} = require('./mapping/braille-only-contractions-columns.js');
const brailleMapFile = `./mapping/braille-ascii.tsv`;
const alphabeticContractionsFile = `./mapping/alphabetic-contractions.tsv`;
const lowerContractionsFile = `./mapping/lower-contractions.tsv`;
const shortFormsFile = `./mapping/shortforms.tsv`;
const brailleOnlyContractionsFile = `./mapping/braille-only-contractions.tsv`;
function getGlyphEffect(meaning) {
if (typeof meaning !== 'string') {
console.error('Meaning', meaning);
throw new Error('Glyph meaning must be a string');
}
if (meaning.charAt(0) === '(') {
return null;
}
return meaning;
}
function removeBrackets(string) {
return string.replace(/[()]/g, '');
}
async function getMappings(asciiBrailleKeyValueTable, keyIndex, valueIndex) {
const mappingsArray = arrayOfLines(asciiBrailleKeyValueTable);
const mappings = {
chars: {},
funcs: {}
};
mappingsArray.forEach( row => {
const isBlankLine = row.replace(/\s/g, '') === '';
if (isBlankLine) {
return;
}
const cols = row.split(/\t/);
const asciiGlyph = cols[keyIndex];
const meaning = cols[valueIndex];
const effect = getGlyphEffect(meaning);
if (typeof effect === 'string') {
mappings.chars[asciiGlyph] = effect;
}
});
return mappings;
}
async function getBrailleMappings() {
const table = await fs.readFile(brailleMapFile, "utf8");
return getMappings(table, ASCII_GLYPH, BRAILLE_MEANING);
}
async function getBrailleOnlyContractions() {
const table = await fs.readFile(brailleOnlyContractionsFile, "utf8");
return getMappings(table, BOC_ASCII_GLYPH, BOC_BRAILLE_MEANING);
}
async function getAlphabeticContractions() {
const contractionsTable = await fs.readFile(alphabeticContractionsFile, "utf8");
const contractionsArray = arrayOfLines(contractionsTable);
const contractions = {
alone: {},
dots_5: {},
dots_45: {},
dots_456: {},
dots_46: {},
dots_56: {}
};
function valid(string = '') {
if (string === '…') return false;
return Boolean(string);
}
// TODO: break words with punctuation into two e.g. x'll
contractionsArray.forEach( row => {
const isBlankLine = row.replace(/\s/g, '') === '';
if (isBlankLine) {
return;
}
const cols = row.split(/\t/);
const letter = cols[LOWERCASE_LETTER];
if (valid(cols[STANDING_ALONE])) contractions.alone[letter] = cols[STANDING_ALONE];
if (valid(cols[WITH_DOTS_5])) contractions.dots_5[letter] = cols[WITH_DOTS_5];
if (valid(cols[WITH_DOTS_45])) contractions.dots_45[letter] = cols[WITH_DOTS_45];
if (valid(cols[WITH_DOTS_456])) contractions.dots_456[letter] = cols[WITH_DOTS_456];
if (valid(cols[WITH_DOTS_46])) contractions.dots_46[letter] = cols[WITH_DOTS_46];
if (valid(cols[WITH_DOTS_56])) contractions.dots_56[letter] = cols[WITH_DOTS_56];
});
// console.info(contractions);
return contractions;
}
async function getLowerContractions() {
const contractionsTable = await fs.readFile(lowerContractionsFile, "utf8");
const contractionsArray = arrayOfLines(contractionsTable);
const contractions = {
alone: {},
start: {},
middle: {},
end: {}
};
function valid(string = '') {
if (string === '…') return false;
return Boolean(string);
}
contractionsArray.forEach( row => {
const isBlankLine = row.replace(/\s/g, '') === '';
if (isBlankLine) {
return;
}
const cols = row.split(/\t/);
const letter = cols[LOWER_ASCII];
if (valid(cols[LOWER_ALONE])) contractions.alone[letter] = cols[LOWER_ALONE];
if (valid(cols[LOWER_START])) contractions.start[letter] = cols[LOWER_START];
if (valid(cols[LOWER_MIDDLE])) contractions.middle[letter] = cols[LOWER_MIDDLE];
if (valid(cols[LOWER_END])) contractions.end[letter] = cols[LOWER_END];
});
// console.info(contractions);
return contractions;
}
async function getShortForms() {
const shortFormsTable = await fs.readFile(shortFormsFile, "utf8");
const shortFormsArray = arrayOfLines(shortFormsTable);
const shortForms = {};
function longestToShortest(shortFormObjectA, shortFormObjectB) {
const colsA = shortFormObjectA.split(/\t/);
const colsB = shortFormObjectB.split(/\t/);
const shortFormA = removeBrackets(colsA[SHORTFORM]);
const shortFormB = removeBrackets(colsB[SHORTFORM]);
return shortFormA.length > shortFormB.length ? -1 : 1;
}
// order by length so "acr" is used in preference to "ac", for example
shortFormsArray.sort(longestToShortest);
shortFormsArray.map( row => {
const isBlankLine = row.replace(/\s/g, '') === '';
if (isBlankLine) {
return;
}
const cols = row.split(/\t/);
const shortForm = removeBrackets(cols[SHORTFORM]);
const longForm = cols[LONGFORM];
shortForms[shortForm] = longForm;
});
// console.info('shortForms', shortForms);
return shortForms;
}
module.exports = {
getBrailleMappings,
getAlphabeticContractions,
getBrailleOnlyContractions,
getLowerContractions,
getShortForms
};