{ "cells": [ { "cell_type": "raw", "id": "3d577700", "metadata": {}, "source": [ "---\n", "title: NLP with Spacy\n", "description: This application shows different tasks of Spacy\n", "show-code: False\n", "params:\n", " sent:\n", " input: text\n", " label: Enter the text to analyze\n", " value: Apple is looking at buying U.K. startup for $1 billion\n", " task:\n", " input: select\n", " label: Choose the tasks to do\n", " choices: [Dependency Parser, Named Entity Recognition, Sentiment Analysis]\n", " value: Named Entity Recognition\n", " multi: True\n", " compact:\n", " input: checkbox\n", " label: Compact(If you chose Dependency Parser, check the below boxes accordingly)\n", " value: False\n", " collapse_punctuation:\n", " input: checkbox\n", " label: Collapse Punctuation\n", " value: False\n", " collapse_phrases:\n", " input: checkbox\n", " label: Collapse Phrases\n", " value: False\n", " entities:\n", " input: select\n", " label: If you chose Named Entity Recognition task then select the named entities you want to see\n", " choices: [PERSON, ORG, DATE, PRODUCT, LOC, GPE, LANGUAGE, FAC, NORP, MONEY]\n", " multi: True\n", "---" ] }, { "cell_type": "markdown", "id": "62ddeb5f", "metadata": {}, "source": [ "#
NLP with SpaCy 💥
\n", "###
A web application built with Mercury and SpaCy
" ] }, { "cell_type": "markdown", "id": "a76b4050", "metadata": {}, "source": [ "This web application is built by converting jupyter notebook using [mercury](https://mljar.com/mercury/). Mercury is a framework that can convert your jupyter notebook into an interactive web application with almost no code. We can share jupyter notebooks easily by hosting them on web platforms. All you need to do is add YAML config to the first cell of your notebook and mercury takes care of the rest. We can deploy the apps made by mercury on popular platforms like Heroku, AWS, and Hugging Face Spaces.\n", "\n", "##### From the official [Github](https://github.com/mljar/mercury) page\n", "> Mercury can convert your jupyter notebook into \n", "> - interactive web app\n", "> - interactive slides\n", "> - data dashboard\n", "> - beautiful report\n", "\n", "All you need to do is start by installing it \n", "**pip install mljar-mercury**\n", "\n", "The main objective of this web application is to demonstrate the ability of Mercury to create NLP web apps from jupyter notebooks. [SpaCy](https://spacy.io/) is used to perform the NLP tasks. This app can do tasks like visualizing Dependency Parser, Named Entity Recognition, and Sentiment Analysis. Follow the below steps to run this application.\n", "\n", "1. Enter the text you want to analyse in the text box that is present in the side bar. \n", "2. Choose the task you want to perform. \n", "3. Choose the required parameters for each task.\n", "4. Click 'Run' \n", "\n", "Wait for the execution of the notebook. You will see your output below." ] }, { "cell_type": "code", "execution_count": 4, "id": "84845499", "metadata": {}, "outputs": [], "source": [ "sent = 'Notebook in watch mode. All changes to Notebook will be automatically visible in Mercury'\n", "compact = False\n", "collapse_punctuation = True\n", "collapse_phrases = False\n", "entities = 'PERSON'" ] }, { "cell_type": "code", "execution_count": null, "id": "7c506125", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import spacy\n", "from spacy import displacy\n", "from spacytextblob.spacytextblob import SpacyTextBlob\n", "from simple_colors import *" ] }, { "cell_type": "code", "execution_count": 6, "id": "16e95d2f", "metadata": {}, "outputs": [], "source": [ "def tasks(task):\n", " if 'Dependency Parser' in task:\n", " options = {\"compact\":compact, \"collapse_punct\":collapse_punctuation, \"collapse_phrases\":collapse_phrases, \"color\": \"black\", \"bg\":\"linear-gradient(90deg, #aa9cfc, #fc9ce7)\",\"distance\":100}\n", " print(red('Dependency Parser', ['bold', 'underlined']))\n", " displacy.render(doc, style=\"dep\", jupyter =True, options=options)\n", " if 'Named Entity Recognition' in task:\n", " try:\n", " options = {\"ents\": entities }\n", " print(red('Named Entities', ['bold', 'underlined']))\n", " if len(doc.ents)==0:\n", " print('Spacy has not detected any entities in the doc object.')\n", " else:\n", " displacy.render(doc, style=\"ent\", jupyter=True, options=options)\n", " except:\n", " print(red('Named Entities', ['bold', 'underlined']))\n", " print('No entity is chosen. Please select atleast one entity from the list on the sidebar.')\n", " if 'Sentiment Analysis' in task:\n", " print(red('Sentiment Analysis', ['bold', 'underlined']))\n", " if doc._.blob.polarity > 0:\n", " print('Positive:'+ ' Polarity score is '+str(doc._.blob.polarity))\n", " elif doc._.blob.polarity < 0:\n", " print('Negative:'+ ' Polarity score is '+str(doc._.blob.polarity))\n", " else:\n", " print('Neutral:'+ ' Polarity score is '+str(doc._.blob.polarity))" ] }, { "cell_type": "code", "execution_count": null, "id": "d2840678", "metadata": {}, "outputs": [], "source": [ "nlp = spacy.load(\"en_core_web_sm\")\n", "nlp.add_pipe('spacytextblob')\n", "doc = nlp(sent)" ] }, { "cell_type": "code", "execution_count": null, "id": "62adc4ef", "metadata": { "scrolled": true }, "outputs": [], "source": [ "tasks(task)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }