I want to extract tweets from twitter for a specific period and do a sentiment analysis, can someone help me?
Show older comments
% So far I can collect tweets with this code, and in the output file of the variables (tweets) I want to have a column with the date, PLEASE HELP ME!
clear;
% a sample structure array to store the credentials
creds = struct('ConsumerKey','xxx',...
'ConsumerSecret','xxx',...
'AccessToken','xxx',...
'AccessTokenSecret','xxx');
% set up a Twitty object
addpath(genpath('C:\Users\Felix\Desktop\Thesis\twitter\twitter analyser\twitty')); % Twitty
addpath(genpath('C:\Users\Felix\Desktop\Thesis\twitter\twitter analyser\JSON Parser')); % Twitty's default json parser
addpath(genpath('C:\Users\Felix\Desktop\Thesis\twitter\twitter analyser\jsonlab')); % I prefer JSONlab, however.
tw = twitty(creds); % instantiate a Twitty object
tw.jsonParser = @loadjson; % specify JSONlab as json parser
% search for English tweets that mention .....
Volkswagen = tw.search('Volkswagen','count',100,'date',2015-07-01,'include_entities','true','lang','en');
Allianz = tw.search('Allianz','count',100,'date',2015-07-01,'include_entities','true','lang','en');
both = tw.search('Volkswagen Allianz','count',100,'include_entities','true','lang','en');
% load supporting data for text processing
scoreFile = 'AFINN/AFINN-111.txt';
stopwordsURL ='http://www.textfixer.com/resources/common-english-words.txt';
% process the structure array with a utility method |extract|
[VolkswagenUsers,VolkswagenTweets] = processTweets.extract(Volkswagen);
% compute the sentiment scores with |scoreSentiment|
VolkswagenTweets.Sentiment = processTweets.scoreSentiment(VolkswagenTweets, ...
scoreFile,stopwordsURL);
% repeat the process for hachette
[AllianzUsers,AllianzTweets] = processTweets.extract(Allianz);
AllianzTweets.Sentiment = processTweets.scoreSentiment(AllianzTweets, ...
scoreFile,stopwordsURL);
% repeat the process for tweets containing both
[bothUsers,bothTweets] = processTweets.extract(both);
bothTweets.Sentiment = processTweets.scoreSentiment(bothTweets, ...
scoreFile,stopwordsURL);
% calculate and print NSRs
VolkswagenNSR = (sum(VolkswagenTweets.Sentiment>=0) ...
-sum(VolkswagenTweets.Sentiment<0)) ...
/height(VolkswagenTweets);
AllianzNSR = (sum(AllianzTweets.Sentiment>=0) ...
-sum(AllianzTweets.Sentiment<0)) ...
/height(AllianzTweets);
bothNSR = (sum(bothTweets.Sentiment>=0) ...
-sum(bothTweets.Sentiment<0)) ...
/height(bothTweets);
fprintf('Volkswagen NSR : %.2f\n',VolkswagenNSR)
fprintf('Allianz NSR: %.2f\n',AllianzNSR)
fprintf('Both NSR : %.2f\n\n',bothNSR)
% plot the sentiment histogram of two brands
binranges = min([VolkswagenTweets.Sentiment; ...
AllianzTweets.Sentiment; ...
bothTweets.Sentiment]): ...
max([VolkswagenTweets.Sentiment; ...
AllianzTweets.Sentiment; ...
bothTweets.Sentiment]);
bincounts = [histc(VolkswagenTweets.Sentiment,binranges)...
histc(AllianzTweets.Sentiment,binranges)...
histc(bothTweets.Sentiment,binranges)];
figure
bar(binranges,bincounts,'hist')
legend('Volkswagen','Allianz','Both','Location','Best')
title('Sentiment Distribution of 100 Tweets')
xlabel('Sentiment Score')
ylabel('# Tweets')
Answers (0)
Categories
Find more on Risk Management Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!