# </segments> ### for _tasks_file in args.tasks_file: tasks_xml_string = None with open(_tasks_file) as infile: tasks_xml_string = unicode(infile.read(), "utf-8") _tree = fromstring(tasks_xml_string.encode("utf-8")) for _child in _tree: segment_id = _child.attrib["id"] source_language = _child.attrib["source-language"] target_language = _child.attrib["target-language"] system_id = _child.findall("system-id")[0].text reference_text = _child.findall("reference")[0].text for candidate in _child.findall("candidate"): candidate_text = candidate.text print candidate_text.encode('utf-8') new_meta = MetaData() new_meta.save() new_task = AbsoluteScoringTask() new_task.segment_id = segment_id new_task.source_language = source_language new_task.target_language = target_language new_task.system_id = system_id new_task.reference = reference_text new_task.candidate = candidate_text new_task.save()
def scoring_handler(request): """ Renders a random AbsoluteScoringTask for testing. """ if request.method == "POST": try: score = int(request.POST['score']) if score != -1: user = request.user task_id = request.POST['task_id'] task = AbsoluteScoringTask.objects.get(id=task_id) # Create new AbsoluteScoringData object scoring_data = AbsoluteScoringData() scoring_data.user = request.user scoring_data.task = task scoring_data.score = score scoring_data.save() scoring_data.metadata.completed = True scoring_data.metadata.end_time = datetime.now() scoring_data.metadata.save() except: print format_exc() pass # Check if there are one or more orphaned MetaData records for this user. # If so, these should be annotated first to make sure we get results :) try: incomplete_metadata = request.user.metadata_set.filter(completed=False) latest_unfinished_metadata = list(incomplete_metadata)[0] except: latest_unfinished_metadata = None # Otherwise, get a random, unassigned AbsoluteScoringTask for annotation. if not latest_unfinished_metadata: open_tasks = AbsoluteScoringTask.objects.filter(metadata__isnull=True) scoring_task_ids = list(open_tasks.values_list('id', flat=True)) shuffle(scoring_task_ids) # We have an orphaned MetaData record, so we process the respetive task. else: related_task = latest_unfinished_metadata.absolutescoringtask_set.get() scoring_task_ids = [related_task.id] try: current_task = AbsoluteScoringTask.objects.get(id=scoring_task_ids[0]) except: current_task = None if not current_task: return HttpResponse("No scoring task available yet...") if current_task.metadata: current_meta = current_task.metadata else: current_meta = MetaData() current_meta.save() current_meta.users.add(request.user) current_meta.assigned = datetime.now() current_task.metadata = current_meta current_task.save() dictionary = { 'action_url': request.path, 'task_id': current_task.id, 'segment_id': current_task.segment_id, 'language_pair': '{0}→{1}'.format(current_task.source_language, current_task.target_language), 'reference_text': current_task.reference, 'candidate_text': current_task.candidate, 'title': 'Scoring' } dictionary.update(BASE_CONTEXT) return render(request, 'beta16/scoring.html', dictionary)