def docCompare(request, document_id, annotator_id1, annotator_id2, error=None): annotator1 = Annotator.objects.get(id=annotator_id1) annotator2 = Annotator.objects.get(id=annotator_id2) doc1 = Document.objects.get(id=document_id) annotations1 = Annotation.objects.filter(document=doc1, annotator__id=annotator_id1) annotations2 = Annotation.objects.filter(document=doc1, annotator__id=annotator_id2) doc2 = Document.objects.get(id=document_id) doc1.text = util.htmlFormat(doc1.text, annotations1) doc2.text = util.htmlFormat(doc2.text, annotations2) doc1.title = doc2.title = util.htmlFormat(doc1.title, []) annotation_types = filter( lambda x: x.type, AnnotationType.objects.filter(project=doc1.project)) c = RequestContext( request, { 'doc1': doc1, 'doc2': doc2, 'annotation_types': annotation_types, 'annotator1': annotator1, 'annotator2': annotator2, 'error_message': error, }) t = loader.get_template('dj/docCompare.html') return HttpResponse(t.render(c))
def docCompare(request, document_id, annotator_id1, annotator_id2, error=None): annotator1=Annotator.objects.get(id=annotator_id1) annotator2=Annotator.objects.get(id=annotator_id2) doc1=Document.objects.get(id=document_id) annotations1=Annotation.objects.filter(document=doc1,annotator__id=annotator_id1) annotations2=Annotation.objects.filter(document=doc1,annotator__id=annotator_id2) doc2=Document.objects.get(id=document_id) doc1.text=util.htmlFormat(doc1.text,annotations1) doc2.text=util.htmlFormat(doc2.text,annotations2) doc1.title=doc2.title=util.htmlFormat(doc1.title,[]) annotation_types=filter(lambda x: x.type,AnnotationType.objects.filter(project=doc1.project)) c = RequestContext(request, { 'doc1': doc1, 'doc2': doc2, 'annotation_types':annotation_types, 'annotator1':annotator1, 'annotator2':annotator2, 'error_message': error,}) t = loader.get_template('dj/docCompare.html') return HttpResponse(t.render(c))
def documentByAnnotator(request, document_id, annotator_id=None, error=None): if annotator_id and request.user.is_superuser: annotator = Annotator.objects.filter(id=annotator_id)[0] else: annotator = Annotator.objects.filter(id=request.user.id)[0] doc = Document.objects.filter(id=document_id)[0] annotations=Annotation.objects.filter(document=doc, annotator=annotator) annotation_types=AnnotationType.objects.filter(project=doc.project) t = loader.get_template('dj/doc.html') doc.text=util.htmlFormat(doc.text, annotations) doc.title=util.htmlFormat(doc.title, []) submissions=Submission.objects.filter(document=doc, annotator=annotator) if submissions: doc.submit_date=submissions[0].submit_date else: doc.submit_date=None c = RequestContext(request, { 'doc': doc, 'annotation_types':annotation_types, 'annotator':request.user, 'error_message': error,}) return HttpResponse(t.render(c))
def newAnnotation(request): #import pdb #pdb.set_trace() document_id=int(request.POST['document_id']) text=request.POST['newNec'] annotation_id=int(request.POST['newNecCategoryId']) if annotation_id == 'Delete': return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) if not text: return documentByAnnotator(request, document_id, annotator_id=request.user.id, error="Please select text to add new anntations") annotator=Annotator.objects.get(id=request.user.id) annotation_type=AnnotationType.objects.get(id=int(annotation_id)) document=Document.objects.get(id=document_id) annotations = Annotation.objects.filter(document=document, annotator=annotator) indices=util.findIndices(document.text, text) for ind in indices: if Annotation.objects.filter(document=document, begin_index=ind[0], end_index=ind[1], annotator=annotator): continue begin=ind[0] end=ind[1] allNamedEntities=Annotation.objects.filter(document=document, annotator=annotator) foundOverlap = False for absNE in allNamedEntities: if util.overlap((absNE.begin_index, absNE.end_index), (ind[0],ind[1])): foundOverlap = True #absNE.delete() # blah! # don't erase any existing entities if foundOverlap: continue #add new one annotation = Annotation(document=document, annotation=text, begin_index=begin, end_index=end, annotation_type=annotation_type, annotator=annotator) annotation.save() annotations = Annotation.objects.filter(document=document, annotator=annotator) text=util.htmlFormat(document.text, annotations) #return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) return HttpResponse(text)
def updateAnnotation(request): document_id=request.POST['document_id'] indices=request.POST['indices'] annotation_type_id=request.POST['category_id'] document=Document.objects.get(id=document_id) annotation_id=indices.split('_')[1] annotation=Annotation.objects.get(id=annotation_id) annotator = annotation.annotator if annotation_type_id == 'Delete': annotation.delete() else: annotation_type=AnnotationType.objects.get(id=annotation_type_id) annotation.annotation_type=annotation_type annotation.save() #return HttpResponse('success: %s %s %s %s %s'%(dir(annotation), document_id, indices, annotation.begin_index, annotation)) annotations = Annotation.objects.filter(document=document, annotator=annotator) text=util.htmlFormat(document.text, annotations) #return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) return HttpResponse(text)
def updateAnnotation(request): document_id = request.POST['document_id'] indices = request.POST['indices'] annotation_type_id = request.POST['category_id'] document = Document.objects.get(id=document_id) annotation_id = indices.split('_')[1] annotation = Annotation.objects.get(id=annotation_id) annotator = annotation.annotator if annotation_type_id == 'Delete': annotation.delete() else: annotation_type = AnnotationType.objects.get(id=annotation_type_id) annotation.annotation_type = annotation_type annotation.save() #return HttpResponse('success: %s %s %s %s %s'%(dir(annotation), document_id, indices, annotation.begin_index, annotation)) annotations = Annotation.objects.filter(document=document, annotator=annotator) text = util.htmlFormat(document.text, annotations) #return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) return HttpResponse(text)
def newAnnotation(request): #import pdb #pdb.set_trace() document_id = int(request.POST['document_id']) text = request.POST['newNec'] annotation_id = int(request.POST['newNecCategoryId']) if annotation_id == 'Delete': return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) if not text: return documentByAnnotator( request, document_id, annotator_id=request.user.id, error="Please select text to add new anntations") annotator = Annotator.objects.get(id=request.user.id) annotation_type = AnnotationType.objects.get(id=int(annotation_id)) document = Document.objects.get(id=document_id) annotations = Annotation.objects.filter(document=document, annotator=annotator) indices = util.findIndices(document.text, text) for ind in indices: if Annotation.objects.filter(document=document, begin_index=ind[0], end_index=ind[1], annotator=annotator): continue begin = ind[0] end = ind[1] allNamedEntities = Annotation.objects.filter(document=document, annotator=annotator) foundOverlap = False for absNE in allNamedEntities: if util.overlap((absNE.begin_index, absNE.end_index), (ind[0], ind[1])): foundOverlap = True #absNE.delete() # blah! # don't erase any existing entities if foundOverlap: continue #add new one annotation = Annotation(document=document, annotation=text, begin_index=begin, end_index=end, annotation_type=annotation_type, annotator=annotator) annotation.save() annotations = Annotation.objects.filter(document=document, annotator=annotator) text = util.htmlFormat(document.text, annotations) #return documentByAnnotator(request, document_id, annotator_id=request.user.id, error=None) return HttpResponse(text)