<commit_before>from django.shortcuts import render
from django.views.generic import ListView, DetailView

import models

# Create your views here.
class TopicModelIndexView(ListView):
    context_object_name = 'topic_models'
    queryset = models.TopicModel.objects.all()
    template_name = 'topic_browser/models.html'


class TopicModelDetailView(DetailView):
    pk_url_kwarg = 'model_id'
    context_object_name = 'topic_model'
    model = models.TopicModel
    template_name = 'topic_browser/model_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicModelDetailView, self).get_context_data(**kwargs)

        # Add in a QuerySet of words for each topic
        words_for_topics = {}
        max_words = 0
        topics = context['topic_model'].topics.all().prefetch_related('words')
        for topic in topics:
            words = list(topic.word_scores.order_by('-probability').prefetch_related('word'))
            words_for_topics[topic.id] = words
            max_words = max(max_words, len(words))

        # transpose the word lists
        word_rows = []
        for word_i in range(max_words):
            row = []

            for topic in topics:
                words = words_for_topics[topic.id]
                if word_i < len(words):
                    row.append(words[word_i])
                else:
                    row.append(None)

            word_rows.append(row)

        context['word_rows'] = word_rows

        return context


class TopicDetailView(DetailView):
    pk_url_kwarg = 'topic_id'
    context_object_name = 'topic'
    model = models.Topic
    template_name = 'topic_browser/topic_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicDetailView, self).get_context_data(**kwargs)
        TopicDetailView.get_topic_data(context, topic=self.object)
        return context

    @classmethod
    def get_topic_data(cls, context, topic, word=None):
        topic_model = topic.model
        context['topic'] = topic
        context['word'] = word
        context['topic_model'] = topic_model
        context['topic_words'] = topic.words.all()

        #Dumb
        topicvector_class = models.MessageTopic


        examples = topicvector_class.get_examples(topic=topic)
        if word:
            examples = examples.filter(message__words__word=word)

        context['examples'] = examples[:20].prefetch_related('message')




class TopicWordDetailView(DetailView):
    pk_url_kwarg = 'word_id'
    context_object_name = 'word'
    model = models.TopicWord
    template_name = 'topic_browser/topic_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicWordDetailView, self).get_context_data(**kwargs)
        topicword = self.object
        TopicDetailView.get_topic_data(context, topic=self.object.topic, word=topicword.word)
        return context
<commit_msg>increase num. of example in topics<commit_after>from django.shortcuts import render
from django.views.generic import ListView, DetailView

import models

# Create your views here.
class TopicModelIndexView(ListView):
    context_object_name = 'topic_models'
    queryset = models.TopicModel.objects.all()
    template_name = 'topic_browser/models.html'


class TopicModelDetailView(DetailView):
    pk_url_kwarg = 'model_id'
    context_object_name = 'topic_model'
    model = models.TopicModel
    template_name = 'topic_browser/model_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicModelDetailView, self).get_context_data(**kwargs)

        # Add in a QuerySet of words for each topic
        words_for_topics = {}
        max_words = 0
        topics = context['topic_model'].topics.all().prefetch_related('words')
        for topic in topics:
            words = list(topic.word_scores.order_by('-probability').prefetch_related('word'))
            words_for_topics[topic.id] = words
            max_words = max(max_words, len(words))

        # transpose the word lists
        word_rows = []
        for word_i in range(max_words):
            row = []

            for topic in topics:
                words = words_for_topics[topic.id]
                if word_i < len(words):
                    row.append(words[word_i])
                else:
                    row.append(None)

            word_rows.append(row)

        context['word_rows'] = word_rows

        return context


class TopicDetailView(DetailView):
    pk_url_kwarg = 'topic_id'
    context_object_name = 'topic'
    model = models.Topic
    template_name = 'topic_browser/topic_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicDetailView, self).get_context_data(**kwargs)
        TopicDetailView.get_topic_data(context, topic=self.object)
        return context

    @classmethod
    def get_topic_data(cls, context, topic, word=None):
        topic_model = topic.model
        context['topic'] = topic
        context['word'] = word
        context['topic_model'] = topic_model
        context['topic_words'] = topic.words.all()

        #Dumb
        topicvector_class = models.MessageTopic


        examples = topicvector_class.get_examples(topic=topic)
        if word:
            examples = examples.filter(message__words__word=word)

        context['examples'] = examples[:100].prefetch_related('message')




class TopicWordDetailView(DetailView):
    pk_url_kwarg = 'word_id'
    context_object_name = 'word'
    model = models.TopicWord
    template_name = 'topic_browser/topic_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicWordDetailView, self).get_context_data(**kwargs)
        topicword = self.object
        TopicDetailView.get_topic_data(context, topic=self.object.topic, word=topicword.word)
        return context

{'fraction_non_alphanumeric': 0.09112903225806451, 'fraction_numerical': 0.0011290322580645162, 'mean_word_length': 2.825416409623689, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6859955', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1741, 'n_words': 381}