def post(self, request, question_pk, choice_number): question = Question.objects.get(pk=question_pk) choice = Sentence.objects.get(question=question, number=choice_number) form = SentenceParseForm(request.POST) if form.is_valid(): lines = form.cleaned_data['text'].split('\r\n') valid = True if len(lines) > 1: valid = False if valid: line = lines[0] line = line.rstrip().lstrip() if ChoiceAnnotation.objects.filter(choice=choice).exists(): choice_annotation = ChoiceAnnotation.objects.get( choice=choice) choice_annotation.text = line else: choice_annotation = ChoiceAnnotation(choice=choice, text=line) choice_annotation.save() if len(question.choices.all()) - 1 == choice.number: pk_list = [q.pk for q in Question.objects.all()] next_question_pk = pk_list[pk_list.index(int(question_pk)) + 1] next_choice_number = 0 else: next_question_pk = question_pk next_choice_number = "%d" % (int(choice_number) + 1) kwargs = { 'question_pk': next_question_pk, 'choice_number': next_choice_number } data = { 'title': 'Success', 'message': 'Choice annotated successfully.', 'link': reverse('semantics-choice-annotate', kwargs=kwargs), 'linkdes': 'Annotate the next choice.' } return render(request, 'result.html', data) data = { 'title': 'Failed', 'message': "%r" % (invalid_lines), 'linkdes': 'Go back and upload the tree again.' } return render(request, 'result.html', data)
def post(self, request, question_pk, sentence_index): question = Question.objects.get(pk=question_pk) sentence = Sentence.objects.get(question=question, index=sentence_index) form = SentenceParseForm(request.POST) if form.is_valid(): lines = form.cleaned_data['parses'].lstrip().rstrip().split('\r\n') valid = True invalid_lines = [] for index, line in enumerate(lines): line = line.rstrip().lstrip() """ if not is_valid_annotation(line): valid = False invalid_lines.append(line) """ if valid: for sentence_annotation in sentence.semantic_parses.all(): sentence_annotation.delete() for index, line in enumerate(lines): sentence_annotation = SentenceAnnotation(sentence=sentence, number=index, parse=line) sentence_annotation.save() if len(question.sentences.all()) - 1 == sentence.index: if QuestionTag.objects.filter(word="unannotated").exists( ) and QuestionTag.objects.filter( word="annotated").exists(): unannotated = QuestionTag.objects.get( word="unannotated") annotated = QuestionTag.objects.get(word="annotated") question.tags.remove(unannotated) question.tags.add(annotated) question.save() return redirect(request.POST['next']) data = { 'title': 'Failed', 'message': "%r" % (invalid_lines), 'linkdes': 'Go back and upload the tree again.' } return render(request, 'result.html', data)
def post(self, request, question_pk, sentence_index): question = Question.objects.get(pk=question_pk) sentence = Sentence.objects.get(question=question, index=sentence_index) form = SentenceParseForm(request.POST) if form.is_valid(): lines = form.cleaned_data['parses'].lstrip().rstrip().split('\r\n') valid = True invalid_lines = [] for index, line in enumerate(lines): line = line.rstrip().lstrip() """ if not is_valid_annotation(line): valid = False invalid_lines.append(line) """ if valid: for sentence_annotation in sentence.semantic_parses.all(): sentence_annotation.delete() for index, line in enumerate(lines): sentence_annotation = SentenceAnnotation(sentence=sentence, number=index, parse=line) sentence_annotation.save() if len(question.sentences.all()) - 1 == sentence.index: if QuestionTag.objects.filter(word="unannotated").exists() and QuestionTag.objects.filter(word="annotated").exists(): unannotated = QuestionTag.objects.get(word="unannotated") annotated = QuestionTag.objects.get(word="annotated") question.tags.remove(unannotated) question.tags.add(annotated) question.save() return redirect(request.POST['next']) data = {'title': 'Failed', 'message': "%r" % (invalid_lines), 'linkdes': 'Go back and upload the tree again.'} return render(request, 'result.html', data)
def post(self, request, question_pk, choice_number): question = Question.objects.get(pk=question_pk) choice = Sentence.objects.get(question=question, number=choice_number) form = SentenceParseForm(request.POST) if form.is_valid(): lines = form.cleaned_data['text'].split('\r\n') valid = True if len(lines) > 1: valid = False if valid: line = lines[0] line = line.rstrip().lstrip() if ChoiceAnnotation.objects.filter(choice=choice).exists(): choice_annotation = ChoiceAnnotation.objects.get(choice=choice) choice_annotation.text = line else: choice_annotation = ChoiceAnnotation(choice=choice, text=line) choice_annotation.save() if len(question.choices.all()) - 1 == choice.number: pk_list = [q.pk for q in Question.objects.all()] next_question_pk = pk_list[pk_list.index(int(question_pk)) + 1] next_choice_number = 0 else: next_question_pk = question_pk next_choice_number = "%d" % (int(choice_number) + 1) kwargs = {'question_pk': next_question_pk, 'choice_number': next_choice_number} data = {'title': 'Success', 'message': 'Choice annotated successfully.', 'link': reverse('semantics-choice-annotate', kwargs=kwargs), 'linkdes': 'Annotate the next choice.'} return render(request, 'result.html', data) data = {'title': 'Failed', 'message': "%r" % (invalid_lines), 'linkdes': 'Go back and upload the tree again.'} return render(request, 'result.html', data)
def get(self, request, question_pk, sentence_index): question = Question.objects.get(pk=question_pk) sentence = Sentence.objects.get(question=question, index=sentence_index) if SentenceAnnotation.objects.filter(sentence=sentence).exists(): annotations = "\n".join( sp.parse for sp in SentenceAnnotation.objects.filter(sentence=sentence)) else: annotations = "" form = SentenceParseForm(initial={'parses': annotations}) data = { 'sentence': sentence, 'form': form, 'next': request.META['HTTP_REFERER'] } return render(request, 'semantics/sentenceparse_annotate.html', data)