Пример #1
0
def index(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['docfile'], request.FILES['docfile'].name)
            # Redirect to the document list after POST
            norm = Normalizer()
            data_save = []
            workpath = os.path.dirname(os.path.abspath(__file__)) #Returns the Path your .py file is in
            data = norm.load_csv(os.path.join(workpath, 'dataset/'+request.FILES['docfile'].name))

            for line in data:
                try:
                    data_save.append(line)
                except IndexError:
                    pass

            '''
            data_normalized = norm.normalization(data_save, 0.0, 1.0, 58)
            stats = norm.statistics(data_normalized, 58)
            '''
            normalizedData = norm.normalization()
            normSplitedData = norm.split(normalizedData)
            normNospams = normSplitedData[1]
            normSpams = normSplitedData[0]
            stats = norm.stats(normSpams, normNospams)

            spam = []
            for i in range(0, 58):
                line = []
                line.append(i)
                for j in range(0, 4):
                    line.append(stats[0][j][i])
                spam.append(line)

            no_spam = []
            for i in range(0, 58):
                line = []
                line.append(i)
                for j in range(0, 4):
                    line.append(stats[1][j][i])
                no_spam.append(line)

            new_stats = []
            for i in range(0, 58):
                line = []
                line.append(i)
                for j in range(0, 4):
                    line.append(stats[0][j][i])
                for j in range(0, 4):
                    line.append(stats[1][j][i])
                new_stats.append(line)

            global nomChamp
            nomChamp = ['word_freq_make', 'word_freq_address', 'word_freq_all',
                        'word_freq_3d', 'word_freq_our', 'word_freq_over', 'word_freq_remove',
                        'word_freq_internet', 'word_freq_order', 'word_freq_mail',
                        'word_freq_receive', 'word_freq_will', 'word_freq_people',
                        'word_freq_report', 'word_freq_addresses', 'word_freq_free',
                        'word_freq_business', 'word_freq_email', 'word_freq_you',
                        'word_freq_credit', 'word_freq_your', 'word_freq_font',
                        'word_freq_000', 'word_freq_money', 'word_freq_hp', 'word_freq_hpl',
                        'word_freq_george', 'word_freq_650', 'word_freq_lab',
                        'word_freq_labs', 'word_freq_telnet', 'word_freq_857',
                        'word_freq_data', 'word_freq_415', 'word_freq_85',
                        'word_freq_technology', 'word_freq_1999', 'word_freq_parts',
                        'word_freq_pm', 'word_freq_direct', 'word_freq_cs',
                        'word_freq_meeting', 'word_freq_original', 'word_freq_project',
                        'word_freq_re', 'word_freq_edu', 'word_freq_table',
                        'word_freq_conference', 'char_freq_semi', 'char_freq_lparen',
                        'char_freq_lbrack', 'char_freq_bang', 'char_freq_dollar',
                        'char_freq_hash', 'capital_run_length_average',
                        'capital_run_length_longest', 'capital_run_length_total',
                        'spam']

            stats_names = []
            stats_names = zip(nomChamp, new_stats)
            return render(request, 'stats.html', {'data': stats_names})
    else:
        form = DocumentForm() # A empty, unbound form
        return render(request, 'index.html', {'form': form})