This extension provides a UDF and TVF that perform sentiment analysis using VADER.
Usage:
sentiment(input TEXT)
Parameters:
- input: The text to analyze.
Returns:
A record four fields, each representing a certain sentiment score as a DOUBLE: compound, positive, negative, and neutral.
Description: This function will return a record with four sentiment scores for the given input.
Example:
select to_json(sentiment("i like ice cream"));+---------------------------------------------------------------------------------------------------------+
| to_json(sentiment("i like ice cream")) |
+---------------------------------------------------------------------------------------------------------+
| {"compound":0.3611575592573076,"negative":0,"neutral":0.4444444444444444,"positive":0.5555555555555556} |
+---------------------------------------------------------------------------------------------------------+Usage:
sentiment_tvf(input TEXT)
Parameters:
- input: The text to analyze.
Returns:
A table with four columns, each representing a certain sentiment score as a DOUBLE: compound, positive, negative, and neutral.
Description: This function is identical to the UDF above, except that it will return a table with the four sentiment scores as columns.
Example:
select * from sentiment_tvf('i like ice cream');+--------------------+--------------------+----------+--------------------+
| compound | positive | negative | neutral |
+--------------------+--------------------+----------+--------------------+
| 0.3611575592573076 | 0.5555555555555556 | 0 | 0.4444444444444444 |
+--------------------+--------------------+----------+--------------------+To install these functions using the MySQL CLI, you can use the following command. Replace $DBUSER, $DBHOST, $DBPORT, and $DBNAME with, respectively, your database username, hostname, port, and the name of the database where you want to deploy the functions.
mysql -u $DBUSER -h $DBHOST -P $DBPORT -D $DBNAME -p < load_standalone.sqlThe example directory provides a data set (YouTube trending data from 2020) that can be used to explore this function. To load it, simply source the examples/load.sql file.
Then, you can run sentiment analysis like this:
select y.video_description, s.compound from youtube y, sentiment_tvf(y.video_description) s;The load_* scripts in this repo should be sufficient to load the pre-built extension into SingleStore. However, if you wish to rebuild it, do the following.
- Install Rust and Cargo
- Install the Rust
wasm32-wasitarget - Run
cargo build --target wasm32-wasi
To build the extension from the Wasm locally, run the following.
cp ./target/wasm32-wasi/release/json-flatten.wasm ./json-flatten.wasm
tar cvf json-flatten.tar json-flatten.sql json-flatten.wasm json-flatten.wit There are automated script tests in the db-tests directory.
They are run against the singlestoredb-dev-image.
In order to run the tests
- Install Python 3
- Install
singlestoredb[pytest](and optionallypytest-xdist) using pip - Set the
SINGLESTORE_LICENSEenvironment variable - Run the
pytestcommand
If you installed pytest-xdist, you can also distribute the tests to multiple workers
by running pytest -n auto or giving a specific number instead of auto
This repo repackages code originally written by Chris Wong (https://github.com/ckw017/vader-sentiment-rust). Thanks, Chris!