Build beautiful and interactive API documentation for ORDS

Image
In this blog post, I will show you how to quickly build beautiful and interactive API documentation for your Oracle APEX REST data sources using  swagger hub . Using APEX v23.1. I downloaded the  titanic data set  and loaded them into tables in my APEX instance, created some authorized restful services and published them using swagger hub. You can create a free account on swagger hub.   Check out my titanic swagger hub here ; Press Authorize. Username REST, password Glasgow123! I won't go through creating RESTful services and just show you the four I created that sit on top of the titanic data set; The GET is a very simple SQL query;      select * from TITANIC_DATA_SET_NEW A handy tip is to add comments, as there will appear on swagger hub, making your API self documenting; Once you have created your modules, press the Generate Swagger Doc button; This will generate an open API for you.  Copy the API and paste it into swagger hub This will generate the documentation; As mentioned b

Sentiment Analysis for Oracle APEX using client side JavaScript

If you have an interactive grid with any sort of review text a nice value add for the reviews is the addition of sentiment analysis.   This post will show you how to add a sentiment column of positive, negative or neutral and a score to your interactive grid using a couple of lines of code.


Demo app

This post is a quick front end value add of sentiment analysis to your app using client side JavaScript.  If this works for you I suggest using Oracle Text or some other sentiment analysis tool to do the sentiment analysis on the database rather than the front end as shown here.

We are using a client side JavaScript library compendium to do the sentiment analysis.  First download compendium.js from here and upload it to static application files in your APEX app.  Get the reference to the JS file, in my case #APP_IMAGES#compendium.js

Create a new page and include the JavaScript library.


Create a new IG region with the static ID ANSWERS_IG. Sample SQL for your interactive grid with 2 placeholder columns for the sentiment analysis, SENTIMENT to hold the text, positive, negative or neutral and SENTIMENT_VALUE to hold the score.


select 'Product 1' as product,
'Good, great, brilliant, superb' as product_review,
       'N/A' as SENTIMENT,
       'N/A' as SENTIMENT_VALUE from dual

union

select 'Product 2' as product,
'Bad, terrible, worst, hated it' as product_review,
       'N/A' as SENTIMENT,
       'N/A' as SENTIMENT_VALUE from dual

union

select 'Product 3' as product,
'OK, standard, not too bad' as product_review,
       'N/A' as SENTIMENT,
       'N/A' as SENTIMENT_VALUE from dual

union

select 'Product 4' as product,
'Worst product ever' as product_review,
       'N/A' as SENTIMENT,
       'N/A' as SENTIMENT_VALUE from dual


We will use JQuery to populate the sentiment columns, so give the columns PRODUCT_REVIEW, SENTIMENT and SENTIMENT_VALUE the same class names so JQuery can access the column text. An example is the PRODUCT_REVIEW column.


Add the following JS to execute when page loads.

$.each($('#ANSWERS_IG tbody tr'), function(i, item) {
     var responseCell = $(item).find('.PRODUCT_REVIEW');

     if (responseCell.text())
     {
        var sentiment = compendium.analyse(responseCell.text())[0];
        if (sentiment.profile.label == 'negative')
        {
           $(item).find('.SENTIMENT').html('<p style="color:red;font-size: 125%">'+sentiment.profile.label+'</p>');
        }
        else if (sentiment.profile.label == 'positive'){
           $(item).find('.SENTIMENT').html('<p style="color:green;font-size: 125%"">'+sentiment.profile.label+'</p>');
        }
        else
        {
           $(item).find('.SENTIMENT').html('<span>'+sentiment.profile.label+'</span>'); 
        } 
        $(item).find('.SENTIMENT_VALUE').text(sentiment.profile.sentiment.toFixed(3));
    }
});

The JQuery code loops round all the rows in your IG.  The analysis happens on this line where the text is passed to the JS code for analysis

var sentiment = compendium.analyse(responseCell.text())[0];

We are interested in the returned sentiment profile which you can view here,

Lastly I have added some styling to the sentiment which you can change to suit.

For example I want positive sentiment to show green with a larger font size.

$(item).find('.SENTIMENT').html('<p style="color:green;font-size: 125%"">'+sentiment.profile.label+'</p>');



Comments

Popular posts from this blog

Oracle APEX Interactive Grid colour cells based on a condition using JavaScript and CSS

Oracle APEX pretty checkbox item plugin

Build beautiful and interactive API documentation for ORDS