SparkNLP - Notebook

Last updated:


Sentiment Analysis using John Snow Labs Spark-NLP

The following code will produce an entire machine learning pipeline aiming to retrieve sentiment analysis from a target distributed dataframe (or dataset). This will utilize several annotators required for such analysis and even an optional one (the spell checker, which is of type Token anyways) which is injected in between the sentiment analysis and the tokens it requires. ViveknSentimentAnalysis is the annotator utilized and it is training with two corpus, one containing positive sentiment examples, and another one containing negative examples. Some additional parameters have been used to fine tune the pipeline, and the Finisher transformer makes sure our final result is exactly the sentiment result of every row.

import sys
from import Pipeline
from pyspark.sql.functions import udf, col
from pyspark.sql.types import *
from sparknlp.annotator import *
from sparknlp.annotation import *
from sparknlp.base import DocumentAssembler
                                                                                                         In [ ]:
#Load the input data to be annotated
data = spark. \
        read. \
        parquet("../sentiment.parquet"). \

### Define the dataframe                                                               In [ ]:
document_assembler = DocumentAssembler() \
### Transform input to appropriate schema
#assembled = document_assembler.transform(data)

### Sentence detector
sentence_detector = SentenceDetectorModel() \
    .setInputCols(["document"]) \
#sentence_data = sentence_detector.transform(checked)
                                                                                                         In [ ]:
### Tokenizer
tokenizer = RegexTokenizer() \
            .setInputCols(["sentence"]) \
#tokenized = tokenizer.transform(assembled)
                                                                                                         In [ ]:
### Spell Checker
spell_checker = NorvigSweetingApproach() \
            .setInputCols(["token"]) \
#checked =
                                                                                                         In [ ]:
sentiment_detector = ViveknSentimentApproach() \
    .setInputCols(["spell", "sentence"]) \
    .setOutputCol("sentiment") \
    .setPositiveSource("../positive") \
    .setNegativeSource("../negative") \

finisher = Finisher() \
    .setInputCols(["sentiment"]) \
    .setIncludeKeys(True) \
                                                                                                         In [ ]:
pipeline = Pipeline(stages=[

sentiment_data =

Spark Summit Europe 2017