control 4 day 16 - 10/01/14

14
control 4 Day 16 - 10/01/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Upload: joshua-gay

Post on 31-Dec-2015

19 views

Category:

Documents


0 download

DESCRIPTION

control 4 Day 16 - 10/01/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

Page 1: control 4 Day 16 - 10/01/14

control 4Day 16 - 10/01/14LING 3820 & 6820

Natural Language Processing

Harry Howard

Tulane University

Page 2: control 4 Day 16 - 10/01/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 4 Day 16 - 10/01/14

Review of control

29-Sept-2014

3

NLP, Prof. Howard, Tulane University

Page 4: control 4 Day 16 - 10/01/14

Conditional expressions

1. >>> if True: 2. ... do something 3. ...

4. >>> if True: 5. ... do something 6. ... elif True: 7. ... do something 8. ... else: 9. ... do something 10. ...

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

4

Page 5: control 4 Day 16 - 10/01/14

For loop & list conmprehension1. >>> for item in container:

2. ... do something to item

3. ...

4. >>> newList = []

5. >>> for item in container:

6. ... newList.append(item)

7. ...

8. >>> newList = [item for item in container]

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

5

Page 6: control 4 Day 16 - 10/01/14

Add a condition

1. >>> newList = []2. >>> for item in container:3. ... if condition: 4. ...

newList.append(item) 5. ... 6. >>> newList = [item for item

in container if condition]29-Sept-2014NLP, Prof. Howard, Tulane University

6

Page 7: control 4 Day 16 - 10/01/14

Open Spyder

29-Sept-2014

7

NLP, Prof. Howard, Tulane University

Page 8: control 4 Day 16 - 10/01/14

6.3.4. How to check a condition in a loop

29-Sept-2014

8

NLP, Prof. Howard, Tulane University

Page 9: control 4 Day 16 - 10/01/14

Chained conditions in a loop

>>> greeting = 'Yo!'>>> caseList = [] >>> for char in greeting: ... if char.islower(): ... caseList.append('yes') ... elif char.isupper(): ... caseList.append('no') ... else: ... caseList.append('whoops!') ... >>> caseList ['no', 'yes', 'whoops!']

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

9

Page 10: control 4 Day 16 - 10/01/14

A chained conditional list comprehension However there is no list comprehension

that is exactly analogous to a chained conditional, since elif is not allowed in them.

A list comprehension only allows if -- else, so the elif has to be decomposed into else -- if.

Here is what it looks like in a loop:

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

10

Page 11: control 4 Day 16 - 10/01/14

Example

1. >>> caseList = [] 2. >>> for char in greeting: 3. ... if char.islower(): 4. ... caseList.append('yes') 5. ... else: 6. ... if char.isupper(): 7. ... caseList.append('no') 8. ... else: 9. ... caseList.append('whoops!') 10. ... 11. >>> caseList 12. ['no', 'yes', 'whoops!'] 13. >> caseList = ['yes' if char.islower() else 'no' if

char.isupper() else 'whoops!' for char in greeting]14. >>> caseList 15. ['no', 'yes', 'whoops!']

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

11

Page 12: control 4 Day 16 - 10/01/14

6.3.5. How to transform items within a loop The argument of append() takes any type that can be an

element of a list, such as strings or integers, so it can hold the result of a method:

1. >>> upperList = [] 2. >>> for char in greeting: 3. ... upperList.append(char.upper()) 4. ... 5. >>> upperList ['Y', 'O', '!'] 6. >>> lenList = [] 7. >>> for word in fruit: 8. ... lenList.append(len(word)) 9. ... 10.>>> lenList 11.[5, 6, 5, 4, 10]

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

12

Page 13: control 4 Day 16 - 10/01/14

A list comprehension can perform the same change by applying it to the first mention of the item:

1. >>> upperList = [char.upper() for char in greeting]

2. >>> upperList 3. ['Y', 'O', '!'] 4. >>> lenList = [len(word) for word in fruit]

5. >>> lenList 6. [5, 6, 5, 4, 10]

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

13

Page 14: control 4 Day 16 - 10/01/14

Finish control

Next time

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

14