Real time textual analysis via OpenAmplify

I’ve blogged before (see related entries below) about the coolness that is OpenAmplify. OpenAmplify provides an APi that does deep language parsing. It’s not perfect of course - but in my testing it can be eerily spot on. In my previous blog entry on the topic I talked about how to use it via ColdFusion. You could imagine using OpenAmplify as a nightly task to scan the user generated content on your site and provide statistics on the general mood as well as topics covered in your discussions. For a site getting lots of dynamic content from their community, that could be vital. What I discovered this week though that is OpenAmplify also provides a JavaScript API. Here is a simple demo I created that uses their API, along with jQuery, to provide contextual analysis while you type. This could get annoying on a real forum perhaps, but it’s kinda cool here.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src=""></script> <script> var api = ""; var apikey = "dhj34antjkh87bs6fmrrhymganaz78d5"; var apiURL = api + "?apiKey="+apikey+"&analysis=styles&outputFormat=json_js"; $(document).ready(function() { var debounce = function (func, threshold, execAsap) { var timeout; return function debounced () { var obj = this, args = arguments; function delayed () { if (!execAsap) func.apply(obj, args); timeout = null; }; if (timeout) clearTimeout(timeout); else if (execAsap) func.apply(obj, args); timeout = setTimeout(delayed, threshold || 100); }; } $("#comments").keyup(debounce(function(e) { $("#commentText").html(""); var text = $.trim($(this).val()); if(text == '') return; if(text.length < 5) return; $.getScript(apiURL + "&inputText="+escape(text),function(data,status) { var content = ""; content += "Your comment is generally " + amplifyOutput.StylesResponse.StylesReturn.Styles.Polarity.Mean.Name + "<br>"; content += "Is your comment offering guidance? " + amplifyOutput.StylesResponse.StylesReturn.Styles.OfferingGuidance.Name + "<br>"; content += "Is your comment requesting guidance? " + amplifyOutput.StylesResponse.StylesReturn.Styles.RequestingGuidance.Name + "<br>"; content += "How decisive is your comment? " + amplifyOutput.StylesResponse.StylesReturn.Styles.Decisiveness.Name + "<br>"; content += "Uses slang? " + amplifyOutput.StylesResponse.StylesReturn.Styles.Slang.Name + "<br>"; content += "How flamboyant? " + amplifyOutput.StylesResponse.StylesReturn.Styles.Flamboyance.Name + "<br>"; $("#commentText").html(content); }); },500)); }); </script> </head> <body> <form> <textarea name="comments" id="comments" cols="30" rows="10"></textarea> <div id="commentText"></div> </form> </body> </html>

The HTML view portion of this is just the form with the text area. Pretend it's the blog comment form. The div beneath is blank and will be used for feedback. Now scroll to the top. The debounce function is something I've mentioned before. It's just going to be used to slow things down a bit as we type. We use it within our keyup event handler for the textarea. I grab the current value, and if it's big enough, we do a getScript() call to load in the remote OpenAmplify service. I specifically focused on the style portion of their API. They provide a lot more. I take the result, a JSON object, and then just create a nice result string from it. So how does it work? Check out the video:


I setup an online demo of this as well, but with my personal API key being used, it may start throwing errors under load. Be gentle.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA