Skip to content

Instantly share code, notes, and snippets.

@michvaldes001
Last active March 20, 2016 00:34
Show Gist options
  • Save michvaldes001/8636ce0c0cdbe7c7b5ab to your computer and use it in GitHub Desktop.
Save michvaldes001/8636ce0c0cdbe7c7b5ab to your computer and use it in GitHub Desktop.
This script is a crude attempt at analyzing the media's tone for each candidate. See comments in code.
<?php
//This script is my crude attempt of trying to quantify the media's tone pertaining to articles about candidates.
//The script scans the 5 most heavily trafficked news sites that have a search function.
//It searches and counts key negative and positive adjectives.
//I excluded Fox News as that would skew my results.
//Working demo here: http://michvaldes001.ddns.net/index.php?section_id=Apps&app_loaded=yes&app_id=election_2016
//Display header.
echo '<h2>Words Used to Describe Candidates (data from Google, Yahoo, CNN, NBC, and NYT news).</h2>';
echo '<p>(These are the top 5 news site that have a search function AND are not Fox News).</p>';
//Declare global lists for candidate data.
global $trump_consolidated_data, $sanders_consolidated_data, $clinton_consolidated_data, $candidate_total_word_count;
//Scan news sites using their search engines.
function scan_news_data($first_name, $last_name) {
$candidate_yahoo_data = file_get_contents('https://news.search.yahoo.com/search?fr=uh3_news_vert_gs&type=2button&p=' . $first_name . '%20' . $last_name . '%20');
$candidate_google_data = file_get_contents('https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q' . $first_name . '+' . $last_name);
$candidate_cnn_data = file_get_contents('http://www.cnn.com/search/?text=');
$candidate_nbc_data = file_get_contents('http://www.nbcnews.com/search/' . $first_name . '%20' . $last_name);
$candidate_nytimes_data = file_get_contents('http://query.nytimes.com/search/sitesearch/?action=click&contentCollection&region=TopBar&WT.nav=searchWidget&module=SearchSubmit&pgtype=Homepage#/' . $first_name . ' ' . $last_name);
//Consolidate all news data.
$consolidated_data = $candidate_yahoo_data . $candidate_google_data . $candidate_cnn_data . $candidate_nbc_data . $candidate_nytimes_data;
return $consolidated_data;
}
//Search for negative adjectives in all news article headlines and summaries.
function negativity($negative_candidate_data) {
global $total_negative_counter;
$total_negative_counter = 0;
$negative_adjectives = array('aggressive','aloof','arrogant','belligerent','big-headed','bitchy','boastful','bone-idle','boring','bossy','callous','cantankerous','careless','changeable','clinging','compulsive','conservative','cowardly','cruel','cunning','cynical',
'deceitful','detached','dishonest','dogmatic','domineering','finicky','flirtatious','foolish','foolhardy','fussy','greedy','grumpy','gullible','harsh',
'impatient','impolite','impulsive','inconsiderate','inconsistent','indecisive','indiscreet','inflexible','interfering','intolerant','irresponsible',
'jealous lazy','Machiavellian','materialistic','mean','miserly','moody','narrowminded','nasty','naughty','nervous','obsessive obstinate', 'overcritical','overemotional','parsimonious','patronizing','perverse','pessimistic','pompous','possessive','pusillanimous','quarrelsome','quick-tempered','resentful','rude','ruthless','sarcastic','secretive','selfish','self-centred','self-indulgent','silly','sneaky','stingy', 'stubborn','stupid','superficial','tactless','timid','touchy','thoughtless','truculent','unkind','unpredictable','unreliable', 'untidy',
'untrustworthy','vague','vain','vengeful','vulgar','weak-willed');
echo '<ul>';
//Iterate over each word in list.
foreach ($negative_adjectives as &$word)
{
$word_results = substr_count(($negative_candidate_data), $word);
if ($word_results != 0) {
echo "<li>" . $word . " : ". $word_results . "</li>";
$total_negative_counter = $total_negative_counter + $word_results;
}
}
echo '</ul>';
}
//Search for negative adjectives in all news article headlines and summaries.
function positivity($positive_candidate_data)
{
global $total_positive_counter;
$total_positive_counter = 0;
$positive_adjectives = array('adaptable','adventurous','affable','affectionate','agreeable','ambitious','amiable','amicable',
'amusing','brave','bright','broad-minded','calm','careful','charming','communicative','compassionate','conscientious','considerate',
'convivial','courageous','courteous','creative','decisive','determined','diligent','diplomatic','discreet','dynamic','easygoing','emotional',
'energetic','enthusiastic','exuberant','fair-minded','faithful','fearless','forceful','frank','friendly','funny','generous','gentle','good',
'gregarious','hard-working','helpful','honest','humorous','imaginative','impartial','independent','intellectual','intelligent','intuitive',
'inventive','kind','loving','loyal','modest','neat','nice','optimistic','passionate','patient','persistent','pioneering','philosophical',
'placid','plucky','polite','powerful','practical','pro-active','quick-witted','quiet','rational','reliable','reserved','resourceful','romantic','self-confident','self-disciplined','sensible','sensitive','sincere','sociable','straightforward','sympathetic','thoughtful',
'tidy','tough','unassuming','understanding','versatile','warmhearted','willing','witty');
echo '<ul>';
//Iterate over each word in list.
foreach ($positive_adjectives as &$word)
{
$word_results = substr_count(($positive_candidate_data) , $word);
if ($word_results != 0)
{
echo '<li>' . $word . ' : '. $word_results . '</li>';
$total_positive_counter = $total_positive_counter + $word_results;
}
}
echo '</ul>';
}
//Generate pie charts.
function create_pie_chart($negative_total, $positive_total, $first_name, $last_name) {
//Calculate and display percentages.
$negative_percent = (($negative_total/$positive_total) * 100);
$positive_percent = 100 - $negative_percent;
echo '<h3>' . $first_name . ' ' . $last_name . '</h3>';
echo '<p>Positive:' . $positive_percent . '%</p>';
echo '<p>Negative:' . $negative_percent . '%</p>';
//Convert percentage to degrees.
$chart_arc_degree = ($positive_percent * .01) * 360;
//Draw charts.
$chart_image = imagecreatetruecolor(300, 300);
$white_color = imagecolorallocate($chart_image, 0xFF, 0xFF, 0xFF);
$blue_color = imagecolorallocate($chart_image, 0x00, 0x00, 0x80);
$red_color = imagecolorallocate($chart_image, 0xFF, 0x00, 0x00);
imagefill($chart_image, 0, 0, $white_color);
imagefilledarc($chart_image, 150, 150, 300, 300, $chart_arc_degree, 360 , $red_color, IMG_ARC_PIE);
imagefilledarc($chart_image, 150, 150, 300, 300, 0, $chart_arc_degree, $blue_color, IMG_ARC_PIE);
imagepng($chart_image, 'apps/election_2016_files/' . $first_name . '_' . $last_name . '_tonechart.png');
echo '<img src = "apps/election_2016_files/' . $first_name . '_' . $last_name . '_tonechart.png"><br><br>';
imagedestroy($chart_image);
}
//Set gloabal lists for each candidate with
$trump_consolidated_data = scan_news_data('Donald','Trump');
$sanders_consolidated_data = scan_news_data('Bernie','Sanders');
$clinton_consolidated_data = scan_news_data('Hillary','Clinton');
echo '<div style = "float: left; width: 50%;">';
echo '<h2>Negative Adjectives in News Counter</h2>';
//Generate detected word list for each candidate.
echo '<h3>Donald Trump</h3>';
negativity($trump_consolidated_data);
$trump_total_negative = $total_negative_counter;
echo '<h3>Hillary Clinton</h3>';
negativity($clinton_consolidated_data);
$clinton_total_negative = $total_negative_counter;
echo '<h3>Bernie Sanders</h3>';
negativity($sanders_consolidated_data);
$sanders_total_negative = $total_negative_counter;
echo '</div>';
echo '<div style = "float: left; width: 50%;">';
echo '<h2>Positive Adjectives in News Counter</h2>';
echo '<h3>Donald Trump</h3>';
positivity($trump_consolidated_data);
$trump_total_positive = $total_positive_counter;
echo '<h3>Hillary Clinton</h3>';
positivity($clinton_consolidated_data);
$clinton_total_positive = $total_positive_counter;
echo '<h3>Bernie Sanders</h3>';
positivity($sanders_consolidated_data);
$sanders_total_positive = $total_positive_counter;
echo '</div>';
//Call pie chart function to display percentages and pie charts for overall media tone. Display candidate pic from Wikipedia.
echo '<br><br>';
echo '<h2>Overall Tone of Media</h2>';
echo '<p>Red = Negative.</p><p>Blue = Positive.</p>';
echo '<div style = "float: left; width: 350px;">';
echo '<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d2/Donald_Trump_August_19%2C_2015_%28cropped%29.jpg/220px-Donald_Trump_August_19%2C_2015_%28cropped%29.jpg" width = "220" height = "275">';
create_pie_chart($trump_total_negative, $trump_total_positive, 'Donald', 'Trump');
echo '</div>';
echo '<div style = "float: left; width: 350px;">';
echo '<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Hillary_Clinton_official_Secretary_of_State_portrait_crop.jpg/220px-Hillary_Clinton_official_Secretary_of_State_portrait_crop.jpg">';
create_pie_chart($clinton_total_negative, $clinton_total_positive, 'Hillary', 'Clinton');
echo '</div>';
echo '<div style = "float: left; width: 350px;">';
echo '<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Bernie_Sanders.jpg/220px-Bernie_Sanders.jpg">';
create_pie_chart($sanders_total_negative, $sanders_total_positive, 'Bernie', 'Sanders');
echo '</div>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment