Introduction to the Special Issue on Computational Linguistics Using
Introduction to Computational Linguistics
description
Transcript of Introduction to Computational Linguistics
![Page 1: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/1.jpg)
Introduction to Computational Linguistics
Programming II
![Page 2: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/2.jpg)
Resumé
calculator mode arithmetic operators, simple and
complex arithmetic expressions saving, checking and running
programs
![Page 3: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/3.jpg)
Exercise 2.1
Write a program that gets 2 string variables and 2 integer
variables from the user, concatenates (joins them together
with no spaces) and displays the strings,
then multiplies the two numbers on a new line.
![Page 4: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/4.jpg)
Answer
s1 = raw_input("give me string 1 ")s2 = raw_input("give me string 2 ")n1 = input("give me number 1 ")n2 = input("give me number 2 ")print s1+ " " + s2, n1+n2
![Page 5: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/5.jpg)
while loop
while <condition> : <statements>i=0while (i<10) :
print i i = i+1 Notice that indentation is used to
group items together
![Page 6: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/6.jpg)
Exercise 2.2
Modify the last program so that it prints the sum of all the numbers.
![Page 7: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/7.jpg)
Exercise 2.2
sum=0i=0while (i<10) :
print i i = i+1 sum = sum+iprint sum
![Page 8: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/8.jpg)
range([start,] stop[, step]) Creates lists containing arithmetic
progressions most often used in for loops. If the step argument is omitted, it
defaults to 1. If the start argument is omitted, it
defaults to 0. Observe the behaviour of the range
function
![Page 9: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/9.jpg)
Use of range function
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9,
10]>>> range(0, 30, 5) [0, 5, 10, 15, 20, 25]>>> range(0, 10, 3) [0, 3, 6, 9]>>> range(0, -10, -1)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]>>> range(0) [] >>> range(1, 0) []
![Page 10: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/10.jpg)
for loop
Basic shape of for statement is thisfor <variable> in <list>: do something
Examples
for i in [1,2,3] : print ifor i in range(1,4): print ifor i in ["comp", "ling"]: print ifor i in ['comp', 'ling']: print i
![Page 11: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/11.jpg)
Exercise 2.3
calculate and print the sum of numbers in range(5,20).
![Page 12: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/12.jpg)
Strings Besides numbers, Python can also manipulate
strings, which can be expressed in several ways. They can be enclosed in single quotes or double
quotes: >>> 'spam eggs‘'spam eggs' >>> 'doesn\'t‘"doesn't" >>> "doesn't“"doesn't" >>> ‘ "Yes," he said.' ‘ "Yes," he said.'
![Page 13: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/13.jpg)
Strings We can get at individual characters
of a string using subscript notation>>> s = 'dog'>>>s[0]'d'>>>s[1]'o'>>>s[2]'g'
![Page 14: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/14.jpg)
Strings
Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> word = 'Help' + 'A' >>> word 'HelpA' >>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
![Page 15: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/15.jpg)
Slice notation I Slice notation is two indices separated by
a colon, and selects that part of a string which begins with the first index and which finishes just before the second>>> s = 'dog'>>> s[0:1]'d'>>> s[0:2]'do'
![Page 16: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/16.jpg)
Slice notation II
Slice indices have useful defaults an omitted first index defaults to zero an omitted second index defaults to
the size of the string being sliced. >>> s[:2] 'do'>>> word[2:] 'g'>>> word[0:] 'dog'
![Page 17: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/17.jpg)
More Data Types We have seen numbers and strings. These are different types of data. Each kind of data has characteristic
operations. Now we look at lists. A list is a compound data type which is
used to group other data types together. example: range(3) = [0,1,2]
![Page 18: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/18.jpg)
Lists The list can be written as a list of comma-
separated values (items) between square brackets.
List items need not all have the same type.
>>> a = ['spam', 'eggs', 100, 1234]>>> a ['spam', 'eggs', 100, 1234]
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
![Page 19: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/19.jpg)
Lists Lists can be sliced, concatenated and so
on: >>> a[0] 'spam' >>> a[3] 1234>>> a[-2] 100>>> a[1:-1] ['eggs', 100]>>> a[:2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] >>> 3*a[:3] + ['Boe!']['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
![Page 20: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/20.jpg)
Lists can be changed
Unlike strings, which are immutable, it is possible to change individual elements of a list>>> a[2] = a[2] + 23
Assignment to slices is also possible, and this can even change the size of the list
![Page 21: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/21.jpg)
Changing Lists# Replace some items:
>>> a[0:2] = [1, 12]>>> a[1, 12, 123, 1234]
# Remove some:>>>a[0:2] = [] >>> a[123, 1234]
# Insert some: >>>a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234]
![Page 22: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/22.jpg)
The built-in function len( )
This function computes the number of elements in a list >>> a = [1,2,3]>>> len(a)8>>>len([1,1,1,1,1,1])6
![Page 23: Introduction to Computational Linguistics](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813e06550346895da7e706/html5/thumbnails/23.jpg)
Nesting Lists
It is possible to nest lists (create lists containing other lists), for example: >>> q = [2, 3]>>> p = [1, q, 4]>>> p[1,[2,3],4]