Download presentation
Presentation is loading. Please wait.
Published byValentine Wade Modified over 9 years ago
1
By: Jordan Hofstaedter
2
Make random outputs from inputs that actually make sense. If we let the computer produce random letters in English, chances are it will be incoherent sentences made up of incoherent words xptmxgn xusaja afqnzgxl lhidlwcd rjdjuvpydrlwnjy If we weigh letters by their frequency of appearance in English text, it’s not much better idtefoae tcs trder jcii ofdslnqetacp t ola Finally words chosen at random from a dictionary won’t make much sense either polydactyl equatorial splashily jowl verandah circumscribe The Markov chain algorithm outputs randomly by choosing a suffix that follows the prefix in a coherent input
3
Set W1 and W2 to the first two words in the text Print W1 and W2 Loop Randomly choose W3, one of the successors of prefix W1,W2 in the text Print W3 Replace W1 and W2 by W2 and W3 Repeat loop
4
Suppose we have the text: “Show your flowcharts and conceal your tables and I will be mystified. Show your tables and your flowcharts will be obvious.” (end) Input Prefix:Suffix words that follow: Show yourFlowcharts or tables your flowchartsand or will flowcharts andconceal flowcharts willbe your tablesand or and will bemystified. or obvious. be mystified.Show be obvious.(end)
6
Uses associative and anonymous arrays and string handling Associative arrays are convenient packing of a hash table Subscripts are arbitrary strings or numbers or comma- separated lists of them Anonymous arrays used to keep track of suffixes Used in place of a third subscript Special characters needed: $ for scalar @ for indexed array, use [ ] brackets to index Use { } brackets to index hashes
7
# markov.pl: markov chain algorithm for 2-word prefixes $MAXGEN = 10000; $NONWORD = "\n"; $w1 = $w2 = $NONWORD; # initial state while (<>) { # read each line of input foreach (split) { push(@{$statetab{$w1}{$w2}}, $_); # THIS LINE PUSHES NEW SUFFIX ONTO THE END OF THE ARRAY ($w1, $w2) = ($w2, $_);# multiple assignment } push(@{$statetab{$w1}{$w2}}, $NONWORD); # add tail $w1 = $w2 = $NONWORD; for ($i = 0; $i < $MAXGEN; $i++) { $suf = $statetab{$w1}{$w2};# array reference, $r = int(rand @$suf);# @$suf is number of elems exit if (($t = $suf->[$r]) eq $NONWORD); print "$t\n"; ($w1, $w2) = ($w2, $t);# advance chain }
8
+ Shorter and more concise + Scripting languages are good for experimental programming, prototypes and production if run-time is not a big issue - Harder to handle prefixes that are not exactly two words, such as hyphenated words - Less clearer than in implementations in other languages
9
Kernighan, Brian W., and Rob Pike. The Practice of Programming (Addison-Wesley Professional Computing Series). New York: Addison-Wesley Professional, 1999. Print.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.