Skip to content

Commit

Permalink
Use pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Feb 4, 2021
1 parent e1c894d commit 69f75d0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path
from setuptools import setup, find_packages
import os

NAME = "markovify"
HERE = os.path.abspath(os.path.dirname(__file__))
HERE = Path(__file__).resolve().parent

version_ns = {}
with open(os.path.join(HERE, NAME, '__version__.py'), encoding='utf-8') as f:
exec(f.read(), {}, version_ns)
exec((HERE / NAME / '__version__.py').read_text(), {}, version_ns)

with open(os.path.join(HERE, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
long_description = (HERE / 'README.md').read_text()

setup(
name="markovify",
Expand Down
50 changes: 22 additions & 28 deletions test/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import operator
import unittest
from pathlib import Path

import markovify
import os
import operator


TEXTS = Path(__file__).resolve().parent / 'texts'


def get_sorted(chain_json):
Expand Down Expand Up @@ -149,9 +153,7 @@ def test_min_words(self):

@staticmethod
def test_newline_text():
with open(os.path.join(os.path.dirname(__file__),
"texts/senate-bills.txt")) as f:
model = markovify.NewlineText(f.read())
model = markovify.NewlineText((TEXTS / 'senate-bills.txt').read_text())
model.make_sentence()

def test_bad_corpus(self):
Expand All @@ -178,25 +180,19 @@ def test_custom_regex(self):
class MarkovifyTest(MarkovifyTestBase):
__test__ = True

with open(os.path.join(os.path.dirname(__file__),
"texts/sherlock.txt")) as f:
sherlock_text = f.read()
sherlock_model = markovify.Text(sherlock_text)
sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2)
sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3)
sherlock_text = (TEXTS / 'sherlock.txt').read_text()
sherlock_model = markovify.Text(sherlock_text)
sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2)
sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3)


class MarkovifyTestCompiled(MarkovifyTestBase):
__test__ = True

with open(os.path.join(os.path.dirname(__file__),
"texts/sherlock.txt")) as f:
sherlock_text = f.read()
sherlock_model = (markovify.Text(sherlock_text)).compile()
sherlock_model_ss2 = (
markovify.Text(sherlock_text, state_size=2)).compile()
sherlock_model_ss3 = (
markovify.Text(sherlock_text, state_size=3)).compile()
sherlock_text = (TEXTS / 'sherlock.txt').read_text()
sherlock_model = markovify.Text(sherlock_text).compile()
sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2).compile()
sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3).compile()

def test_recompiling(self):
model_recompile = self.sherlock_model.compile()
Expand All @@ -211,15 +207,13 @@ def test_recompiling(self):
class MarkovifyTestCompiledInPlace(MarkovifyTestBase):
__test__ = True

with open(os.path.join(os.path.dirname(__file__),
"texts/sherlock.txt")) as f:
sherlock_text = f.read()
sherlock_model = markovify.Text(sherlock_text)
sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2)
sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3)
sherlock_model.compile(inplace=True)
sherlock_model_ss2.compile(inplace=True)
sherlock_model_ss3.compile(inplace=True)
sherlock_text = (TEXTS / 'sherlock.txt').read_text()
sherlock_model = markovify.Text(sherlock_text)
sherlock_model_ss2 = markovify.Text(sherlock_text, state_size=2)
sherlock_model_ss3 = markovify.Text(sherlock_text, state_size=3)
sherlock_model.compile(inplace=True)
sherlock_model_ss2.compile(inplace=True)
sherlock_model_ss3.compile(inplace=True)


if __name__ == '__main__':
Expand Down
23 changes: 13 additions & 10 deletions test/test_combine.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import operator
import unittest
from pathlib import Path

import markovify
import os
import operator


TEXTS = Path(__file__).resolve().parent / 'texts'


def get_sorted(chain_json):
return sorted(chain_json, key=operator.itemgetter(0))


with open(os.path.join(os.path.dirname(__file__), "texts/sherlock.txt")) as f:
sherlock = f.read()
sherlock_model = markovify.Text(sherlock)
sherlock_model_no_retain = markovify.Text(sherlock, retain_original=False)
sherlock_model_compiled = sherlock_model.compile()
sherlock_text = (TEXTS / 'sherlock.txt').read_text()
sherlock_model = markovify.Text(sherlock_text)
sherlock_model_no_retain = markovify.Text(sherlock_text, retain_original=False)
sherlock_model_compiled = sherlock_model.compile()


class MarkovifyTest(unittest.TestCase):
Expand Down Expand Up @@ -55,14 +58,14 @@ def test_bad_weights(self):

def test_mismatched_state_sizes(self):
with self.assertRaises(Exception):
text_model_a = markovify.Text(sherlock, state_size=2)
text_model_b = markovify.Text(sherlock, state_size=3)
text_model_a = markovify.Text(sherlock_text, state_size=2)
text_model_b = markovify.Text(sherlock_text, state_size=3)
markovify.combine([text_model_a, text_model_b])

def test_mismatched_model_types(self):
with self.assertRaises(Exception):
text_model_a = sherlock_model
text_model_b = markovify.NewlineText(sherlock)
text_model_b = markovify.NewlineText(sherlock_text)
markovify.combine([text_model_a, text_model_b])

def test_compiled_model_fail(self):
Expand Down
30 changes: 14 additions & 16 deletions test/test_itertext.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import unittest
from pathlib import Path

import markovify
import os


TEXTS = Path(__file__).resolve().parent / 'texts'


class MarkovifyTest(unittest.TestCase):

@staticmethod
def test_simple():
with open(os.path.join(os.path.dirname(__file__),
"texts/sherlock.txt")) as f:
sherlock_model = markovify.Text(f)
sherlock_model = markovify.Text((TEXTS / 'sherlock.txt').read_text())
sent = sherlock_model.make_sentence()
assert sent is not None
assert len(sent) != 0

@staticmethod
def test_without_retaining():
with open(os.path.join(os.path.dirname(__file__),
"texts/senate-bills.txt")) as f:
senate_model = markovify.Text(f, retain_original=False)
senate_model = markovify.Text((TEXTS / 'senate-bills.txt').read_text(),
retain_original=False)
sent = senate_model.make_sentence()
assert sent is not None
assert len(sent) != 0

@staticmethod
def test_from_json_without_retaining():
with open(os.path.join(os.path.dirname(__file__),
"texts/senate-bills.txt")) as f:
original_model = markovify.Text(f, retain_original=False)
d = original_model.to_json()
senate_model = markovify.Text((TEXTS / 'senate-bills.txt').read_text(),
retain_original=False)
d = senate_model.to_json()
new_model = markovify.Text.from_json(d)
sent = new_model.make_sentence()
assert sent is not None
Expand All @@ -37,11 +37,9 @@ def test_from_json_without_retaining():
@staticmethod
def test_from_mult_files_without_retaining():
models = []
for (dirpath, _, filenames) in os.walk(
os.path.join(os.path.dirname(__file__), "texts")):
for filename in filenames:
with open(os.path.join(dirpath, filename)) as f:
models.append(markovify.Text(f, retain_original=False))
for path in TEXTS.glob('*.txt'):
models.append(markovify.Text(path.read_text(),
retain_original=False))
combined_model = markovify.combine(models)
sent = combined_model.make_sentence()
assert sent is not None
Expand Down

0 comments on commit 69f75d0

Please sign in to comment.