Set functions.
Funciones
Text Set2Txt(Set valSet, Text iniTxt, Text endTxt, Text sepTxt, Text sepLst, Text txtDet, Text datFmt, Text dteDet, Text dteFmt)Set Set2Keyword(Set valSet, Real minChr, Real a2zOrd, Real maxKey)Text Set2TxtKeyword(Set valSet, Real minChr, Real a2zOrd, Real maxKey)Text Set2TxtCommaAmp(Set valSet)Set SetFirstN(Set setInp, Real numEle)Set SetFirstNByFun(Set set, Real num, Code funSor)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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Set2Keyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered az else by ocurrences
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set cmmWrd =
[[
"ambos",
"desde",
"dentro",
"entre",
"esta", "estas", "esto", "estos",
"junto",
"mucho", "muchos",
"otra", "otras", "otro", "otros",
"parte",
"porque",
"puede", "pueden", "pudiendo",
"siguiente", "siguientes",
"sobre",
"sumamente",
"también",
"tanto",
"zulú"
]];
// Convert all to text
Text lstTxt = ToLower(Set2Txt(valSet,"", "", " ", " ", "", "", "", ""));
Text htmNot = TxtOutHtmScr(lstTxt);
Text lstCls = Compact(ReplaceTable(htmNot, // _ must NOT be changed
[[ [[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 that locks like words
Set setSel = Select(setTxt, Real(Text wrdTxt)
{
Real wrdLen = TextLength(wrdTxt);
Text iniLet = Sub(wrdTxt, 1, 1);
Text lstLet = Sub(wrdTxt, wrdLen, wrdLen);
And(GT(wrdLen, minChr), iniLet >= "a", iniLet <= "z",
lstLet >= "a", lstLet <= "z")
});
// 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
If(! a2zOrd, keyFst,
Sort(keyFst, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) })) // Order
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set with all the elements of valSet converted in a
text format, ordered and without repetitions.
Remove all word with LE(TextLength(), minChr).
Returns the maxKey elements more occurrencies.",
Set2Keyword);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtKeyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered az else by ocurrences
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set2Txt(Set2Keyword(valSet, minChr, a2zOrd, maxKey),
"", "", ", ", ", ", "", "", "", "")
};
//////////////////////////////////////////////////////////////////////////////
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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtCommaAmp(Set valSet) // Set of elements
//////////////////////////////////////////////////////////////////////////////
{ Set2Txt(valSet, "", "", ", ", " & ", "", "", "", "") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Return a text list with all the elements of valSet converted in a
text format with commas and & in Html style.
For example Set2TxtCommaAmp([['a','b','c','d']]) returns:
a, b, c & d -> in html -> a, b, c & d.",
Set2TxtCommaAmp);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 SetFirstNByFun(Set set, Real num, Code funSor)
//////////////////////////////////////////////////////////////////////////////
{ SetFirstN(Sort(set, funSor), num) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a subset with the first num elements of a set ordered by funSor.
If the set does not have num elements returns the set ordered by funSor.",
SetFirstNByFun);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// 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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Set2Keyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered az else by ocurrences
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set cmmWrd =
[[
"ambos",
"desde",
"dentro",
"entre",
"esta", "estas", "esto", "estos",
"junto",
"mucho", "muchos",
"otra", "otras", "otro", "otros",
"parte",
"porque",
"puede", "pueden", "pudiendo",
"siguiente", "siguientes",
"sobre",
"sumamente",
"también",
"tanto",
"zulú"
]];
// Convert all to text
Text lstTxt = ToLower(Set2Txt(valSet,"", "", " ", " ", "", "", "", ""));
Text htmNot = TxtOutHtmScr(lstTxt);
Text lstCls = Compact(ReplaceTable(htmNot, // _ must NOT be changed
[[ [[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 that locks like words
Set setSel = Select(setTxt, Real(Text wrdTxt)
{
Real wrdLen = TextLength(wrdTxt);
Text iniLet = Sub(wrdTxt, 1, 1);
Text lstLet = Sub(wrdTxt, wrdLen, wrdLen);
And(GT(wrdLen, minChr), iniLet >= "a", iniLet <= "z",
lstLet >= "a", lstLet <= "z")
});
// 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
If(! a2zOrd, keyFst,
Sort(keyFst, Real(Text a, Text b)
{ Compare(ToLower(a),ToLower(b)) })) // Order
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set with all the elements of valSet converted in a
text format, ordered and without repetitions.
Remove all word with LE(TextLength(), minChr).
Returns the maxKey elements more occurrencies.",
Set2Keyword);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtKeyword(Set valSet, // Set of texts
Real minChr, // Minimum number of chars
Real a2zOrd, // If true ordered az else by ocurrences
Real maxKey) // Maximun number of keywords
//////////////////////////////////////////////////////////////////////////////
{
Set2Txt(Set2Keyword(valSet, minChr, a2zOrd, maxKey),
"", "", ", ", ", ", "", "", "", "")
};
//////////////////////////////////////////////////////////////////////////////
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);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Set2TxtCommaAmp(Set valSet) // Set of elements
//////////////////////////////////////////////////////////////////////////////
{ Set2Txt(valSet, "", "", ", ", " & ", "", "", "", "") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Return a text list with all the elements of valSet converted in a
text format with commas and & in Html style.
For example Set2TxtCommaAmp([['a','b','c','d']]) returns:
a, b, c & d -> in html -> a, b, c & d.",
Set2TxtCommaAmp);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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 SetFirstNByFun(Set set, Real num, Code funSor)
//////////////////////////////////////////////////////////////////////////////
{ SetFirstN(Sort(set, funSor), num) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a subset with the first num elements of a set ordered by funSor.
If the set does not have num elements returns the set ordered by funSor.",
SetFirstNByFun);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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);
//////////////////////////////////////////////////////////////////////////////
iForense construye las paginas del sitio sobre informática forense Forense.Info
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio