def setUpClass(self): with open("./common/_data.json", 'r', encoding="utf-8") as file: self._DATA = json.load(file) # get a data from the json file. self._text = self._DATA[self.text_name]["text"] self._correct_data = self._DATA[self.text_name]["data"] # create a MarkovChain instance for testing. self.chain = MarkovChain() self.chain.init(self._text)
class GenerationTest(object): """Test for generate() method.""" @classmethod def setUpClass(self): with open("./common/_data.json", 'r', encoding="utf-8") as file: self._DATA = json.load(file) # create a MarkovChain instance for testing. self.chain = MarkovChain() self.chain._chain = self._DATA[self.text_name]["chain"][ self.chain_window] # create a generate() parameters. self.max_words = 20 # default. self.max_length = self.max_words * 10 # default. # create a test parameters. self.number_of_tests = 50 def test_empty_text(self): generated_text = self.chain.generate(max_words=self.max_words, max_length=self.max_length) self.assertNotEqual( generated_text, '', "The text with the default data cannot be an empty") def test_type_text(self): generated_text = self.chain.generate(max_words=self.max_words, max_length=self.max_length) self.assertIsInstance(generated_text, str, "The text should have a str type") def test_words_length(self): # self.subTest not needed here. # if first failure, then a chain already incorrect. for i in range(self.number_of_tests): generated_text = self.chain.generate(max_words=self.max_words, max_length=self.max_length) self.assertTrue( len(generated_text.split()) <= self.max_words, "A count of words in the text cannot be > max_words") def test_length_text(self): # self.subTest not needed here. # if first failure, then a chain already incorrect. for i in range(self.number_of_tests): generated_text = self.chain.generate(max_words=self.max_words, max_length=self.max_length) self.assertTrue( len(generated_text) <= self.max_length, "A length of the text cannot be > max_length")
def setUpClass(self): with open("./common/_data.json", 'r', encoding="utf-8") as file: self._DATA = json.load(file) # create a MarkovChain instance for testing. self.chain = MarkovChain() self.chain._chain = self._DATA[self.text_name]["chain"][ self.chain_window] # create a generate() parameters. self.max_words = 20 # default. self.max_length = self.max_words * 10 # default. # create a test parameters. self.number_of_tests = 50
class InitializationTest(object): """Test for init() method.""" @classmethod def setUpClass(self): with open("./common/_data.json", 'r', encoding="utf-8") as file: self._DATA = json.load(file) # get a data from the json file. self._text = self._DATA[self.text_name]["text"] self._correct_data = self._DATA[self.text_name]["data"] # create a MarkovChain instance for testing. self.chain = MarkovChain() self.chain.init(self._text) def test_init_method(self): self.assertEqual(list(self.chain._data), self._correct_data, "_data is not correct")
class CreationTest(object): """Test for create() method.""" @classmethod def setUpClass(self): with open("./common/_data.json", 'r', encoding="utf-8") as file: self._DATA = json.load(file) # get a data from the json file. self._text = self._DATA[self.text_name]["text"] self._correct_chain = self._DATA[self.text_name]["chain"][ self.chain_window] # create a MarkovChain instance for testing. self.chain = MarkovChain(window=self.window) self.chain.init(self._text) self.chain.create() def test_create_method(self): self.assertEqual(self.chain._chain, self._correct_chain, "_chain is not correct")
def start_generation(text_instance, parameters_instance): """Start generation of one parameter example.""" markov_chain = MarkovChain( **parameters_instance.get("parameters", {}).get("init", {}) ) start( chain=markov_chain, text=text_instance, parameters=parameters_instance.get("parameters", {}) ) print('\n')
def main(): """Execute examples.""" text = functions.read_json("./common/text.json") markov_chain = MarkovChain() markov_chain.init(text["text"]["simple"]) markov_chain.create() file.json.save(markov_chain.chain, "./common/simple_example.json") file.pickle.save(markov_chain.chain, "./common/simple_example.pickle") file.sqlite.save(markov_chain.chain, "./common/simple_example.sqlite") markov_chain.chain = file.json.read("./common/simple_example.json") markov_chain.chain = file.pickle.read("./common/simple_example.pickle") # markov_chain.generate() markov_chain_sqlite = file.SQLiteFile(path=".common/simple_example.sqlite")
def generation_arguments(text): """Generation arguments example.""" chain = MarkovChain("Insane test MarkovChain instance") chain.init(text) chain.create() return chain.generate(start="You", max_words=10, max_length=20)
def instance_arguments(text): """Instance arguments example.""" chain = MarkovChain("Hard test MarkovChain instance", window=2) chain.init(text) chain.create() print(chain.generate())
def basic_usage(text): """Basic usage example.""" chain = MarkovChain("Simple test MarkovChain instance") chain.init(text) chain.create() print(chain.generate())