@@ -0,0 +1,1397 @@
+#!/usr/bin/env python3%0A%0Aimport argparse%0Aimport collections%0Aimport random%0A%0Aimport sys%0A%0A%0Adef reconstruct(f_in, f_out):%0A    sentence_starts = %5B%5D%0A%0A    contexts = %7B%7D%0A%0A    for line in f_in:%0A        parts = line.split()%0A        words = parts%5B:-1%5D%0A        count = int(parts%5B-1%5D)%0A%0A        if words%5B0%5D == %22%3Cs%3E%22 and words%5B-1%5D == %22%3C/s%3E%22:%0A            for _ in range(count):%0A                print(%22 %22.join(words), file=f_out)%0A            continue%0A%0A        context = tuple(words%5B:-1%5D)%0A        if context not in contexts:%0A            contexts%5Bcontext%5D = collections.Counter()%0A%0A        contexts%5Bcontext%5D%5Bwords%5B-1%5D%5D += count%0A%0A    random.shuffle(sentence_starts)%0A%0A    c = len(sentence_starts%5B0%5D) - 1%0A%0A    for start in sentence_starts:%0A        line = list(start)%0A        while line%5B-1%5D != %22%3C/s%3E%22:%0A            context = line%5B:-c%5D%0A            next_word = contexts%5Bcontext%5D.most_common(1)%5B0%5D%5B0%5D%0A            contexts%5Bcontext%5D%5Bnext_word%5D -= 1%0A            line.append(next_word)%0A%0A        print(%22 %22.join(line), file=f_out)%0A%0A%0Aif __name__ == %22__main__%22:%0A    parser = argparse.ArgumentParser(description='Construct corpus')%0A%0A    parser.add_argument('infile', nargs='?', type=argparse.FileType('r', encoding='utf-8'), default=sys.stdin)%0A    parser.add_argument('outfile', nargs='?', type=argparse.FileType('w', encoding='utf-8'), default=sys.stdout)%0A%0A    args = parser.parse_args()%0A%0A    reconstruct(args.infile, args.outfile)%0A%0A%0A

Add reconstruct corpus as a test