-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti_source_sentiments.py
More file actions
49 lines (42 loc) · 3.41 KB
/
Copy pathmulti_source_sentiments.py
File metadata and controls
49 lines (42 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sentiment_analyser import SentimentAnalyser
from data_processor import DataProcessor
import datetime
# getting sentiment (polarity and subjectivity) info and graphs for all sources
class MultiSourceSentiments:
# constructor taking in selectors dict with info for each source (name as key, dict value with info needed for data processor object)
def __init__(self, selectors = {'guardian' : {'selector': 'guardian_*.csv'}, 'mirror' : {'selector': 'mirror_*.csv'},
'telegraph': {'selector':'telegraph_*.csv'}, 'sun' : {'selector': 'sun_*.csv'}, 'metro' : {'selector': 'metro_*.csv'},
'express' : {'selector': 'express_*.csv'}, 'mail' : {'selector': 'mail_*.csv', 'cols': ['headline', 'date', 'url'], 'topics_to_remove': ['wires','femail', 'sport', 'showbiz']}
}, data_path= '../../uk_news_scraping/data', save_path = '.', start_date = datetime.date(2019, 12, 1), end_date = datetime.date(2023, 1, 5)):
self.selectors = selectors
self.data_path = data_path # path to directory containing data for all sources to be analysed
self.save_path = save_path # default to current directory, path to save plots inside
self.start_date = start_date # start and end dates to pass to sentiment analyser for getting sentiment over time
self.end_date = end_date
# running _source_sent() method for each source
def run(self):
for name, selector in self.selectors.items():
# note selector is entire value dict for the source not just the selector part
self._source_sent(name, selector)
# running all analysis, plotting and saving for an individual data source
def _source_sent(self, name, selector):
# gathering information for each source from the selector dict, with defaults for some if not present
selector_string = selector.get('selector')
selector_cols = selector.get('cols', ['headline', 'date']) # if has key cols use that else default to ['headline', 'date']
selector_topics_to_remove = selector.get('topics_to_remove', None)
# creating sentiment analyser instance
sentiment_analyser = SentimentAnalyser(DataProcessor(self.data_path, cols = selector_cols, selector = selector_string, topics_to_remove = selector_topics_to_remove, start_date = self.start_date, end_date = self.end_date), source_name = name, save_path = self.save_path, start_date = self.start_date, end_date = self.end_date)
# getting and plotting polarity over time and polarity ratio
polarity_over_time = sentiment_analyser.get_polarity_over_time()
sentiment_analyser.plot_polarity_over_time(polarity_over_time)
polarity_ratio = sentiment_analyser.get_polarity_ratio()
sentiment_analyser.plot_polarity_ratio(polarity_ratio)
# getting and plotting subjectivity over time and subjectivity box plot
subjectivity_over_time = sentiment_analyser.get_subjectivity_over_time()
sentiment_analyser.plot_subjectivity()
sentiment_analyser.plot_subjectivity_over_time(subjectivity_over_time)
# returning resulting data
# polarity_over_time: dict where key = date and value = mean polarity
# polarity_ratio: dict with format {'positive': positive_percent, 'negative': negative_percent, 'neutral': neutral_percent}
# subjectivity_over_time: dict where key = date and value = mean subjectivity
return polarity_over_time, polarity_ratio, subjectivity_over_time