Hello and welcome to another coding challenge video this coding challenge I'm
going to implement something called the diastic machine.
So that I ask machine is that going to build here is going to be based entirely
on a technique that I asked a technique invented in pioneered by Jackson MacLow
artist poets composer playwrights who pioneered a lot of
algorithmic chance operate techniques with around language to generate poetry.
Without the use of a computer.
So there's a long history of artists and poets working with algorithmic
computational methods or to generate language to analyze language and this is
something that it's really easy to forget in the world of kind of computers and
learning to program that so much of this kind of stuff can be done manually and
I encourage you to explore ideas similar to this with manual
processes before you start to implement that digitally as a way of exploring and
playing around you know there's another famous example is William Burroughs caught
up method which is called the cut up because it's literally about making new
text from folding and cutting preexisting text and mashing them up together which of
course you could do with a computer but you could also do with scissors and
that sort of thing but let me get beyond that a great reference for this by the way
that also mention is a virtual music's parents and computer poetry.
This is a book that you can link both to Jackson the page about Jackson
the cloak as well as the virtual use of book in this video tutorial.
But let me just read to you.
The description of the diasporic machine.
From excerpted from Charles O. Hartman's book The process began with
a striking phrase from Virginia Woolf of the ways ridiculous in Piccadilly.
This is going to be known as the seed phrase so
in the program that I'm going to write.
I need a seed phrase.
He rewrote the doll this is referred to Jackson the globe looking for
the first word that like ridiculous.
Begad within our then read the next word following that hat like ridiculous.
I said second letter then the next word Who's the.
Third letter was D. and so on until he spelled through the whole phrase.
So if that didn't make sense to you.
Let me come over here and kind of diagram this out for you.
So let's say Surprise surprise.
I have the word.
And I have some text like Ralph
sat on the left of work actually
painful.
Ferd's.
OK I'm designing something
of designing something to work precisely in a way that I can demonstrate this.
So let me look in this text for a word that starts with our hot roust.
Let me work.
Look at this text for a word that has its second letter A that.
Let me look for a word that has its third letter I not on off but painful.
People its fourth letter and affirms so you can see this idea of taking this C.
phrase in fighting words in the existing body of text with just a single letter
in the same spot and creating a new phrase out of that and this is a way the sense of
say I want to generate poetry but I the writer don't want to
play a sort of human being role that other than sort of selecting the seed phrase and
the source text let the algorithm kind of do the talking.
So to speak and this is I think that you as a viewer of this video.
I would encourage you reference some other examples of this to generate your own
system for doing this and see what types of outcomes you can get if you
make a Twitter bought from it that sort of thing.
OK So coming back over here.
Let's go and start with I have an empty sketch or
entirely empty P five just sketch and I'm saying no.
Can this because I don't want to see a canvas.
I don't drawing anything in this and I'm going to just find this here and
you can see there's a I put a header in the H.T.M.L.
file Jackson the globe diasporic and I don't see anything on the page.
So the first thing that I need to do is I want to.
Get a seed phrase and let the viewer the user type in the C phrase.
So what I'm going to do in the H.T.M.L. file is I'm going to say
make a paragraph element and I'm going to say seed.
Colon input idea equal seed slash input and I'll add.
Make sure it starts with a rainbow.
So now we can see I have on the page a seed and I started that with Rainbow.
Now I need to in my program get access to that and also I think it might make
sense for me to add a button.
Id call submit.
And submit will be the text of the button.
So I'm adding that in so now I have seed rainbow and
I have a submit button and I want to attach up.
So now I want to attach an event.
So this I have to do in javascript as I Now if this is the structure of the page
in H.T.M.L. and
now I'm just going to affect the behavior of the page in my javascript code.
So I'm going to say I'm going to say our seed equal select.
Seed.
Valar submit equal select.
Submit and submit.
Mouse press.
And I'm going to do something I haven't done a lot of my other videos tend to
stay away from this I could just say like do the thing and
then define that function down here but
I take this as an opportunity to show you something which is an anonymous function.
I'm just going to put the call back right in here.
That's very awkward looking but it's actually kind of a nice way of writing
condensed code when you click the mouse on the Submit button button
button the submit button execute this code that's inside this anonymous
functions anonymous because it doesn't have a name.
So what do I want to do right there just to see that it's working.
I'm just a create P C going to need to do not.
See but see value.
The text of the seed itself and I will have to do a lot more there.
Of course but I'm just going to say that for right now.
So now if I hit submit we can see this page is doing some basic stuff that is
at least getting the seat.
So I have the seed.
Right now I have this seed in my code I need some sort of source text
to use to be the words that I search for so
the way that him do that is I'm going to load from a text file.
And I happen to have a text file here which just has
a lot of text from the Wikipedia page on read both.
So I'm going to do it.
The easiest way possible in my code with just preload.
So I better write a preload function.
And I'm going to have a global variable called source text.
I'm going to call it a goofy name and
say Source Text equals load strings rainbow dot.
T.X.T. and one thing you might recall if you watch some of the other videos is that
the load strings function loads in the text but loads it has an array.
So it gives you each a line in that text file as an array and so whatever you do in
set up is once that text is loaded I would say Source Text equals joy in source text
with the space I get rid of that array enjoyed it so I have one long string.
So now I have one large string and I'm just going to make sure it's there and
also safe create P. source text.
So now if I hit run by row if I reload the page I have a seed and
I have the source text.
So now I have set up the page.
That's just sort of step one.
Now I actually need to implement the DIAF Dick.
Algorithm.
OK So let's go back to our code and do that.
This is going to be fun.
And actually I just realized.
Here's something that I want to actually do I want my for my diasporic algorithm.
I want the source text not to be one long string but
I wanted to be just a ray of words a list of words.
So I'm going to I'm going to create a glow.
Variable called words and I'm going to say words equals splits and
I want to use the P. five just function split tokens and
I would say split that source text up by and split token says whatever.
These are the delimiters the second arguments of the delimiters I want to
split it up by space comma exclamation anything that's basically
you know something that's not a so I could I could do this much more effectively.
But I'm just going to do that right now.
And I'm going to do this and I'm going to look just in the.
Debugging console and I can see I have an array of basically.
Many many many many words all the words of an array with every single word as
a separate element and that are A and
you can see that it's two thousand one hundred thirty two words long.
So this is what I'm going to be able to use now I'm going to search through
that a ray to find words that have and
by the way it's probably going to five rainbow over and over again but
I'll deal with that later because I'll give it a different seed phrase.
OK so here we go.
Now what I want to do is instead of creating those paragraphs I want to say
let me execute a diasporic with what I want to pass in.
The value of the C. The seed and I want to all of the words.
So I'm going to write a and I'm going to.
I'm going to have that function take a seat and in a re.
And that function will return a diasporic for the result of this diaster technique.
So I'm just going to go to the top of my code now because I can
kind of worry about this algorithm on it as its own thing completely.
And I need I need a function called diasporic and
it gets seed and a bunch of words and I'm using the same variable name.
So what's the first thing that I need to do the first thing that
I need to do in the algorithm is look at each character one of the time because I
need to find a word that starts with are a word second letters a word with third
third letters I that sort of thing.
So I'm to come.
Back over here.
And I'm going to say what for var I will see your eyes less than see Dot length.
Plus plus.
And then the character that I'm working with is seed car at Boy
this is a function that's part of the javascript string object
I think I neglected to mention in some of my other videos.
If I say car at I want the character at index I write and industry.
All characters have an index index zero index one index two so
if a string is five characters long like hello
to the index the indices go between zero and four.
So once I have that I want to then have another loop.
And what do I want to loop through I want to loop through all of.
I want to loop through every single one of the words.
And I want to say if I
see if words index J. car at.
Equals C..
Then I have found a word right.
I found a word that has its own character at
the same spot I as the character I'm looking for.
And once I do that.
I've just got to say console dot log.
Just to see if this kind of idea works and break.
So here we go.
I'm going to run this program.
I'm pretty sure.
When to run this we should see a bunch of things to look at this.
I got rainbow seven types.
So first of all let's do a different seed phrase.
This is you know.
Testing one just through testing one.
You can see I got T. eat ass.
T. and.
E and that's all.
So you can see these and see this worked and I got some of them twice.
So here's the thing.
One thing I need to build into this is I need to say after
the reason why I got rainbow seven times.
It's because of my loop starts over every single time.
So every single time I find a word I go.
Our matches with our then I get a match with a so
one thing that I need to change about this is.
I need to while I'm doing this keep track of some things.
So I need to first of all I need to have a free i want to kind
of have a phrase which starts like this.
And when I find the word I want to say phrase plus equal.
I want to concatenate So
the plus operator strong means to string together and to say.
Plus equals words index j.
And then I'm going to say return phrase.
So one thing that's going to be nice about this now that I've done this is I can stop
using the console for debugging and I can say create freeze.
So let me run this again and you can see.
I got a rainbow and I say test.
I got that meterological dispersion that.
So one thing I want to do by the way is I also want to add a space.
So I do this a sort of silly way but
I'm just going to have two lines of code there and you can see.
So now what I want to do is make sure I look for
the next instance of a word after the first one I found.
So I'm going to keep track of that index.
Or like current word equals zero.
So I want to start my loop when I look through all of the loops from
the current work so I want to go from current word to the end of that array and
as soon as I find one.
I want to say current word equals J.
Plus one I want to go to the next work.
So instead of this loop of searching through all the words always going back to
the beginning I want to save where I last found one and start from the next one.
So this should do this.
Rainbow cause rainbows rainbows rainbow rainbow rainbow still getting a lot of
rain both but I can now say things like.
I love.
Is due to unicorns exist question mark.
So during good about during moonlit numerically direction
encounter dispersion brightness now.
Amazingly I'm not getting any errors here and I probably should do some errors
checking like what if I got to the end of the array.
And I don't have any more where more to look what if no word exist with
that in there so I'm going to leave this is that I'm going to release this code.
I want you know another thing that I could do was track all the possible.
Words that have zero is the second letter and I can always pick a random one so
that each time I hit submit I get a different one.
That's in it you might add to this error checking is something that you might add.
I don't know if I'm if there's anything weird happening when I have the space
because I've actually spaces technically a character that I'm checking.
So the question mark is in there but seems to be working.
Let's just D..
Space you as Forth.
So you can see so I got to do some stuff clean up that sort of way.
So right so.
So I probably missed something but
I heard you were watching this video to play around this code make your own
diasporic tweak the algorithm generate poetry share it.
Share it with me share it with the world and explore other types of ideas.
If you're at the programming from A to Z.
playlist the next video of the talk a little bit more about this and
a homework assignment for this set of material and some other.
Types of techniques in our historical algorithms like the diasporic that you
might look at as well.
OK Thanks for watching.
And I'll see you in another videos maybe perhaps of day.