#!/usr/bin/env python3
import sys
import random
import itertools
import os
from urllib import request
from pathlib import Path

URL = "https://github.com/first20hours/google-10000-english/raw/master/google-10000-english.txt"
cache = Path("first20hours.o")
if not cache.is_file():
    cache.write_bytes(request.urlopen(URL).read())


def Print(*ap, **an):
    print(*ap, **an)
    if not os.isatty(sys.stdout.fileno()):
        print(*ap, **an, file=sys.stderr)


N = 10 if len(sys.argv) < 2 else int(sys.argv[1])
R = N * 3 // 2 if len(sys.argv) < 3 else int(sys.argv[2])
words = random.sample([w for w in cache.read_text().split() if 2 < len(w) < 8], k=N)

s = 0
res = []
From, To = words[0], words[-1]
for sn in sys.argv[3:] or [N]:
    n = int(sn)
    gen = []
    while len(gen) < n * R / N:
        inp = random.sample(words[s: s + n], k=n)
        gen.extend(list(zip(inp[:-1], inp[1:])))
    res.extend(gen[: 1 + n * R // N])
    print("*", *set(itertools.chain(*gen[: 1 + n * R // N])), file=sys.stderr)
    s += n
random.shuffle(res)
for a, b in res:
    Print("{} {}".format(*((a, b) if random.randrange(2) else (b, a))))
Print(From, To, sep="\n")
