Javascript Test Case Example

Using Ingenia in your website as a client side Javascript component.

Structure

The method for creating client side classification system involves two components; the client side library and a same domain proxy.

The client side component

The client needs to make a call to the same domain with classification text and act on the response.

For example in a user input box where tags need to be suggested. In this example a call must be made back to the application server asking for tags for a piece of text. When (if) a response is returned the code then updates a supporting field suggesting tags for the user to edit. The call being made should not contain the API key for Ingenia.

Feel free to poke around our demo for a more complicated example.

The server side component

In order to circumvent same domain restrictions on ajax calls, the application must proxy calls to Ingenia.

In our example above the incoming text would be sent on with relevant API key to Ingenia for classification. The response would then be passed back to the requesting client.

See here for example proxy code

Example classify code

  var text        = "php php php php php";
  var endpoint    = 'http://localhost:8080'; // your domain local proxy
  var api_key     = 'YOUR_API_KEY'; // shouldn't keep this on client side
  var api_version = '1.0';

  function classify_callback(payload) {

    console.log(payload);

    if(payload.status == 'okay') {

      var classification = payload.data;

      switch(classification.classification_status) {
        case 'complete':

          var results = classification.results;

          for(var ts in results) {

            var tag_set = results[ts];

            console.log( "tag_set: " + ts + "  id: " + tag_set.id );

            var tags = results[ts].tags;
            for(var t = 0; t  tags.length; t++) {

              var tag = tags[t];

              console.log("  tag: " + tag.name + "  score: " + tag.score );
            }
          }

          break;

        default:
          console.log( "there was an error: " + payload.message );
        }
    
    } else {
      console.log("something else died :" + payload.message);
    
    }
  }

  function classify_error() {

    console.log( "network errror" );

  }

  var response = $.post( 
    endpoint + '/classify', 
    { text: text, api_key: api_key, api_version: api_version }, 
    classify_callback, 
    "json" 
  ).error(classify_error);

Example train code

  var tags
  var endpoint    = 'http://localhost:8080'; // your domain
  var api_key     = 'YOUR_API_KEY';
  var api_version = '1.0';

  var text        = "php php php php php";

  var tags = {
    Software: [ 'php', 'code', 'web-development', 'scripting' ]
  };

  var tags_s = JSON.stringify(tags);

  function train_callback(payload) {

    console.log(payload);

    if(payload.status == 'okay') {

      var train_data = payload.data;

      console.log( "document ID: " + train_data.id );

      for(var ts in train_data.tag_sets) {

        var tag_set = train_data.tag_sets[ts];

        console.log( "tag_set: " + ts + "   id: " + tag_set.id );

        for(var t in tag_set.tags) {

          console.log( "  tag: " + t + "  id: " + tag_set.tags[t] );
        }
      }
    
    } else {
      console.log("something else died :" + payload.message);
    
    }
  }

  function train_error() {

    console.log( "network error" );

  }

  var response = $.post( 
    endpoint + '/train', { text: text, tag_sets: tags_s, api_key: api_key, api_version: api_version }, train_callback, "json" ).error(train_error);