1
+ # coding=utf-8
2
+ # Copyright 2017 The Tensor2Tensor Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """Cipher data generators."""
17
+ from __future__ import absolute_import
18
+ from __future__ import division
19
+ from __future__ import print_function
20
+
1
21
from collections import deque
22
+
23
+ # Dependency imports
24
+
2
25
import numpy as np
3
26
4
- from tensor2tensor .data_generators import problem , algorithmic
27
+ from tensor2tensor .data_generators import algorithmic
5
28
from tensor2tensor .utils import registry
6
29
7
30
8
31
@registry .register_problem
9
32
class CipherShift5 (algorithmic .AlgorithmicProblem ):
33
+ """Shift cipher."""
10
34
11
35
@property
12
36
def num_symbols (self ):
@@ -49,6 +73,7 @@ def dev_length(self):
49
73
50
74
@registry .register_problem
51
75
class CipherVigenere5 (algorithmic .AlgorithmicProblem ):
76
+ """Vinegre cipher."""
52
77
53
78
@property
54
79
def num_symbols (self ):
@@ -91,6 +116,7 @@ def dev_length(self):
91
116
92
117
@registry .register_problem
93
118
class CipherShift200 (CipherShift5 ):
119
+ """Shift cipher."""
94
120
95
121
@property
96
122
def num_symbols (self ):
@@ -105,6 +131,7 @@ def distribution(self):
105
131
106
132
@registry .register_problem
107
133
class CipherVigenere200 (CipherVigenere5 ):
134
+ """Vinegre cipher."""
108
135
109
136
@property
110
137
def num_symbols (self ):
@@ -121,16 +148,17 @@ def key(self):
121
148
return [1 , 3 ]
122
149
123
150
124
- class Layer ():
125
- """A single layer for shift"""
151
+ class Layer (object ):
152
+ """A single layer for shift. """
126
153
127
154
def __init__ (self , vocab , shift ):
128
- """Initialize shift layer
155
+ """Initialize shift layer.
129
156
130
157
Args:
131
- vocab (list of String): the vocabulary
132
- shift (Integer): the amount of shift apply to the alphabet. Positive number implies
133
- shift to the right, negative number implies shift to the left.
158
+ vocab: (list of String) the vocabulary
159
+ shift: (Integer) the amount of shift apply to the alphabet.
160
+ Positive number implies shift to the right, negative number
161
+ implies shift to the left.
134
162
"""
135
163
self .shift = shift
136
164
alphabet = vocab
@@ -149,10 +177,17 @@ def decrypt_character(self, character):
149
177
def generate_plaintext_random (plain_vocab , distribution , train_samples ,
150
178
length ):
151
179
"""Generates samples of text from the provided vocabulary.
180
+
181
+ Args:
182
+ plain_vocab: vocabulary.
183
+ distribution: distribution.
184
+ train_samples: samples for training.
185
+ length: length.
186
+
152
187
Returns:
153
- train_indices (np.array of Integers): random integers generated for training.
188
+ train_indices (np.array of Integers): random integers for training.
154
189
shape = [num_samples, length]
155
- test_indices (np.array of Integers): random integers generated for testing.
190
+ test_indices (np.array of Integers): random integers for testing.
156
191
shape = [num_samples, length]
157
192
plain_vocab (list of Integers): unique vocabularies.
158
193
"""
@@ -166,7 +201,8 @@ def generate_plaintext_random(plain_vocab, distribution, train_samples,
166
201
167
202
168
203
def encipher_shift (plaintext , plain_vocab , shift ):
169
- """Encrypt plain text with a single shift layer
204
+ """Encrypt plain text with a single shift layer.
205
+
170
206
Args:
171
207
plaintext (list of list of Strings): a list of plain text to encrypt.
172
208
plain_vocab (list of Integer): unique vocabularies being used.
@@ -177,9 +213,9 @@ def encipher_shift(plaintext, plain_vocab, shift):
177
213
ciphertext = []
178
214
cipher = Layer (plain_vocab , shift )
179
215
180
- for i , sentence in enumerate (plaintext ):
216
+ for _ , sentence in enumerate (plaintext ):
181
217
cipher_sentence = []
182
- for j , character in enumerate (sentence ):
218
+ for _ , character in enumerate (sentence ):
183
219
encrypted_char = cipher .encrypt_character (character )
184
220
cipher_sentence .append (encrypted_char )
185
221
ciphertext .append (cipher_sentence )
@@ -188,11 +224,13 @@ def encipher_shift(plaintext, plain_vocab, shift):
188
224
189
225
190
226
def encipher_vigenere (plaintext , plain_vocab , key ):
191
- """Encrypt plain text with given key
227
+ """Encrypt plain text with given key.
228
+
192
229
Args:
193
230
plaintext (list of list of Strings): a list of plain text to encrypt.
194
231
plain_vocab (list of Integer): unique vocabularies being used.
195
232
key (list of Integer): key to encrypt cipher using Vigenere table.
233
+
196
234
Returns:
197
235
ciphertext (list of Strings): encrypted plain text.
198
236
"""
0 commit comments