<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href='http
Step1:  1. Create a Doc object from the file owlcreek.txt<br>
HINT
Step2:  2. How many tokens are contained in the file?
Step3:  3. How many sentences are contained in the file?<br>HINT
Step4:  4. Print the second sentence in the document<br> HINT
Step5:  5. For each token in the sentence above, print its text, POS tag, dep tag and lemma<br>
CHALLENGE
Step6:  6. Write a matcher called 'Swimming' that finds both occurrences of the phrase "swimming vigorously" in the text<br>
HINT
Step7:  7. Print the text surrounding each found match
Step8:  EXTRA CREDIT
<ASSISTANT_TASK:>
Python Code:
# RUN THIS CELL to perform standard imports:
import spacy
nlp = spacy.load('en_core_web_sm')
Explanation: <a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>
NLP Basics Assessment - Solutions
For this assessment we'll be using the short story An Occurrence at Owl Creek Bridge by Ambrose Bierce (1890). <br>The story is in the public domain; the text file was obtained from Project Gutenberg.
End of explanation
# Enter your code here:
with open('../TextFiles/owlcreek.txt') as f:
    doc = nlp(f.read())
# Run this cell to verify it worked:
doc[:36]
Explanation: 1. Create a Doc object from the file owlcreek.txt<br>
HINT: Use with open('../TextFiles/owlcreek.txt') as f:
End of explanation
len(doc)
Explanation: 2. How many tokens are contained in the file?
End of explanation
sents = [sent for sent in doc.sents]
len(sents)
Explanation: 3. How many sentences are contained in the file?<br>HINT: You'll want to build a list first!
End of explanation
print(sents[1].text)
Explanation: 4. Print the second sentence in the document<br> HINT: Indexing starts at zero, and the title counts as the first sentence.
End of explanation
# NORMAL SOLUTION:
for token in sents[1]:
    print(token.text, token.pos_, token.dep_, token.lemma_)
# CHALLENGE SOLUTION:
    for token in sents[1]:
        print(f'{token.text:{15}} {token.pos_:{5}} {token.dep_:{10}} {token.lemma_:{15}}')
Explanation: 5. For each token in the sentence above, print its text, POS tag, dep tag and lemma<br>
CHALLENGE: Have values line up in columns in the print output.
End of explanation
# Import the Matcher library:
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
# Create a pattern and add it to matcher:
pattern = [{'LOWER': 'swimming'}, {'IS_SPACE': True, 'OP':'*'}, {'LOWER': 'vigorously'}]
matcher.add('Swimming', None, pattern)
# Create a list of matches called "found_matches" and print the list:
found_matches = matcher(doc)
print(found_matches)
Explanation: 6. Write a matcher called 'Swimming' that finds both occurrences of the phrase "swimming vigorously" in the text<br>
HINT: You should include an 'IS_SPACE': True pattern between the two words!
End of explanation
print(doc[1265:1290])
print(doc[3600:3615])
Explanation: 7. Print the text surrounding each found match
End of explanation
for sent in sents:
    if found_matches[0][1] < sent.end:
        print(sent)
        break
for sent in sents:
    if found_matches[1][1] < sent.end:
        print(sent)
        break
Explanation: EXTRA CREDIT:<br>Print the sentence that contains each found match
End of explanation
<END_TASK>