PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/retired/oscars2014.php

http://github.com/ginatrapani/ThinkUp
PHP | 259 lines | 192 code | 21 blank | 46 comment | 92 complexity | f0895f35cc9ec8554318d2b3a886d1fe 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 2014
  4. Description: Did you mention the Oscars?
  5. When: Tuesday, March 4, 2014
  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 Oscars2014Insight 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") == '2014-03-04') {
  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/2014-03/oscars2014.jpg',
  39. 'alt_text' => 'Ellen DeGeneres posted the most popular tweet of all time',
  40. 'credit' => 'Photo: @TheEllenShow',
  41. 'img_link' => 'https://twitter.com/TheEllenShow/status/440322224407314432'
  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_2014', $instance, $insight_date='today',
  48. $regenerate_existing_insight=true, $day_of_week=2, 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 < "2014-03-02 12: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_2014'; //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("/\b12 years a slave/i", $depunctuated_text, $matches)) { $topic = '12 Years a Slave'; }
  104. if(preg_match_all("/\bbrad pitt/i", $depunctuated_text, $matches)) { $topic = '12 Years a Slave'; }
  105. if(preg_match_all("/\bmcqueen/i", $depunctuated_text, $matches)) { $topic = '12 Years a Slave'; }
  106. if(preg_match_all("/\b20 feet from stardom/i", $depunctuated_text, $matches)) {
  107. $topic = '20 Feet From Stardom';
  108. }
  109. if(preg_match_all("/\banderson lopez/i", $depunctuated_text, $matches)) { $topic = 'Let It Go'; }
  110. if(preg_match_all("/\bcate blanchett/i", $depunctuated_text, $matches)) { $topic = 'Cate Blanchett'; }
  111. if(preg_match_all("/\bdallas buyers club/i", $depunctuated_text, $matches)) { $topic = 'Dallas Buyers Club'; }
  112. if(preg_match_all("/\bfrozen/i", $depunctuated_text, $matches)) { $topic = 'Frozen'; }
  113. if(preg_match_all("/\bGravity/", $depunctuated_text, $matches)) { $topic = 'Gravity'; }
  114. if(preg_match_all("/\bgreat beauty/i", $depunctuated_text, $matches)) { $topic = 'Great Beauty'; }
  115. if(preg_match_all("/\bgreat gatsby/i", $depunctuated_text, $matches)) { $topic = 'Great Gatsby'; }
  116. if(preg_match_all("/\bHelium/", $depunctuated_text, $matches)) { $topic = 'Helium'; }
  117. if(preg_match_all("/\bhublot/i", $depunctuated_text, $matches)) { $topic = 'Hublot'; }
  118. if(preg_match_all("/\bidina menzel/i", $depunctuated_text, $matches)) { $topic = 'Let It Go'; }
  119. if(preg_match_all("/\bjared leto/i", $depunctuated_text, $matches)) { $topic = 'Jared Leto'; }
  120. if(preg_match_all("/\blady in number 6/i", $depunctuated_text, $matches)) { $topic = 'Lady In Number 6'; }
  121. if(preg_match_all("/\blupita/i", $depunctuated_text, $matches)) { $topic = 'Lupita Nyong\'o'; }
  122. if(preg_match_all("/\bmcconaughey/i", $depunctuated_text, $matches)) { $topic = 'Matthew McConaughey'; }
  123. if(preg_match_all("/\bjonze/i", $depunctuated_text, $matches)) { $topic = 'Spike Jonze'; }
  124. if(preg_match_all("/\bcuaron/i", $depunctuated_text, $matches)) { $topic = 'Alfonso CuarĂ³n'; }
  125. return $topic;
  126. }
  127. /**
  128. * detect the mention of Oscar-related terms in text.
  129. * @param str $text
  130. * @return str Topic names in $text
  131. */
  132. public static function detectOscarLoserReferences($text) {
  133. $topic = '';
  134. $matches = array();
  135. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  136. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  137. if(preg_match_all("/\bact of killing/i", $depunctuated_text, $matches)) { $topic = 'Act Of Killing'; }
  138. if(preg_match_all("/\ball is lost/i", $depunctuated_text, $matches)) { $topic = 'All Is Lost'; }
  139. if(preg_match_all("/\bamerican hustle/i", $depunctuated_text, $matches)) { $topic = 'American Hustle'; }
  140. if(preg_match_all("/\bamy adams/i", $depunctuated_text, $matches)) { $topic = 'Amy Adams'; }
  141. if(preg_match_all("/\baquel no era yo/i", $depunctuated_text, $matches)) { $topic = 'Aquel No Era Yo'; }
  142. if(preg_match_all("/\bavant que de tout perdre/i", $depunctuated_text, $matches)) {
  143. $topic = 'Avant Que De Tout Perdre';
  144. }
  145. if(preg_match_all("/\bbad grandpa/i", $depunctuated_text, $matches)) { $topic = 'Bad Grandpa'; }
  146. if(preg_match_all("/\bbarkhad abdi/i", $depunctuated_text, $matches)) { $topic = 'Barkhad Abdi'; }
  147. if(preg_match_all("/\bbefore midnight/i", $depunctuated_text, $matches)) { $topic = 'Before Midnight'; }
  148. if(preg_match_all("/\bblue jasmine/i", $depunctuated_text, $matches)) { $topic = 'Blue Jasmine'; }
  149. if(preg_match_all("/\bbook thief/i", $depunctuated_text, $matches)) { $topic = 'Book Thief'; }
  150. if(preg_match_all("/\bbradley cooper/i", $depunctuated_text, $matches)) { $topic = 'Bradley Cooper'; }
  151. if(preg_match_all("/\bbroken circle breakdown/i", $depunctuated_text, $matches)) {
  152. $topic = 'Broken Circle Breakdown';
  153. }
  154. if(preg_match_all("/\bbruce dern/i", $depunctuated_text, $matches)) { $topic = 'Bruce Dern'; }
  155. if(preg_match_all("/\bcaptain phillips/i", $depunctuated_text, $matches)) { $topic = 'Captain Phillips'; }
  156. if(preg_match_all("/\bcavedigger/i", $depunctuated_text, $matches)) { $topic = 'Cavedigger'; }
  157. if(preg_match_all("/\bchiwetel ejiofor/i", $depunctuated_text, $matches)) { $topic = 'Chiwetel Ejiofor'; }
  158. if(preg_match_all("/\bchiwetel/i", $depunctuated_text, $matches)) { $topic = 'Chiwetel Ejiofor'; }
  159. if(preg_match_all("/\bchristian bale/i", $depunctuated_text, $matches)) { $topic = 'Christian Bale'; }
  160. if(preg_match_all("/\bcroods/i", $depunctuated_text, $matches)) { $topic = 'Croods'; }
  161. if(preg_match_all("/\bcutie and the boxer/i", $depunctuated_text, $matches)) { $topic = 'Cutie And The Boxer'; }
  162. if(preg_match_all("/\bdesolation of smaug/i", $depunctuated_text, $matches)) { $topic = 'Desolation Of Smaug'; }
  163. if(preg_match_all("/\bdespicable me 2/i", $depunctuated_text, $matches)) { $topic = 'Despicable Me 2'; }
  164. if(preg_match_all("/\bdirty wars/i", $depunctuated_text, $matches)) { $topic = 'Dirty Wars'; }
  165. if(preg_match_all("/\bernest celestine/i", $depunctuated_text, $matches)) { $topic = 'Ernest Celestine'; }
  166. if(preg_match_all("/\bernest and celestine/i", $depunctuated_text, $matches)) {
  167. $topic = 'Ernest And Celestine';
  168. }
  169. if(preg_match_all("/\bfacing fear/i", $depunctuated_text, $matches)) { $topic = 'Facing Fear'; }
  170. if(preg_match_all("/\bferal/i", $depunctuated_text, $matches)) { $topic = 'Feral'; }
  171. if(preg_match_all("/\bget a horse/i", $depunctuated_text, $matches)) { $topic = 'Get A Horse'; }
  172. if(preg_match_all("/\bhobbit/i", $depunctuated_text, $matches)) { $topic = 'Hobbit'; }
  173. if(preg_match_all("/\binside llewyn davis/i", $depunctuated_text, $matches)) { $topic = 'Inside Llewyn Davis'; }
  174. if(preg_match_all("/\binto darkness/i", $depunctuated_text, $matches)) { $topic = 'Into Darkness'; }
  175. if(preg_match_all("/\binvisible woman/i", $depunctuated_text, $matches)) { $topic = 'Invisible Woman'; }
  176. if(preg_match_all("/\biron man 3/i", $depunctuated_text, $matches)) { $topic = 'Iron Man 3'; }
  177. if(preg_match_all("/\bjennifer lawrence/i", $depunctuated_text, $matches)) { $topic = 'Jennifer Lawrence'; }
  178. if(preg_match_all("/\bjonah hill/i", $depunctuated_text, $matches)) { $topic = 'Jonah Hill'; }
  179. if(preg_match_all("/\bjudi dench/i", $depunctuated_text, $matches)) { $topic = 'Judi Dench'; }
  180. if(preg_match_all("/\bjulia roberts/i", $depunctuated_text, $matches)) { $topic = 'Julia Roberts'; }
  181. if(preg_match_all("/\bjune squibb/i", $depunctuated_text, $matches)) { $topic = 'June Squibb'; }
  182. if(preg_match_all("/\bkaikki hoitaa/i", $depunctuated_text, $matches)) { $topic = 'Kaikki Hoitaa'; }
  183. if(preg_match_all("/\bkarama has no walls/i", $depunctuated_text, $matches)) { $topic = 'Karama Has No Walls'; }
  184. if(preg_match_all("/\bleonardo dicaprio/i", $depunctuated_text, $matches)) { $topic = 'Leonardo Dicaprio'; }
  185. if(preg_match_all("/\blet it go/i", $depunctuated_text, $matches)) { $topic = 'Let It Go'; }
  186. if(preg_match_all("/\blone ranger/i", $depunctuated_text, $matches)) { $topic = 'Lone Ranger'; }
  187. if(preg_match_all("/\blone survivor/i", $depunctuated_text, $matches)) { $topic = 'Lone Survivor'; }
  188. if(preg_match_all("/\bmeryl streep/i", $depunctuated_text, $matches)) { $topic = 'Meryl Streep'; }
  189. if(preg_match_all("/\bmichael fassbender/i", $depunctuated_text, $matches)) { $topic = 'Michael Fassbender'; }
  190. if(preg_match_all("/\bmissing picture/i", $depunctuated_text, $matches)) { $topic = 'Missing Picture'; }
  191. if(preg_match_all("/\bmoon song/i", $depunctuated_text, $matches)) { $topic = 'Moon Song'; }
  192. if(preg_match_all("/\bomar/i", $depunctuated_text, $matches)) { $topic = 'Omar'; }
  193. if(preg_match_all("/\bordinary lacelove/i", $depunctuated_text, $matches)) { $topic = 'Ordinary laceLove'; }
  194. if(preg_match_all("/\bphilomena/i", $depunctuated_text, $matches)) { $topic = 'Philomena'; }
  195. if(preg_match_all("/\bprison terminal/i", $depunctuated_text, $matches)) { $topic = 'Prison Terminal'; }
  196. if(preg_match_all("/\broom on the broom/i", $depunctuated_text, $matches)) { $topic = 'Room On The Broom'; }
  197. if(preg_match_all("/\bsally hawkins/i", $depunctuated_text, $matches)) { $topic = 'Sally Hawkins'; }
  198. if(preg_match_all("/\bsandra bullock/i", $depunctuated_text, $matches)) { $topic = 'Sandra Bullock'; }
  199. if(preg_match_all("/\bsaving mr banks/i", $depunctuated_text, $matches)) { $topic = 'Saving Mr Banks'; }
  200. if(preg_match_all("/\bstar trek into darkness/i", $depunctuated_text, $matches)) {
  201. $topic = 'Star Trek Into Darkness';
  202. }
  203. if(preg_match_all("/\bthe grandmaster/i", $depunctuated_text, $matches)) { $topic = 'The Grandmaster'; }
  204. if(preg_match_all("/\bthe hunt/i", $depunctuated_text, $matches)) { $topic = 'The Hunt'; }
  205. if(preg_match_all("/\bthe square/i", $depunctuated_text, $matches)) { $topic = 'The Square'; }
  206. if(preg_match_all("/\bvoorman problem/i", $depunctuated_text, $matches)) { $topic = 'Voorman Problem'; }
  207. if(preg_match_all("/\bwind rises/i", $depunctuated_text, $matches)) { $topic = 'Wind Rises'; }
  208. if(preg_match_all("/\bwolf of wall street/i", $depunctuated_text, $matches)) { $topic = 'Wolf of Wall Street'; }
  209. return $topic;
  210. }
  211. /**
  212. * detect the mention of the Oscars in text.
  213. * @param str $text
  214. * @return int count of Oscar metnions in $text
  215. */
  216. public static function countOscarMentions($text) {
  217. $count = 0;
  218. $matches = array();
  219. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  220. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  221. preg_match_all("/\boscar/i", $depunctuated_text, $matches);
  222. $count += sizeof($matches[0]);
  223. preg_match_all("/\bacademy award/i", $depunctuated_text, $matches);
  224. $count += sizeof($matches[0]);
  225. preg_match_all("/\bbest picture/i", $depunctuated_text, $matches);
  226. $count += sizeof($matches[0]);
  227. preg_match_all("/\bbest director/i", $depunctuated_text, $matches);
  228. $count += sizeof($matches[0]);
  229. preg_match_all("/\bbest supporting/i", $depunctuated_text, $matches);
  230. $count += sizeof($matches[0]);
  231. return $count;
  232. }
  233. }
  234. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  235. $insights_plugin_registrar->registerInsightPlugin('Oscars2014Insight');