forked from faklyasgyuri/scramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscramble.py
123 lines (108 loc) · 4.11 KB
/
scramble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
__author__ = 'Rudolf Horvath'
import doctest
class TextBlock:
"""
This class represents a text sequence. It can have a given type like *word*, *special*,
and it can be scrambled.
"""
textTypes = {"word":0, "special":1}
def scramble(self):
"""
This function leaves the first and last character in this TextBlock in place,
but the remaining middle characters will be written back in a reverse order.
>>> test = TextBlock("", "word")
>>> test.scramble()
>>> print test
<BLANKLINE>
>>> test.text = "a"
>>> test.scramble()
>>> print test
a
>>> test.text = "ab"
>>> test.scramble()
>>> print test
ab
>>> test.text = "abc"
>>> test.scramble()
>>> print test
abc
>>> test.text = "abcd"
>>> test.scramble()
>>> print test
acbd
"""
block = self.text
length = len(block)
wasStr = False
if length < 4:
return
if type(block) == type(''):
wasStr = True
block = list(block)
for j in range(1, (length/2)):
tmp = block[j]
block[j] = block[length-j-1]
block[length-j-1] = tmp
if wasStr:
self.text = ''.join(block)
else:
self.text = block
def __str__(self):
return self.text
def __init__(self, text, type):
assert type in TextBlock.textTypes, "Not valid textType"
self.type = TextBlock.textTypes[type]
self.text = text
class TextSplitter:
"""
This class splits a given text sequence into words and other remaining character sequences.
It can be given, what characters don't represent words.
The word blocks can be scrambled, to get a special text.
>>> test = TextSplitter("")
>>> test.scrambleWords()
>>> print test
<BLANKLINE>
>>> test = TextSplitter("The result is: 1234", " :0123456789")
>>> test.scrambleWords()
>>> print test
The rluset is: 1234
>>> test = TextSplitter("According to research at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole.")
>>> test.scrambleWords()
>>> print test
Anidroccg to rcraeseh at Cgdirbmae Utisreviny, it dseon't mettar in waht oedrr the lrettes in a wrod are, the olny inatropmt tnihg is taht the fsrit and lsat letter be at the rhgit pcale. The rset can be a tatol mses and you can slitl raed it wuohtit a pelborm. Tihs is bsuacee the hamun mnid deos not raed erevy letter by ilestf, but the wrod as a wlohe.
"""
def scrambleWords(self):
for block in self.blocks:
if block.type == TextBlock.textTypes["word"]:
block.scramble()
def __init__(self, input, splitTokens=" .,?!;'\n\t\"\r"):
self.blocks = []
if len(input) == 0:
return
tmpBlock = ""
splitFlow = input[0] in splitTokens
for item in input:
if item in splitTokens:
if not splitFlow:
newBlock = TextBlock(tmpBlock, "word")
self.blocks.append(newBlock)
tmpBlock = ""
splitFlow = True
else:
if splitFlow:
newBlock = TextBlock(tmpBlock, "special")
self.blocks.append(newBlock)
tmpBlock = ""
splitFlow = False
tmpBlock += item
lastBlockType = "special" if input[-1] in splitTokens else "word"
newBlock = TextBlock(tmpBlock, lastBlockType)
self.blocks.append(newBlock)
def __str__(self):
res = ""
for block in self.blocks:
res += str(block)
return res
if __name__ == "__main__":
doctest.testmod(verbose=True)
#doctest.testfile("scramble.test", verbose=True)