Professional Documents
Culture Documents
UBC Summer School in NLP - VSP 2019 Lecture 7
UBC Summer School in NLP - VSP 2019 Lecture 7
UBC Summer School in NLP - VSP 2019 Lecture 7
• The list is not the correct length, and the index you want should exist, but doesn't
• Make sure the code that builds the list is building one of the right length.
TYPES OF ERRORS
• KeyError:
• This is raised if you try to access a dictionary key that doesn't exist
• The key you want should be in the dictionary, but it isn't. Double-check the code that is
building the dictionary to make sure the key does exist before you try to access it.
• You've asked for an incorrect key name. Check for spelling mistakes if you typed key names
yourself. Otherwise, check that your code is not generating incorrect names.
TYPES OF ERRORS
• AttributeError:
• This is raised if you try to do use a method or attribute that doesn't exist
s = 'some string‘
s.append('!')
• This raises an AttributeError because strings do not have an "append" method
• There are no general solutions on how to solve an AttributeError. Follow the traceback of
the error
TYPES OF ERRORS
• TypeError
• This is raised if you try to do something with a variable of the wrong type.
numbers = [0,1,2,3,4]
print(numbers['0'])
• This raises a TypeError because the index is the wrong type. It has to be an integer, not a
string
• To debug, follow the traceback
ERROR HANDLING
• Python gives us a way to catch errors using a try/except/else block (kind of like an
if/else block)
try:
#some code
except KeyError:
if a KeyError is raised, this happens
except (IndexError, ValueError):
if an IndexError or a ValueError Is raised, this happens
else:
#if any other exception is raised, this happens
ERROR HANDLING
• Generally, you don’t want errors, so you shouldn’t code to include them.
• Of course, just because you shouldn’t, doesn’t mean you can’t. Let’s fix this:
• Import statements should always be the very first thing at the top of your code.
IMPORTING MODULES
• Another useful package/module is the os module
• OS stands for "operating system". This module contains useful functions for dealing with
files and folders.
• Here are two very useful functions:
os.getcwd()
• Returns the "current working directory.“ This means the folder where the Python file is
os.path.join(string1, string2, ...)
• Take the strings and joins them by slashes to make a file path
• These are often combined:
path = os.path.join(os.getcwd(), 'data', 'turkish_words.txt')
PYTHON PACKAGES
• Importing gives us access to the wide-world of things you can do with Python
• Many developers have made Python packages available
• We’ll be installing packages using pip
• PIP is a tongue-and-cheek recursive acronym for Pip installs Packages
• We use it from command prompt/terminal
• Let’s first have some fun with command prompt
• Using text input
NEW FUNCTION: INPUT()
• Since we’ll be working with command prompt to install packages, I thought it’d be
fun to play around a minute with a command line interface
• Remember, using Sublime Text, which is a text editor, we can’t interact with our
script, we just program and run it
• Now we’re using command line, we can input text with the input() function
• Test it out in IDLE:
print(‘please input your name:’
name = input()
print(name)
INTERACTING WITH CMD LINE
• Original computer games started with text-line interfaces
• Today, we can recreate these types of games easily
• Let’s write a fun little text-based game:
• We’ll preset a path that the user must figure out
• The basic interaction asks the user to make a move and lets them know if they made the
right move or not
print(‘make a move (up, down, left, right):’)
curr_move = input()
if curr_move is correct:
print(‘you got it right, you can move on!’)
…
PLAN TODAY
• Review:
• Dictionaries
• Finish of text-to-speech (TTS)
• Write to file
• Access webpage
• Introduce Errors/exceptions
• Practical application
• Introduce importing modules/packages
• Learn to use command line
• Introduce installing packages with pip
• Introduce NLTK (Natural Language Toolkit)
INSTALLING NEW PYTHON
PACKAGES
• We install python packages using the pip install call
• Let’s install the Natural Language Processing Toolkit
• In command line (PC)
• pip install nltk
• In terminal (MAC)
• pip3 install nltk
• You’ll see a bunch of stuff show up on the screen as in installs the package
• There are lots of packages you can install
• If there’s a specific project you’re working on, there’s a good chance someone has made
a package which can help!