Text functions.
Funciones de nombre corto
Text Q(Text txtVal)Text W(Text txtVal)Text R(Text txtLst)Text F(Anything anyVal)Funciones
Set Txt2Set(Text txtInp, Text sepTok)Set TxtTokenizer(Text txtInp, Text tagBrk)Set TxtForChr(Text inpTxt, Code funChr)Text TxtBetween2Tag(Text inpTxt, Text tagIni, Text tagEnd, Real cmpFlg)Text TxtOutside2Tag(Text txtInp, Text tagIni, Text tagEnd)Text TxtOutHtmTag(Text htmTxt)Text TxtOutHtmScr(Text htmTxt)Text TxtReplaceSecuence(Text txtInp, Set repTab)//////////////////////////////////////////////////////////////////////////////
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 W(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Replace(txtVal, "/", "\\") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un camino en formato Unix convertido a formato Windows/DOS.",
W);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txtInp, // Texto de entrada
Text sepTok) // Elemento separador
//////////////////////////////////////////////////////////////////////////////
{
Set setSep = TxtTokenizer(txtInp, sepTok);
Set setCmp = EvalSet(setSep, Text(Text eleTxt) { Compact(eleTxt) });
setCmp
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto a partir de un texto txtInp, troceandolo por un separador
sepTok y dependiendo de ctrFun puede, en este orde, si C compactar, si N
eliminar los texto nulos, si U retornar elementos unicos y si S retornar el
set ordenado.",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de textos resultado de cortar el texto de entrada por un
unico tag tagBrk no incluyendo el tag tagBrk dentro de los textos.
Se soporta en la funcion Tol Tokenizer() que rompe por un unico caracter.
Usa como caracter interno de corte el 7 (bell), esperando que no aparezca.",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtForChr(Text inpTxt, // Texto de entrada
Code funChr) // Funcion tipo Anything(Text oneChr)
//////////////////////////////////////////////////////////////////////////////
{
Real lenTxt = TextLength(inpTxt);
For(1, lenTxt, Anything(Real posTxt) { funChr(Sub(inpTxt,posTxt,posTxt)) })
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna el conjunto resultado de aplicar la funcion funChr(Text oneChr)
a todos los caracteres del texto de entrada txtInp.
Retorna un Set a imagen de las funciones basicas For() y EvalSet().",
TxtForChr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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 TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FILE : txt.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : Text functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// SHORTNAMES
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 W(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Replace(txtVal, "/", "\\") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un camino en formato Unix convertido a formato Windows/DOS.",
W);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txtInp, // Texto de entrada
Text sepTok) // Elemento separador
//////////////////////////////////////////////////////////////////////////////
{
Set setSep = TxtTokenizer(txtInp, sepTok);
Set setCmp = EvalSet(setSep, Text(Text eleTxt) { Compact(eleTxt) });
setCmp
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto a partir de un texto txtInp, troceandolo por un separador
sepTok y dependiendo de ctrFun puede, en este orde, si C compactar, si N
eliminar los texto nulos, si U retornar elementos unicos y si S retornar el
set ordenado.",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de textos resultado de cortar el texto de entrada por un
unico tag tagBrk no incluyendo el tag tagBrk dentro de los textos.
Se soporta en la funcion Tol Tokenizer() que rompe por un unico caracter.
Usa como caracter interno de corte el 7 (bell), esperando que no aparezca.",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtForChr(Text inpTxt, // Texto de entrada
Code funChr) // Funcion tipo Anything(Text oneChr)
//////////////////////////////////////////////////////////////////////////////
{
Real lenTxt = TextLength(inpTxt);
For(1, lenTxt, Anything(Real posTxt) { funChr(Sub(inpTxt,posTxt,posTxt)) })
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna el conjunto resultado de aplicar la funcion funChr(Text oneChr)
a todos los caracteres del texto de entrada txtInp.
Retorna un Set a imagen de las funciones basicas For() y EvalSet().",
TxtForChr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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 TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 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);
//////////////////////////////////////////////////////////////////////////////
Antonio.Salmeron construye las páginas y documentos del sitio web antoniosalmeron.con
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio