Part of the data cleanup problem is the possible presence of profanity or cartoon character names in the data. This can become an issue whenever you are working with data that originated from direct input by end-users to a website. The following code (contained in the BadWordSearch.ECL file) will detect the presence of any of a set of "bad" words in a given field:
IMPORT std; SetBadWords := ['JUNK', 'GARBAGE', 'CRUD']; BadWordDS := DATASET(SetBadWords,{STRING10 word}); SearchDS := DATASET([{1,'FRED','FLINTSTONE'}, {2,'GEORGE','KRUEGER'}, {3,'CRUDOLA','BAR'}, {4,'JUNKER','KNIGHT'}, {5,'GARBAGEGUY','MANGIA'}, {6,'FREDDY','KRUEGER'}, {7,'TIM','TINY'}, {8,'JOHN','JONES'}, {9,'MIKE','JETSON'}], {UNSIGNED6 ID,STRING10 firstname,STRING10 lastname}); outrec := RECORD SearchDS.ID; SearchDS.firstname; BOOLEAN FoundWord; END; {BOOLEAN Found} FindWord(BadWordDS L, STRING10 inword) := TRANSFORM SELF.Found := Std.Str.Find(inword,TRIM(L.word),1)>0; END; outrec CheckWords(SearchDS L) := TRANSFORM SELF.FoundWord := EXISTS(PROJECT(BadWordDS,FindWord(LEFT,L.firstname))(Found=TRUE)); SELF := L; END; result := PROJECT(SearchDS,CheckWords(LEFT)); OUTPUT(result(FoundWord=TRUE)); OUTPUT(result(FoundWord=FALSE));
This code is a simple PROJECT of each record that you want to search. The result will be a record set containing the record ID field, the firstname search field, and a BOOLEAN FoundWord flag field indicating whether any "bad" word was found.
The search itself is done by a nested PROJECT of the field to be searched against the DATASET of "bad" words. Using the EXISTS function to detect if any records are returned from that PROJECT where the returned Found field is TRUE sets the FoundWord flag field value.
The Std.Str.Find function simply detects the presence anywhere within the search string of any of the "bad" words. The OUTPUT of the set of records where the FoundWord is TRUE allows post-processing to evaluate whether the record is worth keeping or garbage (probably requiring human intervention).
The above code is a specific example of this technique, but it would be much more useful to have a MACRO that accomplishes this task, something like this one (also contained in the BadWordSearch.ECL file):
MAC_FindBadWords(BadWordSet,InFile,IDfld,SeekFld,ResAttr,MatchType=1) := MACRO #UNIQUENAME(BadWordDS) %BadWordDS% := DATASET(BadWordSet,{STRING word{MAXLENGTH(50)}}); #UNIQUENAME(outrec) %outrec% := RECORD InFile.IDfld; InFile.SeekFld; BOOLEAN FoundWord := FALSE; UNSIGNED2 FoundPos := 0; END; #UNIQUENAME(ChkTbl) %ChkTbl% := TABLE(InFile,%outrec%); #UNIQUENAME(FindWord) {BOOLEAN Found,UNSIGNED2 FoundPos} %FindWord%(%BadWordDS% L,INTEGER C,STRING inword) := TRANSFORM #IF(MatchType=1) //"contains" search SELF.Found := Std.Str.Find(inword,TRIM(L.word),1) > 0; #END #IF(MatchType=2) //"exact match" search SELF.Found := inword = L.word; #END #IF(MatchType=3) //"starts with" search SELF.Found := Std.Str.Find(inword,TRIM(L.word),1) = 1; #END SELF.FoundPos := IF(SELF.FOUND=TRUE,C,0); END; #UNIQUENAME(CheckWords) %outrec% %CheckWords%(%ChkTbl% L) := TRANSFORM WordDS := PROJECT(%BadWordDS%,%FindWord%(LEFT,COUNTER,L.SeekFld)); SELF.FoundWord := EXISTS(WordDS(Found=TRUE)); SELF.FoundPos := WordDS(Found=TRUE)[1].FoundPos; SELF := L; END; ResAttr := PROJECT(%ChkTbl%,%CheckWords%(LEFT)); ENDMACRO;
This MACRO does a bit more than the previous example. It begins by passing in:
* The set of words to find
* The file to search
* The unique identifier field for the search record
* The field to search in
* The attribute name of the resulting recordset
* The type of matching to do (defaulting to 1)
Passing in the set of words to seek allows the MACRO to operate against any given set of strings. Specifying the result attribute name allows easy post-processing of the data.
Where this MACRO starts going beyond the previous example is in the MatchType parameter, which allows the MACRO to use the Template Language #IF function to generate three different kinds of searches from the same codebase: a "contains" search (the default), an exact match, and a "starts with" search.
It also has an expanded output RECORD structure that includes a FoundPos field to contain the pointer to the first entry in the passed in set that matched. This allows post processing to detect positional matches within the set so that "matched pairs" of words can be detected, as in this example (also contained in the BadWordSearch.ECL file):
SetCartoonFirstNames := ['GEORGE','FRED', 'FREDDY']; SetCartoonLastNames := ['JETSON','FLINTSTONE','KRUEGER']; MAC_FindBadWords(SetCartoonFirstNames,SearchDS,ID,firstname,Res1,2) MAC_FindBadWords(SetCartoonLastNames,SearchDS,ID,lastname,Res2,2) Cartoons := JOIN(Res1(FoundWord=TRUE), Res2(FoundWord=TRUE), LEFT.ID=RIGHT.ID AND LEFT.FoundPos=RIGHT.FoundPos); MAC_FindBadWords(SetBadWords,SearchDS,ID,firstname,Res3,3) MAC_FindBadWords(SetBadWords,SearchDS,ID,lastname,Res4) SetBadGuys := SET(Cartoons,ID) + SET(Res3(FoundWord=TRUE),ID) + SET(Res4(FoundWord=TRUE),ID); GoodGuys := SearchDS(ID NOT IN SetBadGuys); BadGuys := SearchDS(ID IN SetBadGuys); OUTPUT(BadGuys,NAMED('BadGuys')); OUTPUT(GoodGuys,NAMED('GoodGuys'));
Notice that the position of the cartoon character names in their separate sets defines a single character name to search for in multiple passes. Calling the MACRO twice, searching for the first and last names separately, allows you to post-process their results with a simple inner JOIN where the same record was found in each and, most importantly, the positional values of the matches are the same. This prevents "GEORGE KRUEGER" from being mis-labelled a cartoon character name.