在完成 Extraction Rules 活动的配置后,我们可以添加业务规则来对字段值进行校验和规范化。
首先,我们要调整“Doctor ID”字段的属性。Extraction Rules 活动使用的是预识别结果,这些结果中可能包含 OCR 错误和不必要的字符。在找到字段区域后,我们可以通过在字段属性中指定正则表达式来进一步调整字段值。系统将重新提取数据,并消除 OCR 错误。
- 点击技能名称,然后转到 Fields 选项卡。
- 展开“Doctor”组,并通过单击其旁边的设置图标打开“Doctor ID”字段设置。
- 在字段属性的 Value 部分,单击 Regular expression 选项旁边的添加图标。
- 将以下表达式粘贴到 Regular Expression Editor 中:
[0-9]{1}[\/.-]{1}[0-9]{5}[\/.-]{1}[0-9]{2}[\/.-]{1}[0-9]{3}
此编辑器使用的通用表示法与 Extraction Rules Activity 编辑器中使用的表示法不同。有关更多信息,请在 Regular Expression Editor 中单击 Syntax help。
- 单击 Save 关闭 Regular Expression Editor,然后再次单击 Save 关闭字段属性。
患者应在首次生病后的 3 天内拿到病假条。这意味着开具日期必须不晚于疾病开始日期后的第 3 天。
要检查此条件,我们需要设置一个脚本规则。
- 点击数据表单下方的添加 icon。会打开 New Rule 对话框。
如果看不到添加 icon,请切换到文档图像上方的 Reference 部分。
- 选择 Advanced Script Rule 并点击 Next。
- 将规则重命名为“检查有效性”。
- 在字段列表中选择 “Date”、“Start Date” 和 “End Date” 字段。
- 点击 Next。
- 将以下脚本粘贴到脚本编辑器中:
// 创建变量以访问所有需要的字段
var dateField = Context.GetField("Date");
var startField = Context.GetField("Start Date");
var endField = Context.GetField("End Date");
var issueDate = dateField.Value;
var startDate = startField.Value;
//检查文档中是否找到了 "Start Date" 字段
if (startDate && issueDate)
{
//检查开具日期是否不超过开始日期后的 3 天
if ((issueDate.getTime() - startDate.getTime()) / 3600000 / 24 > 3)
{
Context.CheckSucceeded = false;
Context.ErrorMessage = "病假单签发时间过晚";
}
}
- 单击 Save,然后查看该规则在集合中不同文档上的运行效果。要检查该规则如何处理错误,可以在数据表单中的字段中手动输入测试值。每次更改字段值时,该规则都会被重新应用。
我们将使用病假开始和结束日期来计算病假时长。如果文档中指定了时长,我们将检查其是否与计算出的时长一致。如果文档中未包含时长,我们将把计算出的值写入相应的字段中。
- 点击数据表单下方的添加图标以创建规则。
- 选择 Advanced Script Rule 并点击 Next。
- 将规则重命名为“Check duration”。
- 在字段列表中选择“Start Date”、“End Date”和“Duration”字段。务必在两列中都选中这些字段,因为我们不仅要读取字段值,还要在必要时对其进行更正。
- 点击 Next。
- 在脚本编辑器中粘贴以下脚本:
// Create variables for all the fields you're going to access
var startField = Context.GetField("Start Date") ;
var endField = Context.GetField("End Date");
var durationField = Context.GetField("Duration");
var startDate = startField.Value;
var endDate = endField.Value;
//Check if the "Start Date" and "End Date" fields were found on the document
if (endField && endDate && startField && startDate)
{
//Calculate the sick leave duration
var length = (1 + (endDate.getTime() - startDate.getTime()) / 3600000 / 24);
//If the duration field was not found or could not be parsed as a number, pass the calculated value to the field
if (!durationField.Value)
durationField.Value = length;
//If the duration field was found, compare its value with the calculated duration
else if (durationField.Value != length)
{
Context.CheckSucceeded = false;
Context.ErrorMessage = "\"Duration\"字段的值与实际病假持续时间不匹配";
durationField.AddSuggestion(length.toString());
}
}
- 单击 Save,并检查该规则在集合中不同文档上的表现。若要检查规则如何处理错误,您可以在数据表单的字段中手动输入测试值。每次更改字段值时,该规则都会自动重新执行。