Complete the code to calculate the Jaccard similarity between two sets.
def jaccard_similarity(set1, set2): intersection = set1.intersection(set2) union = set1.[1](set2) return len(intersection) / len(union)
The Jaccard similarity is the size of the intersection divided by the size of the union of two sets. Here, union method returns the union of two sets.
Complete the code to convert two sentences into sets of words for Jaccard similarity calculation.
def sentence_to_set(sentence): words = sentence.lower().split() return set([1])
We first split the sentence into words and then convert the list words into a set to remove duplicates.
Fix the error in the Jaccard similarity function to avoid division by zero.
def jaccard_similarity(set1, set2): intersection = set1.intersection(set2) union = set1.union(set2) if len(union) == 0: return [1] return len(intersection) / len(union)
If both sets are empty, their union length is zero. Returning 0 avoids division by zero and logically means no similarity.
Fill both blanks to create a function that computes Jaccard similarity from two sentences.
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)
We use split() to break sentences into words before converting to sets.
Fill all three blanks to create a dictionary of Jaccard similarities for a list of sentence pairs.
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
We split sentences into words, then use intersection to find common words for Jaccard similarity.