1 / 21

control 3 Day 15 - 9/29/14

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

Download Presentation

control 3 Day 15 - 9/29/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 3Day 15 - 9/29/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 The quiz was the review. NLP, Prof. Howard, Tulane University

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

  5. 6.2.1. Chained conditionals NLP, Prof. Howard, Tulane University

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

  7. A chained conditional expression • >>> char = 'Y' • >>> if char.islower(): • ... print 'yes' • ... elif char.isupper(): • ... print 'no' • ... else: • ... print 'whoops!' • ... • no NLP, Prof. Howard, Tulane University

  8. 6.3. Iterating over the items of a collection with a loop 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. NLP, Prof. Howard, Tulane University

  9. 6.3.1. How to examine every item with for • As a simple example, consider printing every letter of a word: • >>> greeting = 'Yo!' • >>> for char in greeting: • ... print char • ... • Y • o • ! NLP, Prof. Howard, Tulane University

  10. Iteration over a list • >>> fruit = ['apple', 'cherry', 'mango', 'pear', 'watermelon'] • >>> for word in fruit: • ... print word • ... • apple • cherry • mango • pear • watermelon NLP, Prof. Howard, Tulane University

  11. 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: • >>> for char in greeting: • ... print char, • ... • Y o ! • >>> for word in fruit: • ... print word, • ... • apple cherry mango pear watermelon NLP, Prof. Howard, Tulane University

  12. 6.3.2. How to make a list during a loop with append() • >>> charlist = [] • >>> for char in greeting: • ... charlist.append(char) • ... • >>> charlist • ['Y', 'o', '!'] NLP, Prof. Howard, Tulane University

  13. Doing the same with a list, redundantly • >>> wordlist = [] • >>> for word in fruit: • ... wordlist.append(word) • ... • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] NLP, Prof. Howard, Tulane University

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

  15. List comprehension with string example • >>> charlist = [] • >>> for char in greeting: • ... charlist.append(char) • ... • >>> charlist • ['Y', 'o', '!'] • >>> charlist = [char for char in greeting] • charlist • ['Y', 'o', '!'] NLP, Prof. Howard, Tulane University

  16. List comprehension with list example • >>> wordlist = [] • >>> for word in fruit: • ... wordlist.append(word) • ... • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] • >>> wordlist = [word for word in fruit] • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] NLP, Prof. Howard, Tulane University

  17. 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)”. NLP, Prof. Howard, Tulane University

  18. 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: • >>> lowchar = [] • >>> for char in greeting: • ... if char.islower(): • ... lowchar.append(char) • ... • >>> lowchar • ['o'] • >>> lowchar = [char for char in greeting if char.islower()] • >>> lowchar • ['o'] NLP, Prof. Howard, Tulane University

  19. List example • >>> melonlist = [] • >>> for word in fruit: • ... if word.endswith('melon'): • ... melonlist.append(word) • ... • >>> melonlist • ['watermelon'] • >>> melonlist = [word for word in fruit if word.endswith('melon')] • ['watermelon'] NLP, Prof. Howard, Tulane University

  20. Chained conditions in a loop • >>> for char in greeting: • ... if char.islower(): • ... print 'yes' • ... elif char.isupper(): • ... print 'no' • ... else: • ... print 'whoops!'' • ... • no • yes • whoops! NLP, Prof. Howard, Tulane University

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

More Related