<commit_before>from collections import defaultdict, namedtuple

from mygpo.core.models import Podcast
from mygpo.decorators import query_if_required
from mygpo.directory.models import Category
from mygpo.utils import multi_request_view


# FIXME: this should be moved to User class once it is migrated to CouchDB
def tags_for_user(user, podcast_id=None):
    """
    Returns a dictionary that contains all podcasts tagged by the user as keys.
    For each podcast, a list of tags can be retrieved
    """

    res = Podcast.view('directory/tags_by_user', startkey=[user.id, podcast_id],
        endkey=[user.id, podcast_id or 'ZZZZZZ'])

    tags = defaultdict(list)
    for r in res:
        tags[r['key'][1]].append(r['value'])
    return tags


def podcasts_for_tag(tag):
    res = multi_request_view(Podcast, 'directory/podcasts_by_tag',
            startkey    = [tag, None],
            endkey      = [tag, 'ZZZZZZ'],
            reduce      = True,
            group       = True,
            group_level = 2
        )

    for r in res:
        yield (r['key'][1], r['value'])


def all_tags():
    res = multi_request_view(Podcast, 'directory/podcasts_by_tag',
            reduce      = True,
            group       = True,
            group_level = 1
        )<commit_msg>add missing parameter in all_tags()<commit_after>from collections import defaultdict, namedtuple

from mygpo.core.models import Podcast
from mygpo.decorators import query_if_required
from mygpo.directory.models import Category
from mygpo.utils import multi_request_view


# FIXME: this should be moved to User class once it is migrated to CouchDB
def tags_for_user(user, podcast_id=None):
    """
    Returns a dictionary that contains all podcasts tagged by the user as keys.
    For each podcast, a list of tags can be retrieved
    """

    res = Podcast.view('directory/tags_by_user', startkey=[user.id, podcast_id],
        endkey=[user.id, podcast_id or 'ZZZZZZ'])

    tags = defaultdict(list)
    for r in res:
        tags[r['key'][1]].append(r['value'])
    return tags


def podcasts_for_tag(tag):
    res = multi_request_view(Podcast, 'directory/podcasts_by_tag',
            startkey    = [tag, None],
            endkey      = [tag, 'ZZZZZZ'],
            reduce      = True,
            group       = True,
            group_level = 2
        )

    for r in res:
        yield (r['key'][1], r['value'])


def all_tags():
    res = multi_request_view(Podcast, 'directory/podcasts_by_tag',
            wrap        = False,
            reduce      = True,
            group       = True,
            group_level = 1
        )
{'fraction_non_alphanumeric': 0.09538344143456697, 'fraction_numerical': 0.003052270125906143, 'mean_word_length': 2.4820717131474104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5181463', 'n_tokens_mistral': 803, 'n_tokens_neox': 781, 'n_words': 232}