SCRIPTS & FUNCTIONS DAY /06/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
Course organization 06-Oct-2014NLP, Prof. Howard, Tulane University 2 The syllabus is under construction. 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
The quiz was the review Review of control 06-Oct NLP, Prof. Howard, Tulane University
Open Spyder 06-Oct NLP, Prof. Howard, Tulane University
Scripts 06-Oct NLP, Prof. 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
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: Harry Howard 5. """ 06-Oct-2014NLP, Prof. Howard, Tulane University 7
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
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
Create a folder for your scripts in your Documents folder 06-Oct-2014NLP, Prof. Howard, Tulane University 10 pyScripts
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
Defaults to run a script also in: Preferences > Run 06-Oct-2014NLP, Prof. Howard, Tulane University 12
Where are my files? Where are your files? 06-Oct-2014NLP, Prof. Howard, Tulane University 13
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
Functions 06-Oct NLP, Prof. Howard, Tulane University
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
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
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