twitter-roberta-base-sentiment-latest for Kjarni
Sentiment of short English posts as negative, neutral or positive, packaged to
run locally with Kjarni: one native library, with no Python
runtime, server or cloud service. It was trained on social media text, so it handles
emoji, slang and short messages well. The model is about 500 MB and downloads on first
use.
Usage
Tested with Kjarni 0.8.2. In Kjarni the model is called roberta-sentiment.
Command line
curl -fsSL https://kjarni.ai/install.sh | sh # Windows: irm https://kjarni.ai/install.ps1 | iex
kjarni classify -m roberta-sentiment "Worst customer service I have ever had."
Model: roberta-sentiment Device: Cpu
Input "Worst customer service I have ever had."
✓ negative ███████████████████░ 95.1%
neutral █░░░░░░░░░░░░░░░░░░░ 4.0%
positive ░░░░░░░░░░░░░░░░░░░░ 0.8%
Python
# pip install kjarni
from kjarni import Classifier
classifier = Classifier(model="roberta-sentiment", quiet=True)
for text in [
"Just got the new phone and I love it 😍",
"Worst customer service I have ever had.",
"Meeting moved to 3pm.",
]:
result = classifier.classify(text)
print(f"{result.label:<8} {result.score:.2f} {text}")
positive 0.99 Just got the new phone and I love it 😍
negative 0.95 Worst customer service I have ever had.
neutral 0.95 Meeting moved to 3pm.
Rust
// cargo add kjarni anyhow tokio --features tokio/full
use kjarni::Classifier;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let classifier = Classifier::new("roberta-sentiment").await?;
for text in [
"Just got the new phone and I love it 😍",
"Worst customer service I have ever had.",
"Meeting moved to 3pm.",
] {
let result = classifier.classify(text).await?;
println!("{:<8} {:.2} {text}", result.label, result.score);
}
Ok(())
}
C#
// dotnet add package Kjarni
using Kjarni;
using var classifier = new Classifier("roberta-sentiment", quiet: true);
foreach (var text in new[] { "Just got the new phone and I love it 😍", "Worst customer service I have ever had.", "Meeting moved to 3pm." })
{
var result = classifier.Classify(text);
Console.WriteLine($"{result.Label,-8} {result.Score:F2} {text}");
}
C++ (C++23)
#include <kjarni.hpp>
#include <cstdio>
int main() {
kjarni::ClassifierOptions options;
options.model = "roberta-sentiment";
auto classifier = kjarni::Classifier::create(options);
if (!classifier) return 1;
for (const char* text : {"Just got the new phone and I love it 😍",
"Worst customer service I have ever had.",
"Meeting moved to 3pm."}) {
auto labels = classifier->classify(text);
if (!labels) return 1;
const auto& top = labels->front();
std::printf("%-8s %.2f %s\n", top.name.c_str(), top.score, text);
}
}
Go
// go get github.com/kjarni-ai/kjarni-go
package main
import (
"fmt"
"log"
kjarni "github.com/kjarni-ai/kjarni-go"
)
func main() {
classifier, err := kjarni.NewClassifier("roberta-sentiment", kjarni.WithQuiet(true))
if err != nil {
log.Fatal(err)
}
defer classifier.Close()
for _, text := range []string{
"Just got the new phone and I love it 😍",
"Worst customer service I have ever had.",
"Meeting moved to 3pm.",
} {
result, err := classifier.Classify(text)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%-8s %.2f %s\n", result.Label, result.Score, text)
}
}
See the Kjarni documentation for other languages and installation details.
What changed from the original
This is not a new model. The weights are unchanged: every tensor is bit-for-bit the
same as in the original's pytorch_model.bin. Only the packaging differs:
- The weights are stored as safetensors.
- The pooler layer and the
position_idsbuffer are left out. Classification doesn't use either. - A
tokenizer.jsonis added, made from the original'svocab.jsonandmerges.txt. - The configuration and tokenizer files are re-saved by a newer version of transformers.
The files also load in transformers. On the three examples above, Kjarni's scores match transformers running the original model to four decimal places.
Attribution
The model is cardiffnlp/twitter-roberta-base-sentiment-latest, by Cardiff NLP at Cardiff University. It is a RoBERTa-base model trained on about 124 million tweets from 2018 to 2021 and fine-tuned for sentiment analysis with the TweetEval benchmark. All credit for the model, its training and its evaluation belongs to them. See the original model card, TimeLMs and TweetNLP for more.
Kjarni and this packaging are not affiliated with or endorsed by Cardiff NLP or Cardiff University.
This repository is released under CC BY 4.0, the same license as the original model.
Citation
@inproceedings{camacho-collados-etal-2022-tweetnlp,
title = "{T}weet{NLP}: Cutting-Edge Natural Language Processing for Social Media",
author = "Camacho-collados, Jose and Rezaee, Kiamehr and Riahi, Talayeh and Ushio, Asahi and Loureiro, Daniel and Antypas, Dimosthenis and Boisson, Joanne and Espinosa Anke, Luis and Liu, Fangyu and Mart{\'\i}nez C{\'a}mara, Eugenio and others",
booktitle = "Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing: System Demonstrations",
month = dec,
year = "2022",
address = "Abu Dhabi, UAE",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2022.emnlp-demos.5",
pages = "38--49"
}
@inproceedings{loureiro-etal-2022-timelms,
title = "{T}ime{LM}s: Diachronic Language Models from {T}witter",
author = "Loureiro, Daniel and Barbieri, Francesco and Neves, Leonardo and Espinosa Anke, Luis and Camacho-collados, Jose",
booktitle = "Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics: System Demonstrations",
month = may,
year = "2022",
address = "Dublin, Ireland",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2022.acl-demo.25",
doi = "10.18653/v1/2022.acl-demo.25",
pages = "251--260"
}