#! /usr/bin/python3
# Last edited on 2026-04-21 18:54:55 by stolfi

import sys, os, re
from sys import stdin as inp, stdout as out, stderr as err
import random; from random import randint as irand, random as frand

def main(ttype, min_wlen, max_wlen):
  # Generates sample text with words of 2 letters (90%) or 20 letters
  # (10%) with 50% probability of long after a long.
  # 
  
  random.seed(4617)
  
  # World length distribution:
  PL = 0.50    # Freq of long words. 
  PS = 1 - PL # Freq of short words.

  # Compute transition probabilities:
  PLL = PL if ttype == 0 else 0.70 # Prob of long after long.
  PLS = 1 - PLL   # Prob of short after long.
  PSL = PLS*PL/PS # Prob of long after short.
  PSS = 1 - PSL   # Prob of short after short;
  err.write(f" Pr(L->L) = {PLL:6.4f} Pr(L->S) = {PLS:6.4f}\n")
  err.write(f" Pr(S->L) = {PSL:6.4f} Pr(S->S) = {PSS:6.4f}\n")
  
  prev = None
  state = 0 if frand() < PS else 1
  N = 200000 # Num of words.
  trcount = [ [ 0, 0 ], [0, 0 ] ]
  for i in range(N):
    prev = state
    if state == 0:
      # Spit out a long word:
      word = ( "shochdy", "qokaiin", "ytchedy" )[ irand(0,2) ]
      out.write(word);
      state = 0 if frand() < PLL else 1
    elif state == 1:
      # Spit out a short word:
      word = ( "ar", "ol", "dy" )[ irand(0,2) ]
      out.write(word)
      state = 1 if frand() < PSS else 0
    else:
      assert False
    out.write("\n")
    trcount[prev][state] += 1
  out.flush()
  err.write(f"{trcount = !r}\n")
  nL = trcount[0][0] + trcount[0][1]; fL = nL/N
  nS = trcount[1][0] + trcount[1][1]; fS = nS/N
  fLL = trcount[0][0]/nL; fLS = trcount[0][1]/nL
  fSL = trcount[1][0]/nS; fSS = trcount[1][1]/nS
  err.write(f" Fr(L->L) = {fLL:6.4f} Fr(L->S) = {fLS:6.4f}\n")
  err.write(f" Fr(S->L) = {fSL:6.4f} Fr(S->S) = {fSS:6.4f}\n")
  
  return
  # ----------------------------------------------------------------------
  
iarg = 1
ttype = int(sys.argv[iarg]); iarg += 1
min_wlen = int(sys.argv[iarg]); iarg += 1
max_wlen = int(sys.argv[iarg]); iarg += 1
main(ttype, min_wlen, max_wlen)
