Markov-Chain Algorithm In Perl

Slides:



Advertisements
Similar presentations
Modeling & Simulation. System Models and Simulation Framework for Modeling and Simulation The framework defines the entities and their Relationships that.
Advertisements

Design and Implementation* Objective: To design and implement a program for a relatively small yet reasonably complicated problem. To introduce and review.
Markov Chain Algorithm in Perl Yu-Chung Chau CS 265.
Program Design and Development
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
1 Content-Aware Device Benchmarking Methodology/Terminology (draft-ietf-bmwg-ca-bench-meth-00) BMWG Meeting IETF-82 Taipei November 2011 Mike Hamilton.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
By: Robert Apeldorn.  Generate statistical random texts that can be read well  Allow for the output texts to be created quickly regardless the size.
1 Content-Aware Device Benchmarking Methodology/Terminology (draft-ietf-bmwg-ca-bench-meth-01) BMWG Meeting IETF-83 Paris March 2012 Mike Hamilton
Chapter 9 Control Structures.
By: Jordan Hofstaedter. Make random outputs from inputs that actually make sense. If we let the computer produce random letters in English, chances are.
Markov Chain Algorithm in Perl Michael Conway CS 265 May 4, 2011.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
 Problem Analysis  Coding  Debugging  Testing.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Arrays Chapter 7.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Arrays.
Chapter 9 Repetition.
Test 2 Review Outline.
3.1 Fundamentals of algorithms
Topics Designing a Program Input, Processing, and Output
Understanding Algorithms and Data Structures
REPETITION CONTROL STRUCTURE
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Data Structures and Algorithms
Lecture – 2 on Data structures
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Control Structures II
CS1371 Introduction to Computing for Engineers
User input We’ve seen how to use the standard output buffer
Repetition-Counter control Loop
Algebra 1 Section 1.7 Identify functions and their parts
Java Programming: Guided Learning with Early Objects
Ch 7: JavaScript Control Statements I.
Starter Write a program that asks the user if it is raining today.
Chapter 5 Repetition.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Describing algorithms in pseudo code
Java for Teachers Intermediate
Chapter 9 Control Structures.
Chapter (3) - Looping Questions.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Algorithm Discovery and Design
Control Statements Loops.
Lec 4: while loop and do-while loop
Design and Implementation*
Chapter 6: Repetition Statements
Just Basic Lessons Mr. Kalmes.
3.1 Iteration Loops For … To … Next 18/01/2019.
Topics Designing a Program Input, Processing, and Output
Programs written in C and C++ can run on many different computers
Chapter 5: Control Structures II (Repetition)
Functions continued.
Python Basics with Jupyter Notebook
Programming Logic and Design Fifth Edition, Comprehensive
Topics Designing a Program Input, Processing, and Output
Chapter 4: Repetition Structures: Looping
Control Statements Loops.
Just Basic Lessons Mr. Kalmes.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Looping and Repetition
Presentation transcript:

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

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

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) { push(@{$statetab{$w1}{$w2}}, $_); ($w1, $w2) = ($w2, $_); # multiple assignment } push(@{$statetable{$w1}{$w2}}, $NONWORD); #add tail

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

Explanation of code, pt. 2: # read in data while (<>) { foreach (split) { ... } push(@{$statetable{$w1}{$w2} }, $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

Explanation of code, pt. 3: # internal code of foreach(split) push(@{$statetab{$w1}{$w2}}, $_); ($w1, $w2) = ($w2, $_); *In previous slide, push(@{$statetable{$w1}{$w2}}, $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

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

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 = int(rand @$suf); # @$suf is number of elems exit if (($t = $suf->[$r]) eq $NONWORD); print “$t\n”; ($w1, $w2) = ($w2, $t); # advance chain }

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

Explanation of code, pt. 5: # internal data of for(), pt. 1 $suf = $statetable{$w1}{$w2}; $r = int(rand @$suf); 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’

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

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

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

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

Now is the time for all good questions…