Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd51660

Browse files
committedOct 17, 2022
method to force time and person of verb issue #3
1 parent a9b1593 commit bd51660

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed
 

‎functions/handlers.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
async function handleType(obj, sentence, langRef, src, defaults){
1+
async function handleType(obj, sentence, langRef, src, defaults, forces){
22
switch (obj.type){
33
case 'VERB':
4-
obj = await handleVERB(obj, sentence, langRef, src, defaults);
4+
obj = await handleVERB(obj, sentence, langRef, src, defaults, forces);
55
break;
66
case 'NOUN':
77
obj = await handleNOUN(obj, sentence, langRef, src, defaults);
@@ -18,7 +18,7 @@ async function handleType(obj, sentence, langRef, src, defaults){
1818
}
1919
}
2020

21-
async function handleVERB(obj, sentence, langRef, src, defaults){
21+
async function handleVERB(obj, sentence, langRef, src, defaults, forces){
2222
//HERE: we are joining prepositions into the previous verb
2323
let prepIndex = obj.types.findIndex(t => t === 'PREP')
2424
while (prepIndex >= 0) {
@@ -30,17 +30,21 @@ async function handleVERB(obj, sentence, langRef, src, defaults){
3030
obj.meta.PERSON = defaults.PERSON;
3131

3232
const childrenSUBJ = obj.children.filter(c => c.type === 'SUBJ' || c.type === 'NOUN') //HERE: check if include NOUN here is ok
33-
if (childrenSUBJ.length > 1) {
33+
if (forces.PERSON){
34+
obj.meta.PERSON = forces.PERSON;
35+
} else if (childrenSUBJ.length > 1) {
3436
const childrenSUBJpersons = childrenSUBJ.map(c => sentence[c.position].meta.PERSON)
3537
const personsPluralsSn = await langRef.child(`PERSONS/PLURALS`).get() //if there's more than one subj token, recall the personPlurals
3638
const personsPlurals = personsPluralsSn.val() || {};
37-
const foundPerson = personsPlurals.find(person => ( childrenSUBJpersons.includes(person[0]) || childrenSUBJpersons.includes(person[1])));
39+
const foundPerson = personsPlurals.find(person => childrenSUBJpersons.includes(person[0]) || childrenSUBJpersons.includes(person[1]));
3840
obj.meta.PERSON = foundPerson[1];
3941
} else if (childrenSUBJ.length === 1) obj.meta.PERSON = sentence[childrenSUBJ[0].position].meta.PERSON || obj.meta.PERSON;
4042

4143
obj.meta.TIME = defaults.TIME;
4244
const childrenADV = obj.children.filter(c => c.type === 'ADV')
43-
if (childrenADV.length > 1) {
45+
if (forces.TIME){
46+
obj.meta.TIME = forces.TIME;
47+
} else if (childrenADV.length > 1) {
4448
const sortedChildrenADV = childrenADV.sort((a, b) => Math.abs(a.position - obj.position) - Math.abs(b.position - obj.position))
4549
for (childADV of sortedChildrenADV) {
4650
if (sentence[childADV.position].meta.TIME) {

‎functions/methods.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ async function parseDependencies(wordList, headlessList, language){ //Algorithm
134134

135135
const { handleType } = require('./handlers.js')
136136

137-
async function handleSentence(sentence, language, src = 'static'){
137+
async function handleSentence(sentence, language, forces, src = 'static'){
138138
const defaultsSn = await rt.ref(`${language}/DEFAULTS`).get()
139139
const defaults = defaultsSn.val() || {};
140140
const langRef = rt.ref(language)
141141

142-
for (obj of sentence) await handleType(obj, sentence, langRef, src, defaults);
142+
for (obj of sentence) await handleType(obj, sentence, langRef, src, defaults, forces);
143143

144144
return sentence;
145145
}

‎functions/routing.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require('express');
2-
const { error } = require("firebase-functions/lib/logger");
2+
const { log, error } = require("firebase-functions/lib/logger");
33

44
const {
55
addLexiconData,
@@ -45,9 +45,10 @@ app.post('/', allowCors, (req, res) => {
4545
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
4646
return;
4747
}
48+
const forces = {...req.query}
4849
prepareSentence(body.words, body.types, body.props || {}, body.language || 'en')
4950
.then(prepared => parseDependencies(prepared, [], body.language || 'en'))
50-
.then(([parsed, headless]) => handleSentence(parsed, body.language || 'en'))
51+
.then(([parsed, headless]) => handleSentence(parsed, body.language || 'en', forces))
5152
.then(handled => res.status(200).json({sentence: handled}))
5253
.catch((err) => {
5354
error(`Error at parsing: ${err.message}`);
@@ -89,9 +90,11 @@ app.post('/realise', allowCors, (req, res) => {
8990
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
9091
return;
9192
}
93+
log(body)
94+
const forces = {...req.query}
9295
prepareSentence(body.words, body.types, body.props || {}, body.language || 'en')
9396
.then(prepared => parseDependencies(prepared, [], body.language || 'en'))
94-
.then(([parsed, headless]) => handleSentence(parsed, body.language || 'en'))
97+
.then(([parsed, headless]) => handleSentence(parsed, body.language || 'en', forces))
9598
.then(handled => realiseSentence(handled, body.language || 'en'))
9699
.then(realised => res.status(200).json({sentence: realised}))
97100
.catch((err) => {

0 commit comments

Comments
 (0)
Please sign in to comment.