Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University."— Presentation transcript:

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

2 Course organization 06-Oct-2014NLP, Prof. Howard, Tulane University 2  http://www.tulane.edu/~howard/LING3820/ http://www.tulane.edu/~howard/LING3820/  The syllabus is under construction.  http://www.tulane.edu/~howard/CompCultEN/ http://www.tulane.edu/~howard/CompCultEN/  Chapter numbering  3.7. How to deal with non-English characters 3.7. How to deal with non-English characters  4.5. How to create a pattern with Unicode characters 4.5. How to create a pattern with Unicode characters  6. Control 6. Control

3 The quiz was the review Review of control 06-Oct-2014 3 NLP, Prof. Howard, Tulane University

4 Open Spyder 06-Oct-2014 4 NLP, Prof. Howard, Tulane University

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

6 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

7 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 2013 3. 4. @author: Harry Howard 5. """ 06-Oct-2014NLP, Prof. Howard, Tulane University 7

8 Type this into your blank script beneath the comment in quotes greeting = '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

9 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

10 Create a folder for your scripts in your Documents folder 06-Oct-2014NLP, Prof. Howard, Tulane University 10 pyScripts

11 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

12 Defaults to run a script also in: Preferences > Run 06-Oct-2014NLP, Prof. Howard, Tulane University 12

13 Where are my files?  Where are your files? 06-Oct-2014NLP, Prof. Howard, Tulane University 13

14 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

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

16 Turn your code into a function def 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

17 In the console, import your function & use it 1. >>> from test import caseChecker 2. >>> caseChecker('Cool!!') 3. ['no', 'yes', 'yes', 'yes', 'whoops!', 'whoops!'] 06-Oct-2014NLP, Prof. Howard, Tulane University 17

18 Homework: turn your answer to today's quiz into a function, import it in the console & test it various lists Start with NLTK Next time 06-Oct-2014NLP, Prof. Howard, Tulane University 18


Download ppt "SCRIPTS & FUNCTIONS DAY 18 - 10/06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University."

Similar presentations


Ads by Google