Expressions Cookbook (Concept): Difference between revisions
Dgreenwood (talk | contribs) No edit summary |
|||
| Line 17: | Line 17: | ||
* <code>Guid.NewGuid</code> | * <code>Guid.NewGuid</code> | ||
Inspecting portions of link path for original file (path, filename, metadata) | Inspecting portions of link path for original file (path, filename, metadata) attached to a '''Batch Folder''' | ||
* These examples extract information (full path & filename, filename, path, extension) from a | * These examples extract information (full path & filename, filename, path, extension) from a '''Batch Folder's''' content link | ||
** <code>Link.FullPath</code> | ** <code>Link.FullPath</code> | ||
** | *** Returns the attached file's path, including the file's name. | ||
** <code>Link.Path</code> | ** <code>Link.Path</code> | ||
** <code>IO.Path.GetFileNameWithoutExtension(Link. | *** Returns the attached file's path, not including the file's name. | ||
** <code>IO.Path.GetExtension(Link. | ** <code>Link.ObjectName</code> | ||
*** Returns the attached file's name. | |||
** <code>Link.LinkName</code> | |||
*** Returns the name of the link (e.g. "Import" or "Export") | |||
** <code>IO.Path.GetFileNameWithoutExtension(Link.ObjectName)</code> | |||
*** Returns the attached file's name without the extension. | |||
** <code>IO.Path.GetExtension(Link.ObjectName)</code> | |||
* These examples extract specific path segments (drive letter, first folder name) from a batch folder's content link | * These examples extract specific path segments (drive letter, first folder name) from a batch folder's content link | ||
** <code>Link.PathSegments(0)</code> | ** <code>Link.PathSegments(0)</code> | ||
*** Returns the first segment of the file path (e.g. "C:" in "C:\Documents\Howdy.pdf"). | |||
** <code>Link.PathSegments(1)</code> | ** <code>Link.PathSegments(1)</code> | ||
*** Returns the second segment of the file path (e.g. "Documents" in "C:\Documents\Howdy.pdf"). | |||
''Note: If you have removed the document link with the '''Execute > Remove Link''' command, you can still access the attached file's name using the expression'' <code>Folder.AttachmentFileName</code> | |||
Populating fields with specific values (i.e. strings, numbers, dates) | Populating fields with specific values (i.e. strings, numbers, dates) | ||
Revision as of 14:03, 8 December 2022
Expressions are snippets of .NET code, allowing Grooper to do various things outside its "normal" parameters. This includes calculating or validating extracted Data Field values in a Data Model, applying conditional execution of a Batch Process or IP Profile, and more! This article collects examples of common (and maybe not so common) uses of expressions in Grooper.
| 😎 | Special thanks to BIS team member Brian Godwin for contributing this article! |
Data Model Expressions
Default Value Expressions
Current date
Now
New Guid (Globally Unique IDentifier)
Guid.NewGuid
Inspecting portions of link path for original file (path, filename, metadata) attached to a Batch Folder
- These examples extract information (full path & filename, filename, path, extension) from a Batch Folder's content link
Link.FullPath- Returns the attached file's path, including the file's name.
Link.Path- Returns the attached file's path, not including the file's name.
Link.ObjectName- Returns the attached file's name.
Link.LinkName- Returns the name of the link (e.g. "Import" or "Export")
IO.Path.GetFileNameWithoutExtension(Link.ObjectName)- Returns the attached file's name without the extension.
IO.Path.GetExtension(Link.ObjectName)
- These examples extract specific path segments (drive letter, first folder name) from a batch folder's content link
Link.PathSegments(0)- Returns the first segment of the file path (e.g. "C:" in "C:\Documents\Howdy.pdf").
Link.PathSegments(1)- Returns the second segment of the file path (e.g. "Documents" in "C:\Documents\Howdy.pdf").
Note: If you have removed the document link with the Execute > Remove Link command, you can still access the attached file's name using the expression Folder.AttachmentFileName
Populating fields with specific values (i.e. strings, numbers, dates)
"Hello world!"123.45DateAdd("d", 30, Now)Now.ToString("yyyyMMddhhmmss")
Populating fields with the name of the document's classified Document Type
ContentTypeName- Note: This is a shortcut for the expression
Folder.ContentType.DisplayName
Populating fields with the name of the document's classified Document Type's Content Category.
Folder.ContentType.ParentNode.DisplayName- Note: When using Content Categories, Document Types are added as child nodes to the Content Category object in the node tree. So, this expression first uses
Folderto select the document folder in the Batch. Then,ContentTypeinspects the document's classified Document Type. The Document Type is a child of the Content Category in this example. Since the Content Category is the Document Type's parent object, theParentNodejumps up the node tree hierarchy to inspect the parent Content Category. Last,DisplayNamereturns the Content Category's name.
Calculate Expressions
Addition of multiple fields
IntegerField1 + IntegerField2DecimalField1 + DecimalField2 + DecimalField3
Concatenation of multiple fields
String.Concat(StringField1, StringField2)String.Concat(StringField1, StringField2, StringField3)String.Concat(StringField1, StringField2, StringField3, StringField4)
Rounding
- This example rounds a decimal value to a precision of 4 digits (e.g. 2.34567891 to 2.3457)
Math.Round(DecimalField1, 4)
Non-integer addition (e.g. of date values)
- These examples increment a date by 30 days ("d"), 1 year ("yyyy"), and the last decrements the date by 3 months ("m")
DateAdd("d", 30, DateField1)DateAdd("yyyy", 1, DateField1)DateAdd("m", -3, DateField1)
Reformatting / Normalization of values
- This example replaces any backslashes with underscores
StringField1.Replace("\", "_")
- This example removes any backslashes
StringField1.Replace("\", "")
Substring calculation
- These examples extract information contained within a string "
ABC123456XXXX654321YYY" by designating the 0-based starting index and desired number of characters- ABC (first 3 characters):
StringField1.Substring(0, 3) - 123456 (6 characters within the string):
StringField1.Substring(3, 6) - XXXX (4 characters within the string):
StringField1.Substring(9, 4) - YYY (last 3 characters):
StringField1.Substring(StringField1.Length - 3)
- ABC (first 3 characters):
Getting the location coordinates of a field on the document
- This could be used to determine the coordinates and size of an extracted value on a document.
GetFieldInstance("Field Name").Location.ToString- Note, this returns logical location in inches, not pixels. So additional work would need to be performed to convert this to pixels if needed.
Validate Expressions
Date in past / future
- This example ensures the date value is a past date
DateField1 < Now
- This example ensures the date value is at least 30 days in the future
DateField1 >= DateAdd("d", 30, Now)
Equality / inequality of two fields (multiple options)
StringField1 = StringField2IntegerField1.Equals(IntegerField2)IntegerField1 <> DecimalField1Not DecimalField1.Equals(DecimalField2)
Summing fields and comparing to another field
IntegerField1 + IntegerField2 = IntegerField3DecimalField1 + DecimalField2 = DecimalField3DecimalField1 = SumFieldInstance("Table1\AmountColumn")
Running regular expression against field
Text.RegularExpressions.Regex.IsMatch(StringField1, "[0-9]{6}")
Inspecting field-level confidence scores
Instance.Confidence > 0.8
Batch Processing Expressions
Should Submit Expression
Inspecting flagged status
- These examples would submit the task when the object (i.e. folder, page) is flagged or not flagged (2nd example)
Item.FlaggedNot Item.Flagged
- This example would submit the task when the object (folder) contains one or more flagged pages
DirectCast(Item, BatchFolder).FlaggedPages.Any()
Inspecting flagged message
Item.FlagReason = "Needs classification"Item.FlagReason <> "Bypass review"
Inspecting presence of local copy in Grooper
DirectCast(Item, BatchFolder).HasLocalCopy
Inspecting existence of native version
DirectCast(Item, BatchFolder).HasAttachment
Inspecting MIME type
- This example would submit the task when the object's (folder) represents a native PDF or the second if its mime type is PDF
DirectCast(Item, BatchFolder).IsNativePDFDirectCast(Item, BatchFolder).AttachmentMimeType = "application/pdf"
Inspecting content type / parent content category
DirectCast(Item, BatchFolder).ContentTypeName = "MyContentType"DirectCast(DirectCast(Item, BatchFolder).ContentType.ParentNode, ContentCategory).Name = "MyContentCategory"
Inspecting if a field is blank / populated
DirectCast(Item, BatchFolder).IndexData.Fields("StringField1").Value <> ""Not String.IsNullOrEmpty(DirectCast(Item, BatchFolder).IndexData.Fields("StringField1").Value)
Inspecting image properties (resolution, color mode, aspect ratio, size (in bytes), pixel count, etc.)
DirectCast(Item, BatchPage).PrimaryImage.ResolutionX < 240DirectCast(Item, BatchPage).PrimaryImage.IsBinaryDirectCast(Item, BatchPage).PrimaryImage.IsColorDirectCast(Item, BatchPage).PrimaryImage.IsLandscapeDirectCast(Item, BatchPage).PrimaryImage.AspectRatio > 1.25DirectCast(Item, BatchPage).PrimaryImage.Size > 40960DirectCast(Item, BatchPage).PrimaryImage.PixelCount > 3500000
Inspecting presence of layout data (of a certain type: lines, OMR boxes, etc.)
DirectCast(Item, BatchFolder).HasLayoutData
Does page / document have OCR text?
DirectCast(Item, BatchFolder).HasRuntimeOCRDirectCast(Item, BatchPage).HasRuntimeOCR
Inspecting classification candidates and classification scores, incl. alternate candidate scores
Next Step Expressions
Inspecting batch creator
If(Batch.CreatedBy.ToLower() = "domain\jusername", TrueStepName, FalseStepName)If(Batch.CreatedByDisplayName = "Joe Username", TrueStepName, FalseStepName)
Inspecting creation time (range, day of week)
If(DatePart(DateInterval.Month, Batch.Created) = 6, TrueStepName, FalseStepName)If(DatePart(DateInterval.Day, Batch.Created) > 15, TrueStepName, FalseStepName)
IP Profile Expressions
IP Command Should Execute Expressions
Inspecting image properties (resolution, color mode, aspect ratio, size, pixel count, etc.)
Image.ResolutionX < 240Image.IsBinaryImage.IsColorImage.IsLandscapeImage.AspectRatio > 1.25Image.Size > 40960Image.PixelCount > 3500000
Inspecting presence of layout data (of a certain type: lines, OMR boxes, etc.)
Results.Line_Detection.HorizontalLines.Any()Results.Line_Detection.VerticalLines.Any()Results.Box_Detection.Boxes.Any()Results.Patch_Code_Detection.PatchCodes.Any()
Decisioning based on image classification (Results.ClassifyImage.whatever)
Results.Classify_Image.ClassName = "Sample 1"
Accessing and inspecting results log of prior IP commands
Results.Measure_Entropy.Entropy > 0.85
Inspecting whether prior commands modified image(s)
ResultList.IsImageSourceImage
Mapping Expressions
Import Mapping Expressions
Value concatenation
String.Concat(field1, field2)String.Concat(field1, " ", field2)
Value padding (adding or removing)
- These examples show how to left-pad a value with zeroes for 20 characters, right-pad a value with spaces for 40 characters, and finally trim a padded value of spaces.
field1.PadLeft(20, "0"c)field2.PadRight(40)field3.Trim()
Adding environment variables (date, user, etc.)
NowEnvironment.MachineNameEnvironment.UserNameEnvironment.UserDomainNameEnvironment.OSVersionEnvironment.ProcessorCount
Export Mapping Expressions
Addition of multiple fields
IntegerField1 + IntegerField2DecimalField1 + DecimalField2 + DecimalField3
Concatenation of multiple fields
String.Concat(StringField1, StringField2)String.Concat(StringField2, ", ", StringField1, ": ", StringField3)
How to access Grooper attributes (content type name, GUID, index data, etc.)
CurrentDocument.ContentTypeNameCurrentDocument.IdCurrentDocument.IndexData.Sections("Section1").Fields("Field1").ValueCurrentDocument.IndexData.Sections("Section1").Sections("SectionA").Fields("Field1A").ValueCurrentDocument.IndexData.Tables("Table1").Rows.First().Cells("Column1").Value
Naming based on original file name
IO.Path.GetFileNameWithoutExtension(CurrentDocument.ContentLink.Name)
Converting a date field to a string in a "year-month-day" format
DateField.ToString("yyyy-MM-dd")
General
| WIP | This section is a work-in-progress. It needs to be expanded for completeness. |
Understanding how to traverse hierarchy of, e.g. batch or content model
Understanding how to parse tables by row & column
Identifying Sections by instance number
How to inspect properties of node
Dynamic referencing vs. GUID referencing
Conditional expressions with IIF / IF
Using LINQ in Expressions
Direct Casting: when to (Cast)