Appendix B. Playful Python and regular expressions

To get the most out of this book, you’ll want to get comfortable with Python. You’ll want to be so comfortable that you get playful. When things don’t work, you’ll need to be able to play around and explore to find a way to make Python do what you want.

And even when your code works, playing around may help you uncover cool new ways of doing things or hidden monsters lurking in your code. Hidden errors and edge cases are very common in natural language processing, because there are so many different ways to say things in a language like English.

To get playful, just experiment with Python code, like children do. If you copy and paste code, change it. Try to break it and then fix it. Pull it apart into as many separate expressions as you can. Create modules out of bits of your code with functions or classes. Then pull it back together into as few lines of code as you can.

Try random things with the data structure or model or function you create. Try to run commands that you think should be included in a module or class. Use the Tab key on your keyboard often. When you press the Tab key, your editor or shell should try to finish your thought by completing the variable, class, function, method, attribute, and path name you started to type.

Use all the help that Python and your shell provides. Like man in a Linux shell, help() is your built-in friend in Python. Try typing help or help(object) in a Python console. It should work even when the IPython ? and ?? fail. Try object? and object?? in a Jupyter Console or Notebook if you’ve never done that before.

The rest of this Python primer introduces the data structures and functions we use throughout this book so you can start playing with them:

  • str and bytes
  • ord and chr
  • .format()
  • dict and OrderedDict
  • list, np.array, pd.Series
  • pd.DataFrame

We also explain some of the patterns and built-in Python functions we sometimes use here and in the nlpia package:

  • List comprehensions—[x for x in range(10)]
  • Generators—(x for x in range(1000000000))
  • Regular expressions—re.match(r'[A-Za-z ]+', 'Hello World')
  • File openers—open('path/to/file.txt')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset