Implement a worker for write-math.com.

def worker():
    """Implement a worker for write-math.com."""
    global n
    global use_segmenter_flag
    if request.method == 'POST':
        raw_data_json = request.form['classify']
        try:
            secret_uuid = request.form['secret']
        except:
            logging.info("No secret uuid given. Create one.")
            secret_uuid = str(uuid.uuid4())

        # Check recording
        try:
            json.loads(raw_data_json)
        except ValueError:
            return "Invalid JSON string: %s" % raw_data_json

        # Classify
        if use_segmenter_flag:
            strokelist = json.loads(raw_data_json)
            beam = utils.get_beam(secret_uuid)

            if beam is None:
                beam = se.Beam()
                for stroke in strokelist:
                    beam.add_stroke(stroke)
                results = beam.get_results()
                utils.store_beam(beam, secret_uuid)
            else:
                stroke = strokelist[-1]
                beam.add_stroke(stroke)
                results = beam.get_results()
                utils.store_beam(beam, secret_uuid)
        else:
            results = classify.classify_segmented_recording(raw_data_json)
        return get_json_result(results, n=n)
    else:
        # Page where the user can enter a recording
        return "Classification Worker (Version %s)" % hwrt.__version__