Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nCfgn-0003Nh-2Y for pgsql-general@arkaria.postgresql.org; Wed, 26 Jan 2022 10:38:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCfgl-0007sl-Ug for pgsql-general@arkaria.postgresql.org; Wed, 26 Jan 2022 10:38:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nCfgl-0007sc-BD for pgsql-general@lists.postgresql.org; Wed, 26 Jan 2022 10:38:07 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCfgh-00031Y-PL for pgsql-general@lists.postgresql.org; Wed, 26 Jan 2022 10:38:06 +0000 Received: from [192.168.27.165] (unknown [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 5D3B821C1AC9 for ; Wed, 26 Jan 2022 13:38:01 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1643193481; bh=/4NYrwOEiF5FV/ae8a+fACVCBUAxfl3Y3zK7zAwkhwY=; h=Subject:To:References:From:Date:In-Reply-To; b=nMz7/WvCIBgMs7jJLCBQ14utv2TdNMTRTDB8OpN8O2aoMHVJ4TA6NXqi5WOdi3dqe NeVt7RDUI5yAckYyuFGb2p8SGeX3rQAO2YQeRp6k7doNA/BNtt5HEcwIw09/8yCyPQ XZgrJbd6oWE3bt48HCihIJgrL9t+/xcBohiCAeZU= Subject: Re: Counting the number of repeated phrases in a column To: pgsql-general@lists.postgresql.org References: <1859eba7-5615-abbd-3c7f-15f836a7b6d6@postgrespro.ru> From: "Ivan E. Panchenko" Message-ID: <80903e46-1996-89a1-c937-831e44fcfcb7@postgrespro.ru> Date: Wed, 26 Jan 2022 13:38:01 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------6EEF46D4F2B12F8013C146F3" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------6EEF46D4F2B12F8013C146F3 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 26.01.2022 11:11, Shaozhong SHI wrote: > > > On Tue, 25 Jan 2022 at 21:33, Ivan Panchenko > > wrote: > > > On 26.01.2022 00:21, benj.dev@laposte.net > wrote: > > 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 > > > This works if the user knows the phrase. As far as I understood, the > phrase is not known, and user wants to count number of repeats of any > phrases. > Of course this can be done with recursive CTE. Split into words, > generate all phrases (AFAIK requires recursion), then group and count. > > But probably in PL/Perl this could be done  more effectively. > > > Is there an example of using recursive CTE to split a text string into > words? Recursion is not needed for splitting into words. This can be done by regexp_split_to_table function. But generation of all possible phrases from the given list of words probably requires recursion. On the first step the list of words becomes a list of a single-worded phrases. On each iteration then, you add the next word to each existing phrase, if it is possible (i.e. until the last word is reached). > > Regards, > > David Regards, Ivan --------------6EEF46D4F2B12F8013C146F3 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 26.01.2022 11:11, Shaozhong SHI wrote:


On Tue, 25 Jan 2022 at 21:33, Ivan Panchenko <i.panchenko@postgrespro.ru> wrote:

On 26.01.2022 00:21, benj.dev@laposte.net wrote:
> 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
>
This works if the user knows the phrase. As far as I understood, the
phrase is not known, and user wants to count number of repeats of any
phrases.
Of course this can be done with recursive CTE. Split into words,
generate all phrases (AFAIK requires recursion), then group and count.

But probably in PL/Perl this could be done  more effectively.

Is there an example of using recursive CTE to split a text string into words?

Recursion is not needed for splitting into words. This can be done by regexp_split_to_table function.

But generation of all possible phrases from the given list of words probably requires recursion. On the first step the list of words becomes a list of a single-worded phrases. On each iteration then, you add the next word to each existing phrase, if it is possible (i.e. until the last word is reached).


Regards,

David 
Regards,
Ivan
--------------6EEF46D4F2B12F8013C146F3--