From: Eugene Petkevich Date: Wed, 21 Jul 2021 16:16:01 +0000 (+0300) Subject: Make simple implementation X-Git-Tag: v0.0.1~3 X-Git-Url: https://git.zuelum.org/?a=commitdiff_plain;h=8ae531b6ca14456d0002e631e2f1f887611b1998;p=word-id-generator.git Make simple implementation This does not take into account some consonant-vowel combinations --- diff --git a/README.md b/README.md index a648f5c..fccdcf9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ *Work is in the earliest stage of development.* -Эта программа создаёт слова для использования в качестве идентификаторов. +Эта программа создаёт слова для использования в качестве уникальных идентификаторов. Каждое слово состоит из нескольких слогов согласная-гласная. ## License diff --git a/wordidgen/wordidgen.py b/wordidgen/wordidgen.py index ff0531f..37da0cd 100755 --- a/wordidgen/wordidgen.py +++ b/wordidgen/wordidgen.py @@ -3,20 +3,61 @@ from argparse import ArgumentDefaultsHelpFormatter from argparse import ArgumentParser +import random + +class WordGenerator: + """Generate word by combining syllables""" + + def __init__(self, + consonants='бвгджзклмнпрстфхцчшщ', + vowels='аяоёуюэеыи', + length=4): + self.consonants = consonants + self.vowels = vowels + self.length = length + self.syllable_count = len(self.consonants) * len(self.vowels) + self.count = self.syllable_count ** self.length + + def generate(self, capitalize=False, number=None): + """Generate a word""" + word = '' + if number is None: + number = random.randrange(self.count) + for i in range(self.length): + r = number % self.syllable_count + number = number // self.syllable_count + c_index = r // len(self.vowels) + v_index = r % len(self.vowels) + word += self.consonants[c_index] + self.vowels[v_index] + print(number, word, r, c_index, v_index) + if capitalize: + word = word.capitalize() + return word + def main(): """Helper command-line tool for word id generator.""" parser = ArgumentParser(description='Word id generator', formatter_class=ArgumentDefaultsHelpFormatter) - parser.add_argument('-l', '--len', + parser.add_argument('-l', '--length', type=int, + default=4, help='number of syllables in the word') - parser.add_argument('-c', '--capitalize', + parser.add_argument('-n', '--number', + type=int, + default=-1, + help='number of the word (random otherwise)') + parser.add_argument('-t', '--title', action='store_true', help='whether the word should be capitalized') + parser.add_argument('-c', '--count', + action='store_true', + help='print number of possible words') args = parser.parse_args() - word = 'хо' * args.len - if args.capitalize: - word = word.capitalize() + worg_generator = WordGenerator(length=args.length) + if args.count: + print(worg_generator.count) + return + word = worg_generator.generate(capitalize=args.title) print(word) if __name__ == '__main__':