Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with:

Applying IM Text Abbreviations With ColdFusion

By Ben Nadel on
Tags: ColdFusion

Last week, I saw that cellular phone commercial where the little girl talks to her mom using nothing but SMS / IM text message abbreviations. So for instance, instead of saying "It's no big deal", she would say "It's NBG". Or, instead of saying "My best friend, Jill", she would say "My BFF, Jill" (BFF = best friends forever). This got me thinking about applying these text mappings in real time. Assuming that we have a dictionary of word replacements, how could we best go about applying them to an existing block of text?

Imagine that we have this kind of a text mapping structure:

  • <!--- Set up text mappings. --->
  • <cfset objMap = StructNew() />
  • <cfset objMap[ "heart" ] = "<3" />
  • <cfset objMap[ "I haven't a clue" ] = "404" />
  • <cfset objMap[ "Any day now" ] = "ADN" />
  • <cfset objMap[ "As far as I know" ] = "AFAIK" />
  • <cfset objMap[ "Away from keyboard" ] = "AFK" />
  • <cfset objMap[ "As soon as possible" ] = "ASAP" />
  • <cfset objMap[ "Age/sex/location?" ] = "A/S/L?" />
  • <cfset objMap[ "Bye for now" ] = "B4N" />
  • <cfset objMap[ "Back at the keyboard" ] = "BAK" />
  • <cfset objMap[ "Big ass smile" ] = "BAS" />
  • <cfset objMap[ "Be back in a bit" ] = "BBIAB" />
  • <cfset objMap[ "Be back later" ] = "BBL" />
  • <cfset objMap[ "Bye bye now" ] = "BBN" />
  • <cfset objMap[ "Be back soon" ] = "BBS" />
  • <cfset objMap[ "Big evil grin" ] = "BEG" />
  • <cfset objMap[ "Boy friend" ] = "BF" />
  • <cfset objMap[ "Big fucking deal" ] = "BFD" />
  • <cfset objMap[ "Bye for now" ] = "BFN" />
  • <cfset objMap[ "Big grin" ] = "BG" />
  • <cfset objMap[ "Beer in, beer out" ] = "BIBO" />
  • <cfset objMap[ "Blow it out your I/O port" ] = "BIOYIOP" />
  • <cfset objMap[ "Belly laughing" ] = "BL" />
  • <cfset objMap[ "Busting my gut with laughter" ] = "BMGWL" />
  • <cfset objMap[ "Back-of-the-envelope calculation" ] = "BOTEC" />
  • <cfset objMap[ "Be right back" ] = "BRB" />
  • <cfset objMap[ "But then again" ] = "BTA" />
  • <cfset objMap[ "Been there, done that" ] = "BTDT" />
  • <cfset objMap[ "By the way" ] = "BTW" />
  • <cfset objMap[ "Bursting with laughter" ] = "BWL" />
  • <cfset objMap[ "But what the heck do I know" ] = "BWTHDIK" />
  • <cfset objMap[ "Coffee in, coffee out" ] = "CICO" />
  • <cfset objMap[ "Chuckle and grin" ] = "C&G" />
  • <cfset objMap[ "Continued in next post" ] = "CNP" />
  • <cfset objMap[ "Come right back" ] = "CRB" />
  • <cfset objMap[ "Crying real big tears" ] = "CRBT" />
  • <cfset objMap[ "See you" ] = "CU" />
  • <cfset objMap[ "See you later" ] = "CUL" />
  • <cfset objMap[ "See you later" ] = "CUL8ER" />
  • <cfset objMap[ "See ya" ] = "CYA" />
  • <cfset objMap[ "Cover your ass" ] = "CYA" />
  • <cfset objMap[ "See you online" ] = "CYO" />
  • <cfset objMap[ "Doing business as" ] = "DBA" />
  • <cfset objMap[ "Dead link" ] = "DL" />
  • <cfset objMap[ "Don't let the bed bugs bite" ] = "DLTBBB" />
  • <cfset objMap[ "Do I know you?" ] = "DIKU" />
  • <cfset objMap[ "Did I tell you I'm distressed" ] = "DITYID" />
  • <cfset objMap[ "Dirty old man" ] = "DOM" />
  • <cfset objMap[ "Dozing off soon" ] = "DOS" />
  • <cfset objMap[ "Don't quote me on this" ] = "DQMOT" />
  • <cfset objMap[ "Do the right thing" ] = "DTRT" />
  • <cfset objMap[ "Don't write back" ] = "DWB" />
  • <cfset objMap[ "Evil grin" ] = "EG" />
  • <cfset objMap[ "Excuse me for butting in" ] = "EMFBI" />
  • <cfset objMap[ "E-mail message" ] = "EMSG" />
  • <cfset objMap[ "End of message" ] = "EOM" />
  • <cfset objMap[ "End of thread" ] = "EOT" />
  • <cfset objMap[ "Face to face" ] = "F2F" />
  • <cfset objMap[ "Frequently-ask question" ] = "FAQ" />
  • <cfset objMap[ "Fingers crossed" ] = "FC" />
  • <cfset objMap[ "First in, still here" ] = "FISH" />
  • <cfset objMap[ "Far more than you ever wanted to know" ] = "FMTYEWTK" />
  • <cfset objMap[ "Falling off my chair laughing" ] = "FOMCL" />
  • <cfset objMap[ "From the bottom of my heart" ] = "FTBOMH" />
  • <cfset objMap[ "Fucked up beyond all repair or recognition" ] = "FUBAR" />
  • <cfset objMap[ "Fear, Uncertainty, and Doubt" ] = "FUD" />
  • <cfset objMap[ "For what it's worth" ] = "FWIW" />
  • <cfset objMap[ "For your information" ] = "FYI" />
  • <cfset objMap[ "Grin" ] = "G" />
  • <cfset objMap[ "Go ahead" ] = "GA" />
  • <cfset objMap[ "Get a life" ] = "GAL" />
  • <cfset objMap[ "Garbage in, garbage out" ] = "GIGO" />
  • <cfset objMap[ "Grinning, ducking, and running" ] = "GD&R" />
  • <cfset objMap[ "Girlfriend" ] = "GF" />
  • <cfset objMap[ "Gone for now" ] = "GFN" />
  • <cfset objMap[ "Gotta go pee" ] = "GGP" />
  • <cfset objMap[ "Gee, I wish I'd said that" ] = "GIWIST" />
  • <cfset objMap[ "Good luck" ] = "GL" />
  • <cfset objMap[ "Give me a break" ] = "GMAB" />
  • <cfset objMap[ "Great minds think alike" ] = "GMTA" />
  • <cfset objMap[ "Giggling out loud" ] = "GOL" />
  • <cfset objMap[ "Going to read mail" ] = "GTRM" />
  • <cfset objMap[ "Glad to see you" ] = "GTSY" />
  • <cfset objMap[ "Hug and kiss" ] = "H&K" />
  • <cfset objMap[ "Hugs and kisses" ] = "H&Ks" />
  • <cfset objMap[ "Have a good night" ] = "HAGN" />
  • <cfset objMap[ "Have a nice day" ] = "HAND" />
  • <cfset objMap[ "Hanging head in shame" ] = "HHIS" />
  • <cfset objMap[ "How's it going" ] = "HIG" />
  • <cfset objMap[ "Hi there" ] = "HT" />
  • <cfset objMap[ "Hope this helps" ] = "HTH" />
  • <cfset objMap[ "Head up butt" ] = "HUB" />
  • <cfset objMap[ "In any case" ] = "IAC" />
  • <cfset objMap[ "In any event" ] = "IAE" />
  • <cfset objMap[ "I am not a lawyer" ] = "IANAL" />
  • <cfset objMap[ "I see" ] = "IC" />
  • <cfset objMap[ "I gotta pee" ] = "IGP" />
  • <cfset objMap[ "I hate acronyms" ] = "IHA" />
  • <cfset objMap[ "I hear you" ] = "IHU" />
  • <cfset objMap[ "If I remember correctly" ] = "IIRC" />
  • <cfset objMap[ "I love you" ] = "ILU" />
  • <cfset objMap[ "Immediate message" ] = "IM" />
  • <cfset objMap[ "In my considered opinion" ] = "IMCO" />
  • <cfset objMap[ "In my humble opinion" ] = "IMHO" />
  • <cfset objMap[ "In my not so humble opinion" ] = "IMNSHO" />
  • <cfset objMap[ "In my opinion" ] = "IMO" />
  • <cfset objMap[ "I am sorry" ] = "IMS" />
  • <cfset objMap[ "In other words" ] = "IOW" />
  • <cfset objMap[ "I'm posting naked" ] = "IPN" />
  • <cfset objMap[ "In real life" ] = "IRL" />
  • <cfset objMap[ "I think I'm going to be sick" ] = "ITIGBS" />
  • <cfset objMap[ "I will always love you" ] = "IWALU" />
  • <cfset objMap[ "If you see what I mean" ] = "IYSWIM" />
  • <cfset objMap[ "Just for grins" ] = "J4G" />
  • <cfset objMap[ "Just a bunch of disks" ] = "JBOD" />
  • <cfset objMap[ "Just in case" ] = "JIC" />
  • <cfset objMap[ "Just kidding" ] = "JK" />
  • <cfset objMap[ "Just my opinion" ] = "JMO" />
  • <cfset objMap[ "Just to let you know" ] = "JTLYK" />
  • <cfset objMap[ "ok" ] = "k" />
  • <cfset objMap[ "Keep it simple stupid" ] = "KISS" />
  • <cfset objMap[ "Keep in touch" ] = "KIT" />
  • <cfset objMap[ "Kiss on the cheek" ] = "KOTC" />
  • <cfset objMap[ "Kiss on the lips" ] = "KOTL" />
  • <cfset objMap[ "Know what I mean?" ] = "KWIM?" />
  • <cfset objMap[ "Later" ] = "L8R" />
  • <cfset objMap[ "Later gator" ] = "L8R G8R" />
  • <cfset objMap[ "Later dude" ] = "LD" />
  • <cfset objMap[ "Long-distance relationship" ] = "LDR" />
  • <cfset objMap[ "Laughing head off" ] = "LHO" />
  • <cfset objMap[ "Lots and lots of thunderous applause" ] = "LLTA" />
  • <cfset objMap[ "Laughing my ass off" ] = "LMAO" />
  • <cfset objMap[ "Laughing my socks off" ] = "LMSO" />
  • <cfset objMap[ "Laughing out loud" ] = "LOL" />
  • <cfset objMap[ "Little Rubber Feet" ] = "LRF" />
  • <cfset objMap[ "Laughing so hard my belly hurts" ] = "LSHMBH" />
  • <cfset objMap[ "Laugh to myself" ] = "LTM" />
  • <cfset objMap[ "Long time no see" ] = "LTNS" />
  • <cfset objMap[ "Long-term relationship" ] = "LTR" />
  • <cfset objMap[ "Love you like a brother" ] = "LULAB" />
  • <cfset objMap[ "Love you like a sister" ] = "LULAS" />
  • <cfset objMap[ "Love you with all my heart" ] = "LUWAMH" />
  • <cfset objMap[ "Love ya" ] = "LY" />
  • <cfset objMap[ "Love ya forever" ] = "LY4E" />
  • <cfset objMap[ "Male or female" ] = "MorF" />
  • <cfset objMap[ "Member of the same sex" ] = "MOSS" />
  • <cfset objMap[ "Member of the opposite sex" ] = "MOTOS" />
  • <cfset objMap[ "More to follow" ] = "MTF" />
  • <cfset objMap[ "Miss you so much" ] = "MUSM" />
  • <cfset objMap[ "Not a darn thing" ] = "NADT" />
  • <cfset objMap[ "No fucking good" ] = "NFG" />
  • <cfset objMap[ "No fucking way" ] = "NFW" />
  • <cfset objMap[ "Naked in front of computer" ] = "NIFOC" />
  • <cfset objMap[ "No problem" ] = "NP" />
  • <cfset objMap[ "No response necessary" ] = "NRN" />
  • <cfset objMap[ "Oh I see" ] = "OIC" />
  • <cfset objMap[ "Online love" ] = "OLL" />
  • <cfset objMap[ "Oh my God" ] = "OMG" />
  • <cfset objMap[ "Off the floor" ] = "OTF" />
  • <cfset objMap[ "On the other hand" ] = "OTOH" />
  • <cfset objMap[ "Off the top of my head" ] = "OTTOMH" />
  • <cfset objMap[ "Pretty awesome new stuff" ] = "PANS" />
  • <cfset objMap[ "Parents are watching" ] = "PAW" />
  • <cfset objMap[ "Public display of affection" ] = "PDA" />
  • <cfset objMap[ "Problem exists between chair and keyboard" ] = "PEBCAK" />
  • <cfset objMap[ "Problem is between keyboard and chair" ] = "PIBKAC" />
  • <cfset objMap[ "Pain in the ass" ] = "PITA" />
  • <cfset objMap[ "Private message" ] = "PM" />
  • <cfset objMap[ "Pardon me for jumping in but" ] = "PMFJIB" />
  • <cfset objMap[ "Put on a happy face" ] = "POAHF" />
  • <cfset objMap[ "Goodbye" ] = "::POOF::" />
  • <cfset objMap[ "Plain old telephone service" ] = "POTS" />
  • <cfset objMap[ "That stinks!" ] = "PU" />
  • <cfset objMap[ "Cutie" ] = "QT" />
  • <cfset objMap[ "Real life" ] = "RL" />
  • <cfset objMap[ "Rolling on the floor laughing" ] = "ROTFL" />
  • <cfset objMap[ "Rolling on the floor laughing my ass off" ] = "ROTFLMAO" />
  • <cfset objMap[ "Rolling on the floor laughing my ass off while peeing in my pants" ] = "ROTFLMAOWPIMP" />
  • <cfset objMap[ "Rolling on the floor laughing my butt off" ] = "ROTFLMBO" />
  • <cfset objMap[ "Role-playing games" ] = "RPG" />
  • <cfset objMap[ "Real soon now" ] = "RSN" />
  • <cfset objMap[ "Real time" ] = "RT" />
  • <cfset objMap[ "Read the fucking manual" ] = "RTFM" />
  • <cfset objMap[ "What's up" ] = "S^" />
  • <cfset objMap[ "Shit eating grin" ] = "SEG" />
  • <cfset objMap[ "Smiling ear to ear" ] = "SETE" />
  • <cfset objMap[ "Surfer-friendly" ] = "SF" />
  • <cfset objMap[ "Slaps head in disgust" ] = "SHID" />
  • <cfset objMap[ "Situation normal, all fucked up" ] = "SNAFU" />
  • <cfset objMap[ "Significant other" ] = "SO" />
  • <cfset objMap[ "Shit out of luck" ] = "SOL" />
  • <cfset objMap[ "Sick of me yet?" ] = "SOMY" />
  • <cfset objMap[ "Short on time" ] = "SOT" />
  • <cfset objMap[ "Short on time must go" ] = "SOTMG" />
  • <cfset objMap[ "Search the fucking Web" ] = "STFW" />
  • <cfset objMap[ "Search the Web" ] = "STW" />
  • <cfset objMap[ "Shut up" ] = "SU" />
  • <cfset objMap[ "Shut up and kiss me" ] = "SUAKM" />
  • <cfset objMap[ "What's up" ] = "SUP" />
  • <cfset objMap[ "Stupid wild-ass guess" ] = "SWAG" />
  • <cfset objMap[ "Sealed with a kiss" ] = "SWAK" />
  • <cfset objMap[ "Screaming with laughter" ] = "SWL" />
  • <cfset objMap[ "See you soon" ] = "SYS" />
  • <cfset objMap[ "Thanks again" ] = "TA" />
  • <cfset objMap[ "That's all for now" ] = "TAFN" />
  • <cfset objMap[ "There ain't no such thing as a free lunch" ] = "TANSTAAFL" />
  • <cfset objMap[ "Take care of yourself" ] = "TCOY" />
  • <cfset objMap[ "Thread from hell" ] = "TFH" />
  • <cfset objMap[ "Thank God it's Friday" ] = "TGIF" />
  • <cfset objMap[ "Thanks" ] = "THX" />
  • <cfset objMap[ "Thanks in advance" ] = "TIA" />
  • <cfset objMap[ "Tell it like it is" ] = "TILII" />
  • <cfset objMap[ "Talk to you later" ] = "TLK2UL8R" />
  • <cfset objMap[ "Too much information" ] = "TMI" />
  • <cfset objMap[ "Till next time" ] = "TNT" />
  • <cfset objMap[ "Til our paths cross again" ] = "TOPCA" />
  • <cfset objMap[ "Thinking of you" ] = "TOY" />
  • <cfset objMap[ "The powers that be" ] = "TPTB" />
  • <cfset objMap[ "Ta-Ta for now" ] = "TTFN" />
  • <cfset objMap[ "Thought that, too" ] = "TTT" />
  • <cfset objMap[ "Talk to you later" ] = "TTYL" />
  • <cfset objMap[ "Thank you" ] = "TU" />
  • <cfset objMap[ "Thank you" ] = "TY" />
  • <cfset objMap[ "You're a pain in the ass" ] = "UAPITA" />
  • <cfset objMap[ "You're welcome" ] = "UW" />
  • <cfset objMap[ "Very big grin" ] = "VBG" />
  • <cfset objMap[ "Very big shit-eating grin" ] = "VBSEG" />
  • <cfset objMap[ "Wild ass guess" ] = "WAG" />
  • <cfset objMap[ "What are you doing" ] = "WAYD" />
  • <cfset objMap[ "Welcome back" ] = "WB" />
  • <cfset objMap[ "Write back soon" ] = "WBS" />
  • <cfset objMap[ "Who died and left you in charge" ] = "WDALYIC" />
  • <cfset objMap[ "Wicked evil grin" ] = "WEG" />
  • <cfset objMap[ "Works for me" ] = "WFM" />
  • <cfset objMap[ "Wouldn't it be nice if" ] = "WIBNI" />
  • <cfset objMap[ "What the ?" ] = "WT?" />
  • <cfset objMap[ "What the Fuck" ] = "WTF" />
  • <cfset objMap[ "Way to go" ] = "WTG" />
  • <cfset objMap[ "Want to go private" ] = "WTGP?" />
  • <cfset objMap[ "What's up?" ] = "WU?" />
  • <cfset objMap[ "Where are you from" ] = "WUF" />
  • <cfset objMap[ "What you see is what you get" ] = "WYSIWYG" />
  • <cfset objMap[ "You'll be sorry" ] = "YBS" />
  • <cfset objMap[ "You gotta be shitting me!" ] = "YGBSM" />
  • <cfset objMap[ "Your mileage may vary" ] = "YMMV" />
  • <cfset objMap[ "You're welcome" ] = "YW" />

... the most obvious solution was just to loop over the keys in the map and then use ReplaceNoCase() for each of the text mappings. As it turns out, this is actually a very fast solution. Here is my ColdFusion user defined function to make this happen:

  • <cffunction
  • name="ApplyTextMap"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="Applies the given map (name-value structure) to the given text value.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Text"
  • type="string"
  • required="true"
  • hint="The text to which we will be applying the text map."
  • />
  •  
  • <cfargument
  • name="Map"
  • type="struct"
  • required="true"
  • hint="The struct containing the key-value text mappings."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Set up a local copy of the string. --->
  • <cfset LOCAL.Text = ARGUMENTS.Text />
  •  
  •  
  • <!--- Loop over all values in the struct. --->
  • <cfloop
  • item="LOCAL.Key"
  • collection="#ARGUMENTS.Map#">
  •  
  • <!--- Replace the text with it's mapped value. --->
  • <cfset LOCAL.Text = ReplaceNoCase(
  • LOCAL.Text,
  • LOCAL.Key,
  • ARGUMENTS.Map[ LOCAL.Key ],
  • "ALL"
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Return the modified arguments. --->
  • <cfreturn LOCAL.Text />
  • </cffunction>

Then, to run this, I set up a text message that could be something like an IM conversation message or an SMS text message and applied the text map to it:

  • <!--- Create a text message without any abbreviations. --->
  • <cfsavecontent variable="strMessage">
  • Oh my god! I miss you so much. When you finally get back
  • to me, I am gonna be so glad to see you and I'm gonna
  • shower you with lots of hugs and kisses. Love ya forever!
  • </cfsavecontent>
  •  
  • <!--- Apply the text mappings. --->
  • #ApplyTextMap(
  • strMessage,
  • objMap
  • )#

Running the above code, we get the following output:

OMG! I MUSM. When you finally get back to me, I am gonna be so GTSY and I'm gonna shower you with lots of H&Ks. LY4E!

Notice that several of our text abbreviations were applied. The funny thing is that when I ran this at home (I am in the office now), the last abbreviation, "LY4E", didn't happen. Instead, when run at home, all I got was "LY" and "forever". This is because both mappings exist in the text mapping structure. And, since ColdFusion structures have no inherent key order, clearly the mappings are going to be applied in a different order from machine to machine.

Now, the ReplaceNoCase() method worked very fast, but I have found in the past that using Java StringBuffers and the regular expression Pattern Matchers can lead to a performance boost. I didn't really see a need for one here, especially with SUCH a simple algorithm, but I thought I would give it a chance. Here is the same ColdFusion user defined function, but this time, using a much more complicated algorithm:

  • <cffunction
  • name="ApplyTextMap"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="Applies the given map (name-value structure) to the given text value.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Text"
  • type="string"
  • required="true"
  • hint="The text to which we will be applying the text map."
  • />
  •  
  • <cfargument
  • name="Map"
  • type="struct"
  • required="true"
  • hint="The struct containing the key-value text mappings."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Set up a local copy of the string. --->
  • <cfset LOCAL.Text = ARGUMENTS.Text />
  •  
  •  
  • <!---
  • We are going to make repeated use of several Java
  • objects. Therefore, let's create the static classes
  • here that we can then use later for construction.
  • NOTICE: We are not calling INIT as we only want the
  • static version of the Java classes.
  • --->
  • <cfset LOCAL.StringBufferClass = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ) />
  •  
  • <!--- Regular expressoin pattern class. --->
  • <cfset LOCAL.PatternClass = CreateObject(
  • "java",
  • "java.util.regex.Pattern"
  • ) />
  •  
  •  
  • <!---
  • We are going to use a regular expression iterator
  • to loop over all the values in string and perform the
  • replace. No matter what, though, we have to loop over
  • all values in the struct.
  • --->
  • <cfloop
  • item="LOCAL.Key"
  • collection="#ARGUMENTS.Map#">
  •  
  • <!---
  • We need to prepare the key for our search. Even
  • though we are using the regular expression pattern
  • matcher, we don't want the key to have any regular
  • expression properties. Therefore, we need to escape
  • all special characters.
  • --->
  • <cfset LOCAL.Key = LOCAL.Key.ReplaceAll(
  • "([\+\*\{\}\[\]\(\)\?\\\$]){1}",
  • "\\$1"
  • ) />
  •  
  • <!---
  • Create the string buffer that will hold the
  • temporary results of the text changes made by
  • this map-application iteration.
  • --->
  • <cfset LOCAL.Buffer = LOCAL.StringBufferClass.Init() />
  •  
  • <!---
  • Create the pattern we are matching. Use the
  • case-insensitive flag.
  • --->
  • <cfset LOCAL.Pattern = LOCAL.PatternClass.Compile(
  • "(?i)\b#LOCAL.Key#\b"
  • ) />
  •  
  • <!--- Get the matcher for this pattern. --->
  • <cfset LOCAL.Matcher = LOCAL.Pattern.Matcher(
  • LOCAL.Text
  • ) />
  •  
  •  
  • <!---
  • Now that we have our matcher, let's keep iterating
  • over the matcher while we can find instances of the
  • pattern in the target text (that we must replace).
  • --->
  • <cfloop condition="LOCAL.Matcher.Find()">
  •  
  • <!---
  • Replace the current match with value in the text
  • map. When performing the match, we have to make
  • sure that it has no regular expression parts.
  • --->
  • <cfset LOCAL.Matcher.AppendReplacement(
  • LOCAL.Buffer,
  • ToString(
  • ARGUMENTS.Map[ LOCAL.Matcher.Group() ]
  • ).ReplaceAll(
  • "(\\|\$){1}",
  • "\\$1"
  • )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • We have replaced all instances of the given
  • pattern. Now, just append what ever we didn't
  • already put in the buffer.
  • --->
  • <cfset LOCAL.Matcher.AppendTail(
  • LOCAL.Buffer
  • ) />
  •  
  •  
  • <!---
  • Now that we have completely replaced the current
  • term in the text map, let's save the results back
  • into the local text result for the next iteration.
  • --->
  • <cfset LOCAL.Text = LOCAL.Buffer.ToString() />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Return the modified arguments. --->
  • <cfreturn LOCAL.Text />
  • </cffunction>

As it turns out, not only is this UDF much more complicated, it also happens to be much slower (by many times). The ReplaceNoCase() method was usually about 10 times faster; just goes to show you that sometimes the simplist answer can sometimes be the most effective. However, if my text mappings did contain regular expressions, this might be a good way to go.




Reader Comments

Sounds like this is something that's a candidate for a web service!

Going that route though, I'd suggest storing the mappings as an XML file (formatted as WDDX) for easy maintainability, or a user-updateable database table.

Reply to this Comment

If you do a Coldfusion IM client application, you might need the reverse where someone IMs in abbreviations and you want to expand the full text. Then maybe you can have your own commercial...

Reply to this Comment

That would be very useful for parents keeping tabs on their teenagers and trying to figure out what's going on.

Reply to this Comment

You probably want to change

<cfset objMap[ "You're a pain in the ass" ] = "UAPITA" />
<cfset objMap[ "You're welcome" ] = "UW" />

to

<cfset objMap[ "Your a pain in the ass" ] = "UAPITA" />
<cfset objMap[ "Your welcome" ] = "UW" />

as you'll get many more matches than you would spelling it correctly :)

Reply to this Comment

Ha ha, yeah, true - misspellings would probably result in better matches :) Silly kids.

Also, yeah, I can see that going the other way would be good, and perhaps even more useful.

Reply to this Comment

I've jst strtd to play w/ twtr. Im nt a sftwr dvlpr but i thnk it wd be cool to hv an app that autmtcly abbvtd yr txt while u write (desktp, lptp & mbl). So I mean not just acrnonyms, but lik im doing here.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.