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+
121from collections import deque
22+
23+ # Dependency imports
24+
225import numpy as np
326
4- from tensor2tensor .data_generators import problem , algorithmic
27+ from tensor2tensor .data_generators import algorithmic
528from tensor2tensor .utils import registry
629
730
831@registry .register_problem
932class CipherShift5 (algorithmic .AlgorithmicProblem ):
33+ """Shift cipher."""
1034
1135 @property
1236 def num_symbols (self ):
@@ -49,6 +73,7 @@ def dev_length(self):
4973
5074@registry .register_problem
5175class CipherVigenere5 (algorithmic .AlgorithmicProblem ):
76+ """Vinegre cipher."""
5277
5378 @property
5479 def num_symbols (self ):
@@ -91,6 +116,7 @@ def dev_length(self):
91116
92117@registry .register_problem
93118class CipherShift200 (CipherShift5 ):
119+ """Shift cipher."""
94120
95121 @property
96122 def num_symbols (self ):
@@ -105,6 +131,7 @@ def distribution(self):
105131
106132@registry .register_problem
107133class CipherVigenere200 (CipherVigenere5 ):
134+ """Vinegre cipher."""
108135
109136 @property
110137 def num_symbols (self ):
@@ -121,16 +148,17 @@ def key(self):
121148 return [1 , 3 ]
122149
123150
124- class Layer ():
125- """A single layer for shift"""
151+ class Layer (object ):
152+ """A single layer for shift. """
126153
127154 def __init__ (self , vocab , shift ):
128- """Initialize shift layer
155+ """Initialize shift layer.
129156
130157 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.
134162 """
135163 self .shift = shift
136164 alphabet = vocab
@@ -149,10 +177,17 @@ def decrypt_character(self, character):
149177def generate_plaintext_random (plain_vocab , distribution , train_samples ,
150178 length ):
151179 """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+
152187 Returns:
153- train_indices (np.array of Integers): random integers generated for training.
188+ train_indices (np.array of Integers): random integers for training.
154189 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.
156191 shape = [num_samples, length]
157192 plain_vocab (list of Integers): unique vocabularies.
158193 """
@@ -166,7 +201,8 @@ def generate_plaintext_random(plain_vocab, distribution, train_samples,
166201
167202
168203def 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+
170206 Args:
171207 plaintext (list of list of Strings): a list of plain text to encrypt.
172208 plain_vocab (list of Integer): unique vocabularies being used.
@@ -177,9 +213,9 @@ def encipher_shift(plaintext, plain_vocab, shift):
177213 ciphertext = []
178214 cipher = Layer (plain_vocab , shift )
179215
180- for i , sentence in enumerate (plaintext ):
216+ for _ , sentence in enumerate (plaintext ):
181217 cipher_sentence = []
182- for j , character in enumerate (sentence ):
218+ for _ , character in enumerate (sentence ):
183219 encrypted_char = cipher .encrypt_character (character )
184220 cipher_sentence .append (encrypted_char )
185221 ciphertext .append (cipher_sentence )
@@ -188,11 +224,13 @@ def encipher_shift(plaintext, plain_vocab, shift):
188224
189225
190226def encipher_vigenere (plaintext , plain_vocab , key ):
191- """Encrypt plain text with given key
227+ """Encrypt plain text with given key.
228+
192229 Args:
193230 plaintext (list of list of Strings): a list of plain text to encrypt.
194231 plain_vocab (list of Integer): unique vocabularies being used.
195232 key (list of Integer): key to encrypt cipher using Vigenere table.
233+
196234 Returns:
197235 ciphertext (list of Strings): encrypted plain text.
198236 """
0 commit comments