Help parsing ocr to form fields

Viewing 9 reply threads
  • Author
    Posts
  • January 29, 2024 at 5:24 PM #50414

    Simon Spiers
    Participant
    I’ve been using Tap Forms for a few years as a digital log book of patients. The primary purpose is to capture billing details. Prompted by watching Sam’s excellent video on OCR Receipt Extraction. I’d like to have a script to auto populate fields with the OCR from a scan of the patient’s ID label.
    .
    My current workflow is to scan a patient’s hospital label with my iPhone, transfer it to the photo field in a new TF record before manually entering data from the scanned image. It’s the digital equivalent of sticking a label in a book in order to annotate it with other data like start and finish times, items, etc.
    .
    What I’d like to do is scan the label with my iPhone (I use Scanner Pro which does a pretty reliable OCR), create a new record in TF that contains the image in a photo field and the OCR’d text in a notes field, then use a form script to automatically populate fields in the TF form. Because the data is sensitive I want to keep it encrypted, only on my own devices and synced using near field only.
    .
    Let me show an example.
    .
    The same fictional patient on different hospital labels gives OCR text:
    .
    HEELER, MR Bandit
    MRN: OT000123456
    DOB: 04/09/1978 Age: 44 (M)
    10 CATTLEDOG ROAD
    BETOOTA HEIGHTS 4483
    Home Ph: 0491 577 426
    MC: 2123456791
    S/Net No:
    DVA:
    BETOOTA PRIVATE
    Adm No
    CT04589023
    Adm Date: 12/01/24
    13:48
    Att Dr: Fixem, Ayecan S
    Ref Dr: Payne, Ophelia M
    Fund: HFHHF
    50006000
    MC Exp: 05/2028 MC Ref: 1
    Pen No:
    0038708Y
    AD: 12/01/2024
    Adm No. 4589023
    HEELER, MR Bandit
    10 Cattledog Rd
    Betoota Heights, QLD 4483
    PH: 0491577426 M: 0491577426
    MC: 2123456791/1 Exp:05/2028
    CL: PI HHF/T 50006000
    PC NIL
    AD: 12/01/2024
    FIXEM, DR AYECAN
    UR: 123456
    Adm No. 4589023
    DOB: 04/09/1978 (44Y)
    MAR: M
    Gender: Male
    BET 0038708Y
    .
    The bits of data I need are first name and surname, DOB, MC, MC Ref, fund code and fund number (‘HHF’ and 50006000 in the example).
    .
    These would map to TF fields surname (text), first name (text), DOB (DD/MM/yyyy), MC (number), MC ref (number), fund code (link from form) and fund number (text; sometimes contains letters) in a single TF form called ‘Cases’.
    .
    The stickers are rectangular and in landscape orientation, so they have information in two columns. The lines of OCR text aren’t always in the same order, e.g. sometimes the admission number or MRN appear between the address lines. The identifier snippets are always present and predictable, though. For instance, fund code is always found after ‘Fund:’ or ‘CL: PI’ which will always be on the same line.
    .
    Sam’s script for his fuel receipts is awesome. I want to do something like it, but I also want to know how it works so I can fix it and build on it. I’m new to scripting and I’m trying to get simple things to work before adding more complexity. Not having much success though. Here’s my attempt:
    .
    function mc() {
    var mc_id = ‘fld-abbabb14632f41718ffb4a259cebe63a’;
    var label_text_id = ‘fld-d80875c6c53048c8bcf97b11e190118d’;
    var input = record.getFieldValue(label_text_id);
    var mcRegex = /[2-6]{1}[0-9]{9}/;
    var found = input.match(mcRegex);
    if (record.getFieldValue(mc_id)) {
    return;
    }
    record.setFieldValue(mc_id, found);
    console.log(found);
    document.saveAllChanges
    }
    mc();
    .
    It finds mc in the notes field text but doesn’t populate the MC field.
    .
    Can anyone help me with this?
    .
    I have a couple of related questions after watching Sam’s video:
    .
    1. How can I create a new record in TF on iOS that contains both the scanned image (jpg) and OCR text string of the scan in a notes field? I’m able to get one piece of information in easily using Shortcuts/x-callback, but two?
    2. How can I create a link from form using scripting? I’d like to extract the fund code (‘HHF’ in the example; Fund:\s?HF?(\D{3})|CL: PI\s?(\D{3})) then link from the HHF record in the ‘Funds’ form.
    January 30, 2024 at 10:08 PM #50418

    Brendan
    Keymaster

    Hi Simon,

    You’re missing the () at the end of document.saveAllChanges;. It should be document.saveAllChanges(); That would prevent the MC field from being saved. Does the if statement work properly for you? Just thought it would be more clear if you were checking if the value was empty or not instead of just calling the function.

    For example:

    if (record.getFieldValue(mc_id) != undefined) {
    return;
    }

    I’m assuming that you’re doing that check because you don’t want to populate the mc_id field if it already has a value.

    1. Might have to ask Sam about that as he’s got way more experience than I do with shortcuts.

    2. You can create a related record using the record.addRecordToField(someRecord, field_id); function.

    Thanks,

    Brendan

    January 31, 2024 at 6:03 PM #50421

    Simon Spiers
    Participant

    Hi Brendan,

    Thanks for the reply and suggestions. Yes, the if statement is to check if the field already has a value and skip the process if so.

    I’ve updated the script but it’s still not populating the form fields:

    function mc() {
    
    var note_id = 'fld-9adb85030f8a42b3aeea043ea5885d05';
    
    var mc_id = 'fld-77a5e9f11f4947caa6135bf312030965';
    
    var input = record.getFieldValue(note_id);
    
    var mcRegex = /[2-6]{1}[0-9]{9}/;
    
    var found = input.match(mcRegex);
    
    if (record.getFieldValue(mc_id) != undefined) {
    
    return;
    
    }
    
    record.setFieldValue(mc_id, found);
    
    document.saveAllChanges();
    
    }
    
    mc();

     

    It seems that the issue is with the record.setFieldValue(mc_id, found); line. I get the correct value if I return found to the console.

    Thanks for looking at it.

    Simon

    • This reply was modified 2 months, 4 weeks ago by Simon Spiers.
    January 31, 2024 at 9:12 PM #50423

    Daniel Leu
    Participant

    Maybe it’s a type issue. Usually Javascript is not very type agnostic. Is MC defined as a number? Does a text field work instead?

    January 31, 2024 at 9:45 PM #50424

    Simon Spiers
    Participant

    No, doesn’t work with text or number field

    February 1, 2024 at 12:21 AM #50426

    Daniel Leu
    Participant

    Can you share your document?

    February 1, 2024 at 1:17 AM #50427

    Simon Spiers
    Participant

    Here it is

    Attachments:
    You must be logged in to view attached files.
    February 1, 2024 at 9:38 AM #50429

    Daniel Leu
    Participant

    I was on the correct track that there’s a type issue. The match() method returns an array. So you can just grab the first element or you might want to additionally check that there’s only one element (found.length ==1):

    record.setFieldValue(mc_id, found[0]);

    Do you have the link to Sam’s parser script? Thanks!

    • This reply was modified 2 months, 4 weeks ago by Daniel Leu.
    February 1, 2024 at 4:44 PM #50438

    Simon Spiers
    Participant

    Thanks Daniel. That makes sense and it now works!

    Sam didn’t provide the script, I saw some of it in his video. It seems complex and suited to his own particular use. I’m hoping he’ll weigh in here.

    February 1, 2024 at 5:14 PM #50439

    Daniel Leu
    Participant

    Great that you got it working, Simon. Okay, so I was it in his video!

     

Viewing 9 reply threads

You must be logged in to reply to this topic.