State of the Art Natural Language Processing
Spark NLP is a Natural Language Processing library built on top of Apache Spark ML. It provides simple, performant & accurate NLP annotations for machine learning pipelines that scale easily in a distributed environment. Spark NLP comes with 1100+ pretrained pipelines and models in more than 192+ languages. It supports state-of-the-art transformers such as BERT, XLNet, ELMO, ALBERT, and Universal Sentence Encoder that can be used seamlessly in a cluster. It also offers Tokenization, Word Segmentation, Part-of-Speech Tagging, Named Entity Recognition, Dependency Parsing, Spell Checking, Multi-class Text Classification, Multi-class Sentiment Analysis, Machine Translation (+180 languages), Summarization and Question Answering (Google T5), and many more NLP tasks.
Take a look at our official Spark NLP page: http://nlp.johnsnowlabs.com/ for user documentation and examples
To use Spark NLP you need the following requirements:
This is a quick example of how to use Spark NLP pre-trained pipeline in Python and PySpark:
$ java -version # should be Java 8 (Oracle or OpenJDK) $ conda create -n sparknlp python=3.7 -y $ conda activate sparknlp # spark-nlp by default is based on pyspark 3.x $ pip install spark-nlp==3.0.2 pyspark==3.1.1
In Python console or Jupyter
Python3kernel:
# Import Spark NLP from sparknlp.base import * from sparknlp.annotator import * from sparknlp.pretrained import PretrainedPipeline import sparknlpStart SparkSession with Spark NLP
start() functions has 4 parameters: gpu, spark23, spark24, and memory
sparknlp.start(gpu=True) will start the session with GPU support
sparknlp.start(spark23=True) is when you have Apache Spark 2.3.x installed
sparknlp.start(spark24=True) is when you have Apache Spark 2.4.x installed
sparknlp.start(memory="16G") to change the default driver memory in SparkSession
spark = sparknlp.start()
Download a pre-trained pipeline
pipeline = PretrainedPipeline('explain_document_dl', lang='en')
Your testing dataset
text = """ The Mona Lisa is a 16th century oil painting created by Leonardo. It's held at the Louvre in Paris. """
Annotate your testing dataset
result = pipeline.annotate(text)
What's in the pipeline
list(result.keys()) Output: ['entities', 'stem', 'checked', 'lemma', 'document', 'pos', 'token', 'ner', 'embeddings', 'sentence']
Check the results
result['entities'] Output: ['Mona Lisa', 'Leonardo', 'Louvre', 'Paris']
For more examples, you can visit our dedicated repository to showcase all Spark NLP use cases!
Spark NLP 3.0.2 has been built on top of Apache Spark 3.x while fully supports Apache Spark 2.3.x and Apache Spark 2.4.x:
| Spark NLP | Apache Spark 2.3.x | Apache Spark 2.4.x | Apache Spark 3.0.x | Apache Spark 3.1.x | |-------------|-----------------------|--------------------|--------------------|--------------------| | 3.0.x |YES |YES |YES |YES | | 2.7.x |YES |YES |NO |NO | | 2.6.x |YES |YES |NO |NO | | 2.5.x |YES |YES |NO |NO | | 2.4.x |Partially |YES |NO |NO | | 1.8.x |Partially |YES |NO |NO | | 1.7.x |YES |NO |NO |NO | | 1.6.x |YES |NO |NO |NO | | 1.5.x |YES |NO |NO |NO |
NOTE: Starting 3.0.2 release, the default
spark-nlpand
spark-nlp-gpupacakges are based on Scala 2.12 and Apache Spark 3.x by default.
NOTE: Starting the 3.0.2 release, we support all major releases of Apache Spark 2.3.x, Apache Spark 2.4.x, Apache Spark 3.0.x, and Apache Spark 3.1.x
Find out more about
Spark NLPversions from our release notes.
Spark NLP 3.0.2 has been tested and is compatible with the following runtimes:
NOTE: The Databricks 8.1 Beta ML with GPU is not supported in Spark NLP 3.0.2 due to its default CUDA 11.x incompatibility
Spark NLP 3.0.2 has been tested and is compatible with the following EMR releases:
Full list of Amazon EMR 5.x releases Full list of Amazon EMR 6.x releases
NOTE: The EMR 6.0.0 is not supported by Spark NLP 3.0.2
Spark NLP supports all major releases of Apache Spark 2.3.x, Apache Spark 2.4.x, Apache Spark 3.0.x, and Apache Spark 3.1.x. That's being said, you need to choose the right package for the right Apache Spark major release:
# CPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
The
spark-nlphas been published to the Maven Repository.
# GPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp-gpu_2.12:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp-gpu_2.12:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp-gpu_2.12:3.0.2
The
spark-nlp-gpuhas been published to the Maven Repository.
# CPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp-spark24_2.11:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp-spark24_2.11:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp-spark24_2.11:3.0.2
The
spark-nlp-spark24has been published to the Maven Repository.
# GPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark24_2.11:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark24_2.11:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark24_2.11:3.0.2
The
spark-nlp-gpu-spark24has been published to the Maven Repository.
# CPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp-spark23_2.11:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp-spark23_2.11:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp-spark23_2.11:3.0.2
The
spark-nlp-spark23has been published to the Maven Repository.
# GPUspark-shell --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark23_2.11:3.0.2
pyspark --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark23_2.11:3.0.2
spark-submit --packages com.johnsnowlabs.nlp:spark-nlp-gpu-spark23_2.11:3.0.2
The
spark-nlp-gpu-spark23has been published to the Maven Repository.
NOTE: In case you are using large pretrained models like UniversalSentenceEncoder, you need to have the following set in your SparkSession:
spark-shell \ --driver-memory 16g \ --conf spark.kryoserializer.buffer.max=2000M \ --packages com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
Our package is deployed to maven central. To add this package as a dependency in your application:
spark-nlp on Apache Spark 3.x:
com.johnsnowlabs.nlp spark-nlp_2.12 3.0.2
spark-nlp-gpu:
com.johnsnowlabs.nlp spark-nlp-gpu_2.12 3.0.2
spark-nlp on Apache Spark 2.4.x:
com.johnsnowlabs.nlp spark-nlp-spark24_2.11 3.0.2
spark-nlp-gpu:
com.johnsnowlabs.nlp spark-nlp-gpu_2.11 3.0.2/version>
spark-nlp on Apache Spark 2.3.x:
com.johnsnowlabs.nlp spark-nlp-spark23_2.11 3.0.2
spark-nlp-gpu:
com.johnsnowlabs.nlp spark-nlp-gpu-spark23_2.11 3.0.2
spark-nlp on Apache Spark 3.x.x:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp" % "3.0.2"
spark-nlp-gpu:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp-gpu libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp-gpu" % "3.0.2"
spark-nlp on Apache Spark 2.4.x:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp-spark24" % "3.0.2"
spark-nlp-gpu:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp-gpu libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp-gpu-spark24" % "3.0.2"
spark-nlp on Apache Spark 2.3.x:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp-spark23 libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp-spark23" % "3.0.2"
spark-nlp-gpu:
// https://mvnrepository.com/artifact/com.johnsnowlabs.nlp/spark-nlp-gpu-spark23 libraryDependencies += "com.johnsnowlabs.nlp" %% "spark-nlp-gpu-spark23" % "3.0.2"
Maven Central: https://mvnrepository.com/artifact/com.johnsnowlabs.nlp
If you are interested, there is a simple SBT project for Spark NLP to guide you on how to use it in your projects Spark NLP SBT Starter
If you installed pyspark through pip/conda, you can install
spark-nlpthrough the same channel.
Pip:
pip install spark-nlp==3.0.2
Conda:
conda install -c johnsnowlabs spark-nlp
PyPI spark-nlp package / Anaconda spark-nlp package
Then you'll have to create a SparkSession either from Spark NLP:
import sparknlpspark = sparknlp.start()
or manually:
spark = SparkSession.builder \ .appName("Spark NLP")\ .master("local[4]")\ .config("spark.driver.memory","16G")\ .config("spark.driver.maxResultSize", "0") \ .config("spark.kryoserializer.buffer.max", "2000M")\ .config("spark.jars.packages", "com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2")\ .getOrCreate()
If using local jars, you can use
spark.jarsinstead for comma-delimited jar files. For cluster setups, of course, you'll have to put the jars in a reachable location for all driver and executor nodes.
Quick example:
import sparknlp from sparknlp.pretrained import PretrainedPipeline#create or get Spark Session
spark = sparknlp.start()
sparknlp.version() spark.version
#download, load and annotate a text by pre-trained pipeline
pipeline = PretrainedPipeline('recognize_entities_dl', 'en') result = pipeline.annotate('The Mona Lisa is a 16th century oil painting created by Leonardo')
sbt assembly
sbt -Dis_gpu=true assembly
sbt -Dis_spark24=true assembly
sbt -Dis_gpu=true -Dis_spark24=true assembly
sbt -Dis_spark23=true assembly
sbt -Dis_gpu=true -Dis_spark23=true assembly
If for some reason you need to use the JAR, you can either download the Fat JARs provided here or download it from Maven Central.
To add JARs to spark programs use the
--jarsoption:
spark-shell --jars spark-nlp.jar
The preferred way to use the library when running spark programs is using the
--packagesoption as specified in the
spark-packagessection.
Use either one of the following options
com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
Apart from the previous step, install the python module through pip
pip install spark-nlp==3.0.2
Or you can install
spark-nlpfrom inside Zeppelin by using Conda:
python.conda install -c johnsnowlabs spark-nlp
Configure Zeppelin properly, use cells with %spark.pyspark or any interpreter name you chose.
Finally, in Zeppelin interpreter settings, make sure you set properly zeppelin.python to the python you want to use and install the pip library with (e.g.
python3).
An alternative option would be to set
SPARK_SUBMIT_OPTIONS(zeppelin-env.sh) and make sure
--packagesis there as shown earlier since it includes both scala and python side installation.
Q: What if I am still on Zeppelin 0.8.x that only supports Apache Spark 2.4.x?
A: You can simply use the
spark-nlp-spark24:3.0.2package or Fat JAR instead.
Recomended:
The easiest way to get this done on Linux and macOS is to simply install
spark-nlpand
pysparkPyPI packages and launch the Jupyter from the same Python environment:
$ conda create -n sparknlp python=3.7 -y $ conda activate sparknlp # spark-nlp by default is based on pyspark 3.x $ pip install spark-nlp==3.0.2 pyspark==3.1.1 jupyter $ jupyter notebook
The you can use
python3kernel to run your code with creating SparkSession via
spark = sparknlp.start().
Optional:
If you are in different operating systems and require to make Jupyter Notebook run by using pyspark, you can follow these steps:
export SPARK_HOME=/path/to/your/spark/folder export PYSPARK_PYTHON=python3 export PYSPARK_DRIVER_PYTHON=jupyter export PYSPARK_DRIVER_PYTHON_OPTS=notebookpyspark --packages com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2
Alternatively, you can mix in using
--jarsoption for pyspark +
pip install spark-nlp
If not using pyspark at all, you'll have to run the instructions pointed here
Google Colab is perhaps the easiest way to get started with spark-nlp. It requires no installation or setup other than having a Google account.
Run the following code in Google Colab notebook and start using spark-nlp right away.
# This is only to setup PySpark and Spark NLP on Colab !wget http://setup.johnsnowlabs.com/colab.sh -O - | bash
This script comes with the two options to define
pysparkand
spark-nlpversions via options:
# -p is for pyspark # -s is for spark-nlp # by default they are set to the latest !bash colab.sh -p 3.1.1 -s 3.0.2
Spark NLP quick start on Google Colab is a live demo on Google Colab that performs named entity recognitions and sentiment analysis by using Spark NLP pretrained pipelines.
Run the following code in Kaggle Kernel and start using spark-nlp right away.
# Let's setup Kaggle for Spark NLP and PySpark !wget http://setup.johnsnowlabs.com/kaggle.sh -O - | bash
Spark NLP quick start on Kaggle Kernel is a live demo on Kaggle Kernel that performs named entity recognitions by using Spark NLP pretrained pipeline.
Create a cluster if you don't have one already
On a new cluster or existing one you need to add the following to the
Advanced Options -> Sparktab:
spark.kryoserializer.buffer.max 2000M spark.serializer org.apache.spark.serializer.KryoSerializer
In
Librariestab inside your cluster you need to follow these steps:
3.1. Install New -> PyPI ->
spark-nlp-> Install
3.2. Install New -> Maven -> Coordinates ->
com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2-> Install
Now you can attach your notebook to the cluster and use Spark NLP!
NOTE: If you are launching a Databricks runtime that is not based on Apache Spark 3.x please choose a compatible Spark NLP package
To lanuch EMR cluster with Apache Spark/PySpark and Spark NLP correctly you need to have bootstrap and software configuration.
A sample of your bootstrap script
#!/bin/bash set -x -eecho -e 'export PYSPARK_PYTHON=/usr/bin/python3 export HADOOP_CONF_DIR=/etc/hadoop/conf export SPARK_JARS_DIR=/usr/lib/spark/jars export SPARK_HOME=/usr/lib/spark' >> $HOME/.bashrc && source $HOME/.bashrc
sudo python3 -m pip install awscli boto spark-nlp
set +x exit 0
A sample of your software configuration in JSON on S3 (must be public access):
[{ "Classification": "spark-env", "Configurations": [{ "Classification": "export", "Properties": { "PYSPARK_PYTHON": "/usr/bin/python3" } }] }, { "Classification": "spark-defaults", "Properties": { "spark.yarn.stagingDir": "hdfs:///tmp", "spark.yarn.preserve.staging.files": "true", "spark.kryoserializer.buffer.max": "2000M", "spark.serializer": "org.apache.spark.serializer.KryoSerializer", "spark.driver.maxResultSize": "0", "spark.jars.packages": "com.johnsnowlabs.nlp:spark-nlp_2.12:3.0.2" } } ]A sample of AWS CLI to launch EMR cluster:
```.sh aws emr create-cluster
--name "Spark NLP 3.0.2"
--release-label emr-6.2.0
--applications Name=Hadoop Name=Spark Name=Hive
--instance-type m4.4xlarge
--instance-count 3
--use-default-roles
--log-uri "s3:///"
--bootstrap-actions Path=s3:///emr-bootstrap.sh,Name=custome
--configurations "https:///sparknlp-config.json"
--ec2-attributes KeyName=,EmrManagedMasterSecurityGroup=,EmrManagedSlaveSecurityGroup=
--profile
If your distributed storage is S3 and you don't have a standard Hadoop configuration (i.e. fs.defaultFS) You need to specify where in the cluster distributed storage you want to store Spark NLP's tmp files. First, decide where you want to put your application.conf file
import com.johnsnowlabs.util.ConfigLoader ConfigLoader.setConfigPath("/somewhere/to/put/application.conf")
And then we need to put in such application.conf the following content
sparknlp { settings { cluster_tmp_dir = "somewhere in s3n:// path to some folder" } }
Spark NLP offers more than
450+ pre-trained pipelinesin
192 languages. |
dependency_parse| 2.4.0 |
en|
Quick example:
import com.johnsnowlabs.nlp.pretrained.PretrainedPipeline import com.johnsnowlabs.nlp.SparkNLPSparkNLP.version()
val testData = spark.createDataFrame(Seq( (1, "Google has announced the release of a beta version of the popular TensorFlow machine learning library"), (2, "Donald John Trump (born June 14, 1946) is the 45th and current president of the United States") )).toDF("id", "text")
val pipeline = PretrainedPipeline("explain_document_dl", lang="en")
val annotation = pipeline.transform(testData)
annotation.show() /* import com.johnsnowlabs.nlp.pretrained.PretrainedPipeline import com.johnsnowlabs.nlp.SparkNLP 2.5.0 testData: org.apache.spark.sql.DataFrame = [id: int, text: string] pipeline: com.johnsnowlabs.nlp.pretrained.PretrainedPipeline = PretrainedPipeline(explain_document_dl,en,public/models) annotation: org.apache.spark.sql.DataFrame = [id: int, text: string ... 10 more fields] +---+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | id| text| document| token| sentence| checked| lemma| stem| pos| embeddings| ner| entities| +---+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | 1|Google has announ...|[[document, 0, 10...|[[token, 0, 5, Go...|[[document, 0, 10...|[[token, 0, 5, Go...|[[token, 0, 5, Go...|[[token, 0, 5, go...|[[pos, 0, 5, NNP,...|[[word_embeddings...|[[named_entity, 0...|[[chunk, 0, 5, Go...| | 2|The Paris metro w...|[[document, 0, 11...|[[token, 0, 2, Th...|[[document, 0, 11...|[[token, 0, 2, Th...|[[token, 0, 2, Th...|[[token, 0, 2, th...|[[pos, 0, 2, DT, ...|[[word_embeddings...|[[named_entity, 0...|[[chunk, 4, 8, Pa...| +---+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ */
annotation.select("entities.result").show(false)
/* +----------------------------------+ |result | +----------------------------------+ |[Google, TensorFlow] | |[Donald John Trump, United States]| +----------------------------------+ */
Spark NLP offers more than
710+ pre-trained modelsin
192 languages.
Some of the selected languages:
Afrikaans, Arabic, Armenian, Basque, Bengali, Breton, Bulgarian, Catalan, Czech, Dutch, English, Esperanto, Finnish, French, Galician, German, Greek, Hausa, Hebrew, Hindi, Hungarian, Indonesian, Irish, Italian, Japanese, Latin, Latvian, Marathi, Norwegian, Persian, Polish, Portuguese, Romanian, Russian, Slovak, Slovenian, Somali, Southern Sotho, Spanish, Swahili, Swedish, Tswana, Turkish, Ukrainian, Zulu
Quick online example:
# load NER model trained by deep learning approach and GloVe word embeddings ner_dl = NerDLModel.pretrained('ner_dl') # load NER model trained by deep learning approach and BERT word embeddings ner_bert = NerDLModel.pretrained('ner_dl_bert')
// load French POS tagger model trained by Universal Dependencies val french_pos = PerceptronModel.pretrained("pos_ud_gsd", lang="fr") // load Italain LemmatizerModel val italian_lemma = LemmatizerModel.pretrained("lemma_dxc", lang="it")
Quick offline example:
PerceptronModelannotator model inside Spark NLP Pipeline
val french_pos = PerceptronModel.load("/tmp/pos_ud_gsd_fr_2.0.2_2.4_1556531457346/") .setInputCols("document", "token") .setOutputCol("pos")
Spark NLP library and all the pre-trained models/pipelines can be used entirely offline with no access to the Internet. If you are behind a proxy or a firewall with no access to the Maven repository (to download packages) or/and no access to S3 (to automatically download models and pipelines), you can simply follow the instructions to have Spark NLP without any limitations offline:
.pretrained()function to download pretrained models, you will need to manually download your pipeline/model from Models Hub, extract it, and load it.
Example of
SparkSessionwith Fat JAR to have Spark NLP offline:
spark = SparkSession.builder \ .appName("Spark NLP")\ .master("local[*]")\ .config("spark.driver.memory","16G")\ .config("spark.driver.maxResultSize", "0") \ .config("spark.kryoserializer.buffer.max", "2000M")\ .config("spark.jars", "/tmp/spark-nlp-assembly-3.0.2.jar")\ .getOrCreate()
hdfs:///tmp/spark-nlp-assembly-3.0.2.jar)
Example of using pretrained Models and Pipelines in offline:
# instead of using pretrained() for online: # french_pos = PerceptronModel.pretrained("pos_ud_gsd", lang="fr") # you download this model, extract it, and use .load french_pos = PerceptronModel.load("/tmp/pos_ud_gsd_fr_2.0.2_2.4_1556531457346/")\ .setInputCols("document", "token")\ .setOutputCol("pos")example for pipelines
instead of using PretrainedPipeline
pipeline = PretrainedPipeline('explain_document_dl', lang='en')
you download this pipeline, extract it, and use PipelineModel
PipelineModel.load("/tmp/explain_document_dl_en_2.0.2_2.4_1556530585689/")
hdfs:///tmp/explain_document_dl_en_2.0.2_2.4_1556530585689/)
Need more examples? Check out our dedicated Spark NLP Showcase repository to showcase all Spark NLP use cases!
Also, don't forget to check Spark NLP in Action built by Streamlit.
Check our Articles and Videos page here
We have published a paper that you can cite for the Spark NLP library:
@article{KOCAMAN2021100058, title = {Spark NLP: Natural language understanding at scale}, journal = {Software Impacts}, pages = {100058}, year = {2021}, issn = {2665-9638}, doi = {https://doi.org/10.1016/j.simpa.2021.100058}, url = {https://www.sciencedirect.com/science/article/pii/S2665963821000063}, author = {Veysel Kocaman and David Talby}, keywords = {Spark, Natural language processing, Deep learning, Tensorflow, Cluster}, abstract = {Spark NLP is a Natural Language Processing (NLP) library built on top of Apache Spark ML. It provides simple, performant & accurate NLP annotations for machine learning pipelines that can scale easily in a distributed environment. Spark NLP comes with 1100+ pretrained pipelines and models in more than 192+ languages. It supports nearly all the NLP tasks and modules that can be used seamlessly in a cluster. Downloaded more than 2.7 million times and experiencing 9x growth since January 2020, Spark NLP is used by 54% of healthcare organizations as the world’s most widely used NLP library in the enterprise.} } }
We appreciate any sort of contributions:
Clone the repo and submit your pull-requests! Or directly create issues in this repo.