Last active
January 4, 2017 08:17
-
-
Save ammgws/9d2b4d278ae281d3ef282c437c0cef5e to your computer and use it in GitHub Desktop.
markovify model export benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import markovify | |
# modified markovify Text class | |
class EditedTextClass(markovify.Text): | |
def __init__(self, input_text, state_size=2, chain=None, runs=None): | |
""" | |
input_text: A string. | |
state_size: An integer, indicating the number of words in the model's state. | |
chain: A trained markovify.Chain instance for this text, if pre-processed. | |
""" | |
self.input_text = input_text | |
self.state_size = state_size | |
self.runs = runs or list(self.generate_corpus(self.input_text)) | |
# Rejoined text lets us assess the novelty of generated setences | |
self.rejoined_text = self.sentence_join(map(self.word_join, self.runs)) | |
self.chain = chain or markovify.Chain(self.runs, state_size) | |
def to_dict(self): | |
return { | |
"input_text": self.input_text, | |
"state_size": self.state_size, | |
"chain": self.chain.to_json(), | |
"runs": self.runs | |
} | |
@classmethod | |
def from_dict(cls, obj): | |
return cls( | |
obj["input_text"], | |
state_size=obj["state_size"], | |
chain=markovify.Chain.from_json(obj["chain"]), | |
runs=obj["runs"] | |
) | |
# Load test corpus to use for tests | |
with open('sherlock.txt', 'r', encoding='utf-8') as f: | |
corpus = f.read() | |
# Create text model and get the generated model & chain JSONs to use in tests | |
# Standard markovify: | |
text_model = markovify.Text(corpus, state_size=2, chain=None) | |
chain_json_ss2 = text_model.chain.to_json() | |
model_json_ss2 = text_model.to_json() | |
# Modified markovify: | |
modified_text_model = EditedTextClass(corpus, state_size=2, chain=None) | |
modifiedmarkov_chain_json_ss2 = modified_text_model.chain.to_json() | |
modifiedmarkov_model_json_ss2 = modified_text_model.to_json() | |
# test functions for modified markovify class | |
def modifiedmarkovify_existing_model_size_2(): | |
return EditedTextClass.from_json(modifiedmarkov_model_json_ss2) | |
def modifiedmarkovify_existing_chain_size_2(): | |
return EditedTextClass.from_chain(modifiedmarkov_chain_json_ss2, corpus=corpus) | |
def modifiedmarkovify_new_model_size_2(): | |
return EditedTextClass(corpus, state_size=2, chain=None) | |
# test functions for current markovify | |
# Using existing model JSON | |
def existing_model_size_2(): | |
return markovify.Text.from_json(model_json_ss2) | |
# Using existing chain JSON | |
def existing_chain_size_2(): | |
return markovify.Text.from_chain(chain_json_ss2, corpus=corpus) | |
# From scratch | |
def new_model_size_2(): | |
return markovify.Text(corpus, state_size=2, chain=None) | |
if __name__ == '__main__': | |
import timeit | |
number_tests = 25 | |
print('Running {} times each, returning the average runtime in seconds.\n'.format(number_tests)) | |
print('Current markovify, create new model, state size 2:') | |
print(timeit.timeit("new_model_size_2()", setup="from __main__ import new_model_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(new_model_size_2().make_short_sentence(120))) | |
print('----------------------------------') | |
print('Current markovify, existing chain JSON, state size 2:') | |
print(timeit.timeit("existing_chain_size_2()", setup="from __main__ import existing_chain_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(existing_chain_size_2().make_short_sentence(120))) | |
print('----------------------------------') | |
print('Current markovify, existing model JSON, state size 2:') | |
print(timeit.timeit("existing_model_size_2()", setup="from __main__ import existing_model_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(existing_model_size_2().make_short_sentence(120))) | |
print('----------------------------------') | |
print('\n\n\n') | |
print('Modified markovify, create new model, state size 2:') | |
print(timeit.timeit("modifiedmarkovify_new_model_size_2()", setup="from __main__ import modifiedmarkovify_new_model_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(modifiedmarkovify_new_model_size_2().make_short_sentence(120))) | |
print('----------------------------------') | |
print('Modified markovify, existing chain JSON, state size 2:') | |
print(timeit.timeit("modifiedmarkovify_existing_chain_size_2()", setup="from __main__ import modifiedmarkovify_existing_chain_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(modifiedmarkovify_existing_chain_size_2().make_short_sentence(120))) | |
print('----------------------------------') | |
print('Modified markovify, existing model JSON, state size 2:') | |
print(timeit.timeit("modifiedmarkovify_existing_model_size_2()", setup="from __main__ import modifiedmarkovify_existing_model_size_2", number=number_tests) / number_tests) | |
print('Example: {}'.format(modifiedmarkovify_existing_model_size_2().make_short_sentence(120))) | |
print('----------------------------------') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import speedtests_newclass_final as speedtests_newclass | |
if __name__ == '__main__': | |
print('Current markovify, create new model, state size 2:') | |
pr1 = cProfile.Profile() | |
pr1.enable() | |
text_model = speedtests_newclass.new_model_size_2() | |
pr1.disable() | |
pr1.dump_stats('current_markovify_create_new_model_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') | |
print('Current markovify, existing chain JSON, state size 2:') | |
pr2 = cProfile.Profile() | |
pr2.enable() | |
text_model = speedtests_newclass.existing_chain_size_2() | |
pr2.disable() | |
pr2.dump_stats('current_markovify_existing_chain_json_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') | |
print('Current markovify, existing model JSON, state size 2:') | |
pr3 = cProfile.Profile() | |
pr3.enable() | |
text_model = speedtests_newclass.existing_model_size_2() | |
pr3.disable() | |
pr3.dump_stats('current_markovify_existing_model_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') | |
print('\n\n\n') | |
print('Modified markovify, create new model, state size 2:') | |
pr7 = cProfile.Profile() | |
pr7.enable() | |
text_model = speedtests_newclass.modifiedmarkovify_new_model_size_2() | |
pr7.disable() | |
pr7.dump_stats('modified_markovify_new_model_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') | |
print('Modified markovify, existing chain, state size 2:') | |
pr8 = cProfile.Profile() | |
pr8.enable() | |
text_model = speedtests_newclass.modifiedmarkovify_existing_chain_size_2() | |
pr8.disable() | |
pr8.dump_stats('modified_markovify_existing_chain_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') | |
print('Modified markovify, existing model, state size 2:') | |
pr9 = cProfile.Profile() | |
pr9.enable() | |
text_model = speedtests_newclass.modifiedmarkovify_existing_model_size_2() | |
pr9.disable() | |
pr9.dump_stats('modified_markovify_existing_model_ss2.stats') | |
print('Example: {}'.format(text_model.make_sentence())) | |
print('----------------------------------') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running 25 times each, returning the average runtime in seconds. | |
Current markovify, create new model, state size 2: | |
0.22077374984000925 | |
Example: And so in ten minutes I had just transferred to it would give him an advance from a boot-lace. | |
---------------------------------- | |
Current markovify, existing chain JSON, state size 2: | |
0.21371726716010017 | |
Example: She was a man without heart or conscience. | |
---------------------------------- | |
Current markovify, existing model JSON, state size 2: | |
0.23536155616006 | |
Example: Posted to-day in Gravesend by a better fit if I did it, Mr. Holmes. | |
---------------------------------- | |
Modified markovify, create new model, state size 2: | |
0.2151293533600983 | |
Example: You took me to cut both ways. | |
---------------------------------- | |
Modified markovify, existing chain JSON, state size 2: | |
0.2092507990000013 | |
Example: The man sat huddled up in his conjecture, however, for he had come in, and the house. | |
---------------------------------- | |
Modified markovify, existing model JSON, state size 2: | |
0.09925109259987948 | |
Example: Your duties, as far as Reading, but could get the address of the journey, but the high, thin breathing of my dress. | |
---------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running 25 times each, returning the average runtime in seconds. | |
Current markovify, create new model, state size 2: | |
16.18081657112576 | |
Example: A blow was struck, and one small job, and that we should need it. | |
---------------------------------- | |
Current markovify, existing chain JSON, state size 2: | |
14.08117773335427 | |
Example: This is very easy. | |
---------------------------------- | |
Current markovify, existing model JSON, state size 2: | |
15.376322960844263 | |
Example: It may only be used on or associated in my hand in a note by the Underground and hurried from the room. | |
---------------------------------- | |
Modified markovify, create new model, state size 2: | |
16.279055197359995 | |
Example: He spoke in a few whispered words of his words, she gave a last hurried glance around, I saw the latter was your son. | |
---------------------------------- | |
Modified markovify, existing chain JSON, state size 2: | |
13.715405708234757 | |
Example: He calls attention, and with almost no restrictions whatsoever. | |
---------------------------------- | |
Modified markovify, existing model JSON, state size 2: | |
6.346636804519221 | |
Example: Instead of making up, and also in a note for me lay upon the previous night. | |
---------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment