0
0
NLPml~10 mins

Jaccard similarity in NLP - Interactive Code Practice

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

Complete the code to calculate the 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'
Aunion
Bdifference
Csymmetric_difference
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using difference instead of union causes incorrect denominator.
Using symmetric_difference excludes common elements, which is wrong.
2fill in blank
medium

Complete the code to convert two sentences into sets of words for Jaccard similarity calculation.

NLP
def sentence_to_set(sentence):
    words = sentence.lower().split()
    return set([1])
Drag options to blanks, or click blank then click option'
Awords
Bsentence
Csentence.split()
Dsentence.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole sentence string instead of the list of words to set().
Not converting to lowercase before splitting causes case mismatches.
3fill in blank
hard

Fix the error in the Jaccard similarity function to avoid division by zero.

NLP
def jaccard_similarity(set1, set2):
    intersection = set1.intersection(set2)
    union = set1.union(set2)
    if len(union) == 0:
        return [1]
    return len(intersection) / len(union)
Drag options to blanks, or click blank then click option'
Alen(intersection)
BNone
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None causes errors when using the result.
Returning 1 incorrectly suggests full similarity for empty sets.
4fill in blank
hard

Fill both blanks to create a function that computes Jaccard similarity from two sentences.

NLP
def jaccard_from_sentences(s1, s2):
    set1 = set(s1.lower().[1]())
    set2 = set(s2.lower().[2]())
    intersection = set1.intersection(set2)
    union = set1.union(set2)
    if len(union) == 0:
        return 0
    return len(intersection) / len(union)
Drag options to blanks, or click blank then click option'
Asplit
Bstrip
Creplace
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip() removes whitespace but does not split words.
Using replace() or join() does not create word lists.
5fill in blank
hard

Fill all three blanks to create a dictionary of Jaccard similarities for a list of sentence pairs.

NLP
def batch_jaccard(pairs):
    results = {}
    for i, (s1, s2) in enumerate(pairs):
        set1 = set(s1.lower().[1]())
        set2 = set(s2.lower().[2]())
        intersection = set1.[3](set2)
        union = set1.union(set2)
        if len(union) == 0:
            results[i] = 0
        else:
            results[i] = len(intersection) / len(union)
    return results
Drag options to blanks, or click blank then click option'
Asplit
Bstrip
Cintersection
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip() instead of split() results in wrong sets.
Using replace() or other methods that do not create word lists.
Using union instead of intersection for common elements.