Beispiel #1
0
 def __init__(self, hit_id=None, parent=None):
     tree.Node.__init__(self, parent=parent)
     self.hit = HIT(id=hit_id) if hit_id is not None else None
     self.parent = parent
Beispiel #2
0
class TranscribePageAttempt(tree.Node):
    def __init__(self, hit_id=None, parent=None):
        tree.Node.__init__(self, parent=parent)
        self.hit = HIT(id=hit_id) if hit_id is not None else None
        self.parent = parent

    @property
    def hit_id(self):
        if self.hit is not None:
            return self.hit.id

    def create_mturk_request(self, layout_id):
        request = mturk.Request(
            layout_id=layout_id,
            title="Transcribe hand-written note (<250 words)",
            description="Transcribe hand-written medical chart note (<250 words)",
            keywords="write, transcribe, english, medical, handwriting",
            reward=HIT_PRICE,
            lifetime=timedelta(days=7),
            duration=timedelta(minutes=20),
            approval_delay=timedelta(days=1),
        )
        request.layout_params["file_url"] = self.parent.page_url
        return request

    def submit(self, layout_id):
        request = self.create_mturk_request(layout_id)
        self.hit = request.submit()

    @property
    def assignment(self):
        if len(self.hit.assignments) > 0:
            return self.hit.assignments[-1]

    def review(self):
        self.hit.refresh()
        if self.assignment and self.assignment.status == "Submitted":
            if self.assignment.answers_dict["validation_code"] != self.parent.validation_code:
                logger.info("Validation code for HIT {0} is incorrect. Approving anyway.".format(self.hit.id))
            self.assignment.approve()
            self.hit.refresh()

    @property
    def status(self):
        if len(self.hit.assignments):
            return self.hit.assignments[0].status
        else:
            return self.hit.status

    def to_dict(self):
        d = OrderedDict()
        d["hit_id"] = self.hit.id
        d["status"] = self.status
        return d

    @classmethod
    def from_dict(cls, d):
        return cls(hit_id=d["hit_id"])

    @property
    def name(self):
        name = self.assignment.answers_dict["name"].upper().strip()
        re_missing = re.compile(".*no.*(date|name).*", re.IGNORECASE)
        re_laura = re.compile("laura.*figoski", re.IGNORECASE)
        if re_missing.match(name) or re_laura.match(name):
            return None
        else:
            return re.sub("[^A-Z]", "_", name)

    @property
    def date(self):
        date = self.assignment.answers_dict["date"].upper().strip()
        re_missing = re.compile(".*no.*(date|name).*", re.IGNORECASE)
        return None if re_missing.match(date) else re.sub("[^\w]", "_", date).strip()

    @property
    def validation_code(self):
        return self.answers_dict["validation_code"].upper().strip()

    @property
    def note(self):
        note = self.assignment.answers_dict["note"]
        note = re.sub(r"\n\s*\-[\s\-]*", "\n- ", "\n" + note)  # Normalize leading dashes
        note = note.replace("\r\n", "\n")
        return note

    @property
    def validation_code(self):
        return self.assignment.answers_dict["validation_code"]