German Sentiment Classification with BERT

Mar 20, 2023 | Educational

Welcome to our guide on harnessing the power of BERT for sentiment classification in the German language. This model has been trained with meticulous care to understand the emotions behind German text, making it an invaluable tool for applications like chatbots, customer feedback analysis, and more.

Getting Started with the Python Package

To ease the implementation process, we’ve developed a convenient Python package that encapsulates all the essential code for preprocessing and inference. Here’s how to get started:

Step 1: Install the Package

First, you’ll need to install the package from PyPI. You can do this easily using pip:

pip install germansentiment

Step 2: Using the Sentiment Model

Once the package is installed, you can begin using the sentiment model with just a few lines of code:

from germansentiment import SentimentModel
model = SentimentModel()
texts = [
    "Mit keinem guten Ergebnis",
    "Das ist gar nicht mal so gut",
    "Total awesome!",
    "nicht so schlecht wie erwartet",
    "Der Test verlief positiv.",
    "Sie fährt ein grünes Auto."
]
result = model.predict_sentiment(texts)
print(result)

The code above will analyze the sentiment of the provided German texts and output a list of sentiment classifications, such as:

[negative, negative, positive, positive, neutral, neutral]

Understanding Output Class Probabilities

To get a deeper insight into the model’s predictions, you can also retrieve the output class probabilities:

from germansentiment import SentimentModel
model = SentimentModel()
classes, probabilities = model.predict_sentiment(["das ist super"], output_probabilities=True)
print(classes, probabilities)

This will yield output classes along with their associated probabilities:

[positive] [[[positive, 0.9761], [negative, 0.0235], [neutral, 0.0003]]]

An Analogy to Understand BERT’s Capability

Imagine teaching a robot to recognize emotions based on various types of letters written in German. In our analogy, the training data (like Twitter posts, reviews, etc.) are the letters scattered throughout different spaces. Each letter conveys a sentiment – happiness, sadness, excitement, or indifference. Just as the robot learns to discern the underlying emotions in every letter based on its experience with numerous examples, our BERT model has been trained on a vast dataset of 1.834 million German texts. Consequently, it can confidently identify the sentiment behind new texts with remarkable insight.

Model and Data Overview

If you’re curious about the intricacies of the model’s training process, be sure to check out the repository and our research paper. The table below illustrates the F1 scores achieved by the model on various datasets:

Dataset                                                       F1 micro Score
[holidaycheck](https://github.com/oliverguhr/german-sentiment)           0.9568
[scare](https://www.romanklinger.de/scare)                               0.9418
[filmstarts](https://github.com/oliverguhr/german-sentiment)            0.9021
[germeval](https://sites.google.com/view/germeval2017-absahome)        0.7536
[PotTS](https://www.aclweb.org/anthology/L16-1181)                       0.6780
[emotions](https://github.com/oliverguhr/german-sentiment)             0.9649
[sb10k](https://www.spinningbytes.com/resources/german-sentiment)      0.7376
[Leipzig Wikipedia Corpus 2016](https://wortschatz.uni-leipzig.de/de/download/german) 0.9967
all                                                                       0.9639

Troubleshooting

If you encounter any issues during installation or usage, consider the following troubleshooting tips:

  • Ensure that your Python environment is properly set up and that pip is installed.
  • Verify that you’re using compatible versions of required libraries.
  • For discrepancies in outputs, ensure that the text inputs conform to the German language structure.
  • If you face further challenges, for more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox