codemonth.dk

One project every month - making stuff better ...

Simple sentiment analysis

Simple sentiment analysis. I was reading up an article about it, and as a simple demonstration I implemented a sentiment analysis in plsql based on the AFINN-111 dictionary. It uses a simple scoring system from -5 to 5, for negative and positive sentiments.

To install the package go to my github repository and download the source. Install the 2 required packages:


@sentiment_ninja_dict.spec.sql
@sentiment_ninja_dict.body.sql
@sentiment_ninja.spec.sql
@sentiment_ninja.body.sql

Once installed, let us test it out, with a couple of test sentences:


SQL> select sentiment_ninja.sentiment('Sentiment ninja is pretty good') from dual;

SENTIMENT_NINJA.SENTIMENT('SENTIMENTNINJAISPRETTYGOOD')
-------------------------------------------------------
						      4

SQL> select sentiment_ninja.sentiment('Sentiment analysis can be bad and good') from dual;

SENTIMENT_NINJA.SENTIMENT('SENTIMENTANALYSISCANBEBADANDGOOD')
-------------------------------------------------------------
							    0

SQL> select sentiment_ninja.sentiment('But bad writing is just really bad') from dual;

SENTIMENT_NINJA.SENTIMENT('BUTBADWRITINGISJUSTREALLYBAD')
---------------------------------------------------------
						       -6

SQL> 

And there is the boolean versions available as well:


SQL> set serverout on
SQL> begin
  2  if sentiment_ninja.is_positive('I wonder if this is good') then
  3  dbms_output.put_line('Yes it is');
  4  end if;
  5  end;
  6  /
Yes it is

PL/SQL procedure successfully completed.

SQL> begin
  2  if sentiment_ninja.is_negative('I wonder if this is bad') then
  3  dbms_output.put_line('Yes it is');
  4  end if;
  5  end;
  6  /
Yes it is

PL/SQL procedure successfully completed.

SQL> 

Tagged in : Pipelined function, regex_substr