<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jose Antonio Torres Garibay</title>
    <description>The latest articles on DEV Community by Jose Antonio Torres Garibay (@pepetorres1998).</description>
    <link>https://dev.to/pepetorres1998</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F427340%2F2855015e-59d7-4ab1-9628-02ee5661bf65.jpeg</url>
      <title>DEV Community: Jose Antonio Torres Garibay</title>
      <link>https://dev.to/pepetorres1998</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pepetorres1998"/>
    <language>en</language>
    <item>
      <title>Markov Chain Sentence Generator (with Sh*tposting capabilities)</title>
      <dc:creator>Jose Antonio Torres Garibay</dc:creator>
      <pubDate>Fri, 10 Jul 2020 20:30:17 +0000</pubDate>
      <link>https://dev.to/pepetorres1998/markov-chain-sentence-generator-with-sh-tposting-capabilities-2hif</link>
      <guid>https://dev.to/pepetorres1998/markov-chain-sentence-generator-with-sh-tposting-capabilities-2hif</guid>
      <description>&lt;p&gt;In the look for a challenge I stumbled upon the pro/g/ramming challenges v4.0 image. And I loved it, I think it has some legit great challenges that I want to make no matter how much it takes, just for the sake of learning.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F7680%2F1%2A5KQlCmSrE8V13p0IE-oeUA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F7680%2F1%2A5KQlCmSrE8V13p0IE-oeUA.png" alt="Popular image with around 150 programming challenges to develop." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pro/g/ramming challenges v4.0&lt;/p&gt;

&lt;p&gt;The first challenge that took mi attention was the Markov chain sentence generator, maybe is because that is one of the first challenges, maybe was the interesting name that I've never heard of before. I don't really know why but it get my attention, and I dived directly to the challenge.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F872%2F0%2A18U4lnR7dFDiA0Ku.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F872%2F0%2A18U4lnR7dFDiA0Ku.jpg" alt="Russian mathematician Andrey Markov" width="436" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Russian mathematician &lt;a href="https://en.wikipedia.org/wiki/Andrey_Markov" rel="noopener noreferrer"&gt;Andrey Markov&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is a Markov chain? I thought to myself. And Medium solved my question with [this story][14], which I recommend to read first. From now on, I will assume that you read the story or know what a Markov chain is and how it works. Also the program I built is based on the code of the mentioned story. But I rewrote it on JavaScript, and added some interesting changes.&lt;/p&gt;

&lt;p&gt;The action plan was very simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First thing is splitting the text. And deleting empty strings, generated by blank lines on text. Then separate words array in two different arrays, one of regular words and another to words before a full stop. (The last words).&lt;/li&gt;
&lt;li&gt;Than I need to pick a random word as initial word, to start constructing the sentence with n-grams. I pick one word, add it to the sentence array, add the next word to it to the sentence array, and pick the third word as the next initial word.&lt;/li&gt;
&lt;li&gt;Make adjustments so it can take up to three words next to the initial word.&lt;/li&gt;
&lt;li&gt;Add a method for constructing a paragraph, calling inside that method multiple times the sentence generator method, and join all the sentences with an "n".&lt;/li&gt;
&lt;li&gt;Add a method for constructing a essay, where inside will be called multiple times the paragraph generator method, and join all the paragraphs with an "nn".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also, as a bonus mentioned in the challenges image, add sh*tposting capabilities.&lt;/p&gt;

&lt;p&gt;If you want to know every detail of how the program developed, I will leave the &lt;a href="https://github.com/pepetorres1998/markov-chain-sentence-generator" rel="noopener noreferrer"&gt;code here.&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Issues.
&lt;/h1&gt;

&lt;p&gt;The first issue that I ran into, was that sentences were repeating themselves, as if they looped.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F821zitcukyxhsi7zw4m4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F821zitcukyxhsi7zw4m4.png" alt="Repeated sentences" width="800" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Quickly, I discovered the issue was on the sentence maker method. And it happened because the function to find the initial word position in the array always picked the first match of the word as its position.&lt;/p&gt;

&lt;p&gt;The solution was to grab all the matches, and selecting the initial word position randomly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;_getNextWords&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentWord&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentWord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_randomIntFromInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wordsToBePushed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_randomIntFromInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;wordsToBePushed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;loopIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;loopIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;wordsToBePushed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;loopIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;loopIndex&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;wordsToBePushed&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allWords&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, if the currentWord (initial word) isn't a empty string, it saves all the indexes, and select one randomly. Then it saves the next words into the sentence array.&lt;/p&gt;

&lt;p&gt;Another issue I ran into was the sh*tposting capabilities, I was dry at the moment and didn't know what to do. Actually, I did split it into a different class, and the best idea I had was to generate the sentence backwards…&lt;/p&gt;

&lt;p&gt;I took the currentWord (initial word) and added the words before it to the sentence array, instead of the words after. Also modified the sentence to LoOk LiKe ThIs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2856%2F1%2ANbo_St1apGEtiwzaTpveVw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2856%2F1%2ANbo_St1apGEtiwzaTpveVw.png" alt="Essay generated with Markov chains" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Markov chains essay generation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2932%2F1%2A5IQ8LWhyAz-OY0t0xFqbBA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2932%2F1%2A5IQ8LWhyAz-OY0t0xFqbBA.png" alt="Shitpost Essay generated with Markov chains" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Markov chains essay generation with sh*tpost capabilities.&lt;/p&gt;

&lt;p&gt;Maybe it was a little lazy on my part, but I couldn't think of a better idea. I encourage you to [contribute][15] if you have a better sh*tposting idea. :)&lt;/p&gt;

&lt;p&gt;Thanks for reading! What challenge should I take now? I want to &lt;a href="https://www.youtube.com/watch?v=LU7nE1e8WbE" rel="noopener noreferrer"&gt;go slowly,&lt;/a&gt; no Bootloader pls.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>markov</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
