Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Sentiment Analysis of Headlines | Automation and Content Analysis in the Newsroom
Python for Journalists and Media

bookChallenge: Sentiment Analysis of Headlines

Sentiment analysis is a powerful tool for editorial teams, allowing you to quickly assess the tone of news content and understand how stories might be perceived by audiences. By systematically scoring headlines or articles as positive, negative, or neutral, you can uncover patterns in coverage, identify potential biases, and inform decisions about story selection or framing. This technique is especially valuable when reviewing large volumes of content, where manual assessment would be impractical.

1234567891011121314151617181920212223242526272829303132333435363738
# Predefined lists of positive and negative words positive_words = ["success", "growth", "win", "improve", "benefit", "progress", "record", "boost"] negative_words = ["decline", "loss", "fail", "drop", "crisis", "scandal", "risk", "cut"] # Sample list of news headlines headlines = [ "City sees record growth in tech sector", "Local school faces budget cuts amid crisis", "Community benefits from new park opening", "Economy at risk as markets decline", "Team wins championship after tough season", "Government faces criticism over policy fail", "Residents celebrate progress in recycling efforts" ] def score_sentiment(headline): words = headline.lower().split() score = 0 for word in words: if word in positive_words: score += 1 elif word in negative_words: score -= 1 return score def classify_sentiment(score): if score > 0: return "positive" elif score < 0: return "negative" else: return "neutral" # Analyze each headline for headline in headlines: score = score_sentiment(headline) sentiment = classify_sentiment(score) print(f"'{headline}' => {sentiment} (score: {score})")
copy

When newsroom teams use sentiment analysis, you gain a data-driven perspective on how your headlines and stories might affect public perception. Regularly reviewing sentiment scores can help editors spot trends—such as a tendency toward negative framing or a lack of positive stories—and lead to productive discussions about editorial balance. This process can also provide insights into how coverage aligns with audience expectations and organizational values, ultimately supporting more informed, intentional journalism.

Uppgift

Swipe to start coding

Write a script to analyze the sentiment of a list of news headlines using predefined positive and negative word lists.

  • For each headline in the headlines list, score the sentiment using the score_sentiment function.
  • Classify each headline as positive, negative, or neutral using the classify_sentiment function.
  • Count the number of headlines in each sentiment category.
  • Calculate the percentage of headlines in each sentiment category.
  • Output a summary report showing the count and percentage for each sentiment category.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how the sentiment scoring works in this example?

What are some limitations of using simple word lists for sentiment analysis?

How can I adapt this approach for longer articles instead of just headlines?

close

bookChallenge: Sentiment Analysis of Headlines

Svep för att visa menyn

Sentiment analysis is a powerful tool for editorial teams, allowing you to quickly assess the tone of news content and understand how stories might be perceived by audiences. By systematically scoring headlines or articles as positive, negative, or neutral, you can uncover patterns in coverage, identify potential biases, and inform decisions about story selection or framing. This technique is especially valuable when reviewing large volumes of content, where manual assessment would be impractical.

1234567891011121314151617181920212223242526272829303132333435363738
# Predefined lists of positive and negative words positive_words = ["success", "growth", "win", "improve", "benefit", "progress", "record", "boost"] negative_words = ["decline", "loss", "fail", "drop", "crisis", "scandal", "risk", "cut"] # Sample list of news headlines headlines = [ "City sees record growth in tech sector", "Local school faces budget cuts amid crisis", "Community benefits from new park opening", "Economy at risk as markets decline", "Team wins championship after tough season", "Government faces criticism over policy fail", "Residents celebrate progress in recycling efforts" ] def score_sentiment(headline): words = headline.lower().split() score = 0 for word in words: if word in positive_words: score += 1 elif word in negative_words: score -= 1 return score def classify_sentiment(score): if score > 0: return "positive" elif score < 0: return "negative" else: return "neutral" # Analyze each headline for headline in headlines: score = score_sentiment(headline) sentiment = classify_sentiment(score) print(f"'{headline}' => {sentiment} (score: {score})")
copy

When newsroom teams use sentiment analysis, you gain a data-driven perspective on how your headlines and stories might affect public perception. Regularly reviewing sentiment scores can help editors spot trends—such as a tendency toward negative framing or a lack of positive stories—and lead to productive discussions about editorial balance. This process can also provide insights into how coverage aligns with audience expectations and organizational values, ultimately supporting more informed, intentional journalism.

Uppgift

Swipe to start coding

Write a script to analyze the sentiment of a list of news headlines using predefined positive and negative word lists.

  • For each headline in the headlines list, score the sentiment using the score_sentiment function.
  • Classify each headline as positive, negative, or neutral using the classify_sentiment function.
  • Count the number of headlines in each sentiment category.
  • Calculate the percentage of headlines in each sentiment category.
  • Output a summary report showing the count and percentage for each sentiment category.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

some-alt