Tuesday, December 30, 2014

A Silly Text Visualization Toy



This little text-to-image replacement toy made me laugh, so I decided to put it up in case it makes you laugh too. In my last project, I did part-of-speech tagging in Python and used that to replace nouns with other nouns (see post and demo); in this one, I did the part-of-speech tagging all in Javascript using the terrific RiTa.js library!

With RiTa, you get the same slightly noisy results I got in the pattern.py tagging I did before: not all the nouns are good "nouns." The API for tagging is super easy:

>RiTa.getPosTagsInline("Silent night, holy night")
>"Silent/jj night/nn , holy/rb night/nn"

After generating the parts of speech, I filtered for just the nouns ("/nn" and "/nns"). I replaced those with words in "span" tags, and then used an ajax call to search for each spanned text in Google's image search API. The whole operation is outlined here, with the logic for getting the local text selected first:

$.when(
      $.get("texts/" + file_name, function (text) {
        lines = text.split('\n');
      })
    )
    .then(function () { 
      return processLines(lines);
    })
    .then(function (text) {
      $(".content").html(text);
    })
    .done(function () {
      $("span.replace").each(function (i, val) {
        getImage(val);
      });
    });


It turns out (of course) that there's a lot of repetition in certain words, especially for holiday songs and poems; so I introduced some random picking of the image thumbnails for variety.

Here's more from "Night Before Christmas" (which is really called "A Visit from St. Nick") -- yes, that's Microsoft Word:


This is the first sentence of Pride & Prejudice; it ends with the single man getting the Good Wife:


And the Road Not Taken:



I think the Night Before Christmas is the best one, but they all have their moments. Try it. Suggestions for other well-known (short) texts to try?