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

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

http://github.com/ginatrapani/ThinkUp
PHP | 169 lines | 129 code | 10 blank | 30 comment | 25 complexity | 6f9768db29b6f9d0a6ef68267208d66f MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Merriam-Webster's Words of the Year 2014 (End of year)
  4. Description: Did you use words Merriam-Webster named Words of the Year in 2014?
  5. When: Dec 17, 2014 until December 31, 2014, Mondays for Twitter, Thursdays for Facebook
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/newdictionarywords.php
  10. *
  11. * Copyright (c) 2014-2016 Gina Trapani
  12. *
  13. * LICENSE:
  14. *
  15. * This file is part of ThinkUp (http://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
  30. * @author Gina Trapani <ginatrapani [at] gmail [dot] com>
  31. */
  32. class EOYWordsOfYearInsight extends InsightPluginParent implements InsightPlugin {
  33. public function getSchedule() {
  34. return array(
  35. 'merriam-webster_2014' => array(
  36. 'words' => array(
  37. 'culture', 'nostalgia', 'insidious', 'legacy', 'feminism', 'je ne sais quoi', 'innovation',
  38. 'surreptitious', 'autonomy', 'morbidity',
  39. ),
  40. 'start' => '2014-12-16',
  41. 'end' => '2014-12-31',
  42. 'headline' => '%username was all over 2014\'s Words of the Year',
  43. 'single_template' => '%username said the word "%word" %total_times on %network since %first_mention, '
  44. . 'and it appears to have caught on: '
  45. . 'The Merriam-Webster Dictionary just named it a <a href="http://www.merriam-webster.com/info'
  46. .'/2014-word-of-the-year.htm">Word of the Year for 2014</a>.',
  47. 'multiple_template' => 'The Merriam-Webster Dictionary just named %word_list '
  48. . '<a href="http://www.merriam-webster.com/info/2014-word-of-the-year.htm">'
  49. . 'Words of the Year 2014</a> but %username was ahead of the curve. '
  50. . 'Since %first_mention, %username said the words '
  51. . '%word_times_list on %network.',
  52. 'hero_image' => array(
  53. 'img_link' => 'https://www.flickr.com/photos/crdot/5510506796/',
  54. 'alt_text' => 'Words of the Year',
  55. 'credit' => 'Photo: Caleb Roenigk',
  56. 'url' => 'https://www.thinkup.com/assets/images/insights/2014-12/dictionary-words-of-year.jpg'
  57. )
  58. ),
  59. );
  60. }
  61. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  62. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  63. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  64. $baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
  65. foreach ($this->getSchedule() as $baseline_slug=>$data) {
  66. $now = TimeHelper::getTime();
  67. if ($now >= strtotime($data['start']) && $now <= strtotime($data['end'])) {
  68. $baseline = $baseline_dao->getMostRecentInsightBaseline($baseline_slug, $instance->id);
  69. if (!$baseline) {
  70. if ( ($instance->network == 'facebook' && date('w') == 0 /*Sunday*/ /*4 *//*Thursday*/)
  71. || ($instance->network == 'twitter' && date('w') == 5 /*Friday*/ /*1 */ /*Monday*/)
  72. || Utils::isTest() ) {
  73. $found = $this->runInsightForConfig($data, $instance);
  74. $baseline_dao->insertInsightBaseline($baseline_slug, $instance->id, $found);
  75. } else {
  76. $this->logger->logInfo("Not today", __METHOD__.','.__LINE__);
  77. }
  78. } else {
  79. $this->logger->logInfo("Already exists", __METHOD__.','.__LINE__);
  80. }
  81. } else {
  82. $this->logger->logInfo("Not time", __METHOD__.','.__LINE__);
  83. }
  84. }
  85. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  86. }
  87. private function runInsightForConfig($config, $instance) {
  88. $regex = '/\b('.join('|', array_map('preg_quote', $config['words'])).')\b/i';
  89. $usage = array_fill_keys(array_map('strtolower', $config['words']), 0);
  90. $post_dao = DAOFactory::getDAO('PostDAO');
  91. $posts = $post_dao->getAllPostsByUsernameIterator($instance->network_username, $instance->network);
  92. $first_date = time();
  93. foreach ($posts as $post) {
  94. if (preg_match_all($regex, $post->post_text, $matches)) {
  95. foreach ($matches[1] as $match) {
  96. $usage[strtolower($match)]++;
  97. }
  98. if (strtotime($post->pub_date) < $first_date) {
  99. $first_date = strtotime($post->pub_date);
  100. }
  101. }
  102. }
  103. $usage = array_filter($usage);
  104. if (count($usage)) {
  105. $formatted_usage = array();
  106. foreach ($usage as $word => $times) {
  107. foreach ($config['words'] as $formatted_word) {
  108. if (strtolower($formatted_word) == $word) {
  109. $formatted_usage[$formatted_word] = $times;
  110. break;
  111. }
  112. }
  113. }
  114. arsort($formatted_usage);
  115. $insight = new Insight();
  116. $insight->slug = 'eoy_words_of_year';
  117. $insight->instance_id = $instance->id;
  118. $insight->date = $this->insight_date;
  119. $insight->filename = basename(__FILE__, ".php");
  120. $insight->emphasis = Insight::EMPHASIS_MED;
  121. if (!empty($config['hero_image'])) {
  122. $insight->setHeroImage($config['hero_image']);
  123. }
  124. if (count($formatted_usage) == 1) {
  125. $words = array_keys($formatted_usage);
  126. $template = $config['single_template'];
  127. $params = array(
  128. 'first_mention' => date('F Y', $first_date),
  129. 'word' => $words[0],
  130. 'total_times' => $this->terms->getOccurrencesAdverb($formatted_usage[$words[0]]),
  131. 'network' => ucfirst($instance->network)
  132. );
  133. } else {
  134. $formatted_usage = array_slice($formatted_usage, 0, 5, true);
  135. $words = array_keys($formatted_usage);
  136. $template = $config['multiple_template'];
  137. $params = array('first_mention' => date('F Y', $first_date), 'network' => ucfirst($instance->network));
  138. $times = array();
  139. $quoted_words = array();
  140. foreach ($formatted_usage as $word => $t) {
  141. $times[] = '"'.$word.'" '.$this->terms->getOccurrencesAdverb($t);
  142. $quoted_words[] = '"'.$word.'"';
  143. }
  144. $last = count($times) - 1;
  145. $times[$last] = 'and '.$times[$last];
  146. $quoted_words[$last] = 'and '.$quoted_words[$last];
  147. $sep = count($times) == 2 ? ' ' : ', ';
  148. $params['word_times_list'] = join($sep, $times);
  149. $params['word_list'] = join($sep, $quoted_words);
  150. }
  151. $insight->text = $this->getVariableCopy(array($template), $params);
  152. $insight->headline = $this->getVariableCopy(array($config['headline']), array('word' => $words[0]));
  153. $this->insight_dao->insertInsight($insight);
  154. }
  155. return array_sum($usage);
  156. }
  157. }
  158. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  159. $insights_plugin_registrar->registerInsightPlugin('EOYWordsOfYearInsight');