from IPython.display import display, HTML
import markovify
import spacy
text = open("./frankenstein.txt").read()
gen = markovify.Text(text, state_size=3)
sentences = [gen.make_sentence(tries=1000) for i in range(5)]
new_para = ' '.join(sentences)
new_para
'But I, the true murderer, felt the never-dying worm alive in my own heart I could give no account. The wet wood which I had consumed, I should guess that I had no means of extracting it. You accuse me of murder, and he endeavoured to prove to me the shifting colours of the landscape and the appearances of the sky. Know that, one by one, my friends were employed in their ordinary occupations. But I was not unacquainted with the language of the country ate, but I will disclose a secret.'
print(new_para)
But I, the true murderer, felt the never-dying worm alive in my own heart I could give no account. The wet wood which I had consumed, I should guess that I had no means of extracting it. You accuse me of murder, and he endeavoured to prove to me the shifting colours of the landscape and the appearances of the sky. Know that, one by one, my friends were employed in their ordinary occupations. But I was not unacquainted with the language of the country ate, but I will disclose a secret.
from textwrap import fill
print(fill(new_para, 60))
But I, the true murderer, felt the never-dying worm alive in my own heart I could give no account. The wet wood which I had consumed, I should guess that I had no means of extracting it. You accuse me of murder, and he endeavoured to prove to me the shifting colours of the landscape and the appearances of the sky. Know that, one by one, my friends were employed in their ordinary occupations. But I was not unacquainted with the language of the country ate, but I will disclose a secret.
display(HTML(new_para))
display(HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/jOBRLH9O2i4" frameborder="0"></iframe>'))
display(HTML("<p>" + new_para + "</p>"))
But I, the true murderer, felt the never-dying worm alive in my own heart I could give no account. The wet wood which I had consumed, I should guess that I had no means of extracting it. You accuse me of murder, and he endeavoured to prove to me the shifting colours of the landscape and the appearances of the sky. Know that, one by one, my friends were employed in their ordinary occupations. But I was not unacquainted with the language of the country ate, but I will disclose a secret.
new_text = ""
for i in range(3):
sentences = [gen.make_short_sentence(80, tries=1000) for i in range(3)]
new_para = " ".join(sentences)
new_text += "<p>" + new_para + "</p>"
display(HTML(new_text))
I thought of the occurrences of the day was spent in observing my friends. Shall I not then expire! I trembled and my heart often sickened at the work of your own hands.
Sometimes I sat with my eyes fixed on the consummation of his happiness. My rage is unspeakable when I reflect that you are sincere. Before this I was not even of the same species and have the same defects.
My companion must be of the same nature as man. But it refreshed me and filled me with an insatiable thirst for vengeance. I had turned loose into the world for my destruction.
new_text = ""
for i in range(3):
sentences = [gen.make_short_sentence(80, tries=1000) for i in range(3)]
new_para = " ".join(sentences)
new_text += "<p style='width: 40em'>" + new_para + "</p>"
display(HTML(new_text))
Between two and three in the morning when I entered my father's house. I ordered it to be composed of wood. Ruined castles hanging on the precipices of an inaccessible mountain.
But I did not speak. I pressed on, and in two months I began to reflect on all that had passed. As I still pursued my journey to the sea of ice.
Sometimes I thought that as I could spare. One day, when my father entered the chamber. Then the appearance of my father is in the greatest danger.
new_text = ""
for i in range(3):
sentences = [gen.make_short_sentence(80, tries=1000) for i in range(3)]
new_para = " ".join(sentences)
new_text += "<p style='width: 40em; font-family: Georgia, serif;'>" + new_para + "</p>"
display(HTML(new_text))
Do your duty towards me, and I began to study them with diligence. For a moment my father entered the chamber. Agatha, the ever-gentle Agatha, kissed the hands of Elizabeth and myself.
Fortunately I had money with me and for me. I leave a sad and bitter world; and if you obey me in this state of degradation. I feared to wander from the sight of which was sickening to me.
Chapter 5 It was on a level with your own. She thanked him in the most melancholy reflections. It was situated against the back of the cottage it was tolerably warm.
nlp = spacy.load('en_core_web_md')
gen_text = gen.make_sentence(tries=1000)
output = ""
for word in nlp(gen_text):
if word.pos_ == 'NOUN':
output += "<span style='color: blue;'>" + word.text + "</span>"
elif word.pos_ == 'VERB':
output += "<span style='color: red;'>" + word.text + "</span>"
else:
output += word.text
output += word.whitespace_
display(HTML(output))
tmpl = """
<!doctype HTML>
<html lang='en'>
<head>
<meta charset="utf-8">
<style type="text/css">
* { box-sizing: border-box; }
html { width: 8.5in; height: 11in; }
body {
height: 100%%;
width: 100%%;
margin: 0;
border: 1px grey dotted; /* comment out this line to remove border */
}
</style>
</head>
<body>
%s
</body>
</html>
"""
print(tmpl % "hello")
<!doctype HTML> <html lang='en'> <head> <meta charset="utf-8"> <style type="text/css"> * { box-sizing: border-box; } html { width: 8.5in; height: 11in; } body { height: 100%; width: 100%; margin: 0; border: 1px grey dotted; } </style> </head> <body> hello </body> </html>
with open("output-test.html", "w") as fh:
fh.write(tmpl % gen.make_sentence(tries=1000))
!open output-test.html
new_text = ""
for i in range(7):
sentences = [gen.make_short_sentence(80, tries=1000) for i in range(3)]
new_para = " ".join(sentences)
new_text += "<p>" + new_para + "</p>"
output = "<div style='width: 25em; font-size: 16pt; line-height: 1.2em; margin: 0 auto;'>" + \
"<h1>My New Frankenstein</h1>" + \
new_text + \
"</div>"
with open("output-test.html", "w") as fh:
fh.write(tmpl % output)
!open output-test.html
import random
random.uniform(0, 100)
66.11020517230256
def topleft(x, y, unit='in'):
left_str = "left: " + str(x) + unit + ";"
top_str = "top: " + str(y) + unit + ";"
return top_str + " " + left_str
print(topleft(5, 15, 'px'))
print(topleft(1.23, 4.56))
top: 15px; left: 5px; top: 4.56in; left: 1.23in;
frank_words = random.sample(text.split(), 500)
output = ""
for word in frank_words:
pos = topleft(random.uniform(0.5, 7.5), random.uniform(0.5, 10.5))
output += "<div style='position: absolute; " + pos + "'>" + word + "</div>"
with open("output-test.html", "w") as fh:
fh.write(tmpl % output)
!open output-test.html
frost_words = open("./frost.txt").read().split()
import math
def polar2xy(r, theta):
x = r * math.cos(theta)
y = r * math.sin(theta)
return (x, y)
polar2xy(2, math.pi / 6)
(1.7320508075688774, 0.9999999999999999)
output = ""
for i, word in enumerate(frost_words):
xpos, ypos = polar2xy(0.5 + (i*0.025), i * 0.25)
output += "<div style='position: absolute; " + \
topleft(xpos + 4.25, ypos + 5.5) + "'>" + word + "</div>"
with open("output-test.html", "w") as fh:
fh.write(tmpl % output)
!open output-test.html