PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/newdictionarywords.php

http://github.com/ginatrapani/ThinkUp
PHP | 196 lines | 130 code | 10 blank | 56 comment | 25 complexity | c87874487922375fb102625e601f7acd MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: New Dictionary-Word Count
  4. Description: Did you use words just added to The Oxford Dictionary Online in August 2014?
  5. When: September 2, 2014 until November 2, 2014, Mondays for Twitter, Thursdays for Facebook
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/newdictionarywords.php
  10. *
  11. * Copyright (c) 2014-2016 Chris Moyer
  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 Chris Moyer
  30. * @author Chris Moyer <chris [at] inarow [dot] net>
  31. */
  32. class NewDictionaryWordsInsight extends InsightPluginParent implements InsightPlugin {
  33. public function getSchedule() {
  34. return array(
  35. // 'oxford_august_2014' => array(
  36. // 'words' => array(
  37. // 'adorbs', 'amazeballs', 'baller', 'binge-watch', 'clickbait', 'cray', 'dox', 'FML', 'hot mess',
  38. // 'humblebrag', 'ICYMI', 'listicle', 'live-tweet', 'mansplain', 'neckbeard', 'side-eye', 'SMH',
  39. // 'subtweet', 'YOLO',
  40. // ),
  41. // 'start' => '2014-09-02',
  42. // 'end' => '2014-11-02',
  43. // 'headline' => 'Before &ldquo;%word&rdquo; went legit',
  44. // 'single_template' => '%username used the word "%word" %total_times since %first_mention, '
  45. // . 'and it appears to have caught on: It\'s '
  46. // . '<a href="http://blog.oxforddictionaries.com/2014/08/oxford-dictionaries-update-august-2014/">'
  47. // .'just been added</a> to the Oxford Dictionary Online.',
  48. // 'multiple_template' => 'The Oxford Dictionary Online '
  49. // .'<a href="http://blog.oxforddictionaries.com/2014/08/'
  50. // .'oxford-dictionaries-update-august-2016/">just added</a>'
  51. // .' %word_list to their online dictionary, '
  52. // . 'but no one has to explain them to %username. Since %first_mention, %username used '
  53. // . '%word_times_list.',
  54. // 'hero_image' => array(
  55. // 'img_link' => 'http://www.flickr.com/photos/bethanyking/822518337',
  56. // 'alt_text' => 'New dictionary words',
  57. // 'credit' => 'Photo: Bethany King',
  58. // 'url' => 'https://www.thinkup.com/assets/images/insights/2014-08/new_dictionary_words.jpg'
  59. // )
  60. // ),
  61. 'oxford_december_2014' => array(
  62. 'words' => array(
  63. 'digital footprint', 'duck face', 'hawt', 'jel', 'izakaya', 'lolcat', 'man crush', 'permadeath',
  64. 'respawn', 'WRT', 'xlnt', 'simples', 'tech wreck', 'crony capitalism', 'flash crash', 'duckface',
  65. ),
  66. 'start' => '2014-12-04',
  67. 'end' => '2015-05-01',
  68. 'headline' => 'Before &ldquo;%word&rdquo; went legit',
  69. 'single_template' => '%username used the word "%word" %total_times since %first_mention, '
  70. . 'and it appears to have caught on: It\'s '
  71. . '<a href="http://blog.oxforddictionaries.com/2014/12/oxford-dictionaries-new-words-december-'
  72. . '2014/">just been added</a> to OxfordDictionariesOnline.com.',
  73. 'multiple_template' => 'OxfordDictionariesOnline.com '
  74. .'<a href="http://blog.oxforddictionaries.com/2014/12/oxford-dictionaries-new-words-december-2014/'
  75. .'">just added</a>'
  76. .' %word_list to their online dictionary, '
  77. . 'but no one has to explain them to %username. Since %first_mention, %username used '
  78. . '%word_times_list.',
  79. 'hero_image' => array(
  80. 'img_link' => 'https://www.flickr.com/photos/seeminglee/4041872282',
  81. 'alt_text' => 'New dictionary words',
  82. 'credit' => 'Photo: See-ming Lee',
  83. 'url' => 'https://www.thinkup.com/assets/images/insights/2015-03/dictionarywordsomglol.jpg'
  84. )
  85. ),
  86. );
  87. }
  88. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  89. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  90. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  91. $baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
  92. foreach ($this->getSchedule() as $baseline_slug=>$data) {
  93. $now = TimeHelper::getTime();
  94. if ($now >= strtotime($data['start']) && $now <= strtotime($data['end'])) {
  95. $this->logger->logInfo("$now is in-schedule", __METHOD__.','.__LINE__);
  96. $baseline = $baseline_dao->getMostRecentInsightBaseline($baseline_slug, $instance->id);
  97. if (!$baseline) {
  98. if ( ($instance->network == 'facebook' && date('w') == 4 /*Thursday*/)
  99. || ($instance->network == 'twitter' && date('w') == 1 /*Monday*/)
  100. || Utils::isTest() ) {
  101. $found = $this->runInsightForConfig($data, $instance);
  102. $baseline_dao->insertInsightBaseline($baseline_slug, $instance->id, $found);
  103. } else {
  104. $this->logger->logInfo("Not today", __METHOD__.','.__LINE__);
  105. }
  106. } else {
  107. $this->logger->logInfo("Already exists", __METHOD__.','.__LINE__);
  108. }
  109. } else {
  110. $this->logger->logInfo("Not in-schedule", __METHOD__.','.__LINE__);
  111. }
  112. }
  113. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  114. }
  115. private function runInsightForConfig($config, $instance) {
  116. $regex = '/\b('.join('|', array_map('preg_quote', $config['words'])).')\b/i';
  117. $usage = array_fill_keys(array_map('strtolower', $config['words']), 0);
  118. $post_dao = DAOFactory::getDAO('PostDAO');
  119. $posts = $post_dao->getAllPostsByUsernameIterator($instance->network_username, $instance->network);
  120. $first_date = time();
  121. foreach ($posts as $post) {
  122. if (preg_match_all($regex, $post->post_text, $matches)) {
  123. foreach ($matches[1] as $match) {
  124. $usage[strtolower($match)]++;
  125. }
  126. if (strtotime($post->pub_date) < $first_date) {
  127. $first_date = strtotime($post->pub_date);
  128. }
  129. }
  130. }
  131. $usage = array_filter($usage);
  132. if (count($usage)) {
  133. $formatted_usage = array();
  134. foreach ($usage as $word => $times) {
  135. foreach ($config['words'] as $formatted_word) {
  136. if (strtolower($formatted_word) == $word) {
  137. $formatted_usage[$formatted_word] = $times;
  138. break;
  139. }
  140. }
  141. }
  142. arsort($formatted_usage);
  143. $insight = new Insight();
  144. $insight->slug = 'new_dictionary_words';
  145. $insight->instance_id = $instance->id;
  146. $insight->date = $this->insight_date;
  147. $insight->filename = basename(__FILE__, ".php");
  148. $insight->emphasis = Insight::EMPHASIS_MED;
  149. if (!empty($config['hero_image'])) {
  150. $insight->setHeroImage($config['hero_image']);
  151. }
  152. if (count($formatted_usage) == 1) {
  153. $words = array_keys($formatted_usage);
  154. $template = $config['single_template'];
  155. $params = array(
  156. 'first_mention' => date('F Y', $first_date),
  157. 'word' => $words[0],
  158. 'total_times' => $this->terms->getOccurrencesAdverb($formatted_usage[$words[0]])
  159. );
  160. } else {
  161. $formatted_usage = array_slice($formatted_usage, 0, 5, true);
  162. $words = array_keys($formatted_usage);
  163. $template = $config['multiple_template'];
  164. $params = array('first_mention' => date('F Y', $first_date));
  165. $times = array();
  166. $quoted_words = array();
  167. foreach ($formatted_usage as $word => $t) {
  168. $times[] = '"'.$word.'" '.$this->terms->getOccurrencesAdverb($t);
  169. $quoted_words[] = '"'.$word.'"';
  170. }
  171. $last = count($times) - 1;
  172. $times[$last] = 'and '.$times[$last];
  173. $quoted_words[$last] = 'and '.$quoted_words[$last];
  174. $sep = count($times) == 2 ? ' ' : ', ';
  175. $params['word_times_list'] = join($sep, $times);
  176. $params['word_list'] = join($sep, $quoted_words);
  177. }
  178. $insight->text = $this->getVariableCopy(array($template), $params);
  179. $insight->headline = $this->getVariableCopy(array($config['headline']), array('word' => $words[0]));
  180. $this->insight_dao->insertInsight($insight);
  181. }
  182. return array_sum($usage);
  183. }
  184. }
  185. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  186. $insights_plugin_registrar->registerInsightPlugin('NewDictionaryWordsInsight');