Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Semantic Similarity Scoring Function

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Semantic Similarity Scoring Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Adobe Firefly evaluation pipelines need a deterministic score for comparing generated captions with reference text. Implement a custom similarity function that handles case differences, punctuation, stop words, and a small synonym vocabulary without using external NLP libraries.

Normalize both strings by lowercasing, extracting alphanumeric word tokens, removing common stop words, and mapping known synonyms to a canonical token. Then calculate:

  1. Unigram Dice similarity: 2 * intersection_count / (generated_count + reference_count) using token frequencies.
  2. Bigram Dice similarity: the same formula using adjacent token pairs, with a score of 1.0 when both normalized texts contain no bigrams.
  3. Return 0.7 * unigram_score + 0.3 * bigram_score, rounded to four decimal places.

Treat two empty normalized texts as identical with a score of 1.0. If only one is empty, return 0.0.

Formal Specification

Implement score_similarity(generated, reference), where both inputs are strings. Return a floating-point number in [0.0, 1.0]. The synonym mapping must include car/automobile/vehicle, image/picture/photo, buy/purchase, create/creates/created/generate/generated, and quick/fast/rapidly.

Constraints

  • Both inputs are strings.
  • Each input contains at most 10^4 characters.
  • Tokens consist of Unicode letters or digits separated by punctuation or whitespace.
  • The synonym vocabulary is fixed and case-insensitive.
  • The returned score must be rounded to four decimal places and lie in [0.0, 1.0].

Function Signature

def score_similarity(generated, reference):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output