pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: benj.dev@laposte.net
To: Shaozhong SHI <shishaozhong@gmail.com>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Counting the number of repeated phrases in a column
Date: Tue, 25 Jan 2022 22:21:05 +0100
Message-ID: <b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net> (raw)
In-Reply-To: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com>
References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com>

Le 25/01/2022 à 18:10, Shaozhong SHI a écrit :
> There is a short of a function in the standard Postgres to do the following:
> 
> It is easy to count the number of occurrence of words, but it is rather 
> difficult to count the number of occurrence of phrases.
> 
> For instance:
> 
> A cell of value:  'Hello World' means 1 occurrence a phrase.
> 
> A cell of value: 'Hello World World Hello' means no occurrence of any 
> repeated phrase.
> 
> But, A cell of value: 'Hello World World Hello Hello World' means 2 
> occurrences of 'Hello World'.
> 
> 'The City of London, London' also has no occurrences of any repeated phrase.
> 
> Anyone has got such a function to check out the number of occurrence of 
> any repeated phrases?
> 
> Regards,
> 
> David

Don't know if it's exactly what you want, but you can replace all 
occurence of the phrase in the text by empty string and compute the diff 
between the initial and the result and next divide by the length of your 
phrase.

Example :
WITH x AS (SELECT 'toto like tata and toto like titi and toto like tutu' 
, 'toto like' phrase)
SELECT (char_length(texte) - char_length(replace(texte, phrase, ''))) / 
char_length(phrase) AS nb_occurence
FROM x

Attachments:

  OpenPGP_signature (application/pgp-signature, 202B) download

view thread (23+ messages)  latest in thread

Message-ID: <b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net>
Permalink:  ../b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net/
Also on:    postgresql.org/message-id/b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: benj.dev@laposte.net, shishaozhong@gmail.com
  Subject: Re: Counting the number of repeated phrases in a column
  In-Reply-To: <b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net>
  References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com> <b1ab1a9f-bcd0-7630-ed2c-4d596bca3e19@laposte.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox