abdullahmubeen10
commited on
Commit
•
4f52f17
1
Parent(s):
d8be452
Upload 12 files
Browse files- .streamlit/config.toml +3 -0
- Demo.py +148 -0
- Dockerfile +70 -0
- images/ngram-generation.jpg +0 -0
- images/ngram-visual.png +0 -0
- inputs/date_matcher/Example1.txt +5 -0
- inputs/date_matcher/Example2.txt +5 -0
- inputs/date_matcher/Example3.txt +6 -0
- inputs/date_matcher/Example4.txt +4 -0
- inputs/date_matcher/Example5.txt +3 -0
- pages/Workflow & Model Overview.py +191 -0
- requirements.txt +5 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
base="light"
|
3 |
+
primaryColor="#29B4E8"
|
Demo.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import sparknlp
|
3 |
+
from sparknlp.base import DocumentAssembler, LightPipeline
|
4 |
+
from sparknlp.annotator import SentenceDetectorDLModel
|
5 |
+
from sparknlp.pretrained import PipelineModel
|
6 |
+
from pyspark.ml import Pipeline
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
# Page configuration
|
10 |
+
st.set_page_config(
|
11 |
+
layout="wide",
|
12 |
+
page_title="Spark NLP Demos App",
|
13 |
+
initial_sidebar_state="auto"
|
14 |
+
)
|
15 |
+
|
16 |
+
# CSS for styling
|
17 |
+
st.markdown("""
|
18 |
+
<style>
|
19 |
+
.main-title {
|
20 |
+
font-size: 36px;
|
21 |
+
color: #4A90E2;
|
22 |
+
font-weight: bold;
|
23 |
+
text-align: center;
|
24 |
+
}
|
25 |
+
.section p, .section ul {
|
26 |
+
color: #666666;
|
27 |
+
}
|
28 |
+
.box {
|
29 |
+
text-align: left;
|
30 |
+
font-family: "IBM Plex Sans", sans-serif;
|
31 |
+
font-weight: normal;
|
32 |
+
width: 100%;
|
33 |
+
box-sizing: border-box;
|
34 |
+
position: relative;
|
35 |
+
font-size: 14px !important;
|
36 |
+
line-height: 26px !important;
|
37 |
+
color: #536B76 !important;
|
38 |
+
}
|
39 |
+
h3 {
|
40 |
+
text-align: centre;
|
41 |
+
box-sizing: border-box;
|
42 |
+
padding: 0;
|
43 |
+
margin: 25px 0 0 !important;
|
44 |
+
font-family: 'Montserrat', sans-serif !important;
|
45 |
+
font-weight: 500 !important;
|
46 |
+
font-size: 18px !important;
|
47 |
+
line-height: 22px;
|
48 |
+
color: #1E77B7 !important;
|
49 |
+
}
|
50 |
+
</style>
|
51 |
+
""", unsafe_allow_html=True)
|
52 |
+
|
53 |
+
# Initialize Spark NLP
|
54 |
+
@st.cache_resource
|
55 |
+
def init_spark():
|
56 |
+
return sparknlp.start()
|
57 |
+
|
58 |
+
# Create Spark NLP pipeline
|
59 |
+
@st.cache_resource
|
60 |
+
def create_pipeline(n):
|
61 |
+
document_assembler = DocumentAssembler().setInputCol("text").setOutputCol("document")
|
62 |
+
tokenizer = Tokenizer().setInputCols(["document"]).setOutputCol("token")
|
63 |
+
ngram = NGramGenerator().setN(n).setInputCols(["token"]).setOutputCol("ngrams")
|
64 |
+
pipeline = Pipeline(stages=[document_assembler, tokenizer, ngram])
|
65 |
+
|
66 |
+
return pipeline
|
67 |
+
|
68 |
+
# Function to fit data to the pipeline and get results
|
69 |
+
def fit_data(pipeline, data):
|
70 |
+
df = spark.createDataFrame([[data]]).toDF("text")
|
71 |
+
model = pipeline.fit(df)
|
72 |
+
light_pipeline = LightPipeline(model)
|
73 |
+
results = light_pipeline.fullAnnotate(data)
|
74 |
+
return results
|
75 |
+
|
76 |
+
# Set up the page layout
|
77 |
+
st.markdown('<div class="main-title">State-of-the-Art NGram Generation with Spark NLP</div>', unsafe_allow_html=True)
|
78 |
+
st.markdown("<h3>Generate meaningful n-grams from text data using Spark NLP's efficient and scalable NGramGenerator, capturing context and identifying key phrases even in large-scale, noisy datasets.</h3>", unsafe_allow_html=True)
|
79 |
+
st.write("")
|
80 |
+
|
81 |
+
# Sidebar configuration
|
82 |
+
NGram_selection_list = {"Unigram": 1, "bigram": 2, "trigram": 3}
|
83 |
+
NGram = st.sidebar.selectbox(
|
84 |
+
"Choose an NGram specification",
|
85 |
+
list(NGram_selection_list.keys()),
|
86 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
87 |
+
)
|
88 |
+
|
89 |
+
# Add the Colab link for the notebook
|
90 |
+
colab_link = """
|
91 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/Certification_Trainings/Public/9.SentenceDetectorDL.ipynb">
|
92 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
93 |
+
</a>
|
94 |
+
"""
|
95 |
+
st.sidebar.title('Reference notebook:')
|
96 |
+
st.sidebar.markdown(colab_link, unsafe_allow_html=True)
|
97 |
+
|
98 |
+
# Sample texts for sentence detection
|
99 |
+
examples = [
|
100 |
+
"Brexit: U.K to ban more EU citizens with criminal records. In a recent Home Office meeting a consensus has been made to tighten border policies. However a no-deal Brexit could make it harder to identify foreign criminals; With the U.K in a transition period since it formally left the EU in January, an EU citizen can currently only be refused entry if they present a genuine, present and serious threat.",
|
101 |
+
"Harry Harding on the US, China, and a ‘Cold War 2.0’. “Calling it a second Cold War is misleading, but to deny that it’s a Cold War is also disingenuous.”, Harding is a specialist on Asia and U.S.-Asian relations. His major publications include Organizing China: The Problem of Bureaucracy, 1949-1966.The phrase “new Cold War” is an example of the use of analogies in understanding the world.The world is a very complicated place.People like to find ways of coming to a clearer and simpler understanding.",
|
102 |
+
"Tesla’s latest quarterly numbers beat analyst expectations on both revenue and earnings per share, bringing in $8.77 billion in revenues for the third quarter.That’s up 39% from the year-ago period.Wall Street had expected $8.36 billion in revenue for the quarter, according to estimates published by CNBC. Revenue grew 30% year-on-year, something the company attributed to substantial growth in vehicle deliveries, and operating income also grew to $809 million, showing improving operating margins to 9.2%.",
|
103 |
+
"2020 is another year that is consistent with a rapidly changing Arctic.Without a systematic reduction in greenhouse gases, the likelihood of our first ‘ice-free’ summer will continue to increase by the mid-21st century;It is already well known that a smaller ice sheet means less of a white area to reflect the sun’s heat back into space. But this is not the only reason the Arctic is warming more than twice as fast as the global average",
|
104 |
+
"HBR: The world is changing in rapid, unprecedented ways, but one thing remains certain: as businesses look to embed lessons learned in recent months and to build enterprise resilience for the future, they are due for even more transformation.As such, most organizations are voraciously evaluating existing and future technologies to see if they’ll be able to deliver the innovation at scale that they’ll need to survive and thrive.However, technology should not be central to these transformation efforts; people should."
|
105 |
+
]
|
106 |
+
|
107 |
+
# User input for text selection
|
108 |
+
selected_text = st.selectbox("Select a sample text", examples)
|
109 |
+
custom_input = st.text_input("Try it for yourself!")
|
110 |
+
|
111 |
+
if custom_input:
|
112 |
+
selected_text = custom_input
|
113 |
+
|
114 |
+
st.subheader('Selected Text')
|
115 |
+
st.write(selected_text)
|
116 |
+
|
117 |
+
# Run the pipeline and display results
|
118 |
+
spark = init_spark()
|
119 |
+
Pipeline = create_pipeline(NGram_selection_list[NGram])
|
120 |
+
output = fit_data(Pipeline, selected_text)
|
121 |
+
|
122 |
+
# Display detected sentences
|
123 |
+
st.subheader('Genrated NGrams')
|
124 |
+
|
125 |
+
data = [ngram.result for ngram in output[0]['ngrams']]
|
126 |
+
df = pd.DataFrame(data)
|
127 |
+
st.dataframe(df)
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
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/ngram-generation.jpg
ADDED
images/ngram-visual.png
ADDED
inputs/date_matcher/Example1.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
David visited the restaurant yesterday with his family. He also visited and the day before, but at ...
|
2 |
+
David visited the restaurant yesterday with his family.
|
3 |
+
He also visited and the day before, but at that time he was alone.
|
4 |
+
David again visited today with his colleagues.
|
5 |
+
He and his friends really liked the food and hoped to visit again tomorrow.
|
inputs/date_matcher/Example2.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
In March 2003 she was seen in the office and appeared to be extremely disturbed emotionally. On 2003...
|
2 |
+
In March 2003 she was seen in the office and appeared to be extremely disturbed emotionally.
|
3 |
+
On 2003-04-04 she again visited and talked about the effects of the medication she has been taking, and seemed positive and in much better shape.
|
4 |
+
She again visited on Fri, 22/4/2003 and looked better.
|
5 |
+
She has been working out and taking her medicines since April 1st 2003.
|
inputs/date_matcher/Example3.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
I have a very busy schedule these days. I have meetings from 7pm. till 11pm. I have 3 meetings the d...
|
2 |
+
I have a very busy schedule these days. I have meetings from 7pm. till 11pm.
|
3 |
+
I have 3 meetings the day after, and have submission deadlines approaching as well.
|
4 |
+
By next mon I have to finalise the architecture, for which i'll have to hold multiple meetings with ARM.
|
5 |
+
Then i'll have to discuss dev plans by next tuesday and develop a thorough plan.
|
6 |
+
The plan should be ready by Nov 30th.
|
inputs/date_matcher/Example4.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
When Tom visited the Bahamas last year, it was his first time travelling. Since then he was travelle...
|
2 |
+
When Tom visited the Bahamas last year, it was his first time travelling.
|
3 |
+
Since then he was travelled a lot. For example, he visited Hawaii last week.
|
4 |
+
The last time we talked, he was planning to travel to Alaska next month.
|
inputs/date_matcher/Example5.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
Isn't it weird that all my family members have the same birth day and month? All of us were born on ...
|
2 |
+
Isn't it weird that all my family members have the same birth day and month? All of us were born on 1st Jan
|
3 |
+
Dad was born on 01/01/1900. Mom has a birth date of 1st Jan 1902. And I was born on 2000/01/01
|
pages/Workflow & Model Overview.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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">Scaling Up Text Analysis: Best Practices with Spark NLP n-gram Generation</div>', unsafe_allow_html=True)
|
39 |
+
|
40 |
+
st.markdown("""
|
41 |
+
<div class="section">
|
42 |
+
<p>Welcome to the Spark NLP n-gram Generation Demo App! N-gram generation is a crucial task in Natural Language Processing (NLP) that involves extracting contiguous sequences of n words from text. This is essential for capturing context and identifying meaningful phrases in natural language.</p>
|
43 |
+
<p>Using Spark NLP, it is possible to efficiently generate n-grams from large-scale text data. This app demonstrates how to use the NGramGenerator annotator to generate n-grams and provides best practices for scaling up text analysis tasks with Spark NLP.</p>
|
44 |
+
</div>
|
45 |
+
""", unsafe_allow_html=True)
|
46 |
+
|
47 |
+
# About N-gram Generation
|
48 |
+
st.markdown('<div class="sub-title">About N-gram Generation</div>', unsafe_allow_html=True)
|
49 |
+
st.markdown("""
|
50 |
+
<div class="section">
|
51 |
+
<p>N-gram generation involves extracting contiguous sequences of n words from text. It is a valuable technique for capturing context and identifying meaningful phrases in natural language. With Spark NLP’s ability to handle distributed computing, researchers and practitioners can scale up their text analysis tasks and unlock valuable insights from large volumes of text data.</p>
|
52 |
+
<p>The NGramGenerator annotator in Spark NLP simplifies the process of generating n-grams by seamlessly integrating with Apache Spark’s distributed computing capabilities. This allows for efficient, accurate, and scalable text analysis.</p>
|
53 |
+
</div>
|
54 |
+
""", unsafe_allow_html=True)
|
55 |
+
st.image('images/ngram-visual.png', use_column_width='auto')
|
56 |
+
|
57 |
+
# Using NGramGenerator in Spark NLP
|
58 |
+
st.markdown('<div class="sub-title">Using NGramGenerator in Spark NLP</div>', unsafe_allow_html=True)
|
59 |
+
st.markdown("""
|
60 |
+
<div class="section">
|
61 |
+
<p>The NGramGenerator annotator in Spark NLP allows users to generate n-grams from text data. This annotator supports various configurations and can be easily integrated into NLP pipelines for comprehensive text analysis.</p>
|
62 |
+
<p>The NGramGenerator annotator in Spark NLP offers:</p>
|
63 |
+
<ul>
|
64 |
+
<li>Efficient n-gram generation for large-scale text data</li>
|
65 |
+
<li>Support for various n-gram configurations (unigrams, bigrams, trigrams, etc.)</li>
|
66 |
+
<li>Seamless integration with other Spark NLP components for comprehensive NLP pipelines</li>
|
67 |
+
</ul>
|
68 |
+
</div>
|
69 |
+
""", unsafe_allow_html=True)
|
70 |
+
|
71 |
+
st.markdown('<h2 class="sub-title">Example Usage in Python</h2>', unsafe_allow_html=True)
|
72 |
+
st.markdown('<p>Here’s how you can implement the NGramGenerator annotator in Spark NLP:</p>', unsafe_allow_html=True)
|
73 |
+
|
74 |
+
# Setup Instructions
|
75 |
+
st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
|
76 |
+
st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
|
77 |
+
st.code("""
|
78 |
+
pip install spark-nlp
|
79 |
+
pip install pyspark
|
80 |
+
""", language="bash")
|
81 |
+
|
82 |
+
st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
|
83 |
+
st.code("""
|
84 |
+
import sparknlp
|
85 |
+
|
86 |
+
# Start Spark Session
|
87 |
+
spark = sparknlp.start()
|
88 |
+
""", language='python')
|
89 |
+
|
90 |
+
# Single N-gram Generation Example
|
91 |
+
st.markdown('<div class="sub-title">Example Usage: Single N-gram Generation with NGramGenerator</div>', unsafe_allow_html=True)
|
92 |
+
st.code('''
|
93 |
+
import sparknlp
|
94 |
+
from sparknlp.base import DocumentAssembler, PipelineModel, LightPipeline
|
95 |
+
from sparknlp.annotator import NGramGenerator, Tokenizer
|
96 |
+
import pyspark.sql.functions as F
|
97 |
+
|
98 |
+
# Start Spark NLP Session
|
99 |
+
spark = sparknlp.start()
|
100 |
+
|
101 |
+
# Sample Data
|
102 |
+
data = [("1", "This is an example sentence."),
|
103 |
+
("2", "Spark NLP provides powerful text analysis tools.")]
|
104 |
+
|
105 |
+
df = spark.createDataFrame(data, ["id", "text"])
|
106 |
+
|
107 |
+
# Document Assembler
|
108 |
+
document_assembler = DocumentAssembler().setInputCol("text").setOutputCol("document")
|
109 |
+
|
110 |
+
# Tokenizer
|
111 |
+
tokenizer = Tokenizer().setInputCols(["document"]).setOutputCol("token")
|
112 |
+
|
113 |
+
# NGramGenerator
|
114 |
+
ngram = NGramGenerator().setN(2).setInputCols(["token"]).setOutputCol("ngrams")
|
115 |
+
|
116 |
+
# Building Pipeline
|
117 |
+
pipeline = Pipeline(stages=[document_assembler, tokenizer, ngram])
|
118 |
+
|
119 |
+
# Fit and Transform
|
120 |
+
model = pipeline.fit(df)
|
121 |
+
result = model.transform(df)
|
122 |
+
|
123 |
+
# Display Results
|
124 |
+
result.select("ngrams.result").show(truncate=False)
|
125 |
+
''', language='python')
|
126 |
+
|
127 |
+
st.text("""
|
128 |
+
+---------------------------------------------------------------------------------------------------+
|
129 |
+
|result |
|
130 |
+
+---------------------------------------------------------------------------------------------------+
|
131 |
+
|[This is, is an, an example, example sentence, sentence .] |
|
132 |
+
|[Spark NLP, NLP provides, provides powerful, powerful text, text analysis, analysis tools, tools .]|
|
133 |
+
+---------------------------------------------------------------------------------------------------+
|
134 |
+
""")
|
135 |
+
|
136 |
+
st.markdown("""
|
137 |
+
<p>The code snippet demonstrates how to set up a pipeline in Spark NLP to generate n-grams using the NGramGenerator annotator. The resulting output shows the generated bigrams from the input text.</p>
|
138 |
+
""", unsafe_allow_html=True)
|
139 |
+
|
140 |
+
# Multi-language N-gram Generation
|
141 |
+
st.markdown('<div class="sub-title">Scaling Up Text Analysis</div>', unsafe_allow_html=True)
|
142 |
+
st.markdown("""
|
143 |
+
<div class="section">
|
144 |
+
<p>In the era of big data, scaling up text analysis tasks is paramount for deriving meaningful insights from vast amounts of textual data. Spark NLP, with its integration with Apache Spark, offers a powerful solution for efficiently processing large-scale text data.</p>
|
145 |
+
<p>The NGramGenerator annotator in Spark NLP provides an essential tool for generating n-grams from text, enabling the extraction of contextual information, and identifying meaningful phrases.</p>
|
146 |
+
</div>
|
147 |
+
""", unsafe_allow_html=True)
|
148 |
+
|
149 |
+
# Summary
|
150 |
+
st.markdown('<div class="sub-title">Summary</div>', unsafe_allow_html=True)
|
151 |
+
st.markdown("""
|
152 |
+
<div class="section">
|
153 |
+
<p>In this demo app, we explored how to generate n-grams using the NGramGenerator annotator in Spark NLP. This is a crucial step in text analysis, allowing us to capture the context and identify meaningful phrases from text data.</p>
|
154 |
+
<p>Spark NLP, with its integration with Apache Spark, provides a powerful and scalable solution for processing large-scale text data efficiently and accurately.</p>
|
155 |
+
</div>
|
156 |
+
""", unsafe_allow_html=True)
|
157 |
+
|
158 |
+
st.markdown("""
|
159 |
+
<div class="section">
|
160 |
+
<p>Thank you for using the Spark NLP n-gram Generation Demo App. We hope you found it useful and informative!</p>
|
161 |
+
</div>
|
162 |
+
""", unsafe_allow_html=True)
|
163 |
+
|
164 |
+
# References and Additional Information
|
165 |
+
st.markdown('<div class="sub-title">For additional information, please check the following references.</div>', unsafe_allow_html=True)
|
166 |
+
|
167 |
+
st.markdown("""
|
168 |
+
<div class="section">
|
169 |
+
<ul>
|
170 |
+
<li>Documentation : <a href="https://nlp.johnsnowlabs.com/docs/en/annotators#ngramgenerator" target="_blank" rel="noopener">NGramGenerator</a></li>
|
171 |
+
<li>Python Docs : <a href="https://nlp.johnsnowlabs.com/api/python/reference/autosummary/sparknlp/annotator/ngramgenerator/index.html" target="_blank" rel="noopener">NGramGenerator</a></li>
|
172 |
+
<li>Scala Docs : <a href="https://nlp.johnsnowlabs.com/api/com/johnsnowlabs/nlp/annotators/NGramGenerator.html" target="_blank" rel="noopener">NGramGenerator</a></li>
|
173 |
+
<li>For extended examples of usage, see the <a href="https://github.com/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/Certification_Trainings/Public/9.SentenceDetectorDL.ipynb" target="_blank" rel="noopener">Spark NLP Workshop repository</a>.</li>
|
174 |
+
</ul>
|
175 |
+
</div>
|
176 |
+
""", unsafe_allow_html=True)
|
177 |
+
|
178 |
+
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
|
179 |
+
st.markdown("""
|
180 |
+
<div class="section">
|
181 |
+
<ul>
|
182 |
+
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
|
183 |
+
<li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li>
|
184 |
+
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
|
185 |
+
<li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
|
186 |
+
<li><a class="link" href="https://twitter.com/spark_nlp" target="_blank">Twitter</a>: Announcements and updates</li>
|
187 |
+
</ul>
|
188 |
+
</div>
|
189 |
+
""", unsafe_allow_html=True)
|
190 |
+
|
191 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
spark-nlp
|
5 |
+
pyspark
|