scripts & functions day 18 - 10/06/14 ling 3820 & 6820 natural language processing harry...

18
Scripts & functions Day 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Upload: esther-benson

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Scripts & functionsDay 18 - 10/06/14LING 3820 & 6820

Natural Language Processing

Harry Howard

Tulane University

Page 2: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Course organization

06-Oct-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: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

The quiz was the review

Review of control

06-Oct-2014

3

NLP, Prof. Howard, Tulane University

Page 4: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Open Spyder

06-Oct-2014

4

NLP, Prof. Howard, Tulane University

Page 5: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Scripts

06-Oct-2014

5

NLP, Prof. Howard, Tulane University

Page 6: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Introduction

By now, you are probably tired of making a tiny error in a long conditional expression and having to rewrite the whole thing.

It would be much easier to write it once, correct it, and then have Python run it as many times as you like.

This is ‘batch’, rather than ‘interactive’ mode, and your code will be saved as a script.

06-Oct-2014NLP, Prof. Howard, Tulane University

6

Page 7: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Scripts in Spyder

It is here that the graphical interface of Spyder shows its superiority.

Under the File menu, choose New file…, which opens a window for a blank Python file.

Or nearly blank, since Spyder fills in the top few lines with a header, such as:

1. """ 2. Created on Mon Jun 3 19:55:43 20133. 4. @author: Harry Howard 5. """

06-Oct-2014NLP, Prof. Howard, Tulane University

7

Page 8: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Type this into your blank scriptbeneath the comment in quotesgreeting = 'Yo!'caseList = [] for char in greeting:

if char.islower(): caseList.append('yes')

elif char.isupper(): caseList.append('no')

else: caseList.append('whoops!')

print caseList

06-Oct-2014NLP, Prof. Howard, Tulane University

8

Page 9: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Saving a script

Save your work by going to the File menu, choosing Save as… and giving the file the name “test”, keeping the .py suffix.

06-Oct-2014NLP, Prof. Howard, Tulane University

9

Page 10: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Create a folder for your scriptsin your Documents folder

06-Oct-2014NLP, Prof. Howard, Tulane University

10

pyScripts

Page 11: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Running your script

Now that you have named and saved your script, go to the Run menu of Spyder and select Run.

The first time that you do this for a script, a dialog window will open like the one below that asks you to make some decisions.

You should set the working directory to the pyScripts folder that you just created in your Documents folder.

06-Oct-2014NLP, Prof. Howard, Tulane University

11

Page 12: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Defaults to run a scriptalso in: Preferences > Run

06-Oct-2014NLP, Prof. Howard, Tulane University

12

Page 13: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Where are my files?

Where are your files?

06-Oct-2014NLP, Prof. Howard, Tulane University

13

Page 14: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

The response

The console should print a response like the following to running the script:

>>> runfile('/Users/harryhow/Documents/pyScripts/test.py', wdir=r'/Users/harryhow/Documents/pyScripts')

['no', 'yes', 'whoops!'] >>>

06-Oct-2014NLP, Prof. Howard, Tulane University

14

Page 15: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Functions

06-Oct-2014

15

NLP, Prof. Howard, Tulane University

Page 16: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Turn your code into a functiondef caseChecker(word):

caseList = []

for char in word:

if char.islower():

caseList.append('yes')

elif char.isupper():

caseList.append('no')

else:

caseList.append('whoops!')

return caseList

06-Oct-2014NLP, Prof. Howard, Tulane University

16

Page 17: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

In the console, import your function & use it

1. >>> from test import caseChecker2. >>> caseChecker('Cool!!')3. ['no', 'yes', 'yes', 'yes',

'whoops!', 'whoops!']

06-Oct-2014NLP, Prof. Howard, Tulane University

17

Page 18: SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Homework: turn your answer to today's quiz into a function, import it in the console & test it various listsStart with NLTK

Next time

06-Oct-2014NLP, Prof. Howard, Tulane University

18