Skip to content

Instantly share code, notes, and snippets.

@itsuncheng
Created January 20, 2021 07:01
Show Gist options
  • Save itsuncheng/407cc83419e95508587b8d2c9931d5fe to your computer and use it in GitHub Desktop.
Save itsuncheng/407cc83419e95508587b8d2c9931d5fe to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Library"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build Sentiment Analysis Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentiment_analysis = pipeline(\"sentiment-analysis\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input Text"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"pos_text = \"I enjoy studying computational algorithms.\"\n",
"neg_text = \"I dislike sleeping late everyday.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Perform Sentiment Analysis"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Label: POSITIVE\n",
"Confidence Score: 0.9980718493461609\n",
"\n",
"Label: NEGATIVE\n",
"Confidence Score: 0.9881595969200134\n"
]
}
],
"source": [
"result = sentiment_analysis(pos_text)[0]\n",
"print(\"Label:\", result['label'])\n",
"print(\"Confidence Score:\", result['score'])\n",
"print()\n",
"\n",
"result = sentiment_analysis(neg_text)[0]\n",
"print(\"Label:\", result['label'])\n",
"print(\"Confidence Score:\", result['score'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment