方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Compute semantic similarity for evaluation

Compute Semantic Similarity for Evaluation

Embedding-based similarity captures whether two texts mean the same thing even with different wording — essential for evaluating open-ended generation where BLEU/ROUGE produce false negatives.

What to build

Implement SemanticSimilarityScorer:

  1. score(prediction, reference) -> float — Embeds both with injected embed_fn, returns cosine similarity (0.0-1.0). Returns 0.0 (with a logged warning) if embed_fn raises.

  2. score_batch(cases: list[dict]) -> dict — Each case has "prediction" and "reference". Returns {"avg_similarity", "above_threshold", "n"}. Threshold set in constructor (default 0.8).

  3. find_low_similarity_cases(cases, threshold=None) -> list[dict] — Returns cases below threshold with "similarity" added.

Constraints

  • Standard library only (implement cosine manually). No external embedding library.

Evaluation / medium / Step 20 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.