|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Author : NowHappy <rlfmalehd@gmail.com> |
| 4 | +Date : 2021-10-09 |
| 5 | +Purpose: Telephone |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import random |
| 10 | +import os |
| 11 | +import string |
| 12 | + |
| 13 | +# -------------------------------------------------- |
| 14 | +def get_args(): |
| 15 | + """Get command-line arguments""" |
| 16 | + |
| 17 | + parser = argparse.ArgumentParser( |
| 18 | + description='Telephone', |
| 19 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 20 | + |
| 21 | + parser.add_argument('text', |
| 22 | + metavar='text', |
| 23 | + help='Input text of file') |
| 24 | + |
| 25 | + parser.add_argument('-s', |
| 26 | + '--seed', |
| 27 | + help='Random seed', |
| 28 | + metavar='seed', |
| 29 | + type=int, |
| 30 | + default=None) |
| 31 | + |
| 32 | + parser.add_argument('-m', |
| 33 | + '--mutations', |
| 34 | + help='Percent mutations', |
| 35 | + metavar='mutations', |
| 36 | + type=float, |
| 37 | + default=0.1) |
| 38 | + |
| 39 | + args = parser.parse_args() |
| 40 | + |
| 41 | + if args.mutations > 1 or args.mutations < 0: |
| 42 | + parser.error(f'--mutations "{args.mutations}" must be between 0 and 1') |
| 43 | + |
| 44 | + return args |
| 45 | + |
| 46 | + |
| 47 | +# -------------------------------------------------- |
| 48 | +def main(): |
| 49 | + """Make a jazz noise here""" |
| 50 | + |
| 51 | + args = get_args() |
| 52 | + text = args.text |
| 53 | + mutations = args.mutations |
| 54 | + random.seed(args.seed) |
| 55 | + alpha = ''.join(sorted(string.ascii_letters + string.punctuation)) |
| 56 | + |
| 57 | + new_text_line = [] |
| 58 | + if os.path.isfile(text): |
| 59 | + print('You said: ', end='') |
| 60 | + for line in open(text, 'rt'): |
| 61 | + new_text_line = list(line.rstrip()) |
| 62 | + print(f'"{line.rstrip()}"') |
| 63 | + len_text = len(line.rstrip()) |
| 64 | + num_mutations = round(len_text * mutations) |
| 65 | + for i in random.sample(range(len_text), num_mutations): |
| 66 | + # print(f'i = {i}, char = {line[i]}, index = {alpha.find(line[i])}') |
| 67 | + # list.index 함수로 색인 위치 찾으면 없을때 error 발생. 있는지 확인하고 쓰던가 아니면 find 함수 쓸 것!! |
| 68 | + # find 반환값 : 색인 위치. 찾을 수 없는 경우 -1 반환 |
| 69 | + new_text_line[i] = random.choice(alpha.replace(line[i], '')) # replace 함수는 line[i] 에 해당하는 문자가 alpha에 '있으면' 해당 문자를 치환한다. 없어도 error 안 남. |
| 70 | + print(f'I heard : "' + ''.join(new_text_line)+ '"') |
| 71 | + else: |
| 72 | + print(f'You said: "{text}"') |
| 73 | + new_text_line = list(text) |
| 74 | + len_text = len(text) |
| 75 | + num_mutations = round(len_text * mutations) |
| 76 | + for i in random.sample(range(len_text), num_mutations): |
| 77 | + new_text_line[i] = random.choice(alpha.replace(text[i], '')) |
| 78 | + print(f'I heard : "' + ''.join(new_text_line)+ '"') |
| 79 | + |
| 80 | + |
| 81 | +# -------------------------------------------------- |
| 82 | +if __name__ == '__main__': |
| 83 | + main() |
0 commit comments