PIXELBANKv9.1.0
Menu

Generate text using a bigram language model with deterministic selection (always pick the most probable next word).

Build a bigram model from training data. Starting from <s>, at each step choose the word with the highest P(w | prev_word). If there is a tie, choose the alphabetically first word. Stop when </s> is generated or max_length words are reached.

Input format:

  • Line 1: Number of training sentences N
  • Lines 2 to N+1: Training sentences
  • Line N+2: max_length (maximum number of generated words, not counting <s>/</s>)

Output: The generated sentence (words only, no <s> or </s>).

Note: Use raw counts (no smoothing) for generation — pick the highest count bigram.

Example:

Input:
3
i love nlp
i love ml
i hate bugs
5
Output:
i love ml
Reasoning:

Bigram counts from training: (<s>, i): 3 — only option after <s> (i, love): 2, (i, hate): 1 — pick "love" (love, nlp): 1, (love, ml): 1 — tie, pick "ml" (alphabetically first) (ml, </s>): 1 — pick </s>, stop

Generated: "i love ml"

Constraints:

  • Start from <s>, pick highest-count next word
  • Ties broken alphabetically (smallest word first)
  • Stop at </s> or when max_length words generated
  • Do NOT include <s> or </s> in output
  • Use raw counts (no smoothing) for generation
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.