NLP/NLU Specialization Notes
  • Natural Language Processing/Understanding Notes, Resources
  • Natural Language Processing With Attention Models
    • NLP With Attention Models
  • Natural Language Processing With Attention Models Notebooks
    • Assignment 1: Neural Machine Translation
    • Basic Attention Operation: Ungraded Lab
    • Calculating the Bilingual Evaluation Understudy (BLEU) score: Ungraded Lab
    • Scaled Dot-Product Attention: Ungraded Lab
    • Assignment 2: Transformer Summarizer
    • The Three Ways of Attention and Dot Product Attention: Ungraded Lab Notebook
    • Masking
    • Positional Encoding
    • Assignment 3: Question Answering
    • Assignment 3: Question Answering
    • Question Answering with BERT and HuggingFace
    • Question Answering with BERT and HuggingFace 🤗 (Fine-tuning)
    • SentencePiece and BPE
  • Natural Language Processing With Classification And Vector Spaces
    • NLP With Classification and Vector Spaces
  • Natural Language Processing With Sequence Models
    • NLP With Sequence Models
  • Natural Language Processing with Classification and Vector Spaces Notebooks
    • Assignment 1: Logistic Regression
    • Preprocessing
    • Building and Visualizing word frequencies
    • Visualizing tweets and the Logistic Regression model
    • Assignment 2: Naive Bayes
    • Assignment 3: Hello Vectors
    • Linear algebra in Python with NumPy
    • Manipulating word embeddings
    • Another explanation about PCA
    • Assignment 4 - Naive Machine Translation and LSH
    • Vector manipulation in Python
    • Hash functions and multiplanes
  • Natural Language Processing with Probabilistic Models
    • NLP With Probabilistic Models
  • Natural Language Processing with Probabilistic Models Notebooks
    • Assignment 1: Autocorrect
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 01
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 02
    • Assignment 2: Parts-of-Speech Tagging (POS)
    • Parts-of-Speech Tagging - First Steps: Working with text files, Creating a Vocabulary and Handling Unknown Words
    • Parts-of-Speech Tagging - Working with tags and Numpy
    • Assignment 3: Language Models: Auto-Complete
    • N-grams Corpus preprocessing
    • Building the language model
    • Out of vocabulary words (OOV)
    • Assignment 4: Word Embeddings
    • Word Embeddings First Steps: Data Preparation
    • Word Embeddings: Intro to CBOW model, activation functions and working with Numpy
    • Word Embeddings: Training the CBOW model
    • Word Embeddings: Hands On
    • Word Embeddings: Ungraded Practice Notebook
  • Natural Language Processing with Sequence Models Notebooks
    • Assignment 1: Deep N-grams
    • Hidden State Activation : Ungraded Lecture Notebook
    • Assignment 1: Sentiment with Deep Neural Networks
    • Vanilla RNNs and GRUs
    • Lab 1: TensorFlow Tutorial and Some Useful Functions
    • Calculating perplexity using numpy: Ungraded Lecture Notebook
    • Assignment 2 - Named Entity Recognition (NER)
    • Vanishing Gradients and Exploding Gradients in RNNs : Ungraded Lecture Notebook
    • Evaluate a Siamese model: Ungraded Lecture Notebook
    • Assignment 3: Question duplicates
    • Modified Triplet Loss : Ungraded Lecture Notebook
    • Creating a Siamese model: Ungraded Lecture Notebook
  • Stanford
    • Stanford CS 224U,224N
  • Udacity
    • NLP Nanodegree
  • Previous
  • Next
  • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 02
  • Candidates from String Edits

NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 02¶

Estimated Time: 20 minutes

Candidates from String Edits¶

Create a list of candidate strings by applying an edit operation

Imports and Data¶

In [ ]:
Copied!
# data
word = 'dearz' # 🦌
# data word = 'dearz' # 🦌

Splits¶

Find all the ways you can split a word into 2 parts !

In [ ]:
Copied!
# splits with a loop
splits_a = []
for i in range(len(word)+1):
    splits_a.append([word[:i],word[i:]])

for i in splits_a:
    print(i)
# splits with a loop splits_a = [] for i in range(len(word)+1): splits_a.append([word[:i],word[i:]]) for i in splits_a: print(i)
In [ ]:
Copied!
# same splits, done using a list comprehension
splits_b = [(word[:i], word[i:]) for i in range(len(word) + 1)]

for i in splits_b:
    print(i)
# same splits, done using a list comprehension splits_b = [(word[:i], word[i:]) for i in range(len(word) + 1)] for i in splits_b: print(i)

Delete Edit¶

Delete a letter from each string in the splits list.
What this does is effectively delete each possible letter from the original word being edited.

In [ ]:
Copied!
# deletes with a loop
splits = splits_a
deletes = []

print('word : ', word)
for L,R in splits:
    if R:
        print(L + R[1:], ' <-- delete ', R[0])
# deletes with a loop splits = splits_a deletes = [] print('word : ', word) for L,R in splits: if R: print(L + R[1:], ' <-- delete ', R[0])

It's worth taking a closer look at how this is excecuting a 'delete'.
Taking the first item from the splits list :

In [ ]:
Copied!
# breaking it down
print('word : ', word)
one_split = splits[0]
print('first item from the splits list : ', one_split)
L = one_split[0]
R = one_split[1]
print('L : ', L)
print('R : ', R)
print('*** now implicit delete by excluding the leading letter ***')
print('L + R[1:] : ',L + R[1:], ' <-- delete ', R[0])
# breaking it down print('word : ', word) one_split = splits[0] print('first item from the splits list : ', one_split) L = one_split[0] R = one_split[1] print('L : ', L) print('R : ', R) print('*** now implicit delete by excluding the leading letter ***') print('L + R[1:] : ',L + R[1:], ' <-- delete ', R[0])

So the end result transforms 'dearz' to 'earz' by deleting the first character.
And you use a loop (code block above) or a list comprehension (code block below) to do
this for the entire splits list.

In [ ]:
Copied!
# deletes with a list comprehension
splits = splits_a
deletes = [L + R[1:] for L, R in splits if R]

print(deletes)
print('*** which is the same as ***')
for i in deletes:
    print(i)
# deletes with a list comprehension splits = splits_a deletes = [L + R[1:] for L, R in splits if R] print(deletes) print('*** which is the same as ***') for i in deletes: print(i)

Ungraded Exercise¶

You now have a list of candidate strings created after performing a delete edit.
Next step will be to filter this list for candidate words found in a vocabulary.
Given the example vocab below, can you think of a way to create a list of candidate words ?
Remember, you already have a list of candidate strings, some of which are certainly not actual words you might find in your vocabulary !

So from the above list earz, darz, derz, deaz, dear.
You're really only interested in dear.

In [ ]:
Copied!
vocab = ['dean','deer','dear','fries','and','coke']
edits = list(deletes)

print('vocab : ', vocab)
print('edits : ', edits)

candidates=[]

### START CODE HERE ###
#candidates = ??  # hint: 'set.intersection'
### END CODE HERE ###

print('candidate words : ', candidates)
vocab = ['dean','deer','dear','fries','and','coke'] edits = list(deletes) print('vocab : ', vocab) print('edits : ', edits) candidates=[] ### START CODE HERE ### #candidates = ?? # hint: 'set.intersection' ### END CODE HERE ### print('candidate words : ', candidates)

Expected Outcome:

vocab : ['dean', 'deer', 'dear', 'fries', 'and', 'coke']
edits : ['earz', 'darz', 'derz', 'deaz', 'dear']
candidate words : {'dear'}

Summary¶

You've unpacked an integral part of the assignment by breaking down splits and edits, specifically looking at deletes here.
Implementation of the other edit types (insert, replace, switch) follows a similar methodology and should now feel somewhat familiar when you see them.
This bit of the code isn't as intuitive as other sections, so well done!
You should now feel confident facing some of the more technical parts of the assignment at the end of the week.


Documentation built with MkDocs.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search