PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/oscars2015.php

http://github.com/ginatrapani/ThinkUp
PHP | 254 lines | 189 code | 19 blank | 46 comment | 54 complexity | 25650619e5f1f089ce760f61cb69c681 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Oscars 2015
  4. Description: Did you mention the Oscars?
  5. When: Monday, February 23, 2015
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/oscars2014.php
  10. *
  11. * Copyright (c) 2014-2016 Gina Trapani, Anil Dash
  12. *
  13. * LICENSE:
  14. *
  15. * This file is part of ThinkUp (https://thinkup.com).
  16. *
  17. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  18. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  19. * later version.
  20. *
  21. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  22. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  26. * <http://www.gnu.org/licenses/>.
  27. *
  28. * @license http://www.gnu.org/licenses/gpl.html
  29. * @copyright 2014-2016 Gina Trapani, Anil Dash
  30. * @author Anil Dash <anil[at]thinkup[dot]com>
  31. */
  32. class Oscars2015Insight extends InsightPluginParent implements InsightPlugin {
  33. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  34. if (Utils::isTest() || date("Y-m-d") == '2015-02-23') {
  35. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  36. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  37. $hero_image = array(
  38. 'url' => 'https://www.thinkup.com/assets/images/insights/2015-02/oscars2015.jpg',
  39. 'alt_text' => 'Oprah got a Lego Oscar!',
  40. 'credit' => 'Photo: Disney | ABC Television Group',
  41. 'img_link' => 'https://www.flickr.com/photos/disneyabc/16620198142'
  42. );
  43. $post_dao = DAOFactory::getDAO('PostDAO');
  44. $last_month_of_posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username,
  45. $network=$instance->network, $count=0, $order_by="pub_date", $in_last_x_days = 30,
  46. $iterator = false, $is_public = false);
  47. if (self::shouldGenerateWeeklyInsight('oscars_2015', $instance, $insight_date='today',
  48. $regenerate_existing_insight=false, $day_of_week=1, count($last_month_of_posts))) {
  49. foreach ($last_month_of_posts as $post) {
  50. $this->logger->logInfo("Post text is: " . $post->post_text, __METHOD__.','.__LINE__);
  51. // see if $post date is before the awards aired
  52. if ($post->pub_date < "2015-02-22 18:00:00") {
  53. $mentioned_oscar_winner = self::detectOscarWinnerReferences($post->post_text);
  54. $mentioned_oscar_loser = self::detectOscarLoserReferences($post->post_text);
  55. $oscar_mention_count = self::countOscarMentions($post->post_text);
  56. if ($mentioned_oscar_winner) {
  57. $this->logger->logInfo("Winner mention: $mentioned_oscar_winner", __METHOD__.','.__LINE__);
  58. $insight_body = "$this->username was talking about $mentioned_oscar_winner before the "
  59. . "Academy Award winners were even announced!";
  60. } else {
  61. $this->logger->logInfo("No winners mentioned, skipping insight. ", __METHOD__.','.__LINE__);
  62. }
  63. if ($mentioned_oscar_loser) {
  64. $this->logger->logInfo("Loser mention: $mentioned_oscar_loser", __METHOD__.','.__LINE__);
  65. $insight_body_suffix = " Looks like the Academy voters might have missed "
  66. . "$this->username's " . $this->terms->getNoun('post', InsightTerms::PLURAL)
  67. . " about " . $mentioned_oscar_loser . ", though.";
  68. }
  69. }
  70. }
  71. if ($insight_body_suffix) {
  72. $insight_text = $insight_body . $insight_body_suffix;
  73. } else {
  74. $insight_text = $insight_body;
  75. }
  76. if ($insight_body) {
  77. $headline = "Somebody was ready for the Oscars party!";
  78. $my_insight = new Insight();
  79. $my_insight->slug = 'oscars_2015'; //slug to label this insight's content
  80. $my_insight->instance_id = $instance->id;
  81. $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date
  82. $my_insight->headline = $headline; // or just set a string like 'Ohai';
  83. $my_insight->text = $insight_text; // or just set a strong like "Greetings humans";
  84. $my_insight->filename = basename(__FILE__, ".php"); //Same for every insight, must be set this way
  85. $my_insight->emphasis = Insight::EMPHASIS_HIGH; //Optional emphasis, default Insight::EMPHASIS_LOW
  86. $my_insight->setHeroImage($hero_image);
  87. $this->insight_dao->insertInsight($my_insight);
  88. }
  89. }
  90. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  91. }
  92. }
  93. /**
  94. * detect the mention of Oscar-related terms in text.
  95. * @param str $text
  96. * @return str Topic names in $text
  97. */
  98. public static function detectOscarWinnerReferences($text) {
  99. $topic = '';
  100. $matches = array();
  101. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  102. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  103. if(preg_match_all("/\balejandro gonzález iñárritu/i", $depunctuated_text, $matches)) { $topic = 'Alejandro González Iñárritu'; }
  104. if(preg_match_all("/\bamerican sniper/i", $depunctuated_text, $matches)) { $topic = 'American Sniper'; }
  105. if(preg_match_all("/\bbig hero 6/i", $depunctuated_text, $matches)) { $topic = 'Big Hero 6'; }
  106. if(preg_match_all("/\bbirdman/i", $depunctuated_text, $matches)) { $topic = 'Birdman'; }
  107. if(preg_match_all("/\bcitizenfour/i", $depunctuated_text, $matches)) { $topic = 'Citizenfour'; }
  108. // if(preg_match_all("/\bcommon/i", $depunctuated_text, $matches)) { $topic = 'Common'; }
  109. if(preg_match_all("/\bcrisis hotline/i", $depunctuated_text, $matches)) { $topic = 'Crisis Hotline'; }
  110. if(preg_match_all("/\beddie redmayne/i", $depunctuated_text, $matches)) { $topic = 'Eddie Redmayne'; }
  111. // if(preg_match_all("/\bfeast/i", $depunctuated_text, $matches)) { $topic = 'Feast'; }
  112. // if(preg_match_all("/\bglory/i", $depunctuated_text, $matches)) { $topic = 'Glory'; }
  113. if(preg_match_all("/\bgrand budapest hotel/i", $depunctuated_text, $matches)) { $topic = 'Grand Budapest Hotel'; }
  114. if(preg_match_all("/\bimitation game/i", $depunctuated_text, $matches)) { $topic = 'The Imitation Game'; }
  115. if(preg_match_all("/\iñárritu/i", $depunctuated_text, $matches)) { $topic = 'Alejandro González Iñárritu'; }
  116. if(preg_match_all("/\binarritu/i", $depunctuated_text, $matches)) { $topic = 'Alejandro González Iñárritu'; }
  117. if(preg_match_all("/\binterstellar/i", $depunctuated_text, $matches)) { $topic = 'Interstellar'; }
  118. if(preg_match_all("/\bj. k. simmons/i", $depunctuated_text, $matches)) { $topic = 'J.K. Simmons'; }
  119. if(preg_match_all("/\bjk simmmons/i", $depunctuated_text, $matches)) { $topic = 'J.K. Simmons'; }
  120. if(preg_match_all("/\bjohn legend/i", $depunctuated_text, $matches)) { $topic = 'John Legend'; }
  121. if(preg_match_all("/\bjulianne moore/i", $depunctuated_text, $matches)) { $topic = 'Julianne Moore'; }
  122. if(preg_match_all("/\bpatricia arquette/i", $depunctuated_text, $matches)) { $topic = 'Patricia Arquette'; }
  123. // if(preg_match_all("/\bthe phone call/i", $depunctuated_text, $matches)) { $topic = 'The Phone Call'; }
  124. if(preg_match_all("/\bveterans press 1/i", $depunctuated_text, $matches)) { $topic = 'Crisis Hotline'; }
  125. // if(preg_match_all("/\bwhiplash/i", $depunctuated_text, $matches)) { $topic = 'Whiplash'; }
  126. return $topic;
  127. }
  128. /**
  129. * detect the mention of Oscar-related terms in text.
  130. * @param str $text
  131. * @return str Topic names in $text
  132. */
  133. public static function detectOscarLoserReferences($text) {
  134. $topic = '';
  135. $matches = array();
  136. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  137. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  138. if(preg_match_all("/\ba single life/i", $depunctuated_text, $matches)) { $topic = 'A Single Life'; }
  139. if(preg_match_all("/\baya/i", $depunctuated_text, $matches)) { $topic = 'Aya'; }
  140. if(preg_match_all("/\bbegin again/i", $depunctuated_text, $matches)) { $topic = 'Begin Again'; }
  141. if(preg_match_all("/\bbenedict cumberbatch/i", $depunctuated_text, $matches)) { $topic = 'Benedict Cumberbatch'; }
  142. if(preg_match_all("/\bbennett miller/i", $depunctuated_text, $matches)) { $topic = 'Bennett Miller'; }
  143. if(preg_match_all("/\bbeyond the lights/i", $depunctuated_text, $matches)) { $topic = 'Beyond The Lights'; }
  144. if(preg_match_all("/\bboogaloo and graham/i", $depunctuated_text, $matches)) { $topic = 'Boogaloo And Graham'; }
  145. if(preg_match_all("/\bboyhood/i", $depunctuated_text, $matches)) { $topic = 'Boyhood'; }
  146. if(preg_match_all("/\bbradley cooper/i", $depunctuated_text, $matches)) { $topic = 'Bradley Cooper'; }
  147. if(preg_match_all("/\bbutter lamp/i", $depunctuated_text, $matches)) { $topic = 'Butter Lamp'; }
  148. if(preg_match_all("/\bcaptain america/i", $depunctuated_text, $matches)) { $topic = 'Captain America: Winter Soldier'; }
  149. if(preg_match_all("/\bdam keeper/i", $depunctuated_text, $matches)) { $topic = 'Dam Keeper'; }
  150. if(preg_match_all("/\bdawn of the planet of the apes/i", $depunctuated_text, $matches)) { $topic = 'Dawn Of The Planet Of The Apes'; }
  151. if(preg_match_all("/\bdays of future past/i", $depunctuated_text, $matches)) { $topic = 'Days Of Future Past'; }
  152. if(preg_match_all("/\bdiane warren/i", $depunctuated_text, $matches)) { $topic = 'Diane Warren'; }
  153. if(preg_match_all("/\bedward norton/i", $depunctuated_text, $matches)) { $topic = 'Edward Norton'; }
  154. if(preg_match_all("/\bemma stone/i", $depunctuated_text, $matches)) { $topic = 'Emma Stone'; }
  155. if(preg_match_all("/\bethan hawke/i", $depunctuated_text, $matches)) { $topic = 'Ethan Hawke'; }
  156. if(preg_match_all("/\beverything is awesome/i", $depunctuated_text, $matches)) { $topic = 'Everything Is Awesome'; }
  157. if(preg_match_all("/\bfelicity jones/i", $depunctuated_text, $matches)) { $topic = 'Felicity Jones'; }
  158. if(preg_match_all("/\bfinding vivian maier/i", $depunctuated_text, $matches)) { $topic = 'Finding Vivian Maier'; }
  159. if(preg_match_all("/\bfive armies/i", $depunctuated_text, $matches)) { $topic = 'Five Armies'; }
  160. if(preg_match_all("/\bfoxcatcher/i", $depunctuated_text, $matches)) { $topic = 'Foxcatcher'; }
  161. if(preg_match_all("/\bglen campbell/i", $depunctuated_text, $matches)) { $topic = 'Glen Campbell'; }
  162. if(preg_match_all("/\bguardians of the galaxy/i", $depunctuated_text, $matches)) { $topic = 'Guardians Of The Galaxy'; }
  163. if(preg_match_all("/\bhobbit/i", $depunctuated_text, $matches)) { $topic = 'Hobbit'; }
  164. if(preg_match_all("/\bhow to train your dragon/i", $depunctuated_text, $matches)) { $topic = 'How To Train Your Dragon 2'; }
  165. if(preg_match_all("/\bi'm not gonna miss you/i", $depunctuated_text, $matches)) { $topic = 'I\'m Not Gonna Miss You'; }
  166. if(preg_match_all("/\binherent vice/i", $depunctuated_text, $matches)) { $topic = 'Inherent Vice'; }
  167. if(preg_match_all("/\binto the woods/i", $depunctuated_text, $matches)) { $topic = 'Into The Woods'; }
  168. if(preg_match_all("/\bjoanna/i", $depunctuated_text, $matches)) { $topic = 'Joanna'; }
  169. if(preg_match_all("/\bkeira knightley/i", $depunctuated_text, $matches)) { $topic = 'Keira Knightley'; }
  170. if(preg_match_all("/\blast days in vietnam/i", $depunctuated_text, $matches)) { $topic = 'Last Days In Vietnam'; }
  171. if(preg_match_all("/\blaura dern/i", $depunctuated_text, $matches)) { $topic = 'Laura Dern'; }
  172. if(preg_match_all("/\blost stars/i", $depunctuated_text, $matches)) { $topic = 'Lost Stars'; }
  173. if(preg_match_all("/\bmaleficent/i", $depunctuated_text, $matches)) { $topic = 'Maleficent'; }
  174. if(preg_match_all("/\bmarion cotillard/i", $depunctuated_text, $matches)) { $topic = 'Marion Cotillard'; }
  175. if(preg_match_all("/\bmark ruffalo/i", $depunctuated_text, $matches)) { $topic = 'Mark Ruffalo'; }
  176. if(preg_match_all("/\bme and my moulton/i", $depunctuated_text, $matches)) { $topic = 'Me And My Moulton'; }
  177. if(preg_match_all("/\bmeryl streep/i", $depunctuated_text, $matches)) { $topic = 'Meryl Streep'; }
  178. if(preg_match_all("/\bmichael keaton/i", $depunctuated_text, $matches)) { $topic = 'Michael Keaton'; }
  179. if(preg_match_all("/\bmorten tyldum/i", $depunctuated_text, $matches)) { $topic = 'Morten Tyldum'; }
  180. if(preg_match_all("/\bmr. turner/i", $depunctuated_text, $matches)) { $topic = 'Mr. Turner'; }
  181. if(preg_match_all("/\bnightcrawler/i", $depunctuated_text, $matches)) { $topic = 'Nightcrawler'; }
  182. if(preg_match_all("/\bour curse/i", $depunctuated_text, $matches)) { $topic = 'Our Curse'; }
  183. if(preg_match_all("/\bparvaneh/i", $depunctuated_text, $matches)) { $topic = 'Parvaneh'; }
  184. if(preg_match_all("/\breese witherspoon/i", $depunctuated_text, $matches)) { $topic = 'Reese Witherspoon'; }
  185. if(preg_match_all("/\brichard linklater/i", $depunctuated_text, $matches)) { $topic = 'Richard Linklater'; }
  186. if(preg_match_all("/\brobert duvall/i", $depunctuated_text, $matches)) { $topic = 'Robert Duvall'; }
  187. if(preg_match_all("/\brosamund pike/i", $depunctuated_text, $matches)) { $topic = 'Rosamund Pike'; }
  188. if(preg_match_all("/\bsalt of the earth/i", $depunctuated_text, $matches)) { $topic = 'Salt Of The Earth'; }
  189. if(preg_match_all("/\bselma/i", $depunctuated_text, $matches)) { $topic = 'Selma'; }
  190. if(preg_match_all("/\bshawn patterson/i", $depunctuated_text, $matches)) { $topic = 'Shawn Patterson'; }
  191. if(preg_match_all("/\bsong of the sea/i", $depunctuated_text, $matches)) { $topic = 'Song Of The Sea'; }
  192. if(preg_match_all("/\bsteve carell/i", $depunctuated_text, $matches)) { $topic = 'Steve Carell'; }
  193. if(preg_match_all("/\bthe bigger picture/i", $depunctuated_text, $matches)) { $topic = 'The Bigger Picture'; }
  194. if(preg_match_all("/\bthe boxtrolls/i", $depunctuated_text, $matches)) { $topic = 'The Boxtrolls'; }
  195. if(preg_match_all("/\bthe reaper/i", $depunctuated_text, $matches)) { $topic = 'The Reaper'; }
  196. if(preg_match_all("/\bprincess kaguya/i", $depunctuated_text, $matches)) { $topic = 'The Tale Of The Princess Kaguya'; }
  197. if(preg_match_all("/\btheory of everything/i", $depunctuated_text, $matches)) { $topic = 'Theory Of Everything'; }
  198. if(preg_match_all("/\bunbroken/i", $depunctuated_text, $matches)) { $topic = 'Unbroken'; }
  199. if(preg_match_all("/\bvirunga/i", $depunctuated_text, $matches)) { $topic = 'Virunga'; }
  200. if(preg_match_all("/\bwes anderson/i", $depunctuated_text, $matches)) { $topic = 'Wes Anderson'; }
  201. if(preg_match_all("/\bwhite earth/i", $depunctuated_text, $matches)) { $topic = 'White Earth'; }
  202. if(preg_match_all("/\bwinter soldier/i", $depunctuated_text, $matches)) { $topic = 'Captain America: Winter Soldier'; }
  203. if(preg_match_all("/\bx-men/i", $depunctuated_text, $matches)) { $topic = 'X-Men'; }
  204. return $topic;
  205. }
  206. /**
  207. * detect the mention of the Oscars in text.
  208. * @param str $text
  209. * @return int count of Oscar metnions in $text
  210. */
  211. public static function countOscarMentions($text) {
  212. $count = 0;
  213. $matches = array();
  214. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  215. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  216. preg_match_all("/\boscar/i", $depunctuated_text, $matches);
  217. $count += sizeof($matches[0]);
  218. preg_match_all("/\bacademy award/i", $depunctuated_text, $matches);
  219. $count += sizeof($matches[0]);
  220. preg_match_all("/\bbest picture/i", $depunctuated_text, $matches);
  221. $count += sizeof($matches[0]);
  222. preg_match_all("/\bbest director/i", $depunctuated_text, $matches);
  223. $count += sizeof($matches[0]);
  224. preg_match_all("/\bbest supporting/i", $depunctuated_text, $matches);
  225. $count += sizeof($matches[0]);
  226. return $count;
  227. }
  228. }
  229. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  230. $insights_plugin_registrar->registerInsightPlugin('Oscars2015Insight');