Presentation is loading. Please wait.

Presentation is loading. Please wait.

Markov-Chain Algorithm In Perl

Similar presentations


Presentation on theme: "Markov-Chain Algorithm In Perl"— Presentation transcript:

1 Markov-Chain Algorithm In Perl
Robert Brown – 10/21/09

2 Markov-Chain Algorithm
Sting-rewriting system: Uses input to write “random” output Grammar rules from input helps write output An example in Pseudo-Code: Set w1 and w2 to the first two strings in the text loop: w3 is the result of markov(w1, w2); w1 = w2; w2 = w3; Repeat loop

3 Algorithm in Perl, pt.1: #markov.pl: markov chain algorithm for 2-word prefixes #part 1: take input and create data-relationship table $MAXGEN = 10000; $NONWORD = “\n”; $w1 = $w2 = $NONWORD; # initial state while (<>) { # read each line of input foreach (split) { $_); ($w1, $w2) = ($w2, $_); # multiple assignment } $NONWORD); #add tail

4 Explanation of code, pt. 1:
# constants and variables $MAXGEN = 10000; $NONWORD = “\n”; $w1 = $w2 = $NONWORD; $MAXGEN – Maximum number of times function is called (constant) $NONWORD – Used at the beginning of loops and acts as terminating condition (constant) $w1, $w2 are static variables used through loops. Initially defined as $NONWORD

5 Explanation of code, pt. 2:
# read in data while (<>) { foreach (split) { ... } }, $NONWORD); while (<>) reads in the current line of input foreach (split) splits the input line into each individual word foreach() then runs internal code on each word of input while (<>) runs foreach() for every line of input push() explained in next slide

6 Explanation of code, pt. 3:
# internal code of foreach(split) $_); ($w1, $w2) = ($w2, $_); *In previous slide, $NONWORD); inserts $NONWORD as the last item in the $statetab. This is used as a terminating condition in part 2. push() inserts word ($_) from foreach() into $statetab* $w1 and $w2 are used as index for stored value $statetab{$w1}{$w2} is equal an array-- can contain multiple entries During first execution, $statetab is created Reassign $w1 and $w2 $w1 = $w2 $w2 = word

7 Example of $statetab: Text used: “Show your flowcharts and conceal your tables and I will be mystified. Show your tables and your flowcharts will be obvious.” $statetab{\n}{\n} = Show $statetab{\n}{Show} = your $statetab{Show}{your} = [flowcharts, tables] $statetab{your}{flowcharts} = [and, will] $statetab{flowcharts}{and} = conceal $statetab{flowcharts}{will} = be … etc … $statetab{be}{obvious.} = ‘\n’ #Terminating condition

8 Algorithm in Perl , pt.2: #part 2: create output from data table created in part 1 $w1 = $w2 = $NONWORD; for ($i = 0; $i < $MAXGEN; $i++) { $suf = $statetable{$w1}{$w2}; # array reference $r = is number of elems exit if (($t = $suf->[$r]) eq $NONWORD); print “$t\n”; ($w1, $w2) = ($w2, $t); # advance chain }

9 Explanation of code, pt. 4:
# output data $w1 = $w2 = $NONWORD; for ($i = 0; $i < $MAXGEN; $i++){ … } Start at beginning of table $w1 and $w2 becomes ‘\n’ $statetab{\n}{\n} is first item in input $statetab{\n}{first item} is second item in input Loop the output at most $MAXGEN times Loop may exit early if terminating condition is met-- see next slide

10 Explanation of code, pt. 5:
# internal data of for(), pt. 1 $suf = $statetable{$w1}{$w2}; $r = exit if (($t = $suf->[$r]) eq $NONWORD); $suf – reference to $statetable{$w1}{$w2} If only one word, $suf is the word If an array, $suf is the entire array $r - random index of $suf 1 word – always 1 Array – between 1 and size of $suf exit() – Terminate loop $suf->[$r] is the ith item in array $suf Only terminates if $suf->[$r] = ‘\n’

11 Explanation of code, pt. 6:
# internal data of for(), pt. 2 print “$t\n”; ($w1, $w2) = ($w2, $t); Print the value contained in $t $t defined in previous expression Only occurs if loop was not terminated Reassign $w1 and $w2 $w1 = $w2 $w2 = $suf->[$r] Update allows the function to assess new values

12 Example of output, pt. 1: Input: “Now is the time for all good people”
Output: Now NOTE: When there are no is arrays in $statetab, the then the output mimics time the input word for for word all good people

13 Example of output, pt. 1: Input: “Now is the time for all good people”
Output: Now NOTE: When there are no is arrays in $statetab, the then the output mimics time the input word for for word all good people

14 Example of output, pt. 2 Input: “Show your flowcharts and conceal your tables and I will be mystified. Show your tables and your flowcharts will be obvious.” Output: Show NOTE: With a large enough input, your text may not end with last [flowcharts, tables] three words [and, will] … etc … will be obvious

15 Now is the time for all good questions…


Download ppt "Markov-Chain Algorithm In Perl"

Similar presentations


Ads by Google