Subject: Overcoming Initcap Function limitations?

From: Bo Guo <[email-masked]>
Date: Sat, 2 Dec 2023 12:03:45 -0700

Hi there,

The Initcap function does not have the option to support the title casing
where articles/prepositions are left in lowercase. Neither does it handle
cultural names such as "MacDonald."

Please kindly advise any options we may have.

Cheers,

Bo

---

From: Greg Sabino Mullane <[email-masked]>
Date: Mon, 4 Dec 2023 13:08:03 -0500

It's not clear exactly what you are trying to achieve, but you can use
Postgres' built-in text searching system to exclude stopwords. For example:

CREATE FUNCTION initcap_realword(myword TEXT)
  returns TEXT language SQL AS
$$
SELECT CASE WHEN length(to_tsvector(myword)) < 1
  THEN myword ELSE initcap(myword) END;
$$;

You could extend that to multi-word strings with a little effort. However,
knowing that macdonald should be MacDonald requires a lot more intelligence
than is provided by any Postgres built-in system or extension that I know
of. What you are looking at is the field of science known as Natural
Language Processing, which can get very complex very quickly. But for a
Postgres answer, you might combine plpython3u with spacy (
https://spacy.io/usage/spacy-101).

Cheers,
Greg

---

From: Steve Midgley <[email-masked]>
Date: Mon, 4 Dec 2023 10:39:12 -0800

On Mon, Dec 4, 2023 at 10:09 AM Greg Sabino Mullane <[email-masked]>
wrote:

> I've been having some pretty good experiences with "hard" text
transformations such as correct capitalization of names like MacDonald
using GPT 3.5 Turbo API which is pretty cheap for the volume of data I've
been working with.. Seems like Spacy might do similar things, and if it can
be run locally, might be much cheaper than a rental API..

Steve

---

From: Bo Guo <[email-masked]>
Date: Mon, 4 Dec 2023 18:39:18 -0700

Thank you, Steve and Greg!

Your suggestions open up new potentials for me to explore.  At this moment,
I lean towards normalizing the database column values in upper case,
thereby out-sourcing the case-changing responsibility to the front end.  I
would love to hear from your thoughts on this pattern.

Cheers,

Bo

On Mon, Dec 4, 2023 at 11:39 AM Steve Midgley <[email-masked]> wrote:

>> You could extend that to multi-word strings with a little effort.
>> However, knowing that macdonald should be MacDonald requires a lot more
>> intelligence than is provided by any Postgres built-in system or extension
>> that I know of. What you are looking at is the field of science known as
>> Natural Language Processing, which can get very complex very quickly. But
>> for a Postgres answer, you might combine plpython3u with spacy (

---

From: Steve Midgley <[email-masked]>
Date: Mon, 4 Dec 2023 20:12:34 -0800

On Mon, Dec 4, 2023, 5:39 PM Bo Guo <[email-masked]> wrote:

> Your suggestions open up new potentials for me to explore.  At this
> moment, I lean towards normalizing the database column values in upper
> case, thereby out-sourcing the case-changing responsibility to the front
> end.  I would love to hear from your thoughts on this pattern.
>
> Cheers,
>
> Bo
>
It really depends on the use case. If your users are happy with all
uppercase, that seems like a great solution: fast, cheap, and reliable!

Note that this list asks posters to add their reply comments at the bottom
of each message, rather than the type which is more typical.. Welcome!

Steve

---

From: Tom Lane <[email-masked]>
Date: Mon, 04 Dec 2023 23:23:55 -0500

> uppercase, that seems like a great solution: fast, cheap, and reliable!

FWIW, as an old database geek I'd lean more towards "store the original
input data".  If you are expecting the client-side display code to
transform the casing anyway, I don't see any advantage in smashing to
upper case beforehand.  And keeping the original casing could have
value down the road, if only for forensic purposes.

			regards, tom lane

---

From: Greg Sabino Mullane <[email-masked]>
Date: Tue, 5 Dec 2023 15:54:08 -0500

I agree with Tom. Store the original: let the frontend uppercase themselves
if they want it that way. If they decide to force the transforms back down
to you, you won't have spoiled your input. If they are querying on that
column, a functional index on lower(col) would be nice. To answer your
original question, yes, let the front end deal with the problem, leaving
you time to worry about more traditional database-related problems. :)

Cheers,
Greg