1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#!/opt/local/bin/gawk -f
# Generate a set of classified terms
function genterm(depth, tmp, res)
{
tmp = rand();
if(depth == 0 || tmp > 0.8)
{
tmp = rand();
if(tmp < 0.25)
{
res = "a";
}
else if(tmp < 0.5)
{
res = "b";
}
else if(tmp < 0.75)
{
res = "c";
}
else
{
res = "d";
}
}
else
{
tmp = rand();
if(tmp < 0.25)
{
res = "i(" genterm(depth-1) ")";
}
else if(tmp < 0.5)
{
res = "j(" genterm(depth-1) ")";
}
else if(tmp < 0.75)
{
res = "f(" genterm(depth-1) "," genterm(depth-1) ")";
}
else
{
res = "g(" genterm(depth-1) "," genterm(depth-1) ")";
}
}
return res;
}
function classifyterm(term)
{
if(index(term, "f(i(")==1 || index(term, "g(i(")==1)
# if(index(term, "i"))
{
return 1;
}
return -1;
}
BEGIN{
if(!ARGV[2])
{
print "Usage: classgen.awk <depth> <number>" > "/dev/stderr";
exit 1;
}
depth = ARGV[1];
number = ARGV[2];
ARGV[1] = "";
ARGV[2] = "";
srand();
i=0;
class1=0;
while(i<number)
{
term = genterm(depth);
if(!(term in terms))
{
class = classifyterm(term);
tmpstring = sprintf("%s: 1: (1, %d).", term, class);
terms[term] = tmpstring;
i++;
if(class == 1)
{
class1++;
}
}
}
print "# " i " terms, split = " class1/i > "/dev/stderr";
for(term in terms)
{
print terms[term];
}
}
|