Sarcasm Classifier

Description

Classify if a text contains sarcasm.

Predicted Entities

normal, sarcasm

Live Demo Open in Colab Download Copy S3 URI

How to use

documentAssembler = DocumentAssembler()\
.setInputCol("text")\
.setOutputCol("document")
use = UniversalSentenceEncoder.pretrained(lang="en") \
.setInputCols(["document"])\
.setOutputCol("sentence_embeddings")
document_classifier = ClassifierDLModel.pretrained('classifierdl_use_sarcasm', 'en') \
.setInputCols(["document", "sentence_embeddings"]) \
.setOutputCol("class")
nlpPipeline = Pipeline(stages=[documentAssembler, use, document_classifier])
light_pipeline = LightPipeline(nlp_pipeline.fit(spark.createDataFrame([['']]).toDF("text")))
annotations = light_pipeline.fullAnnotate('If I could put into words how much I love waking up at am on Tuesdays I would')
val documentAssembler = DocumentAssembler()
.setInputCol("text")
.setOutputCol("document")
val use = UniversalSentenceEncoder.pretrained(lang="en")
.setInputCols(Array("document"))
.setOutputCol("sentence_embeddings")
val document_classifier = ClassifierDLModel.pretrained("classifierdl_use_sarcasm", "en")
.setInputCols(Array("document", "sentence_embeddings"))
.setOutputCol("class")
val pipeline = new Pipeline().setStages(Array(documentAssembler, use, document_classifier))

val data = Seq("If I could put into words how much I love waking up at am on Tuesdays I would").toDF("text")
val result = pipeline.fit(data).transform(data)
import nlu

text = ["""If I could put into words how much I love waking up at am on Tuesdays I would"""]
sarcasm_df = nlu.load('classify.sarcasm.use').predict(text, output_level='document')
sarcasm_df[["document", "sarcasm"]]

Results

+--------------------------------------------------------------------------------------------------------+------------+
|document                                                                                                |class       |
+--------------------------------------------------------------------------------------------------------+------------+
|If I could put into words how much I love waking up at am on Tuesdays I would                           | sarcasm    |
+--------------------------------------------------------------------------------------------------------+------------+

Model Information

Model Name: classifierdl_use_sarcasm
Compatibility: Spark NLP 2.7.1+
License: Open Source
Edition: Official
Input Labels: [sentence_embeddings]
Output Labels: [class]
Language: en
Dependencies: tfhub_use

Data Source

http://www.cs.utah.edu/~riloff/pdfs/official-emnlp13-sarcasm.pdf

Benchmarking

precision    recall  f1-score   support

normal       0.98      0.89      0.93       495
sarcasm       0.60      0.91      0.73        93

accuracy                           0.89       588
macro avg       0.79      0.90      0.83       588
weighted avg       0.92      0.89      0.90       588