PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/allaboutyou.php

http://github.com/ginatrapani/ThinkUp
PHP | 139 lines | 92 code | 13 blank | 34 comment | 9 complexity | e89e4279b39e5197da706f6446820ceb MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: All About You
  4. Description: How often you referred to yourself ("I", "me", "myself", "my") in the past week.
  5. When: Sundays for Twitter, Wednesdays otherwise
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/allaboutyou.php
  10. *
  11. * Copyright (c) 2012-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 2012-2016 Gina Trapani
  30. */
  31. class AllAboutYouInsight extends InsightPluginParent implements InsightPlugin {
  32. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  33. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  34. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  35. $insight_text = '';
  36. if ($instance->network == 'twitter') {
  37. $day_of_week = 0;
  38. } else {
  39. $day_of_week = 3;
  40. }
  41. $should_generate_insight = self::shouldGenerateWeeklyInsight( 'all_about_you', $instance, $insight_date='today',
  42. $regenerate_existing_insight=false, $day_of_week = $day_of_week, count($last_week_of_posts),
  43. $excluded_networks = array('instagram'));
  44. if ($should_generate_insight) {
  45. $text = '';
  46. $count = 0;
  47. foreach ($last_week_of_posts as $post) {
  48. $count += self::hasFirstPersonReferences($post->post_text) ? 1 : 0;
  49. }
  50. if ($count > 1) {
  51. $headline = $this->getVariableCopy(array(
  52. 'A story about %username',
  53. 'Sometimes it\'s all about %username',
  54. 'It\'s getting personal',
  55. 'A moment of self-reflection',
  56. 'Sometimes %network is a first-person story',
  57. ), array('network' => ucfirst($instance->network)));
  58. $percent = round($count / count($last_week_of_posts) * 100);
  59. $plural = count($last_week_of_posts) > 1;
  60. $insight_text = "<strong>$percent%</strong> of $this->username's ".$this->terms->getNoun('post', $plural)
  61. ." contained the words &ldquo;I&rdquo;, &ldquo;me&rdquo;, &ldquo;my&rdquo;, &ldquo;mine&rdquo;,"
  62. ." or &ldquo;myself&rdquo; in the last week.";
  63. $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
  64. $insight_baseline_dao->insertInsightBaseline("all_about_you_percent", $instance->id, $percent,
  65. $this->insight_date);
  66. //Week over week comparison
  67. //Get insight baseline from last Sunday
  68. $last_sunday = date('Y-m-d', strtotime('-7 day'));
  69. $last_sunday_insight_baseline = $insight_baseline_dao->getInsightBaseline("all_about_you_percent",
  70. $instance->id, $last_sunday);
  71. if (isset($last_sunday_insight_baseline) ) {
  72. //compare it to this Sunday's number, and add a sentence comparing it.
  73. $difference = abs($last_sunday_insight_baseline->value - $percent);
  74. if ($last_sunday_insight_baseline->value == $percent ) {
  75. $insight_text .= " So consistent: that's the same amount as the previous week.";
  76. }
  77. else {
  78. if ($last_sunday_insight_baseline->value > $percent ) {
  79. $comparison = 'down';
  80. } else {
  81. $comparison = 'up';
  82. }
  83. $insight_text .= " That's $comparison $difference percentage point".($difference>1?"s":"")
  84. ." from the previous week.";
  85. }
  86. }
  87. $my_insight = new Insight();
  88. $my_insight->slug = 'all_about_you'; //slug to label this insight's content
  89. $my_insight->instance_id = $instance->id;
  90. $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date
  91. $my_insight->headline = $headline; // or just set a string like 'Ohai';
  92. $my_insight->text = $insight_text; // or just set a strong like "Greetings humans";
  93. $my_insight->header_image = $header_image;
  94. $my_insight->filename = basename(__FILE__, ".php"); //Same for every insight, must be set exactly this way
  95. $my_insight->emphasis = Insight::EMPHASIS_LOW; //Set emphasis optionally, default is Insight::EMPHASIS_LOW
  96. $this->insight_dao->insertInsight($my_insight);
  97. }
  98. }
  99. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  100. }
  101. /**
  102. * Determine if "I", "me", "my", "myself" or "mine" appear in text.
  103. * @param str $text
  104. * @return bool Does "I", "me", "my", "myself" or "mine" appear in $text
  105. */
  106. public static function hasFirstPersonReferences($text) {
  107. $count = 0;
  108. $matches = array();
  109. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  110. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  111. preg_match_all('/\b(?:I|myself|my|mine)\b/i', $depunctuated_text, $matches);
  112. $notmes = count($matches[0]);
  113. preg_match_all('/\b\.me\b/i', $text, $me_matches);
  114. $dotmes = count($me_matches[0]);
  115. preg_match_all('/\bme\b/i', $text, $me_matches);
  116. $mes = count($me_matches[0]);
  117. if ($notmes || $mes > $dotmes) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. }
  123. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  124. $insights_plugin_registrar->registerInsightPlugin('AllAboutYouInsight');