<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 NLTK Regular Expressions
Author
Step1:  Let's see which free resources are readily available. And then let's have a closer look at Shakespeare's Hamlet (to pretent we are literature freaks).
Step2:  Regular Expressions
So Shakespeare used 37360 words to write Hamlet. Let's investigate which patterns we find there. 
In which word does the character sequence "wre" occur?
Step3:  And which of them actually start with "wre"?
Step4:  Find all words that start with "T" or "t", end with "r" and have exactly 3 other characters in the middle. To implement the "T" or "t" we use a character class specified by the brackets []. [Tt]matches either "T" or "t". 
For macthing any character (no whitespace) we could use the character class [a-zA-Z], but using the abbreviation \Dis much more convenient. Further predefined character classes are
Step5:  Did Shakespeare use any numbers (written as digits?) For macthing all the digits, we could similarly use [0123456789] or [0-9], but using the abbreviation \dis much more convenient.
Step6:  And is there something that starts with z and ends with g?
Step7:  In the last example we can not be sure whether there is definitely nothing or whether we got the regular expression wrong. To find out which one is the case, create a string you know should match and test your expression there.
<ASSISTANT_TASK:>
Python Code:
import nltk, re
from nltk import word_tokenize
# NOTE if the data (corpora, example files) is not yet downloaded, this needs to be done first
# nltk.download()
Explanation: NLTK Regular Expressions
Author: Christin Seifert, licensed under the Creative Commons Attribution 3.0 Unported License https://creativecommons.org/licenses/by/3.0/
This is a tutorial for simple text processing with python using the nltk library. For further reading I recommend the extensive online book of nltk available here.
In this notebook we will
* load text files from disk
* find word patterns with regular expressions (and see where they fail)
It is assumed that you have some general knowledge on 
* basic python
Setup
If you have never used nltk before you need to download the example copora. Uncomment the nltk.download to do so. We also want the nltk library, the library (re) for regular expression.
End of explanation
print(nltk.corpus.gutenberg.fileids())
hamlet = nltk.corpus.gutenberg.words('shakespeare-hamlet.txt')
print(len(hamlet))
Explanation: Let's see which free resources are readily available. And then let's have a closer look at Shakespeare's Hamlet (to pretent we are literature freaks).
End of explanation
[w for w in hamlet if re.search('wre', w)]
Explanation: Regular Expressions
So Shakespeare used 37360 words to write Hamlet. Let's investigate which patterns we find there. 
In which word does the character sequence "wre" occur?
End of explanation
[w for w in hamlet if re.search('^wre', w)]
Explanation: And which of them actually start with "wre"?
End of explanation
[w for w in hamlet if re.search('^[Tt]\w{5,5}r$', w)]
Explanation: Find all words that start with "T" or "t", end with "r" and have exactly 3 other characters in the middle. To implement the "T" or "t" we use a character class specified by the brackets []. [Tt]matches either "T" or "t". 
For macthing any character (no whitespace) we could use the character class [a-zA-Z], but using the abbreviation \Dis much more convenient. Further predefined character classes are:
\d Matches any decimal digit
\D Matches any non-digit character
\s Matches any whitespace character (this could be line endings, blanks or tabs). This is tricky, because some of them are not visible if you look at the text with a text editor.
End of explanation
[w for w in hamlet if re.search('\d', w)]
Explanation: Did Shakespeare use any numbers (written as digits?) For macthing all the digits, we could similarly use [0123456789] or [0-9], but using the abbreviation \dis much more convenient.
End of explanation
[w for w in hamlet if re.search('^z.*g$', w)]
Explanation: And is there something that starts with z and ends with g?
End of explanation
[w for w in ["zarhhg","zhang","zg","42"] if re.search('^z.*g$', w)]
Explanation: In the last example we can not be sure whether there is definitely nothing or whether we got the regular expression wrong. To find out which one is the case, create a string you know should match and test your expression there.
End of explanation
<END_TASK>