007
10.04.2009, 00:18 Uhr
Bruder Leif
dances with systems (Operator)
|
Die Skriptsprachen sind schnell mal eben runtergehauen. Zuerst AWK:
Code: |
BEGIN { w1=w2=""; }
{ for (i=1; i<=NF; i++) { triples[w1,w2,cnt[w1,w2]++] = $i; w1=w2; w2=$i; } }
END { triples[w1,w2] = "."; w1=w2=""; for (len=0; len<1000; len++) { i=triples[w1,w2,int(rand()*cnt[w1,w2])]; w1=w2; w2=i; if (i == ".") break; printf("%s ", i); } }
|
Dann Lua:
Code: |
triples = {} math.randomseed(os.time())
function add(w1, w2, w3) if not triples[w1..","..w2] then triples[w1..","..w2] = {w3} else table.insert(triples[w1..","..w2], w3) end end
w1, w2 = "", ""
for i in io.lines("test.txt") do string.gsub(i, "([^%s]*)%s*", function(c) add(w1, w2, c); w1, w2 = w2, c end) end add(w1, w2, ".")
w1, w2, out = "", "", ""
for len=1, 1000 do i = triples[w1..","..w2] i = i[math.random(#i)] out, w1, w2 = out .. w1 .. " ", w2, i if i == "." then out = out .. w1 break end end
print((string.gsub(out, "^%s*", "")))
|
Perl:
Code: |
while(<>) { for(split) { push @{$triples{$w1}{$w2}}, $_; ($w1, $w2) = ($w2, $_); } } push @{$triples{$w1}{$w2}}, '.';
($w1, $w2) = ('', '');
for $len (1..1000) { @words = @{$triples{$w1}{$w2}}; $i = $words[int(rand($#words+1))]; ($w1, $w2) = ($w2, $i); last if $i eq '.'; print "$i "; } print "\n";
|
-- Mit 40 Fieber sitzt man nicht mehr vor dem PC. Man liegt im Bett. Mit dem Notebook. |