<commit_before>from django.views.generic import TemplateView
from operator import itemgetter
from random import shuffle

from djofx.utils import get_classifier, get_training_data
from djofx.views.base import PageTitleMixin


class AccuracyView(PageTitleMixin, TemplateView):
    template_name = 'djofx/accuracy.html'
    page_title = 'Classifier Accuracy'

    def get_context_data(self, **kwargs):
        ctx = super(AccuracyView, self).get_context_data(**kwargs)
        training_data = get_training_data(self.request.user)
        classifier = get_classifier(self.request.user, training_data)
        ctx['keywords'] = classifier.informative_features()[:20]

        shuffle(training_data)
        training_size = int(len(training_data) * 0.75)
        training_elements = training_data[:training_size]
        test_elements = training_data[training_size:]

        sample_classifier = get_classifier(
            self.request.user,
            training_elements
        )

        ctx['accuracy'] = sample_classifier.accuracy(test_elements) * 100.0
<commit_msg>Fix error in accuracy report when there's no data for training<commit_after>from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic import TemplateView
from operator import itemgetter
from random import shuffle

from djofx.utils import get_classifier, get_training_data
from djofx.views.base import PageTitleMixin


class AccuracyView(PageTitleMixin, TemplateView):
    template_name = 'djofx/accuracy.html'
    page_title = 'Classifier Accuracy'

    def dispatch(self, request, *args, **kwargs):
        training_data = get_training_data(self.request.user)

        if not training_data:
            messages.info(
                self.request,
                'No transactions available for classifying.'
            )
            return HttpResponseRedirect(reverse('djofx_home'))


        return super(AccuracyView, self).dispatch(
            request,
            *args,
            **kwargs
        )

    def get_context_data(self, **kwargs):
        ctx = super(AccuracyView, self).get_context_data(**kwargs)
        training_data = get_training_data(self.request.user)
        classifier = get_classifier(self.request.user, training_data)
        ctx['keywords'] = classifier.informative_features()[:20]

        shuffle(training_data)
        training_size = int(len(training_data) * 0.75)
        training_elements = training_data[:training_size]
        test_elements = training_data[training_size:]

        sample_classifier = get_classifier(
            self.request.user,
            training_elements
        )

        ctx['accuracy'] = sample_classifier.accuracy(test_elements) * 100.0

{'fraction_non_alphanumeric': 0.08197905381003973, 'fraction_numerical': 0.0065005417118093175, 'mean_word_length': 3.294573643410853, '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': '5967001', 'n_tokens_mistral': 811, 'n_tokens_neox': 758, 'n_words': 161}