From 5a2ad55e9452f01bc265cd7ce8c88d4e78617754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Ma=C3=A7=C3=A3s?= Date: Thu, 11 Apr 2024 11:08:20 +0100 Subject: [PATCH] Confirmed recent changes and quality check --- services/makeTTS.js | 1165 ++++++++++++++++++++++--------------------- 1 file changed, 597 insertions(+), 568 deletions(-) diff --git a/services/makeTTS.js b/services/makeTTS.js index 8e5d10d..15b6e82 100644 --- a/services/makeTTS.js +++ b/services/makeTTS.js @@ -1,12 +1,13 @@ function titleCase(str) { - var splitStr = str.toLowerCase().split(' '); + var splitStr = str.toLowerCase().split(" "); for (var i = 0; i < splitStr.length; i++) { // You do not need to check if i is larger than splitStr length, as your for does that for you // Assign it back to the array - splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); + splitStr[i] = + splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } // Directly return the joined string - return splitStr.join(' ').trim(); + return splitStr.join(" ").trim(); } function addTransfer(p, modes) { @@ -20,29 +21,30 @@ function addTransfer(p, modes) { return numTr > 1 && addedTr == numTr - 1; } - p = p + ' . ( Há correspondência ) com '; + p = p + " . ( Há correspondência ) com "; if (modes.light_rail == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e o ' : 'o ') + 'métro ligeiro '; + p = p + (needsAnd(numTr, addedTr) ? " e o " : " o ") + "métro ligeiro"; addedTr += 1; } if (modes.subway == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e o ' : 'o ') + 'métro '; + p = p + (needsAnd(numTr, addedTr) ? " e o " : " o ") + "métro"; addedTr += 1; } if (modes.train == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e o ' : 'o ') + 'combóio '; + p = p + (needsAnd(numTr, addedTr) ? " e o " : " o ") + "combóio"; addedTr += 1; } if (modes.boat == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e o ' : 'o ') + 'barco '; + p = p + (needsAnd(numTr, addedTr) ? " e o " : " o ") + "barco"; addedTr += 1; } if (modes.bike_sharing == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e ' : '') + 'biciclétas partilhadas '; + p = p + (needsAnd(numTr, addedTr) ? " e " : " ") + "biciclétas partilhadas"; addedTr += 1; } if (modes.airport == 1) { - p = p + (needsAnd(numTr, addedTr) ? 'e o ' : 'o') + 'aéroporto (air port) '; + p = + p + (needsAnd(numTr, addedTr) ? " e o " : " o") + "aéroporto (air port)"; addedTr += 1; } return p; @@ -52,1043 +54,1059 @@ module.exports = (p, modes = {}) => { let thisString = p; /* Uniformize crossing to (X), add spaces around crossing */ let regex = /\(X\)/giu; - thisString = thisString.replace(regex, ' X '); + thisString = thisString.replace(regex, " X "); regex = /\bAnt[e]?[s]?[\s]?[\(]?X[\)]?([\s]+$)/giu; - thisString = thisString.replace(regex, 'Antes do Cruzamento '); - regex = /\bAnt[e]?[s]?[\s]?[\(]?X[\)]?([\s]*[^$])/giu; - thisString = thisString.replace(regex, 'Antes do Cruzamento com '); + thisString = thisString.replace(regex, "Antes do Cruzamento "); + regex = /\bAnt[e]?[s]?[\s]?[\(]?X[\)]?([\s]+[^$])/giu; + thisString = thisString.replace(regex, "Antes do Cruzamento com $1"); regex = /[\(]?X[\)]?[\s]?Ant[e]?[s]?\s/giu; - thisString = thisString.replace(regex, ' Cruzamento Antes de '); + thisString = thisString.replace(regex, " Cruzamento Antes de "); regex = /[\s][\(]?X[\)]?[\s]*($|\))/giu; - thisString = thisString.replace(regex, ' Cruzamento $1'); + thisString = thisString.replace(regex, " Cruzamento $1"); regex = /(^|[\(])[\s]*[\(]?X[\)]?\s/giu; - thisString = thisString.replace(regex, '$1 Cruzamento '); + thisString = thisString.replace(regex, "$1 Cruzamento "); regex = /([gpsgqvzt])X/giu; - thisString = thisString.replace(regex, '$1 X '); + thisString = thisString.replace(regex, "$1 X "); regex = /([^aEIBXN])X([^XTaeIO])/giu; - thisString = thisString.replace(regex, '$1 X $2'); + thisString = thisString.replace(regex, "$1 X $2"); regex = /([^aeiouãõâêîôûáéíóúàèìòùbxn\)\s])([\(]?X[\)]?)(?![aeiou])/giu; - thisString = thisString.replace(regex, '$1 X '); + thisString = thisString.replace(regex, "$1 X "); regex = /([\(]?X[\)]?)([^xaieouãõâêîôûáéíóúàèìòùt\(\s)])/giu; - thisString = thisString.replace(regex, ' X $2'); + thisString = thisString.replace(regex, " X $2"); regex = /([\(]?X[\)]?)(?=Estr)/giu; - thisString = thisString.replace(regex, ' X '); + thisString = thisString.replace(regex, " X "); regex = /([^aeioutfcsrpbndâêîôûáéíóúàèìòù\s])(R\s)/giu; - thisString = thisString.replace(regex, '$1 R '); + thisString = thisString.replace(regex, "$1 R "); /* Add spaces around parenthesis */ regex = /([^\s])\(([^X])/giu; - thisString = thisString.replace(regex, '$1 ( $2'); + thisString = thisString.replace(regex, "$1 ( $2"); regex = /\(([^\sX])/giu; - thisString = thisString.replace(regex, '( $1'); + thisString = thisString.replace(regex, "( $1"); regex = /([^\sX])\)/giu; - thisString = thisString.replace(regex, '$1 )'); + thisString = thisString.replace(regex, "$1 )"); regex = /([^X])\)([^\s])/giu; - thisString = thisString.replace(regex, '$1 ) $2'); + thisString = thisString.replace(regex, "$1 ) $2"); /* Add spaces around road numbers */ regex = /(^|\(|\sX)[\s]*N[ac\s]?((\d)+)([\s\-\(\)]|$)/giu; - thisString = thisString.replace(regex, '$1 Estrada Nacional $2 '); + thisString = thisString.replace(regex, "$1 Estrada Nacional $2 "); regex = /\bN([\d]{3})-(\d)\b/giu; - thisString = thisString.replace(regex, ' Estrada Nacional $1 $2 '); + thisString = thisString.replace(regex, " Estrada Nacional $1 $2 "); regex = /(?!\sE)\sN[.\s]*[º]?[\s]?((\d)+[a-g]?)[\s]*([\(\)]?)$/giu; - thisString = thisString.replace(regex, ' ( Número $1 $3 ) '); + thisString = thisString.replace(regex, " ( Número $1 $3 ) "); /* Add months of the year */ regex = /\b(Jan[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Janeiro$2'); + thisString = thisString.replace(regex, "Janeiro$2"); regex = /\b(Fev[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Fevereiro$2'); + thisString = thisString.replace(regex, "Fevereiro$2"); regex = /\b(Abr[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Abril$2'); + thisString = thisString.replace(regex, "Abril$2"); regex = /\b(Jun[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Junho$2'); + thisString = thisString.replace(regex, "Junho$2"); regex = /\b(Jul[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Julho$2'); + thisString = thisString.replace(regex, "Julho$2"); regex = /\b(Ago[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Agosto$2'); + thisString = thisString.replace(regex, "Agosto$2"); regex = /\b(Set[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Setembro$2'); + thisString = thisString.replace(regex, "Setembro$2"); regex = /\b(Out[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Outubro$2'); + thisString = thisString.replace(regex, "Outubro$2"); regex = /\b(Nov[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Novembro$2'); + thisString = thisString.replace(regex, "Novembro$2"); regex = /\b(Dez[\.]?)(\s|$)/giu; - thisString = thisString.replace(regex, 'Dezembro$2'); + thisString = thisString.replace(regex, "Dezembro$2"); /* Resolve reading dates */ - regex = /\b1[\.\s]?[ºo]?[\s]*(de)?[\s]*(?=Jane|Feve|Març|Abr|Maio|Junh|Julh|Agosto|Setem|Outu|Novem|Dez)/giu; - thisString = thisString.replace(regex, 'Primeiro de '); - regex = /([\d]+|Primeiro)([\s]*)(?=Janeiro|Fevereiro|Março|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro)/giu; - thisString = thisString.replace(regex, '$1 de '); + regex = + /\b1[\.\s]?[ºo]?[\s]*(de)?[\s]*(?=Jane|Feve|Març|Abr|Maio|Junh|Julh|Agosto|Setem|Outu|Novem|Dez)/giu; + thisString = thisString.replace(regex, "Primeiro de "); + regex = + /([\d]+|Primeiro)([\s]*)(?=Janeiro|Fevereiro|Março|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro)/giu; + thisString = thisString.replace(regex, "$1 de "); /* Add spaces around numbers */ regex = /([^\s\d])(\d)/giu; - thisString = thisString.replace(regex, '$1 $2'); + thisString = thisString.replace(regex, "$1 $2"); regex = /(\d)([^\s\dºª])/giu; - thisString = thisString.replace(regex, '$1 $2'); + thisString = thisString.replace(regex, "$1 $2"); regex = /([\d]+)[\s]*\/[\s]*([\d]+)/giu; - thisString = thisString.replace(regex, '$1 $2'); + thisString = thisString.replace(regex, "$1 $2"); regex = /\b([P]\s?[\d]+)(\s|$)/giu; - thisString = thisString.replace(regex, '( $1 ) '); - regex = /(?<=Janeiro|Fevereiro|Março|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro)[\s]*([\d]{1,3}\b)/giu; - thisString = thisString.replace(regex, ' número $1'); + thisString = thisString.replace(regex, "( $1 ) "); + regex = + /(?<=Janeiro|Fevereiro|Março|Abril|Maio|Junho|Julho|Agosto|Setembro|Outubro|Novembro|Dezembro)[\s]*([\d]{1,3}\b)/giu; + thisString = thisString.replace(regex, " número $1"); regex = /(? { /* Due to possible collisions with abbreviations of single letters I and V, */ /* these have stricter criteria and will perform a bit worse to avoid collision */ regex = /([^.]?\b)((XXI)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Vinte Um$4'); + thisString = thisString.replace(regex, "$1Vinte Um$4"); regex = /([^.]?\b)((XX)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Vinte$4'); + thisString = thisString.replace(regex, "$1Vinte$4"); regex = /([^.]?\b)((XII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Doze$4'); + thisString = thisString.replace(regex, "$1Doze$4"); regex = /([^.]?\b)((XIII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Treze$4'); + thisString = thisString.replace(regex, "$1Treze$4"); regex = /([^.]?\b)((XIV)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Catorze$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(IX)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Nona$4'); + thisString = thisString.replace(regex, "$1Catorze$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(IX)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Nona$4"); regex = /([^.]?\b)((IX)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Nono$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VIII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Oitava$4'); + thisString = thisString.replace(regex, "$1Nono$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VIII)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Oitava$4"); regex = /([^.]?\b)((VIII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Oitavo$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Sétima$4'); + thisString = thisString.replace(regex, "$1Oitavo$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VII)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Sétima$4"); regex = /([^.]?\b)((VII)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Sétimo$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VI)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Sexta$4'); + thisString = thisString.replace(regex, "$1Sétimo$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(VI)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Sexta$4"); regex = /([^.]?\b)((VI)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Sexto$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(IV)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Quarta$4'); + thisString = thisString.replace(regex, "$1Sexto$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(IV)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Quarta$4"); regex = /([^.]?\b)((IV)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Quarto$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(III)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Terceira$4'); + thisString = thisString.replace(regex, "$1Quarto$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(III)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Terceira$4"); regex = /([^.]?\b)((III)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Terceiro$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(II)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Segunda$4'); + thisString = thisString.replace(regex, "$1Terceiro$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s(II)\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Segunda$4"); regex = /([^.]?\b)((II)\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1Segundo$4'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s[I]\b)([^.]?)/giu; - thisString = thisString.replace(regex, '$1 Primeira$3'); - regex = /(?<=[oº]|Manuel|Lu[íi]s|Henrique|Henriques|Jos[eé])(\s+[I]\b)([^.]?)/giu; - thisString = thisString.replace(regex, ' Primeiro$2'); - regex = /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s[V])(\s|$)/giu; - thisString = thisString.replace(regex, '$1 Quinta '); + thisString = thisString.replace(regex, "$1Segundo$4"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s[I]\b)([^.]?)/giu; + thisString = thisString.replace(regex, "$1 Primeira$3"); + regex = + /(?<=[oº]|Manuel|Lu[íi]s|Henrique|Henriques|Jos[eé])(\s+[I]\b)([^.]?)/giu; + thisString = thisString.replace(regex, " Primeiro$2"); + regex = + /([aª]|Isabel|Inês|Ines|Beatriz|Matilde|Gertrudes|Judite|Leonor|Lurdes\b)(\s[V])(\s|$)/giu; + thisString = thisString.replace(regex, "$1 Quinta "); regex = /([oº]|Manuel|Luis|Henrique|Henriques\b)(\s[V])(\s|$)/giu; - thisString = thisString.replace(regex, '$1 Quinto '); + thisString = thisString.replace(regex, "$1 Quinto "); /* Replace crossings */ regex = /[\(](([^\)])*)[\s]+(X)[\s]+((.)*)/giu; - thisString = thisString.replace(regex, '( Cruzamento entre ( $1 ) e $4 '); - regex = /(?<=[A-Za-z]\s)((?:Rua|Avenida|Alameda|Praceta|Travessa|Estrada)([^\)])*)[\s]+(X)[\s]+((.)*)/giu; - thisString = thisString.replace(regex, '( Cruzamento entre ( $1 ) e $4 )'); - regex = /(?<=^[A-Za-z\s]*)[\(][\s]*((?:Rua|Avenida|Alameda|Praceta|Travessa|Estrada)([^.])*)[\s]+(X)[\s]+((.)*)/giu; - thisString = thisString.replace(regex, '( Cruzamento entre ( $1 ) e $4 )'); + thisString = thisString.replace(regex, "( Cruzamento entre ( $1 ) e $4 "); + regex = + /(?<=[A-Za-z]\s)((?:Rua|Avenida|Alameda|Praceta|Travessa|Estrada)([^\)])*)[\s]+(X)[\s]+((.)*)/giu; + thisString = thisString.replace(regex, "( Cruzamento entre ( $1 ) e $4 )"); + regex = + /(?<=^[A-Za-z\s]*)[\(][\s]*((?:Rua|Avenida|Alameda|Praceta|Travessa|Estrada)([^.])*)[\s]+(X)[\s]+((.)*)/giu; + thisString = thisString.replace(regex, "( Cruzamento entre ( $1 ) e $4 )"); regex = /((.)*)[\s]+(X)[\s]+((.)*)/giu; - thisString = thisString.replace(regex, 'Cruzamento entre ( $1 ) e $4 '); + thisString = thisString.replace(regex, "Cruzamento entre ( $1 ) e $4 "); regex = /\bEDP\b/giu; - thisString = thisString.replace(regex, 'E. D. P.'); + thisString = thisString.replace(regex, "E. D. P."); regex = /\bFCT\b/giu; - thisString = thisString.replace(regex, 'F. C. T.'); + thisString = thisString.replace(regex, "F. C. T."); regex = /\bIc\s/giu; - thisString = thisString.replace(regex, 'I. C. '); + thisString = thisString.replace(regex, "I. C. "); regex = /\bIts\b/giu; - thisString = thisString.replace(regex, 'I. T. S.'); + thisString = thisString.replace(regex, "I. T. S."); regex = /\bIefp\b/giu; - thisString = thisString.replace(regex, ' Centro de Emprego e Formação '); - /* Trim spaces */ - thisString = thisString.replace(/\s+/g, ' '); - /* Remove underscores */ - thisString = thisString.replace(/\_/g, ''); + thisString = thisString.replace(regex, " Centro de Emprego e Formação "); /* Title case */ thisString = titleCase(thisString); /* Reset "e" casing*/ // thisString = thisString.replace(/\sE\s/g, ' e '); - // Return result - return addTransfer(thisString, modes); + thisString = addTransfer(thisString, modes); + /* Remove underscores */ + thisString = thisString.replace(/\_/g, ""); + /* Trim spaces */ + return thisString.replace(/\s+/g, " "); };