NLP Programming Tutorial 0 – Programming Intro

NLP Programming Tutorial 0 Programming Basics

Graham Neubig
Nara Institute of Science and Technology (NAIST)1NLP Programming Tutorial 0 – Programming Intro

About this Tutorial●14 parts, starting from easier topics●

Each time:
During the tutorial: Learn something new● At home: Do a programming exercise● Next week: Talk about results with your neighbor●●Programming language is your choice
Examples will be in Python, so it is recommended● I can help with Python, C++, Java, Perl●

●Working in pairs is encouraged
2

NLP Programming Tutorial 0 – Programming Intro

Setting Up Your Environment

3

NLP Programming Tutorial 0 – Programming IntroOpen a Terminal●

If you are on Linux or Mac●●

From the program menu select “terminal”

If you are on Windows
Install cygwin● or use “ssh” to log in to a Linux machine●

4

NLP Programming Tutorial 0 – Programming IntroInstall Software (if necessary)●

3 types of software:python: the programming language● a text editor (gvim, emacs, etc.)● git: A version control system●●Linux:●●sudo apt-get install git vim-gnome python

Windows:●

Run cygwin setup.exe, select “git”, “gvim”, and “python”

5

NLP Programming Tutorial 0 – Programming Intro

Download the Tutorial Files from Github●

Use the git “clone” command to download the code

$ git clone https://github.com/neubig/nlptutorial.git●

You should find this PDF in the downloaded directory$ cd nlptutorial$ ls download/00-intro/nlp-programming-en-00-intro.pdf

6

NLP Programming Tutorial 0 – Programming Intro

Using gvim●
●You can use any text editor, but if you are using vim:
If it is your first time, you may want to copy my vim
settings file, which will make vim easier to use:
$ cp misc/vimrc ~/.vimrc●

Open vim:$ gvim test.txt●●

Press “i” to start input and write “test”
Press escape, and type “:wq” to save and quit(“:w” is save, “:q” is quit)

7

NLP Programming Tutorial 0 – Programming Intro

Using git●

You can use git to save your progress●First, add the changed file
$ git add test.txt●

And save your change$ git commit(Enter a message like “added a test file”)●Using git, you can do things like go back to your lastcommit (git reset), download the latest updates (gitpull), or upload code to github (git push)8

NLP Programming Tutorial 0 – Programming Intro

Basic Programming

9

NLP Programming Tutorial 0 – Programming Intro

Hello World!
1)Open my-program.py in an editor (gvim, emacs, gedit)$ gvim my-program.py

2) Type in the following program3) Make the program executable$ chmod 755 my-program.py

4) Run the program$ ./my-program.pyHello World!
10

NLP Programming Tutorial 0 – Programming Intro

Main data types used●

Strings: “hello”, “goodbye”●Integers: -1, 0, 1, 3●

Floats: -4.2, 0.0, 3.14

$ ./my-program.py
string: hello float: 2.500000 int: 4
11

NLP Programming Tutorial 0 – Programming Intro

if/else, for
if this condition is true
then do thisotherwise
do thisfor every element in thisdo this$ ./my-program.py
my_variable is not 4
i == 1i == 2i == 3i == 4Be careful!
12range(1, 5) == (1, 2, 3, 4)

NLP Programming Tutorial 0 – Programming Intro

Storing many pieces of data
Dense Storage

Sparse Storage

Index
0123456Index
49
8196
104

Value
2094
1020
19
3

Value
2094
102

orIndexapple
bananacherry
date

Value
20
94
10213

NLP Programming Tutorial 0 – Programming Intro

Arrays (or “lists” in Python)●Good for dense storage●

Index is an integer, starting at 0Make a list with 5 elements
Add one more element tothe end of the list
Print the length of the list
Print the 4th element
Loop through and print
every element of the list 14

NLP Programming Tutorial 0 – Programming Intro

Maps (or “dictionaries” in Python)●Good for sparse storage:

create pairs of key/value

add a new entry
print size
print one entry
check whether akey exists
print key/value
pairs in order 15

NLP Programming Tutorial 0 – Programming Introdefaultdict●

A useful expansion on dictionary with a default value
import librarydefault value of zeroprint existing key
print non-existent key16

NLP Programming Tutorial 0 – Programming Intro

Splitting and joining strings●In NLP: often split sentences into words
Split string at white space
into an array of words

Combine the array intoa single string, separatingwith “ ||| “$ ./my-program.py...
this ||| is ||| a ||| pen17

NLP Programming Tutorial 0 – Programming Intro

Functions●

Functions take an input, transform the input, andreturn an output
function add_and_abs takes“x” and “y” as input

add x and y together andreturn the absolute value

call add_and_abs with
x=-4 and y=1
18

NLP Programming Tutorial 0 – Programming Intro

Using command line arguments/
Reading files
First argument
Open file for reading with “r”

Read the file one line at a time
Delete the line end symbol “\n”
If the line is not empty, print$ ./my-program.py test.txt

19

NLP Programming Tutorial 0 – Programming Intro

Testing Your Code20

NLP Programming Tutorial 0 – Programming Intro

Simple Input/Output Tests
Example:Program word-count.py should count the words in a file1) Create a small input file2) Count the words by hand, write them in an output filetest-word-count-in.txt

test-word-count-out.txt

abcbcda1b2c2d13) Run the program$ ./word-count.py test-word-count-in.txt > word-count-out.txt4) Compare the results$ diff test-word-count-out.txt word-count-out.txt

21

NLP Programming Tutorial 0 – Programming Intro

Unit Tests●

Write code to test each function●

Test several cases, and print an error if result is wrong●

Return 1 if all tests passed, 0 otherwise

22

NLP Programming Tutorial 0 – Programming Intro

ALWAYS Test your Code●Creating tests:Makes you think about the problem before writing code
● Will reduce your debugging time drastically● Will make your code easier to understand later●23

NLP Programming Tutorial 0 – Programming Intro

Practice Exercise

24

NLP Programming Tutorial 0 – Programming Intro

Practice Exercise●Make a program that counts the frequency of words ina file
this is a pen
this pen is my pen

a1is 2
my 1pen 3
this 2●

Test it on test/00-input.txt, test/00-answer.txt●Run the program on the file data/wiki-en-train.word●Report:The number of unique words● The frequencies of the first few words in the list
●25

NLP Programming Tutorial 0 – Programming Intro

Pseudo-code
create a dictionary counts

create a map to hold counts

open a filefor each line in the filesplit line into words
for w in words
if w exists in counts, add 1 to counts[w]else set counts[w] = 1
print key, value of counts

26