0
0
NLPml~10 mins

Why similarity measures find related text in NLP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate cosine similarity between two vectors.

NLP
from numpy import dot
from numpy.linalg import norm

def cosine_similarity(vec1, vec2):
    return dot(vec1, vec2) / (norm(vec1) [1] norm(vec2))
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication in the denominator.
2fill in blank
medium

Complete the code to convert text into a vector using term frequency.

NLP
def term_frequency(text, word):
    words = text.lower().split()
    return words.count([1]) / len(words)
Drag options to blanks, or click blank then click option'
Aword
Btext
Cwords
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole text or list instead of the target word.
3fill in blank
hard

Fix the error in the code to compute Jaccard similarity between two sets.

NLP
def jaccard_similarity(set1, set2):
    intersection = set1.intersection(set2)
    union = set1.[1](set2)
    return len(intersection) / len(union)
Drag options to blanks, or click blank then click option'
Adifference
Bintersect
Cunion
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'intersect' or 'add' which are not valid set methods.
4fill in blank
hard

Fill both blanks to create a dictionary of word counts for words longer than 3 letters.

NLP
text = 'machine learning finds related text'
words = text.split()
word_counts = {word: words.count(word) for word in words if len(word) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B<
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than.
Using wrong length number.
5fill in blank
hard

Fill all three blanks to filter words and create a dictionary with uppercase keys and counts for words longer than 4 letters.

NLP
text = 'similarity measures find related text'
words = text.split()
filtered_counts = [1]: [2] for word in words if len(word) [3] 4
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
Dwords.count(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase keys.
Using wrong comparison operator.