Set functions. This code assumes the following behaviour of:
The function BinGroup():
Text WriteLn("<"+BinGroup("+",Empty)+">" -> <>
Text WriteLn("<"+BinGroup("+",[["a"]])+">"); -> <a>
Text WriteLn("<"+BinGroup("+",[["a","b"]])+">"); -> <ab>
Text WriteLn("<"+BinGroup("+",[["a","a"]])+">"); -> <aa>
Text WriteLn("<"+BinGroup("+",[["a","b","a"]])+">"); -> <aba>
The function SetSum():
Text WriteLn("<"+SetSum(Empty)+">"); -> <>
Text WriteLn("<"+SetSum([["a"]])+">"); -> <a>
Text WriteLn("<"+SetSum([["a","b"]])+">"); -> <ab>
Text WriteLn("<"+SetSum([["a","a"]])+">"); -> <aa>
Text WriteLn("<"+SetSum([["a","b","a"]])+">"); -> <aba>
Funciones
Text Set2Txt(Set valSet, Text iniTxt, Text endTxt, Text sepTxt, Text sepLst, Text txtDet, Text datFmt, Text dteDet, Text dteFmt)Text Set2TxtKeyword(Set valSet, Real minChr, Real a2zOrd, Real maxKey)Set SetFirstN(Set setInp, Real numEle)Set SetLastN(Set setInp, Real numEle)Set SetSubCicle(Set setInp, Real iniPos, Real lenRet)Anything SetGetRand(Set setInp)//////////////////////////////////////////////////////////////////////////////
Text Set2Txt(Set valSet, // Set of elements
Text iniTxt, // Initial text for list
Text endTxt, // End text for list
Text sepTxt, // Element separators
Text sepLst, // 2 last elements separator
Text txtDet, // Delimiter for texts
Text datFmt, // Format for real numbers
Text dteDet, // Delimiter for dates
Text dteFmt) // Format for dates
//////////////////////////////////////////////////////////////////////////////
{
Real card = Card(valSet);
Text body =
If(EQ(card,0), "",
If(EQ(card,1), F(valSet[1]),
If(EQ(card,2), F(valSet[1])+sepLst+F(valSet[2]),
{
Set txtVal = For(2,card,Text(Real p)
{
If(EQ(p,card),sepLst,sepTxt) + F(valSet[p])
});
F(valSet[1]) + BinGroup("+",txtVal)
})));
iniTxt+body+endTxt
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a text like a list with all the elements of valSet converted in a
text format (elements types: Text, Real or Date).
Arguments:
- iniTxt initial text, for example: '(', '[[', '', etc.
- endTxt end text, for example ')', ']]', '', etc.
- sepTxt elements separator, for example '; ', ',', '', etc.
- sepLst two last elements separator, for example, ' & ', ' and ', etc.,
you can specify the same as sepTxt
- txtDet text delimiters, for exmple, quotes for TOL, single quote for
SQL, nothing, etc.
- datFmt real numbers format, for explame, '%.0lf' for integers, if none
then uses the default TOL real number format.
- dteDet date delimiters, for example, simgle quote for SQL.
- dteFmt date format, for example, '%c%Y%m%d', if none
then uses the default TOL dates format.
Only works with TOL types Text, Real or Date, when find a Set type then
works in a recursive way with the same arguments.",
Set2Txt);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtKeyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set cmmWrd =
[[
"además",
"ambos", "ambas",
"aunque",
"cualquier", "cualquiera",
"cuando",
"deberá",
"desde",
"dentro",
"entre",
"esta", "estas", "esto", "estos",
"junto",
"mismo", "mismos", "misma", "mismas",
"mucho", "muchos", "mucha", "muchas",
"ningún", "ninguna",
"nosotros", "nosotras",
"nuestro", "nuestros", "nuestra", "nuestras",
"otra", "otras", "otro", "otros",
"parte",
"porque",
"puede", "pueden", "pudiendo",
"quienes",
"siempre",
"siguiente", "siguientes",
"sobre",
"sumamente",
"también",
"tanto",
"través",
"zulú"
]];
// Convert all to text
Text lstTxt = ToLower(Set2Txt(valSet,"", "", " ", " ", "", "", "", ""));
Text htmNot = TxtOutHtmScr(lstTxt);
Text lstCls = Compact(ReplaceTable(htmNot,
[[ [[Char(34), " "]], [["'", " "]],
[["." , " "]], [[":", " "]],
[[";", " "]], [[",", " "]],
[["[", " "]], [["]", " "]],
[["(", " "]], [[")", " "]],
/* [["<", " "]], [[">", " "]], Sobra */
[["«", " "]], [["»", " "]], // Titulos de libros
[["¡", " "]], [["!", " "]],
[["¿", " "]], [["?", " "]],
[["/", " "]],
[["&", " "]],
[[" ", " "]],
[[" none ", " "]],
[[" the ", " "]],
[[" and ", " "]],
[[" or ", " "]],
[[" & ", " "]],
[[" y ", " "]],
[[" o ", " "]]
]]));
// Split in words
Set setTxt = Tokenizer(lstCls, " ");
// Select word with more than minChr characters
Set setSel = Select(setTxt, Real(Text a) { GT(TextLength(a), minChr) });
// Classify words
Set setCla = Classify(setSel, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) });
// Make a frecuencies table [ word, number of occurrencies ]
Set tabFrq = EvalSet(setCla, Set(Set cla)
{ [[ cla[1], Card(cla) ]] });
// Sort (with most ocurrencies first)
Set srtFrq = Sort(tabFrq, Real(Set a, Set b)
{ Compare(Real(b[2]), Real(a[2])) });
// Project by word column (the first column)
Set keySet = EvalSet(srtFrq, Text(Set a) { Text(a[1]) });
// Minus common words
Set keyMin = keySet - cmmWrd;
// Select the first maxKey or Card(keySet) or all if maxKey=0
Set keyFst = If(maxKey, SetFirstN(keyMin, maxKey), keyMin);
// Sort if is needed
Set keySor = If(! a2zOrd, keyFst,
Sort(keyFst, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) })); // Order
// Convert to text
Set2Txt(keySor, "", "", ", ", ", ", "", "", "", "")
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a text list with all the elements of valSet converted in a
text format with commas like a keywords list, ordered and without repetitions.
Remove all word with LE(TextLength(), minChr).
Returns the maxKey elements more occurrencies.",
Set2TxtKeyword);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetFirstN(Set setInp, // Set de entrada
Real numEle) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
If(LE(numEle, 0), Empty,
For(1, Min(Card(setInp), numEle), Anything(Real setPos)
{ setInp[setPos] }))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los primeros numEle elementos.
Si el conjunto tiene menos de numEle elementos los retorna todos.
Si numEle es 0 o negativo retorna el conjunto vacio.",
SetFirstN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetLastN(Set setInp, // Set de entrada
Real numEle) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
If(LE(numEle, 0), Empty,
For(Max(1, 1+Card(setInp)-numEle), Card(setInp), Anything(Real setPos)
{ setInp[setPos] }))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los ultimos numEle elementos.
Si el conjunto tiene menos de numEle elementos los retorna todos.",
SetLastN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetSubCicle(Set setInp, // Conjunto de elementos
Real iniPos, // Posicion inicial
Real lenRet) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
Real modCic(Real setPos, Real crdSet)
{
Real modPos = setPos % crdSet;
If(LE(modPos, 0), crdSet, modPos)
};
Real crdSet = Card(setInp);
For(0, lenRet-1, Anything(Real setPos)
{
Real posCic = modCic(iniPos + setPos, crdSet);
setInp[posCic]
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los primeros numEle elementos.
Si el conjunto tiene menos de numEle elementos los extrae por el principio.
Por ejemplo: SetSubCicle([a,b,c,d],3,8) -> [c,d,a,b,c,d,a,b]",
SetSubCicle);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Anything SetGetRand(Set setInp)
//////////////////////////////////////////////////////////////////////////////
{
Real setCrd = Card(setInp);
If(LE(setCrd, 0), FALSE,
{
Real rndPos = Min(setCrd, Max(1, Round(Rand(0, setCrd)+0.5)));
Anything rndVal = setInp[rndPos];
rndVal
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un elemento al azar del conjunto de entrada setInp.
Si setInp es Empty retorna FALSE.",
SetGetRand);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FILE : set.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : Set functions. This code assumes the following behaviour of:
// The function BinGroup():
// _
// Text WriteLn("<"+BinGroup("+",Empty)+">" -> <>
// _
// Text WriteLn("<"+BinGroup("+",[["a"]])+">"); -> <a>
// _
// Text WriteLn("<"+BinGroup("+",[["a","b"]])+">"); -> <ab>
// _
// Text WriteLn("<"+BinGroup("+",[["a","a"]])+">"); -> <aa>
// _
// Text WriteLn("<"+BinGroup("+",[["a","b","a"]])+">"); -> <aba>
// _
// The function SetSum():
// _
// Text WriteLn("<"+SetSum(Empty)+">"); -> <>
// _
// Text WriteLn("<"+SetSum([["a"]])+">"); -> <a>
// _
// Text WriteLn("<"+SetSum([["a","b"]])+">"); -> <ab>
// _
// Text WriteLn("<"+SetSum([["a","a"]])+">"); -> <aa>
// _
// Text WriteLn("<"+SetSum([["a","b","a"]])+">"); -> <aba>
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2Txt(Set valSet, // Set of elements
Text iniTxt, // Initial text for list
Text endTxt, // End text for list
Text sepTxt, // Element separators
Text sepLst, // 2 last elements separator
Text txtDet, // Delimiter for texts
Text datFmt, // Format for real numbers
Text dteDet, // Delimiter for dates
Text dteFmt) // Format for dates
//////////////////////////////////////////////////////////////////////////////
{
Real card = Card(valSet);
Text body =
If(EQ(card,0), "",
If(EQ(card,1), F(valSet[1]),
If(EQ(card,2), F(valSet[1])+sepLst+F(valSet[2]),
{
Set txtVal = For(2,card,Text(Real p)
{
If(EQ(p,card),sepLst,sepTxt) + F(valSet[p])
});
F(valSet[1]) + BinGroup("+",txtVal)
})));
iniTxt+body+endTxt
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a text like a list with all the elements of valSet converted in a
text format (elements types: Text, Real or Date).
Arguments:
- iniTxt initial text, for example: '(', '[[', '', etc.
- endTxt end text, for example ')', ']]', '', etc.
- sepTxt elements separator, for example '; ', ',', '', etc.
- sepLst two last elements separator, for example, ' & ', ' and ', etc.,
you can specify the same as sepTxt
- txtDet text delimiters, for exmple, quotes for TOL, single quote for
SQL, nothing, etc.
- datFmt real numbers format, for explame, '%.0lf' for integers, if none
then uses the default TOL real number format.
- dteDet date delimiters, for example, simgle quote for SQL.
- dteFmt date format, for example, '%c%Y%m%d', if none
then uses the default TOL dates format.
Only works with TOL types Text, Real or Date, when find a Set type then
works in a recursive way with the same arguments.",
Set2Txt);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtKeyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set cmmWrd =
[[
"además",
"ambos", "ambas",
"aunque",
"cualquier", "cualquiera",
"cuando",
"deberá",
"desde",
"dentro",
"entre",
"esta", "estas", "esto", "estos",
"junto",
"mismo", "mismos", "misma", "mismas",
"mucho", "muchos", "mucha", "muchas",
"ningún", "ninguna",
"nosotros", "nosotras",
"nuestro", "nuestros", "nuestra", "nuestras",
"otra", "otras", "otro", "otros",
"parte",
"porque",
"puede", "pueden", "pudiendo",
"quienes",
"siempre",
"siguiente", "siguientes",
"sobre",
"sumamente",
"también",
"tanto",
"través",
"zulú"
]];
// Convert all to text
Text lstTxt = ToLower(Set2Txt(valSet,"", "", " ", " ", "", "", "", ""));
Text htmNot = TxtOutHtmScr(lstTxt);
Text lstCls = Compact(ReplaceTable(htmNot,
[[ [[Char(34), " "]], [["'", " "]],
[["." , " "]], [[":", " "]],
[[";", " "]], [[",", " "]],
[["[", " "]], [["]", " "]],
[["(", " "]], [[")", " "]],
/* [["<", " "]], [[">", " "]], Sobra */
[["«", " "]], [["»", " "]], // Titulos de libros
[["¡", " "]], [["!", " "]],
[["¿", " "]], [["?", " "]],
[["/", " "]],
[["&", " "]],
[[" ", " "]],
[[" none ", " "]],
[[" the ", " "]],
[[" and ", " "]],
[[" or ", " "]],
[[" & ", " "]],
[[" y ", " "]],
[[" o ", " "]]
]]));
// Split in words
Set setTxt = Tokenizer(lstCls, " ");
// Select word with more than minChr characters
Set setSel = Select(setTxt, Real(Text a) { GT(TextLength(a), minChr) });
// Classify words
Set setCla = Classify(setSel, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) });
// Make a frecuencies table [ word, number of occurrencies ]
Set tabFrq = EvalSet(setCla, Set(Set cla)
{ [[ cla[1], Card(cla) ]] });
// Sort (with most ocurrencies first)
Set srtFrq = Sort(tabFrq, Real(Set a, Set b)
{ Compare(Real(b[2]), Real(a[2])) });
// Project by word column (the first column)
Set keySet = EvalSet(srtFrq, Text(Set a) { Text(a[1]) });
// Minus common words
Set keyMin = keySet - cmmWrd;
// Select the first maxKey or Card(keySet) or all if maxKey=0
Set keyFst = If(maxKey, SetFirstN(keyMin, maxKey), keyMin);
// Sort if is needed
Set keySor = If(! a2zOrd, keyFst,
Sort(keyFst, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) })); // Order
// Convert to text
Set2Txt(keySor, "", "", ", ", ", ", "", "", "", "")
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a text list with all the elements of valSet converted in a
text format with commas like a keywords list, ordered and without repetitions.
Remove all word with LE(TextLength(), minChr).
Returns the maxKey elements more occurrencies.",
Set2TxtKeyword);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetFirstN(Set setInp, // Set de entrada
Real numEle) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
If(LE(numEle, 0), Empty,
For(1, Min(Card(setInp), numEle), Anything(Real setPos)
{ setInp[setPos] }))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los primeros numEle elementos.
Si el conjunto tiene menos de numEle elementos los retorna todos.
Si numEle es 0 o negativo retorna el conjunto vacio.",
SetFirstN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetLastN(Set setInp, // Set de entrada
Real numEle) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
If(LE(numEle, 0), Empty,
For(Max(1, 1+Card(setInp)-numEle), Card(setInp), Anything(Real setPos)
{ setInp[setPos] }))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los ultimos numEle elementos.
Si el conjunto tiene menos de numEle elementos los retorna todos.",
SetLastN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set SetSubCicle(Set setInp, // Conjunto de elementos
Real iniPos, // Posicion inicial
Real lenRet) // Numero de elementos a retornar
//////////////////////////////////////////////////////////////////////////////
{
Real modCic(Real setPos, Real crdSet)
{
Real modPos = setPos % crdSet;
If(LE(modPos, 0), crdSet, modPos)
};
Real crdSet = Card(setInp);
For(0, lenRet-1, Anything(Real setPos)
{
Real posCic = modCic(iniPos + setPos, crdSet);
setInp[posCic]
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subconjunto de un conjunto con los primeros numEle elementos.
Si el conjunto tiene menos de numEle elementos los extrae por el principio.
Por ejemplo: SetSubCicle([a,b,c,d],3,8) -> [c,d,a,b,c,d,a,b]",
SetSubCicle);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Anything SetGetRand(Set setInp)
//////////////////////////////////////////////////////////////////////////////
{
Real setCrd = Card(setInp);
If(LE(setCrd, 0), FALSE,
{
Real rndPos = Min(setCrd, Max(1, Round(Rand(0, setCrd)+0.5)));
Anything rndVal = setInp[rndPos];
rndVal
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un elemento al azar del conjunto de entrada setInp.
Si setInp es Empty retorna FALSE.",
SetGetRand);
//////////////////////////////////////////////////////////////////////////////
con-Q.tv construye las páginas del sitio y newsletter Con-Q.tv
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio