ShakespeareBot

MATLAB Production Server demo

by Toshi Takeuchi

HTML

<script src="http://matlabtricks.com/highlighter/matlab-highlighter-1.55.min.js"></script>
<link rel="stylesheet" href="http://matlabtricks.com/highlighter/matlab-highlighter-1.55.css">
<h2>ShakespeareBot - MATLAB Production Server Demo</h2>
<p><strong>ShakespeareBot</strong> generates random fake quotes from "Romeo and Juliet".</p>
<p>
  <img src="http://blogs.mathworks.com/images/loren/2015/wordle.png" />
</p>
<div id="main">
  <label><span>Enter a starting word or leave it blank:</span>
    <input type="text" class="n" name="fn">
  </label>
  <input type="button" value="submit">
</div>
<div id="container">
  <p>Result from MATLAB:</p>
  <div id="answer"></div>
</div>
<hr/>
<p class="note">
  Here is the code running on MATLAB Production Server. It calls <code>textGen.m</code> and runs the pre-trainied trigram language model <code>triMdl</code> from the blog post <a href="http://blogs.mathworks.com/loren/2015/09/09/text-mining-shakespeare-with-matlab/">Text Mining Shakespeare with MATLAB</a>
</p>
<p class="note">
Click "<strong>Edit in JSFiddle</strong>" above right to see the source code.
</p>
<blockquote><pre class="matlab-code"><code>
function quotes = bard(startingword)
%BARD generates fake Shakespearean quotes
%   You can specify a starting word, this function uses that. 
%   Otherwise it will generate text with a random starting word. 

if nargin == 0                              % if no input argument
    startingword = '&lt;s&gt;';                   % start with a random word
end

load myobj                                  % load 'triMdl'

s = lower(startingword);                    % lowercase
if ismember(s,triMdl.unigrams)              % if known word
    quotes = textGen(triMdl, s);            % call textGen
    quotes = cellfun(@(x) ['"' x '",'], ... % add quotation marks
        quotes, 'UniformOutput', false);
    quotes{end}(end) = [];                  % remove last comma
    quotes = ['[' quotes{:} ']'];           % convert to JSON array
else
 ...

CSS

#main {
  width: 400px;
}

#container {
  width: 400px;
}

#answer {
  font-size: 14pt;
}

body {
  background: #FFFFFF;
  font-family: 'Open Sans', sans-serif;
}

hr {
  margin: 30px 100px;
}

.note {
  font-size: small;
}

.logo_container {
  margin-top: 5px;
  margin-bottom: 6px;
  float: left;
  width: 200px;
}

JavaScript

// This function runs when the page is loaded
$(document).ready(function() {
  /* enable MATLAB Highligher that applies syntax highlighting 
     to MATLAB code on the page */
  highlightMATLABCode();

  // This captures the onclick event on submit button
  $('input:button').click(function() {
    var word = $("input:first").val(); // input text
    word = $.trim(word);               // trim white space
    
     // Call MATLAB Production Server and pass the user input
    bardMPS(word);
  });
});

/* Function to call ShakespeareBot on MATLAB Production Server
   MATLAB Production Server is running the custom MATLAB
   function bard.m on Amazon AWS (see the URL). 
   This function uses AJAX to post JSON object to the server
   and parse the JSON response object to display a fake quote  */

function bardMPS(startingword) {
  if (startingword.length == 0) { // no starting word provided
    startingword = []; // set the starting word to []
  }

  // Create a simple AJAX request to the MATLAB Production Server
  $.ajax({
    url: 'http://ec2-52-38-254-185.us-west-2.compute.amazonaws.com:31415/shakespeareBot/bard',
    type: "POST",
    timeout: 5000, // sets timeout to 5 seconds
    /* Pass the starting word in "rhs" as input argument for bard.m, 
       and we get one return value ("nargout") */
    data: JSON.stringify({
      "rhs": startingword,
      "nargout": 1
    }),
    contentType: 'application/json',
    success: function(data) {
    
      // Parse the response and display it on the page. 
      var jsonResponse = JSON.parse(data.lhs[0].mwdata[0]);
      var len = jsonResponse.length;               // number of quotes
      $('#answer').empty();                        // clear previous answer
      for (var i = 0; i < len; i++) {              // for each quote
        $('#answer').append("<p>" + jsonResponse[i] + "</p>"); // append
      }
      $('#answer').effect("highlight", {           // flash the quotes
        color: "#FFFF00"
      }, 1000);
   ...