abdullahmubeen10 commited on
Commit
a5d5930
ยท
verified ยท
1 Parent(s): ad11dd7

Upload 28 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="#29B4E8"
Demo.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sparknlp
3
+ import os
4
+ import pandas as pd
5
+
6
+ from sparknlp.base import *
7
+ from sparknlp.annotator import *
8
+ from pyspark.ml import Pipeline
9
+ from sparknlp.pretrained import PretrainedPipeline
10
+
11
+ # Page configuration
12
+ st.set_page_config(
13
+ layout="wide",
14
+ page_title="Spark NLP Demos App",
15
+ initial_sidebar_state="auto"
16
+ )
17
+
18
+ # CSS for styling
19
+ st.markdown("""
20
+ <style>
21
+ .main-title {
22
+ font-size: 36px;
23
+ color: #4A90E2;
24
+ font-weight: bold;
25
+ text-align: center;
26
+ }
27
+ .section p, .section ul {
28
+ color: #666666;
29
+ }
30
+ </style>
31
+ """, unsafe_allow_html=True)
32
+
33
+ @st.cache_resource
34
+ def init_spark():
35
+ return sparknlp.start()
36
+
37
+ @st.cache_resource
38
+ def create_pipeline(model):
39
+ documentAssembler = DocumentAssembler()\
40
+ .setInputCol("text")\
41
+ .setOutputCol("document")
42
+
43
+ use = UniversalSentenceEncoder.pretrained("tfhub_use", "en")\
44
+ .setInputCols(["document"])\
45
+ .setOutputCol("sentence_embeddings")
46
+
47
+
48
+ sentimentdl = SentimentDLModel.pretrained(model, "en")\
49
+ .setInputCols(["sentence_embeddings"])\
50
+ .setOutputCol("sentiment")
51
+
52
+ nlpPipeline = Pipeline(stages=[documentAssembler, use, sentimentdl])
53
+
54
+ return nlpPipeline
55
+
56
+ def fit_data(pipeline, data):
57
+ empty_df = spark.createDataFrame([['']]).toDF('text')
58
+ pipeline_model = pipeline.fit(empty_df)
59
+ model = LightPipeline(pipeline_model)
60
+ results = model.fullAnnotate(data)[0]
61
+
62
+ return results['sentiment'][0].result
63
+
64
+ # Set up the page layout
65
+ st.markdown('<div class="main-title">State-of-the-Art Sentiment Detection with Spark NLP</div>', unsafe_allow_html=True)
66
+
67
+ # Sidebar content
68
+ model_name = st.sidebar.selectbox(
69
+ "Choose the pretrained model",
70
+ ["sentimentdl_use_imdb", "sentimentdl_use_twitter"],
71
+ help="For more info about the models visit: https://sparknlp.org/models"
72
+ )
73
+
74
+ # Reference notebook link in sidebar
75
+ link = """
76
+ <a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/SENTIMENT_EN.ipynb">
77
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
78
+ </a>
79
+ """
80
+ st.sidebar.markdown('Reference notebook:')
81
+ st.sidebar.markdown(link, unsafe_allow_html=True)
82
+
83
+ # Load examples
84
+ folder_path = f"inputs/{model}"
85
+ examples = [
86
+ lines[1].strip()
87
+ for filename in os.listdir(folder_path)
88
+ if filename.endswith('.txt')
89
+ for lines in [open(os.path.join(folder_path, filename), 'r', encoding='utf-8').readlines()]
90
+ if len(lines) >= 2
91
+ ]
92
+
93
+ st.subheader("Detect the general sentiment expressed in a movie review or tweet by using our pretrained Spark NLP DL classifier.")
94
+
95
+ selected_text = None
96
+ result_type = 'tweet'
97
+ if 'imdb' in model.lower() or 't5' in model.lower():
98
+ selected_text = st.selectbox("Select a sample IMDB review", examples)
99
+ result_type = 'review'
100
+ else:
101
+ selected_text = st.selectbox("Select a sample Tweet", examples)
102
+
103
+ custom_input = st.text_input("Try it for yourself!")
104
+
105
+ if custom_input:
106
+ selected_text = custom_input
107
+ elif selected_text:
108
+ selected_text = selected_text
109
+
110
+ st.subheader('Selected Text')
111
+ st.write(selected_text)
112
+
113
+ # Initialize Spark and create pipeline
114
+ spark = init_spark()
115
+ pipeline = create_pipeline(model)
116
+ output = fit_data(pipeline, selected_text)
117
+
118
+ # Display output sentence
119
+ if output in ['pos', 'positive', 'POSITIVE']:
120
+ st.markdown("""<h3>This seems like a <span style="color: green">{}</span> {}. <span style="font-size:35px;">&#128515;</span></h3>""".format('positive', result_type), unsafe_allow_html=True)
121
+ elif output in ['neg', 'negative', 'NEGATIVE']:
122
+ st.markdown("""<h3>This seems like a <span style="color: red">{}</span> {}. <span style="font-size:35px;">&#128544;</span?</h3>""".format('negative', result_type), unsafe_allow_html=True)
123
+
124
+
Dockerfile ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Download base image ubuntu 18.04
2
+ FROM ubuntu:18.04
3
+
4
+ # Set environment variables
5
+ ENV NB_USER jovyan
6
+ ENV NB_UID 1000
7
+ ENV HOME /home/${NB_USER}
8
+
9
+ # Install required packages
10
+ RUN apt-get update && apt-get install -y \
11
+ tar \
12
+ wget \
13
+ bash \
14
+ rsync \
15
+ gcc \
16
+ libfreetype6-dev \
17
+ libhdf5-serial-dev \
18
+ libpng-dev \
19
+ libzmq3-dev \
20
+ python3 \
21
+ python3-dev \
22
+ python3-pip \
23
+ unzip \
24
+ pkg-config \
25
+ software-properties-common \
26
+ graphviz \
27
+ openjdk-8-jdk \
28
+ ant \
29
+ ca-certificates-java \
30
+ && apt-get clean \
31
+ && update-ca-certificates -f;
32
+
33
+ # Install Python 3.8 and pip
34
+ RUN add-apt-repository ppa:deadsnakes/ppa \
35
+ && apt-get update \
36
+ && apt-get install -y python3.8 python3-pip \
37
+ && apt-get clean;
38
+
39
+ # Set up JAVA_HOME
40
+ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
41
+ RUN mkdir -p ${HOME} \
42
+ && echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
43
+ && chown -R ${NB_UID}:${NB_UID} ${HOME}
44
+
45
+ # Create a new user named "jovyan" with user ID 1000
46
+ RUN useradd -m -u ${NB_UID} ${NB_USER}
47
+
48
+ # Switch to the "jovyan" user
49
+ USER ${NB_USER}
50
+
51
+ # Set home and path variables for the user
52
+ ENV HOME=/home/${NB_USER} \
53
+ PATH=/home/${NB_USER}/.local/bin:$PATH
54
+
55
+ # Set the working directory to the user's home directory
56
+ WORKDIR ${HOME}
57
+
58
+ # Upgrade pip and install Python dependencies
59
+ RUN python3.8 -m pip install --upgrade pip
60
+ COPY requirements.txt /tmp/requirements.txt
61
+ RUN python3.8 -m pip install -r /tmp/requirements.txt
62
+
63
+ # Copy the application code into the container at /home/jovyan
64
+ COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
65
+
66
+ # Expose port for Streamlit
67
+ EXPOSE 7860
68
+
69
+ # Define the entry point for the container
70
+ ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
images/Sentiment-Analysis.jpg ADDED
images/dataset.png ADDED
images/johnsnowlabs-sentiment-output.png ADDED
inputs/sentimentdl_use_imdb/Example1.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Demonicus is a movie turned into a video game! I just love the story and the things that goes on in ...
2
+ Demonicus is a movie turned into a video game! I just love the story and the things that goes on in the film.It is a B-film ofcourse but that doesn`t bother one bit because its made just right and the music was rad! Horror and sword fight freaks,buy this movie now!
inputs/sentimentdl_use_imdb/Example10.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ This is to the Zatoichi movies as the "Star Trek" movies were to "Star Trek"--except that in this ca...
2
+ This is to the Zatoichi movies as the "Star Trek" movies were to "Star Trek"--except that in this case every one of the originals was more entertaining and interesting than this big, shiny re-do, and also better made, if substance is more important than surface. Had I never seen them, I would have thought this good-looking but empty; since I had, I thought its style inappropriate and its content insufficient. The idea of reviving the character in a bigger, slicker production must have sounded good, but there was no point in it, other than the hope of making money; it's just a show, which mostly fails to capture the atmosphere of the character's world and wholly fails to take the character anywhere he hasn't been already (also, the actor wasn't at his best). I'd been hoping to see Ichi at a late stage of life, in a story that would see him out gracefully and draw some conclusion from his experience overall; this just rehashes bits and pieces from the other movies, seasoned with more sex and sfx violence. Not the same experience at all.
inputs/sentimentdl_use_imdb/Example2.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Back when Alec Baldwin and Kim Basinger were a mercurial, hot-tempered, high-powered Hollywood coupl...
2
+ Back when Alec Baldwin and Kim Basinger were a mercurial, hot-tempered, high-powered Hollywood couple they filmed this (nearly) scene-for-scene remake of the 1972 Steve McQueen-Ali MacGraw action-thriller about a fugitive twosome. It almost worked the first time because McQueen was such a vital presence on the screen--even stone silent and weary, you could sense his clock ticking, his cagey magnetism. Baldwin is not in Steve McQueen's league, but he has his charms and is probably a more versatile actor--if so, this is not a showcase for his attributes. Basinger does well and certainly looks good, but James Woods is artificially hammy in a silly mob-magnet role. A sub-plot involving another couple taken hostage by Baldwin's ex-partner was unbearable in the '72 film and plays even worse here. As for the action scenes, they're pretty old hat, which causes one to wonder: why even remake the original?
inputs/sentimentdl_use_imdb/Example3.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Despite a tight narrative, Johnnie To's Election feels at times like it was once a longer picture, w...
2
+ Despite a tight narrative, Johnnie To's Election feels at times like it was once a longer picture, with many characters and plot strands abandoned or ultimately unresolved. Some of these are dealt with in the truly excellent and far superior sequel, Election 2: Harmony is a Virtue, but it's still a dependably enthralling thriller about a contested Triad election that bypasses the usual shootouts and explosions (though not the violence) in favour of constantly shifting alliances that can turn in the time it takes to make a phone call. It's also a film where the most ruthless character isn't always the most threatening one, as the chilling ending makes only too clear: one can imagine a lifetime of psychological counselling being necessary for all the trauma that one inflicts on one unfortunate bystander. Simon Yam, all too often a variable actor but always at his best under To's direction, has possibly never been better in the lead, not least because Tony Leung's much more extrovert performance makes his stillness more the powerful.
inputs/sentimentdl_use_imdb/Example4.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ This movie has successfully proved what we all already know, that professional basket-ball players s...
2
+ This movie has successfully proved what we all already know, that professional basket-ball players suck at everything besides playing basket-ball. Especially rapping and acting. I can not even begin to describe how bad this movie truly is. First of all, is it just me, or is that the ugliest kid you have ever seen? I mean, his teeth could be used as a can-opener. Secondly, why would a genie want to pursue a career in the music industry when, even though he has magical powers, he sucks horribly at making music? Third, I have read the Bible. In no way shape or form did it say that Jesus made genies. Fourth, what was the deal with all the crappy special effects? I assure you that any acne-addled nerdy teenager with a computer could make better effects than that. Fifth, why did the ending suck so badly? And what the hell is a djin? And finally, whoever created the nightmare known as Kazaam needs to be thrown off of a plane and onto the Eiffel Tower, because this movie take the word "suck" to an entirely new level.
inputs/sentimentdl_use_imdb/Example5.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ The fluttering of butterfly wings in the Atlantic can unleash a hurricane in the Pacific. According ...
2
+ The fluttering of butterfly wings in the Atlantic can unleash a hurricane in the Pacific. According to this theory (somehow related to the Chaos Theory, I'm not sure exactly how), every action, no matter how small or insignificant, will start a chain reaction that can lead to big events. This small jewel of a film shows us a series of seemingly-unrelated characters, most of them in Paris, whose actions will affect each others' lives. (The six-degrees-of-separation theory can be applied as well.) Each story is a facet of the jewel that is this film. The acting is finely-tuned and nuanced (Audrey Tautou is luminous), the stories mesh plausibly, the humor is just right, and the viewer leaves the theatre nodding in agreement.
inputs/sentimentdl_use_imdb/Example6.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ There have been very few films I have not been able to sit through. I made it through Battle Field E...
2
+ There have been very few films I have not been able to sit through. I made it through Battle Field Earth no problem. But this, This is one of the single worst films EVER to be made. I understand Whoopi Goldberg tried to get of acting in it. I do not blame her. I would feel ashamed to have this on a resume. I belive it is a rare occasion when almost every gag in a film falls flat on it's face. Well it happens here. Not to mention the SFX, look for the dino with the control cables hanging out of it rear end!!!!!! Halfway through the film I was still looking for a plot. I never found one. Save yourself the trouble of renting this and save 90 minutes of your life.
inputs/sentimentdl_use_imdb/Example7.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ After a long hard week behind the desk making all those dam serious decisions this movie is a great ...
2
+ After a long hard week behind the desk making all those dam serious decisions this movie is a great way to relax. Like Wells and the original radio broadcast this movie will take you away to a land of alien humor and sci-fi paraday. 'Captain Zippo died in the great charge of the Buick. He was a brave man.' The Jack Nicholson impressions shine right through that alien face with the dark sun glasses and leather jacket. And always remember to beware of the 'doughnut of death!' Keep in mind the number one rule of this movie - suspension of disbelief - sit back and relax - and 'Prepare to die Earth Scum!' You just have to see it for yourself.
inputs/sentimentdl_use_imdb/Example8.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ When Ritchie first burst on to movie scene his films were hailed as funny, witty, well directed and ...
2
+ When Ritchie first burst on to movie scene his films were hailed as funny, witty, well directed and original. If one could compare the hype he had generated with his first two attempts and the almost universal loathing his last two outings have created one should consider - has Ritchie been found out? Is he really that talented? Does he really have any genuine original ideas? Or is he simply a pretentious and egotistical director who really wants to be Fincher, Tarantino and Leone all rolled into one colossal and disorganised heap? After watching Revolver one could be excused for thinking were did it all go wrong? What happened to his great sense of humour? Where did he get all these mixed and convoluted ideas from? Revolver tries to be clever, philosophical and succinct, it tries to be an intelligent psychoanalysis, it tries to be an intricate and complicated thriller. Ritchie does make a gargantuan effort to fulfil all these many objectives and invests great chunks of a script into existential musings and numerous plot twists. However, in the end all it serves is to construct a severely disjointed, unstructured and ultimately unfriendly film to the audience. Its plagiarism is so sinful and blatant that although Ritchie does at least attempt to give his own spin he should be punished for even trying to pass it off as his own work. So what the audience gets ultimately is a terrible screenplay intertwined with many pretentious oneliners and clumsy setpieces.<br /><br />Revolver is ultimately an unoriginal and bland movie that has stolen countless themes from masterpieces like Fight Club, Usual Suspects and Pulp Fiction. It aims high, but inevitably shots blanks aplenty.<br /><br />Revolver deserves to be lambasted, it is a truly poor film masquerading as a wannabe masterpiece from a wannabe auteur. However, it falls flat on its farcical face and just fails at everything it wants to be and achieve.
inputs/sentimentdl_use_imdb/Example9.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ I always thought this would be a long and boring Talking-Heads flick full of static interior takes, ...
2
+ I always thought this would be a long and boring Talking-Heads flick full of static interior takes, dude, I was wrong. "Election" is a highly fascinating and thoroughly captivating thriller-drama, taking a deep and realistic view behind the origins of Triads-Rituals. Characters are constantly on the move, and although as a viewer you kinda always remain an outsider, it's still possible to feel the suspense coming from certain decisions and ambitions of the characters. Furthermore Johnnie To succeeds in creating some truly opulent images due to meticulously composed lighting and atmospheric light-shadow contrasts. Although there's hardly any action, the ending is still shocking in it's ruthless depicting of brutality. Cool movie that deserves more attention, and I came to like the minimalistic acoustic guitar score quite a bit.
inputs/sentimentdl_use_twitter/Example1.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @Mbjthegreat i really dont want AT&amp;T phone service..they suck when it comes to having a signal...
2
+ @Mbjthegreat i really dont want AT&amp;T phone service..they suck when it comes to having a signal
inputs/sentimentdl_use_twitter/Example10.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @PDubyaD right!!! LOL we'll get there!! I have high expectations, Warren Buffet style....
2
+ @PDubyaD right!!! LOL we'll get there!! I have high expectations, Warren Buffet style.
inputs/sentimentdl_use_twitter/Example2.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ holy crap. I take a nap for 4 hours and Pitchfork blows up my twitter dashboard. I wish I was at Coa...
2
+ holy crap. I take a nap for 4 hours and Pitchfork blows up my twitter dashboard. I wish I was at Coachella.
inputs/sentimentdl_use_twitter/Example3.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @Susy412 he is working today ive tried that still not working..... hmmmm!! im rubbish with computer...
2
+ @Susy412 he is working today ive tried that still not working..... hmmmm!! im rubbish with computers haha!
inputs/sentimentdl_use_twitter/Example4.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Brand New Canon EOS 50D 15MP DSLR Camera Canon 17-85mm IS Lens ...: Web Technology Thread, Brand New...
2
+ Brand New Canon EOS 50D 15MP DSLR Camera Canon 17-85mm IS Lens ...: Web Technology Thread, Brand New Canon EOS 5.. http://u.mavrev.com/5a3t
inputs/sentimentdl_use_twitter/Example5.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Watching a programme about the life of Hitler, its only enhancing my geekiness of history....
2
+ Watching a programme about the life of Hitler, its only enhancing my geekiness of history.
inputs/sentimentdl_use_twitter/Example6.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GM says expects announcment on sale of Hummer soon - Reuters: WDSUGM says expects announcment on sal...
2
+ GM says expects announcment on sale of Hummer soon - Reuters: WDSUGM says expects announcment on sale of Hummer .. http://bit.ly/4E1Fv
inputs/sentimentdl_use_twitter/Example7.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @accannis @edog1203 Great Stanford course. Thanks for making it available to the public! Really help...
2
+ @accannis @edog1203 Great Stanford course. Thanks for making it available to the public! Really helpful and informative for starting off!
inputs/sentimentdl_use_twitter/Example8.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @the_real_usher LeBron is cool. I like his personality...he has good character....
2
+ @the_real_usher LeBron is cool. I like his personality...he has good character.
inputs/sentimentdl_use_twitter/Example9.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @sketchbug Lebron is a hometown hero to me, lol I love the Lakers but let's go Cavs, lol...
2
+ @sketchbug Lebron is a hometown hero to me, lol I love the Lakers but let's go Cavs, lol
pages/Workflow & Model Overview.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Custom CSS for better styling
4
+ st.markdown("""
5
+ <style>
6
+ .main-title {
7
+ font-size: 36px;
8
+ color: #4A90E2;
9
+ font-weight: bold;
10
+ text-align: center;
11
+ }
12
+ .sub-title {
13
+ font-size: 24px;
14
+ color: #4A90E2;
15
+ margin-top: 20px;
16
+ }
17
+ .section {
18
+ background-color: #f9f9f9;
19
+ padding: 15px;
20
+ border-radius: 10px;
21
+ margin-top: 20px;
22
+ }
23
+ .section h2 {
24
+ font-size: 22px;
25
+ color: #4A90E2;
26
+ }
27
+ .section p, .section ul {
28
+ color: #666666;
29
+ }
30
+ .link {
31
+ color: #4A90E2;
32
+ text-decoration: none;
33
+ }
34
+ </style>
35
+ """, unsafe_allow_html=True)
36
+
37
+ # Introduction
38
+ st.markdown('<div class="main-title">Sentiment Analysis with Spark NLP</div>', unsafe_allow_html=True)
39
+
40
+ st.markdown("""
41
+ <div class="section">
42
+ <p>Welcome to the Spark NLP Sentiment Analysis Demo App! Sentiment analysis is an automated process capable of understanding the feelings or opinions that underlie a text. This process is considered a text classification and is one of the most interesting subfields of NLP. Using Spark NLP, it is possible to analyze the sentiment in a text with high accuracy.</p>
43
+ <p>This app demonstrates how to use Spark NLP's SentimentDetector to perform sentiment analysis using a rule-based approach.</p>
44
+ </div>
45
+ """, unsafe_allow_html=True)
46
+
47
+ st.image('images/Sentiment-Analysis.jpg',caption="Difference between rule-based and machine learning based sentiment analysis applications", use_column_width='auto')
48
+
49
+ # About Sentiment Analysis
50
+ st.markdown('<div class="sub-title">About Sentiment Analysis</div>', unsafe_allow_html=True)
51
+ st.markdown("""
52
+ <div class="section">
53
+ <p>Sentiment analysis studies the subjective information in an expression, such as opinions, appraisals, emotions, or attitudes towards a topic, person, or entity. Expressions can be classified as positive, negative, or neutral โ€” in some cases, even more detailed.</p>
54
+ <p>Some popular sentiment analysis applications include social media monitoring, customer support management, and analyzing customer feedback.</p>
55
+ </div>
56
+ """, unsafe_allow_html=True)
57
+
58
+ # Using SentimentDetector in Spark NLP
59
+ st.markdown('<div class="sub-title">Using SentimentDetector in Spark NLP</div>', unsafe_allow_html=True)
60
+ st.markdown("""
61
+ <div class="section">
62
+ <p>The SentimentDetector annotator in Spark NLP uses a rule-based approach to analyze the sentiment in text data. This method involves using a set of predefined rules or patterns to classify text as positive, negative, or neutral.</p>
63
+ <p>Spark NLP also provides Machine Learning (ML) and Deep Learning (DL) solutions for sentiment analysis. If you are interested in those approaches, please check the <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#viveknsentiment" target="_blank" rel="noopener">ViveknSentiment </a> and <a class="link" href="https://nlp.johnsnowlabs.com/docs/en/annotators#sentimentdl" target="_blank" rel="noopener">SentimentDL</a> annotators of Spark NLP.</p>
64
+ </div>
65
+ """, unsafe_allow_html=True)
66
+
67
+ st.markdown('<h2 class="sub-title">Example Usage in Python</h2>', unsafe_allow_html=True)
68
+ st.markdown('<p>Hereโ€™s how you can implement sentiment analysis using the SentimentDetector annotator in Spark NLP:</p>', unsafe_allow_html=True)
69
+
70
+ # Setup Instructions
71
+ st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
72
+ st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
73
+ st.code("""
74
+ pip install spark-nlp
75
+ pip install pyspark
76
+ """, language="bash")
77
+
78
+ st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
79
+ st.code("""
80
+ import sparknlp
81
+
82
+ # Start Spark Session
83
+ spark = sparknlp.start()
84
+ """, language='python')
85
+
86
+ # load data
87
+ st.markdown('<div class="sub-title">Start by loading the Dataset, Lemmas and the Sentiment Dictionary.</div>', unsafe_allow_html=True)
88
+ st.code("""
89
+ !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/lemma-corpus-small/lemmas_small.txt -P /tmp
90
+ !wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/sentiment-corpus/default-sentiment-dict.txt -P /tmp
91
+ """, language="bash")
92
+
93
+ st.image('images/dataset.png', caption="First few lines of the lemmas and sentiment dictionary", use_column_width='auto')
94
+
95
+ # Sentiment Analysis Example
96
+ st.markdown('<div class="sub-title">Example Usage: Sentiment Analysis with SentimentDetector</div>', unsafe_allow_html=True)
97
+ st.code('''
98
+ from sparknlp.base import DocumentAssembler, Pipeline, Finisher
99
+ from sparknlp.annotator import (
100
+ SentenceDetector,
101
+ Tokenizer,
102
+ Lemmatizer,
103
+ SentimentDetector
104
+ )
105
+ import pyspark.sql.functions as F
106
+
107
+ # Step 1: Transforms raw texts to document annotation
108
+ document_assembler = (
109
+ DocumentAssembler()
110
+ .setInputCol("text")
111
+ .setOutputCol("document")
112
+ )
113
+
114
+ # Step 2: Sentence Detection
115
+ sentence_detector = SentenceDetector().setInputCols(["document"]).setOutputCol("sentence")
116
+
117
+ # Step 3: Tokenization
118
+ tokenizer = Tokenizer().setInputCols(["sentence"]).setOutputCol("token")
119
+
120
+ # Step 4: Lemmatization
121
+ lemmatizer = (
122
+ Lemmatizer()
123
+ .setInputCols("token")
124
+ .setOutputCol("lemma")
125
+ .setDictionary("/tmp/lemmas_small.txt", key_delimiter="->", value_delimiter="\\t")
126
+ )
127
+
128
+ # Step 5: Sentiment Detection
129
+ sentiment_detector = (
130
+ SentimentDetector()
131
+ .setInputCols(["lemma", "sentence"])
132
+ .setOutputCol("sentiment_score")
133
+ .setDictionary("/tmp/default-sentiment-dict.txt", ",")
134
+ )
135
+
136
+ # Step 6: Finisher
137
+ finisher = (
138
+ Finisher()
139
+ .setInputCols(["sentiment_score"])
140
+ .setOutputCols(["sentiment"])
141
+ )
142
+
143
+ # Define the pipeline
144
+ pipeline = Pipeline(
145
+ stages=[
146
+ document_assembler,
147
+ sentence_detector,
148
+ tokenizer,
149
+ lemmatizer,
150
+ sentiment_detector,
151
+ finisher,
152
+ ]
153
+ )
154
+
155
+ # Create a spark Data Frame with an example sentence
156
+ data = spark.createDataFrame(
157
+ [
158
+ ["The restaurant staff is really nice"]
159
+ ]
160
+ ).toDF("text") # use the column name `text` defined in the pipeline as input
161
+
162
+ # Fit-transform to get predictions
163
+ result = pipeline.fit(data).transform(data).show(truncate=50)
164
+ ''', language='python')
165
+
166
+ st.text("""
167
+ +-----------------------------------+----------+
168
+ | text| sentiment|
169
+ +-----------------------------------+----------+
170
+ |The restaurant staff is really nice|[positive]|
171
+ +-----------------------------------+----------+
172
+ """)
173
+
174
+ st.markdown("""
175
+ <p>The code snippet demonstrates how to set up a pipeline in Spark NLP to perform sentiment analysis on text data using the SentimentDetector annotator. The resulting DataFrame contains the sentiment predictions.</p>
176
+ """, unsafe_allow_html=True)
177
+
178
+ # One-liner Alternative
179
+ st.markdown('<div class="sub-title">One-liner Alternative</div>', unsafe_allow_html=True)
180
+ st.markdown("""
181
+ <div class="section">
182
+ <p>In October 2022, John Snow Labs released the open-source <code>johnsnowlabs</code> library that contains all the company products, open-source and licensed, under one common library. This simplified the workflow, especially for users working with more than one of the libraries (e.g., Spark NLP + Healthcare NLP). This new library is a wrapper on all of John Snow Labโ€™s libraries and can be installed with pip:</p>
183
+ <p><code>pip install johnsnowlabs</code></p>
184
+ </div>
185
+ """, unsafe_allow_html=True)
186
+
187
+ st.markdown('<p>To run sentiment analysis with one line of code, we can simply:</p>', unsafe_allow_html=True)
188
+ st.code("""
189
+ # Import the NLP module which contains Spark NLP and NLU libraries
190
+ from johnsnowlabs import nlp
191
+
192
+ sample_text = "The restaurant staff is really nice"
193
+
194
+ # Returns a pandas DataFrame, we select the desired columns
195
+ nlp.load('en.sentiment').predict(sample_text, output_level='sentence')
196
+ """, language='python')
197
+
198
+ st.image('images/johnsnowlabs-sentiment-output.png', use_column_width='auto')
199
+
200
+ st.markdown("""
201
+ <p>This approach demonstrates how to use the <code>johnsnowlabs</code> library to perform sentiment analysis with a single line of code. The resulting DataFrame contains the sentiment predictions.</p>
202
+ """, unsafe_allow_html=True)
203
+
204
+ # Conclusion
205
+ st.markdown("""
206
+ <div class="section">
207
+ <h2>Conclusion</h2>
208
+ <p>In this app, we demonstrated how to use Spark NLP's SentimentDetector annotator to perform sentiment analysis on text data. These powerful tools enable users to efficiently process large datasets and identify sentiment, providing deeper insights for various applications. By integrating these annotators into your NLP pipelines, you can enhance text understanding, information extraction, and customer sentiment analysis.</p>
209
+ </div>
210
+ """, unsafe_allow_html=True)
211
+
212
+ # References and Additional Information
213
+ st.markdown('<div class="sub-title">For additional information, please check the following references.</div>', unsafe_allow_html=True)
214
+
215
+ st.markdown("""
216
+ <div class="section">
217
+ <ul>
218
+ <li>Documentation :&nbsp;<a href="https://nlp.johnsnowlabs.com/docs/en/transformers#sentiment" target="_blank" rel="noopener">SentimentDetector</a></li>
219
+ <li>Python Docs :&nbsp;<a href="https://nlp.johnsnowlabs.com/api/python/reference/autosummary/sparknlp/annotator/sentiment/index.html#sparknlp.annotator.sentiment.SentimentDetector" target="_blank" rel="noopener">SentimentDetector</a></li>
220
+ <li>Scala Docs :&nbsp;<a href="https://nlp.johnsnowlabs.com/api/com/johnsnowlabs/nlp/annotators/sentiment/SentimentDetector.html" target="_blank" rel="noopener">SentimentDetector</a></li>
221
+ <li>Example Notebook :&nbsp;<a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/blob/master/jupyter/training/english/classification/Sentiment%20Analysis.ipynb" target="_blank" rel="noopener">Sentiment Analysis</a></li>
222
+ </ul>
223
+ </div>
224
+ """, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ numpy
4
+ spark-nlp
5
+ pyspark