150 likes | 249 Views
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.
E N D
NLTK & PythonDay 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. LING 681.02, Prof. Howard, Tulane University
NLPP §1.3. Computing with Language: Simple Statistics
Summary of freq distTable 1.2 LING 681.02, Prof. Howard, Tulane University
NLPP 1.4 Back to Python: Making Decisions and Taking Control
Relational operatorsTable 1.3 LING 681.02, Prof. Howard, Tulane University
String comparison operators Table 1.4 LING 681.02, Prof. Howard, Tulane University
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
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
Making more complex conditions • not condition • condition and condition • condition or condition LING 681.02, Prof. Howard, Tulane University
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
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
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
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
Next time First quiz/project NLPP: §2