introduction to computational linguistics programming i

22
Introduction to Computational Linguistics Programming I

Upload: geoffrey-norton

Post on 16-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computational Linguistics Programming I

Introduction to Computational Linguistics

Programming I

Page 2: Introduction to Computational Linguistics Programming I

Resumé Algorithm: A well defined procedure for

the solution of a given problem in a finite number of steps.

Program: A set of instructions, written in a specific programming language, which a computer follows in solving a problem.

Control Structures specify how instructions are followed

Page 3: Introduction to Computational Linguistics Programming I

Examples ofControl Structures Sequence: a series of instructions that

are executed in order. Branch: an instruction involving a

condition that affects the next instruction executed.

Loop: for handling repeated execution. Module: a named collection of

instructions that we write once but use in different contexts

Page 4: Introduction to Computational Linguistics Programming I

Sequence

Page 5: Introduction to Computational Linguistics Programming I

Branch

Page 6: Introduction to Computational Linguistics Programming I

Loop

Page 7: Introduction to Computational Linguistics Programming I

Module

Page 8: Introduction to Computational Linguistics Programming I

Python

Python is a programming language that allows us to write programs. Python programs have to obey certain

syntactic conventions Python is also an interpreter, i.e. a

machine which understands the conventions of the language

Page 9: Introduction to Computational Linguistics Programming I

Python is also an Interpreter that Understands Python Programs

PROGRAMWRITTEN

IN PYTHON

PYTHONINTERPRETER

OUTPUTINPUT

Page 10: Introduction to Computational Linguistics Programming I

Here is the World’s SimplestPython Program

print “Hello World”

When this program is run (interpreted) it causes “Hello World” to be printed on the screen.

Page 11: Introduction to Computational Linguistics Programming I

Demo

Page 12: Introduction to Computational Linguistics Programming I

The print statement

The print statement consists of the word print followed by one or more arguments separated by commas:

>>> print "a", "b", "c"abc

In this case the arguments are strings.

Page 13: Introduction to Computational Linguistics Programming I

Strings

In Python strings are enclosed in either single quotes or double quotes.

'abc' "abc" "my name is" "Mike's name is"

Page 14: Introduction to Computational Linguistics Programming I

Multi-line programs

Here is a program consisting of a sequence of three instructions

print "My name is "print "Mike"print "!"

Page 15: Introduction to Computational Linguistics Programming I

Data Data is the raw material that programs

manipulate. Data comes in different flavours called

types Examples of data types are

integer strings

Each type of data is associated with characteristic operations.

Page 16: Introduction to Computational Linguistics Programming I

Examples of operators

Integers are associated with operators such as +, -, * etc.

Using these operators we can write down complex expressions

e.g. 2+2; 2-2; 2*2

Page 17: Introduction to Computational Linguistics Programming I

Other Arithmetic Operators

Operation Symbol Example

Exponentiation ** 5 ** 2 == 25

Multiplication * 2 * 3 == 6

Division / 14 / 3 == 4

Remainder % 14 % 3 == 2

Addition + 1 + 2 == 3

Subtraction - 4 - 3 == 1

Page 18: Introduction to Computational Linguistics Programming I

Arithmetic expressions a number: 1 a variable: b an expression involving an operator

a + 1 a complex expression

a + 3*2 / 3 to eliminate ambiguity complex

expressions can use parentheses (a + (3*2)) /3

Page 19: Introduction to Computational Linguistics Programming I

Exercise 1.1

Discover the value of the following arithmetic expressions using the python interpreter

1 + 2/3 + 4 (1 + 2) / (3 + 4) 100 % 10 115 % 10

Page 20: Introduction to Computational Linguistics Programming I

Multi-line program using arithmetic expressions

print "2 + 2 is", 2+2

print "3 * 4 is", 3 * 4

print 100 - 1, " = 100 – 1"

print "(33 + 2) / 5 + 11.5 = ",(33 + 2) / 5 + 11.5

What is the output of this program?

Page 21: Introduction to Computational Linguistics Programming I

Python Editor Python allows us to edit, save, check

and run programs. In python shell, go to File menu. Select New Editor window appears Type program into editor window. To check or run program – go to Run

menu and select check or run. You will be prompted to save file. Any filename is OK, but filename.py is best.

Page 22: Introduction to Computational Linguistics Programming I

Exercise 1.2

Use editor to write programs that print your name and date of birth

on same line on separate lines

print the numbers from 0 to 4 Save the work in different files Run the programs