Introduction to Computational Linguistics Programming I.
-
Upload
geoffrey-norton -
Category
Documents
-
view
229 -
download
0
Transcript of Introduction to Computational Linguistics Programming I.
![Page 1: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/1.jpg)
Introduction to Computational Linguistics
Programming I
![Page 2: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/4.jpg)
Sequence
![Page 5: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/5.jpg)
Branch
![Page 6: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/6.jpg)
Loop
![Page 7: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/7.jpg)
Module
![Page 8: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/9.jpg)
Python is also an Interpreter that Understands Python Programs
PROGRAMWRITTEN
IN PYTHON
PYTHONINTERPRETER
OUTPUTINPUT
![Page 10: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/11.jpg)
Demo
![Page 12: Introduction to Computational Linguistics Programming I.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649e805503460f94b8476f/html5/thumbnails/22.jpg)
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