示例#1
0
    def get(self, request):
        try:
            document = Sentence.objects.get(id=request.GET.get("document"))
        except:
            raise ValueError("Invalid or missing 'document' value passed in the query string.")
        qs = Annotation.objects.filter(document=document)
        return {
			"total": qs.count(),
			"rows": Annotation.as_list(qs=qs, user=request.user)
		}
示例#2
0
    def get(self, request):
        try:
            document = Sentence.objects.get(id=request.GET.get("document"))
        except:
            raise ValueError("Invalid or missing 'document' value passed in the query string.")
        qs = Annotation.objects.filter(document=document)
        return {
			"total": qs.count(),
			"rows": Annotation.as_list(qs=qs, user=request.user)
		}
示例#3
0
def load_annotations(annotator):
    annotation_df = pd.read_excel('data/all_clinical_human_target_' +
                                  annotator.first_name + '.xlsx')

    column_to_index = dict([(c, i + 1)
                            for i, c in enumerate(annotation_df.columns)])

    for record in annotation_df.itertuples():
        mdr = record[column_to_index['mdr1']]
        if mdr == '<none>':
            mdr = record[column_to_index['mdr2']]

        annotation = Annotation(
            key=Drug.objects.get(
                key=record[column_to_index['Drug.Key..Unique.ID.']]),
            cui=record[column_to_index['cui']],
            mdr1=mdr,
        )
        annotation.save()
    print("Loaded {} annotations".format(len(annotation_df)))
示例#4
0
def handle_upload(request, doc_id):
    if not request.user.is_authenticated():
        raise PermissionDenied(
            "You do not have permission to perform this action.")
    page = request.POST['next']
    has_headers = request.POST['has_headers']
    start = 1 if has_headers else 0
    # try:
    t = [i.strip('\r\n') for i in request.FILES['InputFile'].readlines()]
    # f = codecs.open('/home/elmira/heritage_corpus/tempfiles/request.txt', 'w')
    # prevsent = 0
    # wordn = 1
    for ln in range(start, len(t)):
        sent, word, wordnum, _, _, tag, corr, annotator = t[ln].split('\t')
        sent = int(sent)
        # if word == ' ':
        #     continue
        # if sent == prevsent:
        #     wordn += 1
        # else:
        #     prevsent = sent
        #     wordn = 1
        if tag:
            tag1 = [i.strip() for i in tag.split(',')]
            # f.write(str(len(word.decode('utf-8'))))
            # f.write('\r\n')
            sent = Sentence.objects.get(pk=sent)
            owner = User.objects.get(username=annotator)
            annot = Annotation(owner=owner,
                               document=sent,
                               guid=str(uuid.uuid4()),
                               data=json.dumps({
                                   "ranges": [{
                                       "start":
                                       "/span[" + wordnum + "]",
                                       "end":
                                       "/span[" + wordnum + "]",
                                       "startOffset":
                                       0,
                                       "endOffset":
                                       len(word.decode('utf-8'))
                                   }],
                                   "quote":
                                   word,
                                   "text":
                                   "",
                                   "tags":
                                   tag1,
                                   "corrs":
                                   corr
                               }))
            annot.tag = tag
            annot.start, annot.end = int(wordnum), int(wordnum)
            annot.save()

    a = False
    # f.close()
    # except:
    #     a = True
    return redirect(page, alert=a)
示例#5
0
def handle_upload(request, doc_id):
    if not request.user.is_authenticated():
        raise PermissionDenied("You do not have permission to perform this action.")
    page = request.POST['next']
    has_headers = request.POST['has_headers']
    start = 1 if has_headers else 0
    # try:
    t = [i.strip('\r\n') for i in request.FILES['InputFile'].readlines()]
    # f = codecs.open('/home/elmira/heritage_corpus/tempfiles/request.txt', 'w')
    # prevsent = 0
    # wordn = 1
    for ln in range(start, len(t)):
        sent, word, wordnum, _, _, tag, corr, annotator = t[ln].split('\t')
        sent = int(sent)
        # if word == ' ':
        #     continue
        # if sent == prevsent:
        #     wordn += 1
        # else:
        #     prevsent = sent
        #     wordn = 1
        if tag:
            tag1 = [i.strip() for i in tag.split(',')]
            # f.write(str(len(word.decode('utf-8'))))
            # f.write('\r\n')
            sent = Sentence.objects.get(pk=sent)
            owner = User.objects.get(username=annotator)
            annot = Annotation(owner=owner, document=sent, guid=str(uuid.uuid4()),
                                             data=json.dumps({"ranges": [{"start": "/span["+wordnum+"]", "end": "/span["+wordnum+"]", "startOffset": 0, "endOffset": len(word.decode('utf-8'))}], "quote": word, "text": "", "tags": tag1, "corrs": corr}))
            annot.tag = tag
            annot.start, annot.end = int(wordnum), int(wordnum)
            annot.save()

    a = False
    # f.close()
    # except:
    #     a = True
    return redirect(page, alert=a)
def handle_upload(request, doc_id):
    if not request.user.is_authenticated():
        raise PermissionDenied(
            "You do not have permission to perform this action.")
    page = request.POST['next']
    has_headers = request.POST['has_headers']
    start = 1 if has_headers else 0
    t = [i.strip('\r\n') for i in request.FILES['InputFile'].readlines()]
    for ln in range(start, len(t)):
        sent, word, wordnum, _, _, tag, corr, annotator = t[ln].split('\t')
        sent = int(sent)
        if tag:
            tag1 = [i.strip() for i in tag.split(',')]
            sent = Sentence.objects.get(pk=sent)
            owner = User.objects.get(username=annotator)
            annot = Annotation(owner=owner,
                               document=sent,
                               guid=str(uuid.uuid4()),
                               data=json.dumps({
                                   "ranges": [{
                                       "start":
                                       "/span[" + wordnum + "]",
                                       "end":
                                       "/span[" + wordnum + "]",
                                       "startOffset":
                                       0,
                                       "endOffset":
                                       len(word.decode('utf-8'))
                                   }],
                                   "quote":
                                   word,
                                   "text":
                                   "",
                                   "tags":
                                   tag1,
                                   "corrs":
                                   corr
                               }))
            annot.tag = tag
            annot.start, annot.end = int(wordnum), int(wordnum)
            annot.save()

    a = False
    return redirect(page, alert=a)
示例#7
0
 def post(self, request, client_data):
     # create. Creates an annotation object and returns a 303.
     obj = Annotation()
     obj.owner = request.user if request.user.is_authenticated() else None
     try:
         obj.document = Sentence.objects.get(id=client_data.get("document"))
     except:
         raise ValueError("Invalid or missing 'document' value passed in annotation data.")
     obj.set_guid()
     obj.data = "{ }"
     obj.update_from_json(client_data)
     obj.save()
     return obj.as_json(request.user)  # Spec wants redirect but warns of browser bugs, so return the object.
示例#8
0
 def get(self, request):
     # index. Returns ALL annotation objects. Seems kind of not scalable.
     return Annotation.as_list()
示例#9
0
 def post(self, request, client_data):
     # create. Creates an annotation object and returns a 303.
     obj = Annotation()
     obj.owner = request.user if request.user.is_authenticated() else None
     try:
         # print 'get sent'
         obj.document = Sentence.objects.get(id=client_data.get("document"))
     except:
         # print 'bad'
         raise ValueError(
             "Invalid or missing 'document' value passed in annotation data."
         )
     # print 'get guid'
     obj.set_guid()
     # print 'get data'
     obj.data = "{ }"
     # print 'upd'
     obj.update_from_json(client_data)
     # print 'save'
     obj.save()
     # print 'return'
     return obj.as_json(
         request.user
     )  # Spec wants redirect but warns of browser bugs, so return the object.
示例#10
0
 def get(self, request):
     # index. Returns ALL annotation objects. Seems kind of not scalable.
     return Annotation.as_list()
示例#11
0
    def post(self, request, format=None):
        """Creates a new task"""
        required_fields = [
            'question_id', 'answer_id', 'annotation_url', 'task_type', 'phrase'
        ]
        if not all(param in request.data for param in required_fields):
            errorMsg = {'Error': "Input Error", 'Message': "Missing fields"}
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        taskType = int(request.data['task_type'])

        if not 0 <= taskType <= 2:
            errorMsg = {
                'Error': "Input Error",
                'Message': "Task Type not in range (0-2)"
            }
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        # create a new annotation with data
        newAnnotation = Annotation()
        newAnnotation.question_id = request.data['question_id']
        newAnnotation.answer_id = request.data['answer_id']
        newAnnotation.phrase = request.data['phrase']
        newAnnotation.save()

        # append and shorten url
        appended_url = request.data['annotation_url'] + "/" \
                                                      + str(newAnnotation.id) \
                                                      + "?taskType=" \
                                                      + str(taskType)

        post_url_with_key = settings.POST_URLSHORTENER_GOOGLE_URL +\
                            "?key=" + settings.GOOGLE_URL_SHORTENER_KEY
        post_header = {'Content-Type': 'application/json'}
        google_response = requests.post(post_url_with_key,
                                        data=json.dumps(
                                            {'longUrl': appended_url}),
                                        headers=post_header).json()
        if 'id' not in google_response:
            errorMsg = {
                'Error': "Google URL Shortener Error",
                'Google Response': google_response.pop('error')
            }
            newAnnotation.delete()
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        shortened_url = google_response['id']
        message = self.create_message(str(request.data['phrase'][:6] + ".."),
                                      taskType, shortened_url)

        # post twitter message
        auth = OAuth1(settings.TWITTER_CONSUMER_KEY,
                      settings.TWITTER_CONSUMER_SECRET,
                      settings.TWITTER_ACCESS_TOKEN,
                      settings.TWITTER_ACCESS_TOKEN_SECRET)
        twitter_response = requests.post(settings.POST_STATUS_TWITTER_URL,
                                         data={'status': message},
                                         auth=auth)
        tweet_info = twitter_response.json()
        if 'id' not in tweet_info:
            errorMsg = {
                'Error': "Twitter Error",
                'Twitter Response': tweet_info.pop('errors')
            }
            newAnnotation.delete()
            return Response(errorMsg, status=status.HTTP_400_BAD_REQUEST)

        # create a new task
        task = Task()
        task.tweet_id = tweet_info['id']
        task.annotation_id = newAnnotation.id
        task.task_type = taskType
        task.created_on = task.checked_on = timezone.now()
        task.save()

        return Response(TaskSerializer(task).data,
                        status=status.HTTP_201_CREATED)