In [9]:
from IPython.display import display, HTML

In [44]:
import markovify
import spacy


## why display html?¶

### View your text with nice linebreaks¶

In [108]:
text = open("./frankenstein.txt").read()

In [47]:
gen = markovify.Text(text, state_size=3)

In [56]:
sentences = [gen.make_sentence(tries=1000) for i in range(5)]

In [57]:
new_para = ' '.join(sentences)

In [58]:
new_para

Out[58]:
'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.'
In [59]:
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.

In [61]:
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.

In [62]:
display(HTML(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.

### embed stuff¶

In [68]:
display(HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/jOBRLH9O2i4" frameborder="0"></iframe>'))


Tutorials from Allison:

General refs:

In [63]:
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.

In [74]:
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.

In [72]:
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.

In [71]:
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.

### highlighting¶

In [76]:
nlp = spacy.load('en_core_web_md')

In [109]:
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))

The old man appeared enraptured and said some words which Agatha endeavoured to explain to me that I should make use of the same species and have the same defects.

## page layouts¶

In [124]:
tmpl = """
<!doctype HTML>
<html lang='en'>
<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>
<body>
%s
</body>
</html>
"""

In [90]:
print(tmpl % "hello")

<!doctype HTML>
<html lang='en'>
<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>
<body>
hello
</body>
</html>


In [82]:
with open("output-test.html", "w") as fh:
fh.write(tmpl % gen.make_sentence(tries=1000))
!open output-test.html

In [102]:
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>"

In [103]:
output = "<div style='width: 25em; font-size: 16pt; line-height: 1.2em; margin: 0 auto;'>" + \
"<h1>My New Frankenstein</h1>" + \
new_text + \
"</div>"

In [104]:
with open("output-test.html", "w") as fh:
fh.write(tmpl % output)
!open output-test.html


### Concrete compositions¶

In [114]:
import random

In [117]:
random.uniform(0, 100)

Out[117]:
66.11020517230256
In [128]:
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;

In [123]:
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

In [125]:
frost_words = open("./frost.txt").read().split()

In [132]:
import math
def polar2xy(r, theta):
x = r * math.cos(theta)
y = r * math.sin(theta)
return (x, y)
polar2xy(2, math.pi / 6)

Out[132]:
(1.7320508075688774, 0.9999999999999999)
In [142]:
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