145
Perl for Linguists Michael Hammond University of Arizona Perl for Linguists – p.1/38

Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for Linguists

Michael Hammond

University of Arizona

Perl for Linguists – p.1/38

Page 2: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for linguists

Perl for Linguists – p.2/38

Page 3: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for linguists

� Why programming?

Perl for Linguists – p.2/38

Page 4: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for linguists

� Why programming? Why Perl?

Perl for Linguists – p.2/38

Page 5: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for linguists

� Why programming? Why Perl?

� Learn a little Perl.

Perl for Linguists – p.2/38

Page 6: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl for linguists

� Why programming? Why Perl?

� Learn a little Perl.

� More advanced Perl. . .

Perl for Linguists – p.2/38

Page 7: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why programming?

Perl for Linguists – p.3/38

Page 8: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why programming?

� Collect data

Perl for Linguists – p.3/38

Page 9: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why programming?

� Collect data

� Analyze data

Perl for Linguists – p.3/38

Page 10: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why programming?

� Collect data

� Analyze data

� Model theory

Perl for Linguists – p.3/38

Page 11: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why programming?

� Collect data

� Analyze data

� Model theory

� General professional skills

Perl for Linguists – p.3/38

Page 12: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Code for this tutorial

All the code for this presentation is available overthe web at the following URL:

http://linguistics.arizona.edu/~hammond/berkeley.html

Perl for Linguists – p.4/38

Page 13: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Unzipping the programs

1. Download the file programfiles.zip byright-clicking on the link, selecting ‘save targetas’, and selecting your desktop as thedestination.

2. Unzip the file by double-clicking on thedownloaded file on your desktop and movingall the files there to a new directory on thedesktop.

3. Open the MS-DOS prompt in the newdirectory by double-clicking on the filedoswindow.bat.

Perl for Linguists – p.5/38

Page 14: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

Perl for Linguists – p.6/38

Page 15: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

� Running experiments locally (expprog.pl)

Perl for Linguists – p.6/38

Page 16: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

Running experiments locally (expprog.pl)

Running experiments locally with a GUI(tkexp.pl)

Perl for Linguists – p.6/38

Page 17: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

Running experiments locally (expprog.pl)

Running experiments locally with a GUI(tkexp.pl)

Running experiments remotely(Bailey & Hahn replication, bhrep.cgi)

Perl for Linguists – p.6/38

Page 18: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

� Running experiments locally (expprog.pl)

� Running experiments locally with a GUI(tkexp.pl)

� Running experiments remotely(Bailey & Hahn replication, bhrep.cgi)

� Assembling corpora from local staticresources (makecorpus.pl)

Perl for Linguists – p.6/38

Page 19: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Collecting data

� Running experiments locally (expprog.pl)

� Running experiments locally with a GUI(tkexp.pl)

� Running experiments remotely(Bailey & Hahn replication, bhrep.cgi)

� Assembling corpora from local staticresources (makecorpus.pl)

� Assembling corpora from nonlocal dynamicresources: the web (websearch.pl)

Perl for Linguists – p.6/38

Page 20: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Analyzing data

Perl for Linguists – p.7/38

Page 21: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Analyzing data

Looking for patterns (visgrep.pl)

Perl for Linguists – p.7/38

Page 22: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Analyzing data

� Looking for patterns (visgrep.pl)

� Counting things (neightk.pl)

Perl for Linguists – p.7/38

Page 23: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Analyzing data

� Looking for patterns (visgrep.pl)

� Counting things (neightk.pl)

� Finding verbs (verbs.pl)

Perl for Linguists – p.7/38

Page 25: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling theory

� Optimality Theory (web interface,sylpars.pl)

Perl for Linguists – p.8/38

Page 26: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling theory

� Optimality Theory (web interface,sylpars.pl)

� N-gram models (a bunch of examples from acourse on Statistical NLP that I did recently)

Perl for Linguists – p.8/38

Page 27: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

General professional skills

Perl for Linguists – p.9/38

Page 28: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

General professional skills

� General programming skills

Perl for Linguists – p.9/38

Page 29: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

General professional skills

� General programming skills

� Web programming

Perl for Linguists – p.9/38

Page 34: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why Perl?

� Free

� Multi-platform

� Easy

� Multiple dialects

Perl for Linguists – p.10/38

Page 35: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why Perl?

� Free

� Multi-platform

� Easy

� Multiple dialects

� Powerful regular expression tools

Perl for Linguists – p.10/38

Page 36: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why Perl?

� Free

� Multi-platform

� Easy

� Multiple dialects

� Powerful regular expression tools

� Written by a “linguist”

Perl for Linguists – p.10/38

Page 37: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why Perl?

� Free

� Multi-platform

� Easy

� Multiple dialects

� Powerful regular expression tools

� Written by a “linguist”

� Perl poetry

Perl for Linguists – p.10/38

Page 38: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Why Perl?

� Free

� Multi-platform

� Easy

� Multiple dialects

� Powerful regular expression tools

� Written by a “linguist”

� Perl poetry

� Obfuscated perl, “japhs”, etc.

Perl for Linguists – p.10/38

Page 39: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

Perl for Linguists – p.11/38

Page 40: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

� Windows basics

Perl for Linguists – p.11/38

Page 41: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

� Windows basics

� Perl syntax

Perl for Linguists – p.11/38

Page 42: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

� Windows basics

� Perl syntax

� IO

Perl for Linguists – p.11/38

Page 43: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

� Windows basics

� Perl syntax

� IO

� Regular expressions

Perl for Linguists – p.11/38

Page 44: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Learn a little perl. . .

Windows basics

Perl syntax

IO

Regular expressions

Where to find out more

Perl for Linguists – p.11/38

Page 45: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Windows basics

Perl for Linguists – p.12/38

Page 46: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Windows basics

! It is easiest to invoke perl programs from theDOS prompt. (If configured properly, you canjust double-click them though.)

Perl for Linguists – p.12/38

Page 47: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Windows basics

" It is easiest to invoke perl programs from theDOS prompt. (If configured properly, you canjust double-click them though.)

" perl myprogram.pl: invokes the perlinterpreter with a program myprogram.pl.

Perl for Linguists – p.12/38

Page 48: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Windows basics

# It is easiest to invoke perl programs from theDOS prompt. (If configured properly, you canjust double-click them though.)

# perl myprogram.pl: invokes the perlinterpreter with a program myprogram.pl.

# ctrl-c: stops the current program

Perl for Linguists – p.12/38

Page 49: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

A goal

$ We will build our efforts around an exampleprogram: a program that parses a text file inEnglish into sentences and then tries to findall the verbs (verbs.pl).

$ This exemplifies simple data and controlstructures and what we might call“computational linguistic reasoning”.

$ We won’t get to the full version of thisprogram—it’s infinitely expandableactually—but we can get a good start.

Perl for Linguists – p.13/38

Page 50: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Programming overview

Perl for Linguists – p.14/38

Page 51: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Programming overview

% Implement some idea as perl code (write theprogram).

Perl for Linguists – p.14/38

Page 52: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Programming overview

& Implement some idea as perl code (write theprogram).

& Convert code into something the computercan execute (run perl on your code).

Perl for Linguists – p.14/38

Page 53: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Programming overview

' Implement some idea as perl code (write theprogram).

' Convert code into something the computercan execute (run perl on your code).

' Program execution (run perl on your code).

Perl for Linguists – p.14/38

Page 54: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Programming overview

( Implement some idea as perl code (write theprogram).

( Convert code into something the computercan execute (run perl on your code).

( Program execution (run perl on your code).

( Results (what happens as a consequence).

Perl for Linguists – p.14/38

Page 55: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl syntax

Perl for Linguists – p.15/38

Page 56: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl syntax

) A perl program is a series of statements.

Perl for Linguists – p.15/38

Page 57: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl syntax

* A perl program is a series of statements.

* Statements can be organized into groups.

Perl for Linguists – p.15/38

Page 58: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl syntax

+ A perl program is a series of statements.

+ Statements can be organized into groups.

+ Statements are operations on some bit ofdata, e.g. “print this string”, “add thesenumbers”, etc.

Perl for Linguists – p.15/38

Page 59: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Perl syntax

, A perl program is a series of statements.

, Statements can be organized into groups.

, Statements are operations on some bit ofdata, e.g. “print this string”, “add thesenumbers”, etc.

, Groups of statements can apply:1. only when specific conditions hold, or2. more than once, etc.

Perl for Linguists – p.15/38

Page 60: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Statements

Perl for Linguists – p.16/38

Page 61: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Statements

- A statement includes some operation(typically marked with following parentheses),ends with a semicolon, and may containmore. For example:

Perl for Linguists – p.16/38

Page 62: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Statements

. A statement includes some operation(typically marked with following parentheses),ends with a semicolon, and may containmore. For example:

. print("Hello!"); (prints the string“Hello!”)

Perl for Linguists – p.16/38

Page 63: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Statements

/ A statement includes some operation(typically marked with following parentheses),ends with a semicolon, and may containmore. For example:

/ print("Hello!"); (prints the string“Hello!”)

/ rand(5); (gets a random number between 0and 5)

Perl for Linguists – p.16/38

Page 64: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Statements

0 A statement includes some operation(typically marked with following parentheses),ends with a semicolon, and may containmore. For example:

0 print("Hello!"); (prints the string“Hello!”)

0 rand(5); (gets a random number between 0and 5)

0 localtime(); (gets the current time)

Perl for Linguists – p.16/38

Page 65: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

Perl for Linguists – p.17/38

Page 66: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

1. Open the DOS window (through the startmenu or by clicking on the doswindow.baticon.)

Perl for Linguists – p.17/38

Page 67: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

1. Open the DOS window (through the startmenu or by clicking on the doswindow.baticon.)

2. Type edit myprogram.pl

Perl for Linguists – p.17/38

Page 68: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

1. Open the DOS window (through the startmenu or by clicking on the doswindow.baticon.)

2. Type edit myprogram.pl

3. Type print("I am a linguist!");

Perl for Linguists – p.17/38

Page 69: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

1. Open the DOS window (through the startmenu or by clicking on the doswindow.baticon.)

2. Type edit myprogram.pl

3. Type print("I am a linguist!");

4. Select Save and then Exit from the Filemenu.

Perl for Linguists – p.17/38

Page 70: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Try it

1. Open the DOS window (through the startmenu or by clicking on the doswindow.baticon.)

2. Type edit myprogram.pl

3. Type print("I am a linguist!");

4. Select Save and then Exit from the Filemenu.

5. Type perl myprogram.pl at the prompt.

Perl for Linguists – p.17/38

Page 71: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

Perl for Linguists – p.18/38

Page 72: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

1 Notice how rand() and localtime() don’tseem to do anything.

Perl for Linguists – p.18/38

Page 73: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

2 Notice how rand() and localtime() don’tseem to do anything.

2 What they do is return a string. You can savethat result and then print it:

Perl for Linguists – p.18/38

Page 74: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

3 Notice how rand() and localtime() don’tseem to do anything.

3 What they do is return a string. You can savethat result and then print it:

$myvariable = localtime();print($myvariable);

Perl for Linguists – p.18/38

Page 75: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

Perl for Linguists – p.19/38

Page 76: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

4 Simple “scalar” variables: $myvar,$aVariable, $x.

Perl for Linguists – p.19/38

Page 77: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

5 Simple “scalar” variables: $myvar,$aVariable, $x.

5 A value is assigned to a variable with theassignment operator =.

Perl for Linguists – p.19/38

Page 78: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Variables

6 Simple “scalar” variables: $myvar,$aVariable, $x.

6 A value is assigned to a variable with theassignment operator =.

6 Variables can hold numbers, strings, etc.

Perl for Linguists – p.19/38

Page 79: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Reading a file

Perl for Linguists – p.20/38

Page 80: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Reading a file

7 First use open() to open a file and associateit with a handle, e.g.open(F, "myfile.txt");.

Perl for Linguists – p.20/38

Page 81: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Reading a file

8 First use open() to open a file and associateit with a handle, e.g.open(F, "myfile.txt");.

8 Read a line from the file with record-readingoperator: 9

F

: .

Perl for Linguists – p.20/38

Page 82: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Reading a file

; First use open() to open a file and associateit with a handle, e.g.open(F, "myfile.txt");.

; Read a line from the file with record-readingoperator: <

F

= .

; When you’re done, close the handle:close(F);.

Perl for Linguists – p.20/38

Page 83: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Sample code

open(F,"myfile.txt");$line = <F>;print($line);close(F);

Perl for Linguists – p.21/38

Page 84: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Control structures

> if: conditional application

> while: iteration as long as some condition istrue

> for: iteration for a specific number of times

> foreach: iteration for every member of a list

Perl for Linguists – p.22/38

Page 85: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

More sample code

open(F,"myfile.txt");while ($line = <F>) {print($line);

}close(F);

Perl for Linguists – p.23/38

Page 86: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Arrays

Perl for Linguists – p.24/38

Page 87: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Arrays

? Array variables: a list of variables groupedtogether: @myarray, @thisBigArray,@them.

Perl for Linguists – p.24/38

Page 88: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Arrays

@ Array variables: a list of variables groupedtogether: @myarray, @thisBigArray,@them.

@ Adding an element to the end of an array:push(@myarray, "hat");.

Perl for Linguists – p.24/38

Page 89: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Arrays

A Array variables: a list of variables groupedtogether: @myarray, @thisBigArray,@them.

A Adding an element to the end of an array:push(@myarray, "hat");.

A Retrieving an element from the end of anarray: $it = pop(@myarray);.

Perl for Linguists – p.24/38

Page 90: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Arrays

B Array variables: a list of variables groupedtogether: @myarray, @thisBigArray,@them.

B Adding an element to the end of an array:push(@myarray, "hat");.

B Retrieving an element from the end of anarray: $it = pop(@myarray);.

B Retrieving a specific element:print($myarray[4]); (array indices startat 0).

Perl for Linguists – p.24/38

Page 91: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Sample code again

open(F,"myfile.txt");while ($line = <F>) {push(@mylines, $line);

}close(F);foreach $theline (@mylines) {print($theline);

}

Perl for Linguists – p.25/38

Page 92: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Regular expressions

Perl for Linguists – p.26/38

Page 93: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Regular expressions

C “Regular expression”: a restrictive way ofindicating string patterns.

Perl for Linguists – p.26/38

Page 94: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Regular expressions

D “Regular expression”: a restrictive way ofindicating string patterns.

D $myvar =~ /regexp/: does $myvarcontain the regular expression?

Perl for Linguists – p.26/38

Page 95: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Regular expressions

E “Regular expression”: a restrictive way ofindicating string patterns.

E $myvar =~ /regexp/: does $myvarcontain the regular expression?

E $myvar =~ s/regexp/string/: if$myvar contains the regular expression,replace it with the string.

Perl for Linguists – p.26/38

Page 96: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

The verb-finding program

Perl for Linguists – p.27/38

Page 97: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

The verb-finding program

F Recall that the program breaks an Englishtext file into sentences and does its best atfinding all the verbs.

Perl for Linguists – p.27/38

Page 98: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

The verb-finding program

G Recall that the program breaks an Englishtext file into sentences and does its best atfinding all the verbs.

G An hour is not enough time to figure out howto do all of this, but just the bits we’ve coveredare a lot of it (verbs.pl).

Perl for Linguists – p.27/38

Page 99: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Where to find out more

Perl for Linguists – p.28/38

Page 100: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Where to find out more

H The free ActiveState Perl we’re using comeswith extensive web-based documentation.

Perl for Linguists – p.28/38

Page 101: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Where to find out more

I The free ActiveState Perl we’re using comeswith extensive web-based documentation.

I In any perl implementation the perldoccommand can be used to find out lots andlots of stuff.

Perl for Linguists – p.28/38

Page 102: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Where to find out more

J The free ActiveState Perl we’re using comeswith extensive web-based documentation.

J In any perl implementation the perldoccommand can be used to find out lots andlots of stuff.

J The official and best perl website iswww.cpan.org, but see also www.perl.org.

Perl for Linguists – p.28/38

Page 103: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced stuff

Perl for Linguists – p.29/38

Page 104: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced stuff

K Let’s now do some more advanced stuff.

Perl for Linguists – p.29/38

Page 105: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced stuff

L Let’s now do some more advanced stuff.

L ‘Advanced’ in terms of perl.

Perl for Linguists – p.29/38

Page 106: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced stuff

M Let’s now do some more advanced stuff.

M ‘Advanced’ in terms of perl.

M ‘Advanced’ in terms of linguistics.

Perl for Linguists – p.29/38

Page 107: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced perl

Perl for Linguists – p.30/38

Page 108: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced perl

N Object-oriented programming and public perlmodules

Perl for Linguists – p.30/38

Page 109: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced perl

O Object-oriented programming and public perlmodules

O Tk (editperl.pl, visgrep.pl)

Perl for Linguists – p.30/38

Page 110: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Advanced perl

P Object-oriented programming and public perlmodules

P Tk (editperl.pl, visgrep.pl)

P Remote computing (bhexp.cgi, dbiex.pl,websearch.pl)

Perl for Linguists – p.30/38

Page 111: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Object-oriented programming

Perl for Linguists – p.31/38

Page 112: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Object-oriented programming

Q Object-oriented (OO) programming is anotherstyle of programming.

Perl for Linguists – p.31/38

Page 113: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Object-oriented programming

R Object-oriented (OO) programming is anotherstyle of programming.

R OO Programs are a network of things orobjects, not a list of commands.

Perl for Linguists – p.31/38

Page 114: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Object-oriented programming

S Object-oriented (OO) programming is anotherstyle of programming.

S OO Programs are a network of things orobjects, not a list of commands.

S Objects have their own data and specializedfunctions for dealing with their own data.

Perl for Linguists – p.31/38

Page 115: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Object-oriented programming

T Object-oriented (OO) programming is anotherstyle of programming.

T OO Programs are a network of things orobjects, not a list of commands.

T Objects have their own data and specializedfunctions for dealing with their own data.

T Objects can be refer to other objects or inheritthe properties of other objects.

Perl for Linguists – p.31/38

Page 116: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Translating to OO

Perl for Linguists – p.32/38

Page 117: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Translating to OO

U Original verb-finding program: verbs.pl.

Perl for Linguists – p.32/38

Page 118: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Translating to OO

V Original verb-finding program: verbs.pl.

V OO verb-finding program: verbsOO.pl.

Perl for Linguists – p.32/38

Page 119: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

Perl for Linguists – p.33/38

Page 120: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

W OO programs are longer.

Perl for Linguists – p.33/38

Page 121: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

X OO programs are longer.

X OO programs are slower.

Perl for Linguists – p.33/38

Page 122: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

Y OO programs are longer.

Y OO programs are slower.

Y OO programming in perl is not orthodox OO(lots of “oddments”).

Perl for Linguists – p.33/38

Page 123: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

Z OO programs are longer.

Z OO programs are slower.

Z OO programming in perl is not orthodox OO(lots of “oddments”).

Z 00 programming isn’t intuitive for most folks.

Perl for Linguists – p.33/38

Page 124: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Caveats

[ OO programs are longer.

[ OO programs are slower.

[ OO programming in perl is not orthodox OO(lots of “oddments”).

[ 00 programming isn’t intuitive for most folks.

[ Unfortunately, 00 programming is essential forsome modules.

Perl for Linguists – p.33/38

Page 125: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Tk

Perl for Linguists – p.34/38

Page 126: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Tk

\ Tk is an independent language for makinggraphical user interfaces.

Perl for Linguists – p.34/38

Page 127: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Tk

] Tk is an independent language for makinggraphical user interfaces.

] There is a special perl module so that youcan build GUIs indirectly using Tk.

Perl for Linguists – p.34/38

Page 128: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Tk

^ Tk is an independent language for makinggraphical user interfaces.

^ There is a special perl module so that youcan build GUIs indirectly using Tk.

^ This module is widely available for Unix/Linuxand Windows, but not clear if it’s available forMacs.

Perl for Linguists – p.34/38

Page 129: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Tk

_ Tk is an independent language for makinggraphical user interfaces.

_ There is a special perl module so that youcan build GUIs indirectly using Tk.

_ This module is widely available for Unix/Linuxand Windows, but not clear if it’s available forMacs.

_ If you want to write programs in Perl thatreally look like modern computer programs,then you need to use Tk.

Perl for Linguists – p.34/38

Page 130: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

GUI coding

Perl for Linguists – p.35/38

Page 131: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

GUI coding

` Tk programs use OO programming style;buttons, windows, etc. are all “objects”.

Perl for Linguists – p.35/38

Page 132: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

GUI coding

a Tk programs use OO programming style;buttons, windows, etc. are all “objects”.

a You create these objects and then yourprogram waits for the user to interact withthem.

Perl for Linguists – p.35/38

Page 133: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

GUI coding

b Tk programs use OO programming style;buttons, windows, etc. are all “objects”.

b You create these objects and then yourprogram waits for the user to interact withthem.

b makecorpus.pl makecorpusTk.pl

Perl for Linguists – p.35/38

Page 134: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Remote computing

Perl for Linguists – p.36/38

Page 135: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Remote computing

c CGI (“Common Gateway Interface”):programs that run remotely (generatingjavascript and HTML: bhexp.cgi)

Perl for Linguists – p.36/38

Page 136: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Remote computing

d CGI (“Common Gateway Interface”):programs that run remotely (generatingjavascript and HTML: bhexp.cgi)

d Interacting with local or remote databases(generating sql: dbiex.pl)

Perl for Linguists – p.36/38

Page 137: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Remote computing

e CGI (“Common Gateway Interface”):programs that run remotely (generatingjavascript and HTML: bhexp.cgi)

e Interacting with local or remote databases(generating sql: dbiex.pl)

e Interacting with the web generally(websearch.pl)

Perl for Linguists – p.36/38

Page 138: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling

Perl for Linguists – p.37/38

Page 139: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling

f Perl not good for computational clarity; notwell-defined

Perl for Linguists – p.37/38

Page 140: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling

g Perl not good for computational clarity; notwell-defined

g Perl exceptionally good at string processing

Perl for Linguists – p.37/38

Page 141: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

Modeling

h Perl not good for computational clarity; notwell-defined

h Perl exceptionally good at string processing

h Computational tasks as string processing:sylpars.pl

Perl for Linguists – p.37/38

Page 142: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

What now?

Perl for Linguists – p.38/38

Page 143: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

What now?

i Programming and perl hopefullydemystified. . .

Perl for Linguists – p.38/38

Page 144: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

What now?

j Programming and perl hopefullydemystified. . .

j Some ideas about what you can do with it ifyou’re thinking that you need to program.

Perl for Linguists – p.38/38

Page 145: Perl for Linguists - University of ArizonaA goal $ We will build our efforts around an example program: a program that parses a text file in English into sentences and then tries

What now?

k Programming and perl hopefullydemystified. . .

k Some ideas about what you can do with it ifyou’re thinking that you need to program.

k If you already know some perl, perhaps someother ideas about what to do with what youalready know.

Perl for Linguists – p.38/38