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