Coverage for src/typoglycemia/main.py: 62%

1 import random

2 import argparse

3 import re

4

5 __version__ = "0.1.0"

6

7

8 def shuffle(word):

9 if len(word) <= 3:

10 return word

11

12 parts = re.split(r'([_\W]+)', word)

13 shuffled_parts = [

14 part if len(part) <= 3 or not part.isalnum() else part[0] + ''.join(random.sample(part[1:-1], len(part[1:-1]))) + part[-1]

15 for part in parts

16 ]

17

18 return ''.join(shuffled_parts)

19

20

21 def make_typoglycemia(sentence):

22 if not sentence:

23 return ""

24

25 return ' '.join(shuffle(word) for word in sentence.split())

26

27

28 def cli() -> int:

29 parser = argparse.ArgumentParser(

30 prog="Typoglycemia",

31 description="CLI tool to display text with Typoglycemia effect.",

32 )

33

34 parser.add_argument("-t", "--text", required=True, help="Text to process.")

35 parser.add_argument(

36 "-v",

37 "--version",

38 action="version",

39 version=__version__,

40 )

41 args = parser.parse_args()

42

43 typoglycemia = make_typoglycemia(args.text)

44 print(typoglycemia)

45 return 0

46

47

48 if __name__ == "__main__":

49 cli()