One of many aspects of a project I am responsible for is generating an abbreviation, that is no more than
n characters, for a given string. We use this in the organization for a couple things, and it only happens once for each “thing”, so performance isn’t hyper-critical.
An example is something like “The Main Test Company” — when asking for a 3-character abbreviation, I want to be able to generate a string like
MTC. Now this would also be the same string as
My Tiny Cat, but the idea is that a given company can be abbreviated and it can be done safely and effectively.
Also, if the abbreviations aren’t large enough for the requested characters (say we request 4 characters from
My Awesome Company) it should, for each abbreviation character, start adding characters to the total (so our example is
I wrote the whole thing in F#, and it seems pretty readable and followable, I’m also including some test strings to verify functionality. Any comments are welcome.
let genAbbr ignoreList padding charCount (str : string) = if str.Length <= charCount then match padding with | Some c -> str |> Seq.append (String(c, charCount - str.Length)) |> Seq.toArray | None -> str.ToCharArray() |> Array.map Char.ToUpper |> String else let words = [|' '|] |> Array.append (str.ToCharArray()) |> Array.fold (fun (acc, cs) c -> match c |> Char.IsUpper, c = ' ', cs with | true, _,  -> (acc, [c]) | true, _, cs -> (cs::acc, [c]) | _, true,  -> (acc, ) | _, true, cs -> (cs::acc, ) | _, _, cs -> (acc, c::cs)) (, ) |> fst |> Array.ofList |> Array.map (Array.ofList >> Array.rev >> String) |> Array.rev |> Array.map (fun s -> s.ToCharArray()) let ignoreWords = words |> Array.filter (fun w -> ignoreList |> Array.contains ((w |> String).ToLower()) |> not) let abbrCs = if ignoreWords |> Array.length >= charCount then ignoreWords |> Array.map Seq.head else if words |> Array.length >= charCount then words |> Array.map Seq.head else let firstCaps = words |> Array.map (fun s -> if s.Length > charCount then s.[0..charCount] else s) |> Array.length printfn "%A %A %A" ignoreWords words firstCaps words |> Array.fold (fun (acc, r) w -> if w.Length > r then (w.[0..(r - 1)]::acc, 1) else (w::acc, r - (w.Length - 1))) (, charCount - firstCaps + 1) |> fst |> Array.ofList |> Array.rev |> Array.concat |> Array.map Char.ToUpper abbrCs |> Array.take charCount |> String
And lastly, some POC’s:
["The Main Test Company"; "Main Test Company"; "The Main Company"; "The Company"; "Company"; "MainCompany"; "The MainCompany"; "MainTestCompany"; "SomeRX"; "SomeCompanyT"] |> List.map (genAbbr [|"the"|] None 3) // Should be ["MTC"; "MTC"; "TMC"; "THC"; "COM"; "MAC"; "TMC"; "MTC"; "SRX"; "SCT"]
Thanks for any commentary, and I look forward to this being brutally decimated. 🙂
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!