Funciones para el manejo de textos, gramática Text, y
que también retornan un texto, Text.
Las funciones de esta página están ordenadas de forma alfabética por las diferentes gramáticas del lenguaje Tol (
Text, Set, Serie, Anything, Code, Date, Real,...) y,
dentro de cada gramática, por el nombre de la función.
Pueden encontrarse 2 o más funciones con idéntico nombre,
pero con distintas maneras de programarse o con diferentes comentarios en diferentes idiomas,
estas funciones aparecerán unas a continuación de las otras.
//////////////////////////////////////////////////////////////////////////////
Text F(Anything anyVal)
//////////////////////////////////////////////////////////////////////////////
{
Text graVal = Grammar(anyVal);
Case
(
graVal=="Text", anyVal, // Ya es texto
graVal=="Real", If(EQ(anyVal, Round(anyVal)),
FormatReal(anyVal, "%.0lf"), // Entero sin decimales
FormatReal(anyVal, "%.2lf")), // 2 decimales
graVal=="Date", If(Hour(anyVal),
FormatDate(anyVal, "%c%Y/%m/%d %h:%i:%s"), // Tiempo
FormatDate(anyVal, "%c%Y/%m/%d")), // Fecha
graVal=="Set",
{
Real crdSet = Card(anyVal);
Case(
EQ(crdSet,0), "[]",
EQ(crdSet,1), "["+F(anyVal[1])+"]",
TRUE, "["+F(anyVal[1])+SetSum(For(2,crdSet,Text(Real setPos)
{ "|"+F(anyVal[setPos]) }))+"]")
},
TRUE, "Not basic type"
)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna numeros, fechas, textos, conjuntos como un texto de formato simple.",
F);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Q(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Char(34) + txtVal + Char(34) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto entre dobles comillas. Equivalente a la funcion Tol Qt().",
Q);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text R(Text txtLst) // Set of texts
//////////////////////////////////////////////////////////////////////////////
{ Anything SetGetRand(Txt2Set(txtLst, "|", "CN")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto elegido al azar de entre los tokens (|) de otro texto.
Se basa en una version especial de la funcion Txt2Set().",
R);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text R(Text txtLst) // List of texts separated with |
//////////////////////////////////////////////////////////////////////////////
{ SetGetRand(Txt2Set(txtLst, "|")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto elegido al azar de entre los tokens (|) de otro texto.",
R);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text inpTxt, // Texto de entrada
Text tagIni, // Tag inicial
Text tagEnd, // Tag final
Real cmpFlg) // Si true aplica Compact()
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(inpTxt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(inpTxt, tagEnd, posSub);
If(LE(posEnd, 0), "", Sub(inpTxt, posSub, posEnd-1))
});
If(cmpFlg, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subtexto entre la primera ocurrencia de tagIni y tagEnd.
Si tagIni o tagEnd no aparecen retorna la tira vacia.
Si cmpFlg es cierto entonces aplica la funcion Compact() al texto que retorna.
Por ejemplo:
TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
retorna 'c'.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text txt, // Text
Text tagIni, // Initial tag
Text tagEnd, // End tag
Real cmp) // If true apply the Compact() function
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result =
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
Text subTxt = If(LE(posEnd,0),
Sub(txt,posSub,TextLength(txt)),
Sub(txt,posSub,posEnd-1));
subTxt
});
If(cmp, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a subtext of text between the first ocurrence of tagIni and tagEnd.
If tagIni or tagEnd does not ocurr then returns the empty text.
If cmp argument is true then apply the Compact() function to the returning
text.
For example: TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
returns a 'c'.
There are other version of this function with more functionalities.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtCat(Text iniTxt, // Texto inicial
Text sepTxt, // Texto de separacion
Text endTxt) // Texto final
//////////////////////////////////////////////////////////////////////////////
{
Case(
iniTxt=="", endTxt,
endTxt=="", iniTxt,
TRUE, iniTxt + sepTxt + endTxt)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto resultado de concatenar 2 textos con un separador en medio,
si alguno de los 2 textos son la tira vacia retorna el otro texto.",
TxtCat);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2Tag(Text txt, Text tagIni, Text tagEnd)
//
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
If(And(EQ(posIni,1),LE(posEnd,0)), txt,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txt,posIni, TextLength(txt)),
Sub(txt,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2Tag(Sub(txt, posEnd+TextLength(tagEnd), TextLength(txt)),
tagIni, tagEnd)))
});
ReplaceTable(result, [[ [[tagIni, ""]], [[tagEnd, ""]] ]])
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <::--->.",
TxtInside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2Tag(Text txtInp, // Texto de entrada
Text tagIni, // Tag inicial
Text tagEnd) // Tag final
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txtInp, tagEnd, posSub);
If(And(EQ(posIni,1),LE(posEnd,0)), txtInp,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txtInp,posIni, TextLength(txtInp)),
Sub(txtInp,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2Tag(Sub(txtInp, posEnd+TextLength(tagEnd), TextLength(txtInp)),
tagIni, tagEnd)))
});
ReplaceTable(result, [[ [[tagIni, ""]], [[tagEnd, ""]] ]])
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todo el texto entre 2 tags (tagIni y tagEnd) dentro de txtIpn.
Por ejemplo: <aaa(::)bbb(---)ccc>, <(>, <)> -> <::--->.",
TxtInside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2TagPlus(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, tagIni);
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txtInp, tagEnd, posSub);
Real txtLen = TextLength(txtInp);
If(And(EQ(posIni,1),LE(posEnd,0)), txtInp,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txtInp,posIni, txtLen),
Sub(txtInp,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2TagPlus(Sub(txtInp, posEnd+TextLength(tagEnd), txtLen),
tagIni, tagEnd)))
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags plus theis tags (tagIni and tagEnd)
in txtInp.
Is diferente that the classic TxtInside2Tag().
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <(::)(---)>.",
TxtInside2TagPlus);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags and outside the scripts.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html y de los scripts.
Sirve para extraer texto limpio del que sacar palabras clave.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{
Text notScr = TxtOutside2Tag(htmTxt, "<script", "</script>"); // No scripts
Text notAmp = TxtOutside2Tag(notScr, "&", ";"); // No &xxx;
Text notHtm = TxtOutside2Tag(notAmp, "<", ">"); // No html
Text htmQuo = Replace(htmTxt, Char(34), "'"); // Unifica comillas
Text difSep = Char(7); // Separador no usual
Text difEnd = difSep+" "; // Terminador no usual
Text titTag = "title='"; // Titulos de la imagenes separados por un blanco
Text titTxt = TxtInside2Tag(Replace(htmQuo, titTag, difEnd), difSep, "'");
Text altTag = "alt='"; // Alts de la imagenes separados por un blanco
Text altTxt = TxtInside2Tag(Replace(htmQuo, altTag, difEnd), difSep, "'");
Compact(notHtm + titTxt + altTxt)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html, de los scripts e incluye
el que esta dentro de los alt y los titles de las imagenes.
Sirve para extraer texto limpio del que sacar palabras clave.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutside2Tag(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Set txtSet = TxtTokenizer(tagIni + tagEnd + txtInp, tagIni);
Set txtCic = EvalSet(txtSet, Text(Text txtTok)
{ TxtBetween2Tag(txtTok + tagIni, tagEnd, tagIni, FALSE) });
SetSum(txtCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts outside 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(:::)ccc>, <(>, <)> -> <aaabbbccc>.",
TxtOutside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtParagraphsWrap(Text txtInp, // Texto de entrada
Real linMax) // Maximo numero de caracteres por linea
//////////////////////////////////////////////////////////////////////////////
{
Text txtCls = ReplaceTable(txtInp,
[[
[[" \n","\n"]] // Limpiar blancos finales
]]);
Set parSet = TxtTokenizer(txtCls, ".\n"); // Respetar los parrafos puros
Set parCic = EvalSet(parSet, Text(Text inpPar)
{
If(inpPar=="", "",
{
Real iniWhi = TxtCountLeftChars(inpPar, " "); // Indentacion del parrafo
Set linSet = TxtParagraphWrap(inpPar+".", linMax - iniWhi, TRUE);
Text indent = Repeat(" ", iniWhi);
Set linCic = EvalSet(linSet, Text(Text linInp) { indent+linInp+"\n" });
SetSum(linCic)
})
});
SetSum(parCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un textos resultado de aplicar la funcion TxtParagraphWrap() a todos
sus parrafos.
Entiende que cada parrafo termina en un punto y salto de linea.",
TxtParagraphsWrap);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtRand(Real len) // Random text length
//////////////////////////////////////////////////////////////////////////////
{ SetSum(For(1,len,Text(Real t) { Char(Min(90,Max(65,Rand(64,91)))) })) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a random text with len chars from A to Z.
Use Min() y Max() because in some old versions of TOL the Rand() functions
sometimes returns numbers out of range.",
TxtRand);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceFirst(Text txtInp, // Input text
Text txtOld, // Old text
Text txtNew) // New text
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, txtOld);
Text result = If(LE(posIni,0), txtInp, // No aparece, no se cambia nada
{
Real lenIni = TextLength(txtOld);
Real posSub = posIni + lenIni;
Real posEnd = TextLength(txtInp);
Sub(txtInp, 1, posIni-1) + txtNew + Sub(txtInp, posSub, posEnd)
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the result of change, only the very first time, in the input text
txtInp the old text txtOld by the new text txtNew.",
TxtReplaceFirst);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceSecuence(Text txtInp, // Texto de entrada
Set repTab) // Tabla de reemplazamientos
//////////////////////////////////////////////////////////////////////////////
{
Real crdTab = Card(repTab); // Numero de cambios a realizar
If(!crdTab, txtInp, // Nada que hacer
{
Text txtNew = Replace(txtInp, repTab[1][1], repTab[1][2]); // Un cambio
TxtReplaceSecuence(txtNew, SetLastN(repTab,crdTab-1)) // Resto de cambios
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto resultado de aplicar la tabla de reemplazamientos repTab al
texto de entrada txtIno.
Es una version de ReplaceTable(txtInp, repTab, 1) que garantiza la secuencia
de los reemplazamientos del primero al ultimo de los reemplazamientos.
Es una funcion recursiva.
Cada reemplazamiento solo se efectua una vez.",
TxtReplaceSecuence);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceTree(Text txtInp, // Texto de entrada
Set setTre, // Conjunto de filas N:1
Real numCic) // Numero de ciclos, 0->hasta fin cambios
//////////////////////////////////////////////////////////////////////////////
{
Set expAll = EvalSet(setTre, Set(Set oneTre) // Expandir todos los arboles
{
Set setOld = oneTre[1]; // Viejos textos a reemplazar
Text txtNew = oneTre[2]; // Texto de destino
Set expTre = EvalSet(setOld, Set(Text txtOld) { [[txtOld, txtNew]] })
});
Set repTab = BinGroup("<<", expAll); // De conjunto de conjuntos a tabla
ReplaceTable(txtInp, repTab, numCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Equivalente a la funcion ReplaceTable() que en vez de recibir una tabla de
de filas con 2 textos (viejo, nuevo) recibe un conjunto de filas formadas
por un conjunto de textos que se cambian por un solo texto de destino.",
TxtReplaceTree);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtTwitterWrap(Text txtCut, // Texto inicial cortable
Text txtNot) // Texto final no cortable
//////////////////////////////////////////////////////////////////////////////
{
Real chrTwi = 117; // Number of characters for Twitter, teoricamente 140
Set setTwi = TxtLineWrap(txtCut, chrTwi-TextLength(txtNot), TRUE);
Compact(setTwi[1]+txtNot)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto que no supere el maximo de caracteres de Twitter a partir
de un texto inicial que se puede cortar y uno final que no se puede.",
TxtTwitterWrap);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text W(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Replace(txtVal, "/", "\\") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un camino en formato Unix convertido a formato Windows/DOS.",
W);
//////////////////////////////////////////////////////////////////////////////
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio