2015-05-15 08:30:58 -05:00
|
|
|
"""Manipulate Markov data via the Django site."""
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
import logging
|
2014-04-05 14:26:06 -05:00
|
|
|
import time
|
2014-04-05 10:52:29 -05:00
|
|
|
|
2014-04-05 11:20:59 -05:00
|
|
|
from django.contrib.auth.decorators import permission_required
|
2014-04-05 10:52:29 -05:00
|
|
|
from django.http import HttpResponse
|
2014-04-05 14:26:06 -05:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2014-04-05 10:52:29 -05:00
|
|
|
|
2014-04-05 14:53:05 -05:00
|
|
|
from markov.forms import LogUploadForm, TeachLineForm
|
2015-05-15 08:36:17 -05:00
|
|
|
import markov.lib as markovlib
|
|
|
|
from markov.models import MarkovContext
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
|
2015-05-15 08:30:58 -05:00
|
|
|
log = logging.getLogger('markov.views')
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
"""Display nothing, for the moment."""
|
|
|
|
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
2014-04-05 14:26:06 -05:00
|
|
|
def context_index(request, context_id):
|
|
|
|
"""Display the context index for the given context."""
|
|
|
|
|
|
|
|
start_t = time.time()
|
|
|
|
context = get_object_or_404(MarkovContext, pk=context_id)
|
2015-05-15 08:36:17 -05:00
|
|
|
chain = u" ".join(markovlib.generate_line(context))
|
2014-04-05 14:26:06 -05:00
|
|
|
end_t = time.time()
|
|
|
|
|
2015-05-15 08:30:58 -05:00
|
|
|
return render(request, 'context.html', {'chain': chain,
|
|
|
|
'context': context,
|
|
|
|
'elapsed': end_t - start_t})
|
2014-04-05 14:26:06 -05:00
|
|
|
|
|
|
|
|
2014-04-05 11:20:59 -05:00
|
|
|
@permission_required('import_log_file', raise_exception=True)
|
2014-04-05 10:52:29 -05:00
|
|
|
def import_file(request):
|
|
|
|
"""Accept a file upload and turn it into markov stuff.
|
|
|
|
|
|
|
|
Current file formats supported:
|
|
|
|
* weechat
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = LogUploadForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
log_file = request.FILES['log_file']
|
|
|
|
context = form.cleaned_data['context']
|
2014-04-05 18:00:45 -05:00
|
|
|
ignores = form.cleaned_data['ignore_nicks'].split(',')
|
|
|
|
strips = form.cleaned_data['strip_prefixes'].split(' ')
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
whos = []
|
|
|
|
for line in log_file:
|
2014-04-05 16:39:44 -05:00
|
|
|
(timestamp, who, what) = line.decode('utf-8').split('\t', 2)
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
if who in ('-->', '<--', '--', ' *'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if who in ignores:
|
|
|
|
continue
|
|
|
|
|
|
|
|
whos.append(who)
|
|
|
|
|
|
|
|
# this is a line we probably care about now
|
2014-04-05 18:00:45 -05:00
|
|
|
what = [x for x in what.rstrip().split(' ') if x not in strips]
|
2015-05-15 08:36:17 -05:00
|
|
|
markovlib.learn_line(' '.join(what), context)
|
2014-04-05 10:52:29 -05:00
|
|
|
|
|
|
|
log.debug(set(whos))
|
|
|
|
else:
|
|
|
|
form = LogUploadForm()
|
|
|
|
|
2015-05-15 08:30:58 -05:00
|
|
|
return render(request, 'import_file.html', {'form': form})
|
2014-04-05 14:53:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
@permission_required('teach_line', raise_exception=True)
|
|
|
|
def teach_line(request):
|
|
|
|
"""Teach one line directly."""
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = TeachLineForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
line = form.cleaned_data['line']
|
|
|
|
context = form.cleaned_data['context']
|
2014-04-05 18:00:45 -05:00
|
|
|
strips = form.cleaned_data['strip_prefixes'].split(' ')
|
|
|
|
what = [x for x in line.rstrip().split(' ') if x not in strips]
|
2015-05-15 08:36:17 -05:00
|
|
|
markovlib.learn_line(' '.join(what), context)
|
2014-04-05 14:53:05 -05:00
|
|
|
else:
|
|
|
|
form = TeachLineForm()
|
|
|
|
|
2015-05-15 08:30:58 -05:00
|
|
|
return render(request, 'teach_line.html', {'form': form})
|