Abbreviate

So I have the expression rule that will abbreviate a string. Thoughts on how to improve?

 

/*Replace all special characters with spaces*/
/*Replace extra spaces with since space*/
/*Wrap numbers in spaces*/
/*Split words and number sets into array*/
/*ForEach item in array grab number or first letter of word*/
/*Join what is left, upper case string and grab left 50 characters*/
with(
  local!nospecialcharacters: regexreplaceall(
    "[^\w\s]",
    ri!in,
    " "
  ),
  local!extraspaceswithsinglespace: regexreplaceall(
    "\s+",
    local!nospecialcharacters,
    " "
  ),
local!numberwrapper: 
  if(regexmatch("\d+",local!extraspaceswithsinglespace),
  
  regexinsertmatchmarkers(
    "\d+",
    local!extraspaceswithsinglespace,
    " ",
    " ",
    false
  ),
  local!extraspaceswithsinglespace
  
  ),
  local!segments: split(
    regexreplaceall(
      "\s+",
      local!numberwrapper,
      "|"
    ),
    "|"
  ),
  local!keepers: a!forEach(
    local!segments,
    if(
      isnull(
        fv!item
      ),
      null,
      if(
        not(
          isnull(
            tointeger(
              fv!item
            )
          )
        ),
        fv!item,
        if(
          fv!item = " ",
          null,
          if(
            regexmatch("^[a-zA-Z]+$",
              fv!item
            ),
            left(
              fv!item,
              1
            ),
            null
          )
        )
      )
    )
  ),
  left(
    upper(
      joinarray(
        local!keepers,
        ""
      )
    ),
    50
  )
)

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    Hi Rick,

    Try the code below.
    I tested it with few variations seems to be covering all, let me know if you see any difference or you may try changing if you see a missed scenario.

    Performance wise I used a string - "This is a 1 re45al big te8xt to be abbre3viat88ed     and can 77 have perf67ormance i !!ssue" and I think Regex is taking 10+ ms in best run and  the code snippet i used is doing in <1 ms.

    Also on performance improvement, I would suggest use apply and not forEach. We have noticed 5 times of performance degradation when you loop 100+ items. Since in this case we are not looping extensively it might not be noticeable easily. For the above input if you turn on the For each (commented) you will see it goes 2+ ms.

     

     

    load(
      /*place spaces around all numeric values in the string*/
      local!formattedString:reduce(
        fn!substitute,
        lower(ri!in),
        merge(
          enumerate(10),
          {" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "}
        )
      ),
      /*break the above string to arraym, separated by space  */
      local!words: split(
        cleanwith(
          local!formattedString,
          /*your allowed character list  */
          "abcdefghijklmnopqrstuvwxyz0123456789 "
        ),
        char(32)
      ),
      /*abbreviate */
      upper(
        joinarray(
          /*a!forEach(*/
            /*local!words,*/
            /*charat(fv!item,1)*/
          /*)*/
          apply(
            charat(_,1),
            local!words
          )
        )    
      )
    )

Reply
  • +1
    Certified Lead Developer

    Hi Rick,

    Try the code below.
    I tested it with few variations seems to be covering all, let me know if you see any difference or you may try changing if you see a missed scenario.

    Performance wise I used a string - "This is a 1 re45al big te8xt to be abbre3viat88ed     and can 77 have perf67ormance i !!ssue" and I think Regex is taking 10+ ms in best run and  the code snippet i used is doing in <1 ms.

    Also on performance improvement, I would suggest use apply and not forEach. We have noticed 5 times of performance degradation when you loop 100+ items. Since in this case we are not looping extensively it might not be noticeable easily. For the above input if you turn on the For each (commented) you will see it goes 2+ ms.

     

     

    load(
      /*place spaces around all numeric values in the string*/
      local!formattedString:reduce(
        fn!substitute,
        lower(ri!in),
        merge(
          enumerate(10),
          {" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "}
        )
      ),
      /*break the above string to arraym, separated by space  */
      local!words: split(
        cleanwith(
          local!formattedString,
          /*your allowed character list  */
          "abcdefghijklmnopqrstuvwxyz0123456789 "
        ),
        char(32)
      ),
      /*abbreviate */
      upper(
        joinarray(
          /*a!forEach(*/
            /*local!words,*/
            /*charat(fv!item,1)*/
          /*)*/
          apply(
            charat(_,1),
            local!words
          )
        )    
      )
    )

Children
No Data