1 / 14

control 4 Day 16 - 10/01/14

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

joshua-gay
Download Presentation

control 4 Day 16 - 10/01/14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Course organization • 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 NLP, Prof. Howard, Tulane University

  3. Review of control NLP, Prof. Howard, Tulane University

  4. Conditional expressions • >>> if True: • ... do something • ... • >>> if True: • ... do something • ... elif True: • ... do something • ... else: • ... do something • ... NLP, Prof. Howard, Tulane University

  5. For loop & list conmprehension • >>> for item in container: • ... do something to item • ... • >>> newList = [] • >>> for item in container: • ... newList.append(item) • ... • >>> newList = [item for item in container] NLP, Prof. Howard, Tulane University

  6. Add a condition • >>> newList = [] • >>> for item in container: • ... if condition: • ... newList.append(item) • ... • >>> newList = [item for item in container if condition] NLP, Prof. Howard, Tulane University

  7. Open Spyder NLP, Prof. Howard, Tulane University

  8. 6.3.4. How to check a condition in a loop NLP, Prof. Howard, Tulane University

  9. 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!'] NLP, Prof. Howard, Tulane University

  10. 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: NLP, Prof. Howard, Tulane University

  11. Example • >>> caseList = [] • >>> for char in greeting: • ... if char.islower(): • ... caseList.append('yes') • ... else: • ... if char.isupper(): • ... caseList.append('no') • ... else: • ... caseList.append('whoops!') • ... • >>> caseList • ['no', 'yes', 'whoops!'] • >> caseList = ['yes' if char.islower() else 'no' if char.isupper() else 'whoops!' for char in greeting] • >>> caseList • ['no', 'yes', 'whoops!'] NLP, Prof. Howard, Tulane University

  12. 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: • >>> upperList = [] • >>> for char in greeting: • ... upperList.append(char.upper()) • ... • >>> upperList ['Y', 'O', '!'] • >>> lenList = [] • >>> for word in fruit: • ... lenList.append(len(word)) • ... • >>> lenList • [5, 6, 5, 4, 10] NLP, Prof. Howard, Tulane University

  13. A list comprehension can perform the same change by applying it to the first mention of the item: • >>> upperList = [char.upper() for char in greeting] • >>> upperList • ['Y', 'O', '!'] • >>> lenList = [len(word) for word in fruit] • >>> lenList • [5, 6, 5, 4, 10] NLP, Prof. Howard, Tulane University

  14. Next time Finish control NLP, Prof. Howard, Tulane University

More Related