Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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.

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

bookChallenge: Sentiment Analysis of Headlines

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7
single

single

some-alt