Skip to main content

NewsSentiment: easy-to-use, high-quality target-dependent sentiment classification for news articles

NewsSentiment is an easy-to-use Python library that achieves state-of-the-art performance for target-dependent sentiment classification on news articles. NewsSentiment uses the currently best performing targeted sentiment classifier for news articles. In contrast to regular sentiment classification, targeted sentiment classification allows you to provide a target in a sentence. Only for this target, the sentiment is then predicted. This is more reliable in many cases, as demonstrated by the following simplistic example: "I like Bert, but I hate Robert."

We designed NewsSentiment to serve as an easy-to-use wrapper around the sophisticated GRU-TSC model, which was trained on the NewsMTSC dataset consisting of more than 10k labeled sentences sampled from political news articles. More information on the dataset and the model can be found here. The dataset, the model, and its source code can be viewed in our GitHub repository.

Installation

It's super easy, we promise!

You just need a Python 3.8 environment. See here if you don't have Python or a different version (run python --version in a terminal to see your version). Then run:

pip3 install NewsSentiment        # without cuda support (choose this if you don't know what cuda is)
pip3 install NewsSentiment[cuda]  # with cuda support

You're all set now :-)

Target-dependent Sentiment Classification

Note that using NewsSentiment the first time will take a few minutes because it needs to download the fine-tuned language model. Please do not abort this initial download. Since this is a one-time process, future use of NewsSentiment will be much faster.

from NewsSentiment import TargetSentimentClassifier
tsc = TargetSentimentClassifier()

data = [
    ("I like ", "Peter", " but I don't like Robert."),
    ("", "Mark Meadows", "'s coverup of Trump’s coup attempt is falling apart."),
]

sentiments = tsc.infer(targets=data)

for i, result in enumerate(sentiments):
    print("Sentiment: ", i, result[0])

This method will internally split the data into batches of size 16 for increased speed. You can adjust the batch size using the batch_size parameter, e.g., batch_size=32.

Alternatively, you can also use the infer_from_text method to infer sentiment for a single target:

sentiment = tsc.infer_from_text("I like " ,"Peter", " but I don't like Robert.")
print(sentiment[0])

How to identify a person in a sentence?

In case your data is not separated as shown in the examples above, i.e., in three segments, you will need to identify one (or more) targets first. How this is done best depends on your project and analysis task but you may, for example, use NER. This example shows a simple way of doing so.

Acknowledgements

Thanks to Tilman Hornung for adding the batching functionality and various other improvements.

How to cite

If you use the dataset or model, please cite our paper (PDF):

@InProceedings{Hamborg2021b,
  author    = {Hamborg, Felix and Donnay, Karsten},
  title     = {NewsMTSC: (Multi-)Target-dependent Sentiment Classification in News Articles},
  booktitle = {Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics (EACL 2021)},
  year      = {2021},
  month     = {Apr.},
  location  = {Virtual Event},
}

Release files for NewsSentiment 1.2.28

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for NewsSentiment 1.2.28
File Size Uploaded
NewsSentiment-1.2.28.tar.gz 889.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for NewsSentiment 1.2.28
File Interpreter ABI Platform
NewsSentiment-1.2.28-py3-none-any.whl Python 3 none any Details

Total release size: 1.8 MB

Release files / NewsSentiment-1.2.28.tar.gz

Download URL NewsSentiment-1.2.28.tar.gz
Size 889.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f26da590b3bad6f63225668c2eaf63e1dcae8c1923f81e4fa3082935ef5141e9
BLAKE2b-256 checksum
How to use checksums
d164ae206dd6e24c0da623ec1ef6f51653361cb6d8683b980563c4fdfa92d68c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release files / NewsSentiment-1.2.28-py3-none-any.whl

Download URL NewsSentiment-1.2.28-py3-none-any.whl
Size 919.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a0e695a521b69a7535de7e12b06fb17a0e70d19d8afb9e49ff7a5fb783f516d
BLAKE2b-256 checksum
How to use checksums
ed2da27a9b0cec5f06b0d20ea38f48c8c1948aa0d26fdbe2e4e058decea67e98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release history Release notifications | RSS feed

This release

1.2.28 This release

2 release files

1.2.27

2 release files

1.2.26

2 release files

1.2.25

2 release files

1.1.23

2 release files

1.1.21

2 release files

1.1.20

2 release files

1.1.18

2 release files

1.1.13

2 release files

1.1.12

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page