Ejemplo n.º 1
0
def list():
    uid = session["user_id"]
    user = User.objects(id=uid).first()
    segs = []
    for i, sid in enumerate(user.segs):
        seg = Seg.objects(sid=sid).first()
        utt = []
        flag_seg = False
        for pos in seg.utt:
            utt.append(pos["word"])
            if pos.get("flags", {}).get(uid, False):
                flag_seg = True
        utt = " ".join(utt)
        segs.append(dict(i=i + 1, sid=seg.sid, oral=seg.oral, utt=utt,
                         flag_seg=flag_seg))
    total = 0
    batches = []
    for batch, batch_size in enumerate(user.batches):
        # all batches except the last one are by definition done
        batches.append(dict(batch=batch + 1, assigned=batch_size,
                            done=batch_size, remaining=0))
        total += batch_size
    # correct stats for the last batch
    penalty = 1 if user.assigned else 0
    remaining = total - len(segs) + penalty
    done = user.batches[-1] - remaining
    batches[-1].update(done=done, remaining=remaining)
    return render_template("list.html", segs=segs, batches=batches)
Ejemplo n.º 2
0
 def post(self, sid):
     request.get_data()
     utt = request.json
     uid = session["user_id"]
     user = User.objects(id=uid).first()
     seg = Seg.objects(sid=sid).first()
     if uid not in seg.users:
         abort(403, messages=[["danger", SegSid.edit_err]])
     if not len(seg["utt"]) == len(utt):
         abort(400, messages=[["danger", SegSid.len_err.format(sid)]])
     for i, dbpos, postpos in zip(range(1, len(seg["utt"]) + 1), seg["utt"], utt):
         if not postpos.get("lemma", False):
             abort(400, messages=[["warning", SegSid.miss_l_err.format(i)]])
         if not postpos.get("tag", False):
             abort(400, messages=[["warning", SegSid.miss_t_err.format(i)]])
         if dbpos["word"] == postpos["word"]:
             # only save flag if it's True (it might be present, but set to
             # False)
             if postpos.get("flag"):
                 dbpos.setdefault("flags", {})[uid] = True
                 # only save note if flag was True (a note might be present
                 # along with a False flag)
                 if "note" in postpos:
                     dbpos.setdefault("notes", {})[uid] = postpos["note"]
             else:
                 # the flag may also have been removed after having been
                 # added in an earlier POST; make sure it's gone by deleting
                 # it
                 dbpos.setdefault("flags", {}).pop(uid, None)
                 dbpos.setdefault("notes", {}).pop(uid, None)
             if "pool" not in dbpos:
                 continue
             elif postpos["lemma"] in dbpos["pool"]:
                 dbpos.setdefault("lemmas", {})[uid] = postpos["lemma"]
             else:
                 abort(400, messages=[["danger", SegSid.err]])
             if postpos["tag"] in dbpos["pool"].get(postpos["lemma"], {}):
                 dbpos.setdefault("tags", {})[uid] = postpos["tag"]
             else:
                 abort(400, messages=[["danger", SegSid.err]])
         else:
             abort(400, messages=[["danger", SegSid.word_err.format(postpos["word"], dbpos["word"], i, sid)]])
     seg.modify(utt=seg["utt"])
     # only remove segment assignment if the user is currently posting their
     # most recently asssigned segment, which means they're ready to be
     # assigned a new one (otherwise they're just re-editing a segment from
     # their history, in which case we want them to keep the segment they
     # have been assigned)
     if user.assigned == seg.sid:
         user.modify(assigned=None)
     return {}
Ejemplo n.º 3
0
 def get(self, sid):
     seg = Seg.objects(sid=sid).first()
     if seg is None:
         abort(400, messages=[["danger", SegSid.seg_err.format(sid)]])
     seg = seg.to_mongo()
     uid = session["user_id"]
     for pos in seg["utt"]:
         if pos.get("lemmas", None):
             pos["lemma"] = pos["lemmas"].get(uid, None)
         if pos.get("tags", None):
             pos["tag"] = pos["tags"].get(uid, None)
         pos["flag"] = pos.get("flags", {}).get(uid, None)
         pos["note"] = pos.get("notes", {}).get(uid, None)
     return seg
Ejemplo n.º 4
0
 def get(self):
     max_done = app.config["MAX_DISAMB_PASSES"]
     uid = session["user_id"]
     user = User.objects(id=uid).first()
     if user.assigned:
         seg = Seg.objects(sid=user.assigned).first().to_mongo()
         return seg
     # if user.assigned == "", then the user has completed all segs in
     # user.segs and might be done with their batch
     elif len(user.segs) >= sum(user.batches):
         abort(404, messages=[["success", SegAssign.done]])
     else:
         # assign the already rated (i.e. the most rated → order_by) segs
         # first
         seg = Seg.objects(ambiguous=True, users_size__lt=max_done, users__nin=[uid]).order_by("-users_size").first()
         try:
             users = set(seg.users + [uid])
             users_size = len(users)
             seg.modify(users=users, users_size=users_size)
             user.modify(assigned=seg.sid, add_to_set__segs=seg.sid)
         except AttributeError:
             # seg is None
             abort(400, messages=[["danger", SegAssign.noseg_err]])
         return seg.to_mongo()