TagMiner.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Muzich\CoreBundle\Mining\Tag;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\Bundle\MongoDBBundle\ManagerRegistry as MongoManagerRegistry;
  5. use Doctrine\ODM\MongoDB\DocumentRepository;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Muzich\CoreBundle\Document\EntityTags;
  8. use Muzich\CoreBundle\Document\UserTags;
  9. use Muzich\CoreBundle\Document\GroupTags;
  10. use Muzich\CoreBundle\Document\PlaylistTags;
  11. use Doctrine\ORM\QueryBuilder;
  12. use Muzich\CoreBundle\lib\Tag as TagOrderer;
  13. use Muzich\CoreBundle\lib\TagScorer;
  14. use Muzich\CoreBundle\Entity\User;
  15. use Muzich\CoreBundle\Managers\PlaylistManager;
  16. class TagMiner
  17. {
  18. protected $doctrine_entity_manager;
  19. protected $mongo_manager_registry;
  20. protected $tag_scorer;
  21. protected $tag_orderer;
  22. public function __construct(EntityManager $doctrine_entity_manager, MongoManagerRegistry $mongo_manager_registry)
  23. {
  24. $this->doctrine_entity_manager = $doctrine_entity_manager;
  25. $this->mongo_manager_registry = $mongo_manager_registry;
  26. $this->tag_scorer = new TagScorer();
  27. $this->tag_orderer = new TagOrderer();
  28. }
  29. /** @return EntityManager */
  30. protected function getDoctrineEntityManager()
  31. {
  32. return $this->doctrine_entity_manager;
  33. }
  34. /** @return DocumentRepository */
  35. protected function getMongoRepository($repository)
  36. {
  37. return $this->mongo_manager_registry->getRepository($repository);
  38. }
  39. /** @return DocumentManager */
  40. protected function getMongoManager()
  41. {
  42. return $this->mongo_manager_registry->getManager();
  43. }
  44. /** @return TagScorer */
  45. protected function getTagsScorer()
  46. {
  47. return $this->tag_scorer;
  48. }
  49. /** @return TagOrderer */
  50. protected function getTagOrderer()
  51. {
  52. return $this->tag_orderer;
  53. }
  54. /**
  55. * @param QueryBuilder $query_builder
  56. * @param string $user_alias
  57. */
  58. public function adaptQueryBuilderSelectorsForUser(QueryBuilder $query_builder, $user_alias = 'user')
  59. {
  60. // Adapt query builder to necessary data in mining
  61. $query_builder->leftJoin($user_alias.'.elements', 'element_owned');
  62. $query_builder->leftJoin('element_owned.tags', 'element_owned_tags');
  63. $query_builder->leftJoin($user_alias.'.elements_favorites', 'element_favorite');
  64. $query_builder->leftJoin('element_favorite.element', 'element_favorite_element');
  65. $query_builder->select($user_alias.', element_owned, element_owned_tags, element_favorite');
  66. }
  67. /**
  68. * @param array $users
  69. */
  70. public function mineTagsForUsers($users)
  71. {
  72. foreach ($users as $user)
  73. {
  74. $user_tags = $this->getEntityTagsDocument($user->getId(), EntityTags::TYPE_USER);
  75. $this->scoreUserDiffusionsTags($user_tags, $user);
  76. $this->scoreUserFavoritesTags($user_tags, $user);
  77. $this->scoreUserPlaylistsTags($user_tags, $user);
  78. $this->scoreUserTags($user_tags, $user);
  79. $this->determineTagsTops($user_tags);
  80. $this->getMongoManager()->persist($user_tags);
  81. }
  82. $this->getMongoManager()->flush();
  83. }
  84. /** @return EntityTags */
  85. protected function getEntityTagsDocument($ref, $type)
  86. {
  87. if (!($user_tags = $this->getMongoManager()->createQueryBuilder('MuzichCoreBundle:'.$type.'Tags')
  88. ->field('ref')->equals((int)$ref)
  89. ->getQuery()->getSingleResult()
  90. ))
  91. {
  92. $user_tags = $this->getObjectTypeTags($type);
  93. $user_tags->setRef($ref);
  94. }
  95. return $user_tags;
  96. }
  97. /** @return EntityTags */
  98. protected function getObjectTypeTags($type)
  99. {
  100. switch ($type)
  101. {
  102. case EntityTags::TYPE_USER:
  103. return new UserTags();
  104. break;
  105. case EntityTags::TYPE_GROUP:
  106. return new GroupTags();
  107. break;
  108. case EntityTags::TYPE_PLAYLIST:
  109. return new PlaylistTags();
  110. break;
  111. }
  112. }
  113. protected function scoreUserDiffusionsTags(EntityTags $user_tags, User $user)
  114. {
  115. $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($user->getElements());
  116. $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
  117. $user_tags->setElementDiffusionTags($scoreds_tags_ids);
  118. }
  119. protected function scoreUserFavoritesTags(EntityTags $user_tags, User $user)
  120. {
  121. $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($user->getElementsFavoritesElements());
  122. $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
  123. $user_tags->setElementFavoriteTags($scoreds_tags_ids);
  124. }
  125. protected function scoreUserPlaylistsTags(EntityTags $user_tags, User $user)
  126. {
  127. $playlist_manager = new PlaylistManager($this->getDoctrineEntityManager());
  128. $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($playlist_manager->getElementsOfPlaylists($this->getUserPlaylists($user)));
  129. $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
  130. $user_tags->setElementPlaylistTags($scoreds_tags_ids);
  131. }
  132. protected function getUserPlaylists(User $user)
  133. {
  134. $playlists = $user->getPlaylistsOwneds();
  135. foreach ($user->getPickedsPlaylists() as $picked_playlist)
  136. {
  137. $found = false;
  138. foreach ($playlists as $playlist)
  139. {
  140. if ($playlist->getId() == $picked_playlist->getId())
  141. {
  142. $found = true;
  143. }
  144. }
  145. if (!$found)
  146. $playlists[] = $picked_playlist;
  147. }
  148. return $playlists;
  149. }
  150. protected function scoreUserTags(EntityTags $user_tags, User $user)
  151. {
  152. $all_tags_ordered = $this->getTagsScorer()->scoreEntireOrderedTagsIds(array(
  153. $user_tags->getElementDiffusionTags(),
  154. $user_tags->getElementFavoriteTags(),
  155. $user_tags->getElementPlaylistTags()
  156. ), $user->getTagsFavoritesQuickIds());
  157. $user_tags->setTagsAll($all_tags_ordered);
  158. }
  159. protected function determineTagsTops(EntityTags $user_tags)
  160. {
  161. $user_tags->setTagsTop1($this->getTopTagsRange($user_tags->getTagsAll(), 1));
  162. $user_tags->setTagsTop2($this->getTopTagsRange($user_tags->getTagsAll(), 2));
  163. $user_tags->setTagsTop3($this->getTopTagsRange($user_tags->getTagsAll(), 3));
  164. $user_tags->setTagsTop5($this->getTopTagsRange($user_tags->getTagsAll(), 5));
  165. $user_tags->setTagsTop10($this->getTopTagsRange($user_tags->getTagsAll(), 10));
  166. $user_tags->setTagsTop25($this->getTopTagsRange($user_tags->getTagsAll(), 25));
  167. }
  168. protected function getTopTagsRange($tags, $range_end)
  169. {
  170. $tags_top = array();
  171. if ($range_end <= count($tags))
  172. {
  173. $max = $range_end;
  174. }
  175. else
  176. {
  177. $max = count($tags);
  178. }
  179. for ($index = 0; $index <= $max-1; $index++)
  180. {
  181. $tags_top[] = $tags[$index];
  182. }
  183. return $tags_top;
  184. }
  185. }