\documentclass{article} \usepackage{/home/jzuidema/latex/Styles/jelle, multicol,verbatim,fullpage,graphicx,subfigure} \title{Cognition, Language \& Communication} \author{Tutorial II (Computer lab) \& Assignment II} \date{(13-9-2012)} \newtheorem{question}{Question} \begin{document} \maketitle \begin{abstract} The goals of today's computer lab are (i) to introduce you to Processing, and (ii) to sharpen your intuitions on what certain simple grammar formalisms can and cannot do. Processing is a user-friendly programming environment, based on the popular Java programming language, but providing some simple to use tools to make interactive, graphical applications that can easily be put on the web (applets). In recent years, Processing is increasingly used as a platform for webexperiments, and webexperiments have become a popular way of quickly and cheaply doing pilot experiments. To learn how to work with Processing, you will first play with an existing program (``engineered'' by someone else). Next, you will try to ``reverse-engineer'' it: to infer from a working system how it could have been designed. You can then compare your guesses with the actual program. Finally, you will take an existing program and, depending on your programming skills, adapt it a little or a lot and thus create a new (web)experiment. \end{abstract} \section{Processing} \label{sec:download-processing} Download Processing from \verb+processing.org+. Save the zip-file on the Desktop and unpack it. Downloading might take a while, so continue with (2). \paragraph{SenGen} \label{sec:sengen} In a browser, go to \verb+http://www.illc.uva.nl/laco/clas/clc12/sengen/applet/+ . This will start the sengen-applet, a very simple program that generates a sentence every time you click with the mouse or press a key. \begin{question} Reverse-engineer (without looking at the source code) the steps that this computer program must go through. In particular, what are the grammar rules that the program is using? \end{question} \begin{enumerate} \item Generate an orange screen of 400x400 pixels. \item ... \item \item \item \item \item \item When asked to generate a noun, randomly add 'cat' or 'dog' to the current sentence. \end{enumerate} \begin{question} Below is the source code for the actual program. Indicate with numbers where you find the steps you identified in question (1). \end{question} \begin{verbatim} // Global variables - variables that Processing knows about // throughout the program, and their initial values int X=5; // the x-coordinate of the next sentence to be shown int Y=20; // the y-coordinate of the next sentence to be shown String nextsentence; // the actual next sentence to be shown boolean something_happened = false; // a variable set to 'true' or 'false' specifying // whether you have just clicked the mouse or hit a key. // setup() - this is the function that Processing calls when you start the program void setup() { size(400, 400); background(192, 64, 0); stroke(255); nextsentence = new String(""); } // mousePressed() and keyPressed() - these are the functions Processing calls // when you click the mouse or hit a key. void mousePressed() { something_happened=true; } void keyPressed() { something_happened=true; } // draw() - this is the function that Processing calls every few milliseconds. // In our case, it only does anything when something_happened==true. void draw() { if (something_happened) { something_happened = false; generateS(); text(nextsentence,X,Y); X=5; Y+=20; if (Y>380) {Y=20; background(192, 64, 0);} nextsentence = new String(""); } } // generateX() - these are the function for generating noun phrases, determiners, nouns, // (in)transitive or intentional verbs, verb phrases and sentences. void generateNP() { int r = int(random(5)); if (r==0) { nextsentence = nextsentence.concat("John "); } if (r==1) { nextsentence = nextsentence.concat("Mary "); } if (r==2) { nextsentence = nextsentence.concat("Pete "); } if (r==3) { nextsentence = nextsentence.concat("Lisa "); } if (r==4) { generateDET(); generateN(); } } void generateDET() { int r = int(random(2)); if (r==0) { nextsentence = nextsentence.concat("a "); } if (r==1) { nextsentence = nextsentence.concat("the "); } } void generateN() { int r = int(random(2)); if (r==0) { nextsentence = nextsentence.concat("cat "); } if (r==1) { nextsentence = nextsentence.concat("dog "); } } void generateTV() { int r = int(random(2)); if (r==0) { nextsentence = nextsentence.concat("loves "); } if (r==1) { nextsentence = nextsentence.concat("hates "); } } void generateIV() { int r = int(random(2)); if (r==0) { nextsentence = nextsentence.concat("walks "); } if (r==1) { nextsentence = nextsentence.concat("sits "); } } void generateEV() { int r = int(random(2)); if (r==0) { nextsentence = nextsentence.concat("says "); } if (r==1) { nextsentence = nextsentence.concat("thinks "); } } void generateVP() { int r = int(random(3)); if (r==0) { generateIV(); } if (r==1) { generateTV(); generateNP(); } if (r==2) { generateEV(); generateS(); } } void generateS() { generateNP(); generateVP(); } \end{verbatim} If you don't know any programming language related to java (like C, C++, matlab etc), pay some special attention to the following features (and ask fellow students, the lecturer or assistant to make sure you understand it): \begin{itemize*} \item At the beginning of the program, \emph{variables} are ``declared'' and initialized. E.g., the program must specify that \verb+x+ is a variable that stands for a 'whole' number (integer) like 1,2,5, 1000. \item The rest of the program consists of \emph{functions} that at some point are executed (they are ``called''). In Processing, some functions are automatically called, e.g. \verb+setup()+ is called when the program starts. For other functions, the program has to contain explicit instructions for when they are called (inside other functions). Note that functions are defined in a format \verb+void FUNCTIONNAME (ARGUMENTS) {...}+. Between the curly brackets we then see particular instructions. \item Note that instructions always end with a semicolon (;). Instructions might involve, e.g., setting a variable to a specific value as in \verb+x=5;+ or calling a function, as in \verb+generateS();+. \item Further note that many instructions are only carried out if a particular condition is met. \\ \verb+if (CONDITION) {...}+ means ``carry out the instructions between the curly brackets if the specified CONDITION is met. \end{itemize*} \paragraph{Start programming} %\label{sec:start-programming} Start the \verb+processing.exe+ program that you have downloaded and unpacked. In the menu under File/Examples you can find many examples. Load 1 or 2 of those (for instance, the first two from the Learn Processing book), and run them by clicking on the play button on the top left of the Processing screen. \begin{question} (\emph{Move to question 6 if you already know how to program and know some formal language theory}). Adapt the program seen in question 2 to generate sentences from the languages $(ab)^n$ and $a^nb^n$. In order to this, write down the grammar rules that you need to generate those language, and test them in the computer program. \end{question} \paragraph{Pyramid} %\label{sec:pyramid} Finally, in a browser, run the pyramid-applet:\\ \verb+http://www.illc.uva.nl/laco/clas/clc12/pyramid/applet/+. Try to solve 5 puzzles. Look through the source and discuss the various components with fellow students to understand, roughly, how it works. \section{Homework assignment (due Monday 17/9, 13h)} \label{sec:homework-assignment} Install Processing, download the pyramid source at\\ \verb+http://www.illc.uva.nl/laco/clas/clc12/pyramid.zip+, unpack and save it to your Processing sketchbook folder. \begin{question} Formulate an hypothesis on the meetings and readings of this week. \end{question} Choose one of the questions 5 or 6. Submit an overview only of the changes. \begin{question} Adapt the lines in the code such that the program uses rules from the grammar you designed for $(ab)^n$ and $a^nb^n$, and target strings from those languages. \end{question} (\emph{for experienced programmers only)} \begin{question} Think of an improvement of the pyramid program, and implement it. For instance, (i) remove the restriction that the right-hand sides of all rules must contain exactly 2 symbols or (ii) allow the players of the game to reverse a decision they have made. \end{question} \begin{question} Some researchers have criticized Chomsky's theories for being unfalsifiable\footnote{Note: falsifiability is a concept from philosophy of science, and generally seen as a requirement for scientific theories; a theory is \emph{falsifiable} if it is possible, in principle, to disprove the theory. That something is falsifiable does not mean it is false; rather, that if it is false, then this can be shown by observation or experiment. }. Do you agree? Give arguments for your position (maximum 1/2 page). \end{question} \end{document}