2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from mygpo
.data
.models
import PodcastTag
19 from django
.db
.models
import Sum
, Count
, Avg
20 from collections
import defaultdict
23 def get_source_weights():
24 categories
= [x
['source'] for x
in PodcastTag
.objects
.filter().values('source').distinct()]
27 tags
= PodcastTag
.objects
.filter(source
=c
)
28 total
= tags
.aggregate(total_weight
=Sum('weight'))['total_weight']
29 number
= tags
.aggregate(count
=Count('weight'))['count']
30 avg
= float(total
) / number
31 total_weights
[c
] = 1. / avg
36 def get_weighted_tags(podcast
, source_weights
):
38 tags
= defaultdict(int)
39 for t
in PodcastTag
.objects
.filter(podcast
=podcast
):
42 # promote more prominent tags of a podcast, demote less-prominent
43 src_avg
= PodcastTag
.objects
.filter(podcast
=podcast
, source
=t
.source
).aggregate(weight
=Avg('weight'))['weight']
45 tags
[tag
] = tags
[tag
] + t
.weight
/ src_avg
* source_weights
[t
.source
]
48 subscriber_factor
= math
.log10(podcast
.subscriber_count())
53 for t
in tags
.iterkeys():
54 tags
[t
] = tags
[t
] * subscriber_factor
59 def get_weighted_group_tags(group
, source_weights
):
63 for p
in group
.podcasts():
64 podcast_tags
.append(get_weighted_tags(p
, source_weights
))
66 tags
= reduce(lambda x
, y
: x
+y
, [x
.keys() for x
in podcast_tags
])
70 max_tags
[tag
] = max([x
[tag
] for x
in podcast_tags
])