def test_markov_with_higher_level(size=100, file_name="sample1.txt"): markov = Markov() mark = markov.train_model_higher( normalize(os.getcwd() + "\\samples\\" + file_name)) text = markov.generate_sentence(size, mark) save_text(text) save_long_file(text, file_name) print(text)
def main(argv): update_data = False generate_sentences = False interactive_mode = False if len(argv) < 2: print("No params specified. Using default values.") argv.extend(("-u", "-g")) args_dict = { i : True for i in argv } if "-u" in args_dict: update_data = True if "-g" in args_dict: generate_sentences = True if "-h" in args_dict: print(get_help_info()) return if "-a" in args_dict: print(get_about_info()) return if "-i" in args_dict: interactive_mode = True if update_data: dirname = "rawdata" shutil.rmtree(dirname, ignore_errors=True) filename = "news.zip" yesterday = datetime.date.today() - datetime.timedelta(days=1) network.download_file(network.URL.format(yesterday), filename) with zipfile.ZipFile(filename, 'r') as zip_ref: zip_ref.extractall(dirname) parse_files(dirname + "/day") if generate_sentences: markov = Markov(OUTFILE) markov.get_sentences() if interactive_mode: markov = Markov(OUTFILE) outfile = open(GOODFILE, 'a', encoding="utf-8") # TODO: post these news while True: sentence = markov.generate_sentence() print(sentence) user_input = getch() if user_input == "\x1b": break elif user_input == "+": outfile.write(sentence + "\n") pass outfile.close()
class TestMarkovMethods(unittest.TestCase): """Test the Markov-Chain.""" def setUp(self): """Set up a Markov Chain instance for testing.""" self.m = Markov() def test_new_word_entry(self): """Make sure that a new word and its next word are properly entered.""" self.m.add_next_word('hello', 'world') self.assertEqual(self.m.chain, {'hello': ['world']}) def test_additional_next_word_entry(self): """Test insertion of a new next word for an existing current word.""" self.m.add_next_word('hello', 'world') self.m.add_next_word('hello', 'you') self.assertEqual(self.m.chain, {'hello': ['world', 'you']}) def test_paragraph_entry(self): """ Test insertion of a paragraph into the chains. Verify the logic concerning the period. """ self.m.add_text(['hello', 'world', '.', 'goodnight', 'moon', '.']) self.assertEqual(self.m.chain, { '.': ['hello', 'goodnight'], 'hello': ['world'], 'world': ['.'], 'goodnight': ['moon'], 'moon': ['.']}) def test_sentence_generation(self): """Test traversing through Markov-Chain dict to create sentence.""" self.m.add_text(['hello', 'world', '.']) self.m.add_text(['hello', 'you', '.']) self.assertIn( self.m.generate_sentence(4), ['hello world.', 'hello you.'])
def gen_markov_higher(lst, size=100, file_name="sample1.txt"): markov = Markov() mark = markov.train_model_higher(normalize(os.getcwd() + "\\samples\\" + file_name)) for i in range(30): lst.append(markov.generate_sentence(size, mark)) return lst