Skip to content

Commit 99643d2

Browse files
committed
implementation for issue #11
changed findART to splitART added splitDefinitive added handler from verb token to next token sequence
1 parent d9d79d4 commit 99643d2

File tree

2 files changed

+117
-28
lines changed

2 files changed

+117
-28
lines changed

functions/handlers.js

+80-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
const { dbGetter } = require("./getter.js");
22

3+
async function findART(obj, langRef){
4+
const addART = []
5+
let i = 0, j = 0;
6+
while(i<obj.words.length){
7+
if(obj.types[i] === 'NOUN' || obj.types[i] === 'SUBJ'){
8+
const articlesGenders = await dbGetter.getPersistent(langRef, 'ARTICLES/GENDERS', {});
9+
addART.push([i, articlesGenders[genders[j]]])
10+
j++;
11+
}
12+
i++;
13+
}
14+
addART.forEach((v, i) => {
15+
obj.words.splice(v[0] + i, 0, v[1])
16+
obj.types.splice(v[0] + i, 0, 'ART')
17+
})
18+
}
19+
320
async function handleType(obj, sentence, langRef, src, forces){
21+
if(obj.position > 0){
22+
if(sentence[obj.position-1].props.next){
23+
obj.props.prev = sentence[obj.position-1].props.next;
24+
}
25+
}
426
switch (obj.type){
527
case 'VERB':
628
obj = await handleVERB(obj, sentence, langRef, src, forces);
@@ -60,15 +82,16 @@ async function handleVERB(obj, sentence, langRef, src, forces){
6082
}
6183

6284
async function conjugateVERB(obj, langRef, src){
63-
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {})
85+
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {});
6486
const newWordsInf = obj.words.map(word => getVERBInfinitive(word, langRef));
6587
const newWordsSeq = newWordsInf.map(p => new Promise((resolve) => {
6688
p.then(inf => dbGetter.getOnce(langRef, `VERBS/SEQUENCES/${inf}`))
6789
.then(seq => resolve(seq))
68-
}))
90+
}));
6991
const newWords = obj.words.map((w, i) => new Promise((resolve) => {
7092
Promise.all([newWordsInf[i], newWordsSeq[i-1]])
7193
.then(([inf, seq]) => {
94+
seq = i == 0 ? obj.props.prev?.VERB : seq?.VERB
7295
if(seq){
7396
if (seq === 'INF') return inf.split(' ')
7497
return Promise.all(inf.split(' ').map(winf => getVERBConjugations(winf, src, langRef, seq)))
@@ -79,8 +102,10 @@ async function conjugateVERB(obj, langRef, src){
79102
return Promise.all(inf.split(' ').map(winf => getVERBConjugations(winf, src, langRef, obj.meta.TIME, obj.meta.PERSON)))
80103
}
81104
}).then(conj => resolve(conj.join(' ')))
82-
}))
83-
obj.words = await Promise.all(newWords)
105+
}));
106+
obj.words = await Promise.all(newWords);
107+
obj.infs = await Promise.all(newWordsInf);
108+
obj.props.next = await newWordsSeq.at(-1);
84109
if (obj.words.at(-1).includes(':CON:')){
85110
obj.words.splice(-1, 1, ...[defaults.CON, obj.words.at(-1).replace(' :CON:', '')])
86111
obj.types.splice(-1, 0, 'CON')
@@ -127,11 +152,34 @@ async function getVERBInfinitive(word, langRef){
127152

128153
async function handleNOUN(obj, sentence, langRef, src){
129154
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {})
155+
if(obj.props.prev){
156+
if(obj.props.prev[obj.types[0]]){
157+
const toPrepend = obj.props.prev[obj.types[0]].EXCEPTION
158+
? obj.props.prev[obj.types[0]].EXCEPTION[obj.words[0]] || obj.props.prev[obj.types[0]].DEFAULT
159+
: obj.props.prev[obj.types[0]].DEFAULT;
160+
const toPrependSplit = toPrepend.split(',')
161+
for(let t=toPrependSplit.length-1; t<=0; t--){
162+
const tp = toPrependSplit[t];
163+
switch(tp){
164+
case 'ART':
165+
const articlesGenders = await dbGetter.getPersistent(langRef, 'ARTICLES/GENDERS', {});
166+
obj.words.unshift(articlesGenders[obj.genders[0]])
167+
obj.types.unshift('ART')
168+
break;
169+
case 'NONE':
170+
continue;
171+
default:
172+
obj.words[0] = [tp, obj.words[0]].join(' ')
173+
break;
174+
}
175+
}
176+
}
177+
}
130178
if (obj.composed) {
131-
if(obj.types.at(-2) === 'NOUN'){
179+
if(obj.types.at(-2) === 'NOUN' || obj.types.at(-2) === 'SUBJ'){
132180
obj.words.splice(-1, 0, defaults.CON);
133181
obj.types.splice(-1, 0, "CON");
134-
} else if (obj.types.at(-2) === 'ART' && obj.types.at(-3) === 'NOUN'){
182+
} else if (obj.types.at(-2) === 'ART' && (obj.types.at(-3) === 'NOUN' || obj.types.at(-3) === 'SUBJ')){
135183
obj.words.splice(-2, 0, defaults.CON);
136184
obj.types.splice(-2, 0, "CON");
137185
}
@@ -149,11 +197,35 @@ async function handleNOUN(obj, sentence, langRef, src){
149197

150198
async function handleSUBJ(obj, sentence, langRef, src){
151199
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {})
200+
if(obj.props.prev){
201+
if(obj.props.prev[obj.types[0]]){
202+
const toPrepend = obj.props.prev[obj.types[0]].EXCEPTION
203+
? obj.props.prev[obj.types[0]].EXCEPTION[obj.words[0]] || obj.props.prev[obj.types[0]].DEFAULT
204+
: obj.props.prev[obj.types[0]].DEFAULT;
205+
const toPrependSplit = toPrepend.split(',')
206+
for(let t=toPrependSplit.length-1; t>=0; t--){
207+
const tp = toPrependSplit[t];
208+
console.log(tp)
209+
switch(tp){
210+
case 'ART':
211+
const articlesGenders = await dbGetter.getPersistent(langRef, 'ARTICLES/GENDERS', {});
212+
obj.words.unshift(articlesGenders[obj.genders[0]])
213+
obj.types.unshift('ART')
214+
break;
215+
case 'NONE':
216+
continue;
217+
default:
218+
obj.words[0] = [tp, obj.words[0]].join(' ')
219+
break;
220+
}
221+
}
222+
}
223+
}
152224
if (obj.composed) {
153-
if(obj.types.at(-2) === 'SUBJ'){
225+
if(obj.types.at(-2) === 'NOUN' || obj.types.at(-2) === 'SUBJ'){
154226
obj.words.splice(-1, 0, defaults.CON);
155227
obj.types.splice(-1, 0, "CON");
156-
} else if (obj.types.at(-2) === 'ART' && obj.types.at(-3) === 'SUBJ'){
228+
} else if (obj.types.at(-2) === 'ART' && (obj.types.at(-3) === 'NOUN' || obj.types.at(-3) === 'SUBJ')){
157229
obj.words.splice(-2, 0, defaults.CON);
158230
obj.types.splice(-2, 0, "CON");
159231
}

functions/prepares.js

+37-20
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ async function solveMOD(tokens, langRef){
4242

4343
async function solveMISC(words, types, langRef){
4444
for (let i=0; i<types.length; i++){
45-
if (types[i] === 'MISC') types[i] = await dbGetter.getOnce(langRef, `DEFINITIVES/MISC/${obj.words[i]}`) || 'MISC';
45+
if (types[i] === 'MISC') types[i] = await dbGetter.getOnce(langRef, `DEFINITIVES/MISC/${words[i]}`) || 'MISC';
4646
}
4747
return [words, types];
4848
}
4949

50-
async function findART(obj, langRef){
50+
async function splitART(obj, langRef){
5151
for(let i=0; i<obj.words.length; i++){
5252
const wordsSplit = obj.words[i].split(' ');
5353
if(wordsSplit.length > 1){
@@ -79,6 +79,35 @@ async function findART(obj, langRef){
7979
}
8080
}
8181

82+
async function splitDefinitives(obj, langRef){
83+
for(let i=0; i<obj.words.length; i++){
84+
const wordsSplit = obj.words[i].split(' ');
85+
if(wordsSplit.length > 1){
86+
const newWords = []
87+
const newTypes = []
88+
let currentNewWord = []
89+
const definitives = await dbGetter.getPersistent(langRef, 'DEFINITIVES/MISC', {});
90+
for(w of wordsSplit){
91+
if(definitives[w]){
92+
if(currentNewWord.length > 0){
93+
newWords.push(currentNewWord.join(' '))
94+
newTypes.push(obj.types[i])
95+
currentNewWord = []
96+
}
97+
newWords.push(w)
98+
newTypes.push(definitives[w])
99+
} else {
100+
currentNewWord.push(w)
101+
}
102+
}
103+
newWords.push(currentNewWord.join(' '))
104+
newTypes.push(obj.types[i])
105+
obj.words.splice(i, 1, ...newWords)
106+
obj.types.splice(i, 1, ...newTypes)
107+
}
108+
}
109+
}
110+
82111
async function prepareMeta(obj, langRef){
83112

84113
switch(obj.type){
@@ -97,30 +126,16 @@ async function prepareMeta(obj, langRef){
97126
async function prepareMetaNOUN(obj, langRef){
98127

99128
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {});
100-
101-
await findART(obj, langRef)
129+
await splitDefinitives(obj, langRef)
102130
const genders = []
103-
const addART = []
104131
for(let i=0; i<obj.words.length; i++){
105132
if (obj.types[i] === 'NOUN'){
106133
const personsGenders = await dbGetter.getPersistent(langRef, 'PERSONS/GENDERS', {});
107134
if(personsGenders.flat(1).includes(obj.words[i])) genders.push(obj.words[i])
108135
else if(obj.props[i]) genders.push(obj.props[i].gender || defaults.GENDER)
109136
else genders.push(await dbGetter.getOnce(langRef, `DEFINITIVES/GENDER/${obj.words[i]}`) || defaults.GENDER)
110137
}
111-
if (i === 0){
112-
const articlesGenders = await dbGetter.getPersistent(langRef, 'ARTICLES/GENDERS', {});
113-
addART.push([0, articlesGenders[genders.at(-1)]])
114-
}
115-
else if (obj.types[i-1] !== 'ART' && obj.types[i-1] !== 'CON'){
116-
const articlesGenders = await dbGetter.getPersistent(langRef, 'ARTICLES/GENDERS', {});
117-
addART.push([i, articlesGenders[genders.at(-1)]])
118-
}
119138
}
120-
addART.forEach((v, i) => {
121-
obj.words.splice(v[0] + i, 0, v[1])
122-
obj.types.splice(v[0] + i, 0, 'ART')
123-
})
124139
if(genders.length > 0){
125140
const personsGenders = await dbGetter.getPersistent(langRef, 'PERSONS/GENDERS', {});
126141
const personsPlurals = await dbGetter.getPersistent(langRef, 'PERSONS/PLURALS', {});
@@ -133,16 +148,16 @@ async function prepareMetaNOUN(obj, langRef){
133148
}
134149

135150
//HERE: this might be temporary, for now it seems to be no difference between a SUBJ and a NOUN
136-
obj.type = 'SUBJ'
137-
obj.types = obj.types.map(t => t === 'NOUN' ? 'SUBJ' : t)
151+
obj.type = 'SUBJ'
152+
obj.genders = genders;
138153

139154
return obj;
140155
}
141156

142157
async function prepareMetaSUBJ(obj, langRef){
143158

144159
const defaults = await dbGetter.getPersistent(langRef, 'DEFAULTS', {});
145-
160+
await splitDefinitives(obj, langRef)
146161
const genders = []
147162
for(let i=0; i<obj.words.length; i++){
148163
if (obj.types[i] === 'SUBJ'){
@@ -162,6 +177,8 @@ async function prepareMetaSUBJ(obj, langRef){
162177
} else {
163178
obj.meta.PERSON = defaults.GENDER;
164179
}
180+
181+
obj.genders = genders;
165182

166183
return obj;
167184
}

0 commit comments

Comments
 (0)