Text Normalizer [Its so cool]

:wave: Hey all!

I had a problem earlier today where I needed to normalize text into title case, or proper case, or whatever the English language calls it. Essentially:

  • Of mice AnD mEN → Of Mice and Men
  • MY NAME IS SCOTT → My Name is Scott

Here’s the current doc: Would love input if theres an easier way to accomplish it. . .Also, for some reasons, now matter what I do, the Omitted Words function will catch any word excpet for the. I think it might be a Coda bug?

Check it out and let me know what you think! Its pretty fun!

Here’s a more approachable formula IMO:

List("on", "or", "of", "and", "is").WithName(LowercaseWords,
List("LLC").WithName(IgnoredWords,
  thisRow.[All Caps Title].Split(" ").FormulaMap(
    CurrentValue.Lower().WithName(CurrentWordLowercase,
      SwitchIf(
        CurrentValue.In(IgnoredWords),
        CurrentValue,
        CurrentWordLowercase.In(LowercaseWords),
        CurrentWordLowercase,
        Concatenate(CurrentValue.Left(1).Upper(), CurrentWordLowercase.Slice(2))
      )
    )
  ).Join(" ").WithName(Sentence,
    Concatenate(Sentence.Left(1).Upper(), Sentence.Slice(2))
  )
))

It also does the split, and then for each word a clear centralized switchif to see if the word should be ignored, lowercased, or title-cased. Then separately I’m capitalizing the whole sentence.

In your formula, there’s a .Split(",").FormulaMap(Trim(...)) within a loop — not an optimal approach since the splitting and trimming is unnecessarily repeated for each check. It’s much better to extract the splitting outside the loop — either as a separate column on the Omitted words table that’ll be already calculated to a nice trimmed list of text values, or in a WithName declaration in the start of the formula — similarly how I did in mine but yours would be @[Omitted Words].Words.Split(",").FormulaMap(CurrentValue.Trim()).WithName(...)

2 Likes

Love it - I was hoping you’d comment. Have you played with my solution? Any reason why my formula isn’t catching the the word whatsoever?

Yep — the issue with “the” is not a bug in Coda but your formula, here:
image

You think you’re testing whether it’s the first word that you’re currently processing — but in fact you’re testing that this is not what the sentence starts with. So if it’s the second “the” in your sentence, list.Find(CurrentValue) will still return 1 because the first word is also “the”.

To properly implement it your way, you’d have to Sequence().FormulaMap() over indices and test whether CurrentValue is 1. I thought of that, but then I thought it was easier to just “title case” everything including the first word, and then capitalize the first character of the sentence separately afterwards.

Ahhhhhhh there we go - - - Ok once I find time Ill need to go in and adjust the formula

1 Like