#!/usr/bin/env python3 # # Copyright 2023 David Soulayrol. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: import args import json import random import sys def select_next_word(word, stats): state = stats[word] choices = random.choices(list(state.keys()), weights=list(state.values())) return choices[0] if __name__ == '__main__': properties = args.parse('f:filename:s,w:first_word:s,n:words:d', sys.argv[1:]) stats = {} if len(properties.filename) == 0: print('Missing statistics file.') sys.exit(1) with open(properties.filename, 'r') as f: stats = json.loads(f.read()) n = properties.words if properties.words > 2 else 24 word = properties.first_word if len(properties.first_word) \ else random.choice(list(stats.keys())) if word not in stats.keys(): print('Unavailable word "%s".' % word) sys.exit(1) print(word, end=' ') for i in range(min(n - 1, 1000)): word = select_next_word(word, stats) print(word, end=' ')