1 / 15

NLTK & Python Day 6

NLTK & Python Day 6. LING 681.02 Computational Linguistics Harry Howard Tulane University. Course organization. I have requested that Python and NLTK be installed on the computers in this room. NLPP. §1.3. Computing with Language: Simple Statistics. Summary of freq dist Table 1.2. NLPP.

vince
Download Presentation

NLTK & Python Day 6

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. NLTK & PythonDay 6 LING 681.02 Computational Linguistics Harry Howard Tulane University

  2. Course organization • I have requested that Python and NLTK be installed on the computers in this room. LING 681.02, Prof. Howard, Tulane University

  3. NLPP §1.3. Computing with Language: Simple Statistics

  4. Summary of freq distTable 1.2 LING 681.02, Prof. Howard, Tulane University

  5. NLPP 1.4 Back to Python: Making Decisions and Taking Control

  6. Relational operatorsTable 1.3 LING 681.02, Prof. Howard, Tulane University

  7. String comparison operators Table 1.4 LING 681.02, Prof. Howard, Tulane University

  8. Conditions • Both sets of operators return a value of yes or no, true or false. • Thus they can be used as conditions to test something: [w for w in text if condition] LING 681.02, Prof. Howard, Tulane University

  9. Some examples >>> sorted([w for w in set(text1) if w.endswith('ableness')]) ['comfortableness', 'honourableness', 'immutableness', 'indispensableness', ...] >>> sorted([term for term in set(text4) if 'gnt' in term]) ['Sovereignty', 'sovereignties', 'sovereignty'] >>> sorted([item for item in set(sent7) if item.isdigit()]) ['29', '61'] • Get rid of punctuation marks: [word.lower() for word in text1 if word.isalpha()]) LING 681.02, Prof. Howard, Tulane University

  10. Making more complex conditions • not condition • condition and condition • condition or condition LING 681.02, Prof. Howard, Tulane University

  11. Conditionals • What if you want to do something when the condition is true beside build a list? • Follow the condition with a colon: >>> word = 'cat' >>> if len(word) < 5: ... print 'word length is less than 5' ... word length is less than 5 >>> >>> if len(word) >= 5: ... print 'word length is greater than or equal to 5' ... >>> LING 681.02, Prof. Howard, Tulane University

  12. More complex conditionals • if, elif, else >>> if len(word) < 5: ... print 'word length is less than 5' ... elif len(word) > 5: ... print 'word length is greater than 5' ... else len(word) == 5: ... print 'word length is equal to 5' ... >>> LING 681.02, Prof. Howard, Tulane University

  13. Looping with for • We have already seen for as the operator for iterating through a list. • More generally, for starts a loop that runs through a number of elements: >>> for word in ['Call', 'me', 'Ishmael', '.']: ... print word ... Call me Ishmael . >>> LING 681.02, Prof. Howard, Tulane University

  14. Conditions with for >>> sent1 = ['Call', 'me', 'Ishmael', '.'] >>> for token in sent1: ... if token.islower(): ... print token, 'is a lowercase word' ... elif token.istitle(): ... print token, 'is a titlecase word' ... else: ... print token, 'is punctuation' ... LING 681.02, Prof. Howard, Tulane University

  15. Next time First quiz/project NLPP: §2

More Related