Contextual matching (CONTEXT and APPLY) is an efficient way to specify a set of contextual keywords which when present, determines whether the GLASS engine reports or ignores a potential match.
For example, instruct the GLASS engine to only report
matches for
ACME Corporation's customer ID
if related keywords such as customer
or client
are found within a certain
range of the customer ID match.
When applying contextual matching in a GLASS data type, you need to define one or more context rules that:
Specify where the GLASS engine should search for the contextual keywords:
Before (BEFORE), after (AFTER), or around (AROUND) the potential match.
Instruct the GLASS engine to
(i) Report the match if at least one context keyword from the MAP appears within a certain proximity of the potential match (REQUIRE), or (ii) Ignore a match if any context keyword from the MAP appears within a certain proximity of the potential match (DENY).
(Optional) Specify the range (distance) within which the context keywords must appear from the match.
By default, the GLASS engine searches for contextual keywords within 64 bytes of the potential match.
The generic GLASS syntax for CONTEXT and APPLY is:
MAP [NOCASE|DECOMPOSE] '<namespace>' [NOCASE|DECOMPOSE] '<key 1>', [NOCASE|DECOMPOSE] '<key 2>', ..., [NOCASE|DECOMPOSE] '<key N>'
CONTEXT '<context namespace>' [BEFORE|AFTER|AROUND] [REQUIRE|DENY] '<namespace>' [WITHIN <integer> <range>]
CONTEXT '<context namespace>' [BEFORE|AFTER|AROUND] [REQUIRE|DENY] '<namespace>' [WITHIN <integer> <range>]
<search pattern> APPLY '<context namespace>'
You can define contextual matching rules that apply to a specific expression, or the whole custom GLASS data type pattern.
ACME Corporation wants to search for any occurrence of regional customer IDs in its storage systems with the following additional requirements:
cust id
, custid
, customer
client
cliente
kunde
고객
Based on the requirements, you can define a custom GLASS data type as below:
# Namespace defining the invalid serial number.
MAP 'INVALID_SERIAL_NUM' 0
# Namespace containing all accepted ccTLD values.
MAP NOCASE 'ACME_CUST_ID_CCTLD' 'AU', 'IE', 'KR', 'SG', 'UK', 'US'
# Namespace containing all contextual keywords.
MAP NOCASE 'ACME_CUST_ID_CONTEXTS' 'cust id', 'custid', 'customer', 'client', 'cliente', 'kunde', '고객'
# Context rules for custom data type.
CONTEXT 'ACME_CUST_ID_CONTEXT' BEFORE REQUIRE 'ACME_CUST_ID_CONTEXTS'
( \
( \
( \
GROUP 'ACME_CUST_ID_CCTLD' THEN \
(RANGE DIGIT TIMES 9 EXCLUDE 'INVALID_SERIAL_NUM') THEN \
RANGE DIGIT \
) CHECK 'PASSPORTMOD10' \
) BOUND NONALNUM \
) APPLY 'ACME_CUST_ID_CONTEXT'
Using the GLASS expression above, Line 1, Line 2 and Line 3 will be returned as match locations by the GLASS pattern matching engine.
1 | …the customer ID is AU1122334453… |
2 | Client number: IE9988776655 |
3 | 고객 | KR0000135798 |
4 | The customer ID is …<more than 64 bytes between the contextual keyword and expected match>… IE9988776655 |
The regional customer ID number in Line 4, while valid, is not returned as a
match location as the context keyword (e.g. customer
) is more than 64 bytes
distance from the ID number.
The equivalent configuration in GLASS Studio Visual Builder mode is:
# Namespace defining a set of "happy" colors.
MAP 'HAPPY_COLORS' 'Red', 'Green', 'Rainbow'
# Namespace defining a set of "sad" colors.
MAP 'SAD_COLORS' 'Black', 'White'
# First context rule for custom data type. One or more "happy" colors must be present within 64 bytes for a potential match to be reported.
CONTEXT 'CONTEXT_COLORS' BEFORE REQUIRE 'HAPPY_COLORS'
# Second context rule for custom data type. If one or more "sad" colors are present within 64 bytes, the match will be rejected.
CONTEXT 'CONTEXT_COLORS' AROUND DENY 'SAD_COLORS'
WORD 'Flag' APPLY 'CONTEXT_COLORS'
Using the GLASS expression above, Line 1 will be returned as a match location by the GLASS pattern matching engine.
1 | Red Flag |
2 | <adding filler text to ensure Line 1 and Line 3 are more than 64 bytes apart> |
3 | White Flag |
4 | Rainbow Flag Black |
The match in Line 3 is rejected as a DENY
context (White
) is present.
Even though a REQUIRE context (Rainbow
) is
present in Line 4, the match is not reported as a
DENY context (Black
) is found within 64 bytes
after the potential match.
The equivalent configuration in GLASS Studio Visual Builder mode is:
To add a CONTEXT (and APPLY) rule to the data type in GLASS Studio Visual Builder Mode:
Select the type of CONTEXT rule to apply.
Require (REQUIRE) -
Report the match if at least one context keyword from the
MAP appears within a certain proximity of
the potential match.
Deny (DENY) -
Ignore a match if any context keyword from the
MAP appears within a certain proximity of
the potential match.
Define where the GLASS engine should search for the contextual keywords relative to the potential match.
Before (BEFORE) - Search for
contextual keywords before a potential match.
After (AFTER)- Search for
contextual keywords after a potential match.
Around (AROUND) - Search for
contextual keywords before or after a potential match.
Create new map
Enter a Map Name and the keys associated with the
MAP. Click on Save Map.
See Adding MAP Namespaces for more information.
Choose an existing map
Select from the list of existing MAP
namespaces.
32
) within which the context
keyword(s) must be found from the potential match.To view all the CONTEXT rules that have been defined for the GLASS Studio data type project, click on the Context icon in the GLASS Studio playground to open the CONTEXT dialog.
All independent CONTEXT rules are displayed in the Rules Applied pane with the following details:
You can modify the type, position, and/or select a new MAP namespace to be associated with a CONTEXT rule.
To edit a CONTEXT rule:
32
) within which the context
keyword(s) must be found from the potential match.To remove a CONTEXT rule: