control 3 Day 15 - 9/29/14

21
control 3 Day 15 - 9/29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

description

control 3 Day 15 - 9/29/14. LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University. Course organization. http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/CompCultEN/ Chapter numbering - PowerPoint PPT Presentation

Transcript of control 3 Day 15 - 9/29/14

Page 1: control 3 Day 15 - 9/29/14

control 3Day 15 - 9/29/14LING 3820 & 6820

Natural Language Processing

Harry Howard

Tulane University

Page 2: control 3 Day 15 - 9/29/14

Course organization

29-Sept-2014NLP, Prof. Howard, Tulane University

2

http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/

CompCultEN/ Chapter numbering

3.7. How to deal with non-English characters 4.5. How to create a pattern with Unicode

characters 6. Control

Page 3: control 3 Day 15 - 9/29/14

The quiz was the review.

Review of control

29-Sept-2014

3

NLP, Prof. Howard, Tulane University

Page 4: control 3 Day 15 - 9/29/14

Open Spyder

29-Sept-2014

4

NLP, Prof. Howard, Tulane University

Page 5: control 3 Day 15 - 9/29/14

6.2.1. Chained conditionals

29-Sept-2014

5

NLP, Prof. Howard, Tulane University

Page 6: control 3 Day 15 - 9/29/14

Chained conditions

Imagine that you wanted to check whether a character is lowercase.

You would test for two conditions: whether it is lowercase or whether it is uppercase.

But there are a lot of leftovers which are neither one – the punctuation.

These three conditions are mutually exclusive, so they cannot be stated as three ifs.

29-Sept-2014NLP, Prof. Howard, Tulane University

6

Page 7: control 3 Day 15 - 9/29/14

A chained conditional expression1. >>> char = 'Y' 2. >>> if char.islower(): 3. ... print 'yes'4. ... elif char.isupper(): 5. ... print 'no'6. ... else: 7. ... print 'whoops!' 8. ... 9. no

29-Sept-2014NLP, Prof. Howard, Tulane University

7

Page 8: control 3 Day 15 - 9/29/14

In computer science, the programming construct for examining every item of a collection is called a loop. This could be every character in a string or every word in a list.

6.3. Iterating over the items of a collection with a loop

29-Sept-2014

8

NLP, Prof. Howard, Tulane University

Page 9: control 3 Day 15 - 9/29/14

6.3.1. How to examine every item with for

As a simple example, consider printing every letter of a word:

1.>>> greeting = 'Yo!' 2.>>> for char in greeting: 3.... print char 4.... 5.Y 6.o 7.!

29-Sept-2014NLP, Prof. Howard, Tulane University

9

Page 10: control 3 Day 15 - 9/29/14

Iteration over a list

1. >>> fruit = ['apple', 'cherry', 'mango', 'pear', 'watermelon']

2. >>> for word in fruit: 3. ... print word 4. ... 5. apple 6. cherry 7. mango 8. pear 9. watermelon

29-Sept-2014NLP, Prof. Howard, Tulane University

10

Page 11: control 3 Day 15 - 9/29/14

A trick for printing

To save space in your Python console, adding a comma after the variable to be printed puts the output on the same line:

1. >>> for char in greeting: 2. ... print char, 3. ... 4. Y o ! 5. >>> for word in fruit: 6. ... print word, 7. ... 8. apple cherry mango pear watermelon

29-Sept-2014NLP, Prof. Howard, Tulane University

11

Page 12: control 3 Day 15 - 9/29/14

6.3.2. How to make a list during a loop with append()

1. >>> charlist = []2. >>> for char in greeting: 3. ... charlist.append(char) 4. ... 5. >>> charlist 6. ['Y', 'o', '!']

29-Sept-2014NLP, Prof. Howard, Tulane University

12

Page 13: control 3 Day 15 - 9/29/14

Doing the same with a list, redundantly

1. >>> wordlist = [] 2. >>> for word in fruit: 3. ... wordlist.append(word) 4. ... 5. >>> wordlist 6. ['apple', 'cherry', 'mango',

'pear', 'watermelon']

29-Sept-2014NLP, Prof. Howard, Tulane University

13

Page 14: control 3 Day 15 - 9/29/14

6.3.3. How to pack a loop into a list with a list comprehension Creating a list from a loop is such a

frequent task that Python has a breathtakingly elegant idiom for accomplishing it, the list comprehension.

It consists of putting the whole for statement within square brackets, with the appending signaled by the brackets themselves.

29-Sept-2014NLP, Prof. Howard, Tulane University

14

Page 15: control 3 Day 15 - 9/29/14

List comprehension with string example1. >>> charlist = []2. >>> for char in greeting: 3. ... charlist.append(char) 4. ... 5. >>> charlist 6. ['Y', 'o', '!']7. >>> charlist = [char for char in

greeting]8. charlist9. ['Y', 'o', '!']

29-Sept-2014NLP, Prof. Howard, Tulane University

15

Page 16: control 3 Day 15 - 9/29/14

List comprehension with list example1. >>> wordlist = [] 2. >>> for word in fruit: 3. ... wordlist.append(word) 4. ... 5. >>> wordlist 6. ['apple', 'cherry', 'mango', 'pear',

'watermelon']7. >>> wordlist = [word for word in fruit]8. >>> wordlist9. ['apple', 'cherry', 'mango', 'pear',

'watermelon']

29-Sept-2014NLP, Prof. Howard, Tulane University

16

Page 17: control 3 Day 15 - 9/29/14

6.3.4. The list comprehension in set theory

The format of the list comprehension is inspired on a similar expression in set theory:

{e | e ∈ F & P(e)} This is read as “the set of e’s such that e

is an element of F and P of e (e has the property P)”.

29-Sept-2014NLP, Prof. Howard, Tulane University

17

Page 18: control 3 Day 15 - 9/29/14

6.3.5. How to check a condition in a loop The ultimate step in making a decision about a

collection of items is to make membership in the output contingent on a condition:

1. >>> lowchar = []2. >>> for char in greeting:3. ... if char.islower(): 4. ... lowchar.append(char) 5. ... 6. >>> lowchar7. ['o']8. >>> lowchar = [char for char in greeting if char.islower()]

9. >>> lowchar10.['o']

29-Sept-2014NLP, Prof. Howard, Tulane University

18

Page 19: control 3 Day 15 - 9/29/14

List example

1. >>> melonlist = [] 2. >>> for word in fruit: 3. ... if word.endswith('melon'): 4. ... melonlist.append(word)5. ...6. >>> melonlist7. ['watermelon']8. >>> melonlist = [word for word in

fruit if word.endswith('melon')]9. ['watermelon']

29-Sept-2014NLP, Prof. Howard, Tulane University

19

Page 20: control 3 Day 15 - 9/29/14

Chained conditions in a loop

1. >>> for char in greeting: 2. ... if char.islower(): 3. ... print 'yes' 4. ... elif char.isupper(): 5. ... print 'no' 6. ... else: 7. ... print 'whoops!'' 8. ... 9. no 10. yes 11. whoops!

29-Sept-2014NLP, Prof. Howard, Tulane University

20

Page 21: control 3 Day 15 - 9/29/14

Finish control

Next time

29-Sept-2014NLP, Prof. Howard, Tulane University

21