TagScorer.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Muzich\CoreBundle\lib;
  3. class TagScorer
  4. {
  5. protected function scoreOrderedsTagsIds($ordered_tags)
  6. {
  7. $tags_ordered_by_score = array();
  8. $score_max = count($ordered_tags);
  9. foreach ($ordered_tags as $tag_id)
  10. {
  11. $tags_ordered_by_score[(int)$score_max] = $tag_id;
  12. $score_max -= 1;
  13. }
  14. return $tags_ordered_by_score;
  15. }
  16. public function scoreEntireOrderedTagsIds($ordereds_elements_tags_ids, $favoriteds_tags)
  17. {
  18. $tags_score = array();
  19. foreach ($ordereds_elements_tags_ids as $ordereds_tags_ids)
  20. {
  21. $scored_tags_ids = $this->scoreOrderedsTagsIds($ordereds_tags_ids);
  22. foreach ($scored_tags_ids as $score_tag => $tag_id)
  23. {
  24. if (!array_key_exists($tag_id, $tags_score))
  25. $tags_score[$tag_id] = 0;
  26. $tags_score[$tag_id] += $score_tag;
  27. }
  28. }
  29. foreach ($favoriteds_tags as $favorite_tag_id)
  30. {
  31. if (!array_key_exists($favorite_tag_id, $tags_score))
  32. $tags_score[$favorite_tag_id] = 0;
  33. $tags_score[$favorite_tag_id] += 5;
  34. }
  35. arsort($tags_score);
  36. $oredered_tags_ids_without_score = array();
  37. foreach ($tags_score as $tag_id => $tag_score)
  38. {
  39. $oredered_tags_ids_without_score[] = $tag_id;
  40. }
  41. return $oredered_tags_ids_without_score;
  42. }
  43. }