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 1nCTR6-0002Pd-PL for pgsql-general@arkaria.postgresql.org; Tue, 25 Jan 2022 21:33:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCTR5-0002HT-He for pgsql-general@arkaria.postgresql.org; Tue, 25 Jan 2022 21:33: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 1nCTR4-0002HJ-Ug for pgsql-general@lists.postgresql.org; Tue, 25 Jan 2022 21:33:06 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCTR2-0005Mh-Hc for pgsql-general@lists.postgresql.org; Tue, 25 Jan 2022 21:33:05 +0000 Received: from [192.168.28.29] (cyclops.postgrespro.ru [93.174.131.138]) (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 B81BE21C312F for ; Wed, 26 Jan 2022 00:33:03 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1643146383; bh=a+HDwjj3V/vvhCTnB7kCPYFhQrCWLsz2PAA/Yox4CGk=; h=Subject:To:References:From:Date:In-Reply-To; b=dxudlAzU8qhTKH/ZkfJgM6UmH57xs6q3j+On4gA+mnijJNE+zlGMHayXa3X3Lhu9C kpc6dHeSxt41pvHr0muJ/DhshrA1pu793mtISFJ6x61Vn8pHi/8hP4e6o6oE8jseOW Q5oZ6KS7HQ3t5Ot6yP4r8dLQNLaEc1DELWq5N60U= Subject: Re: Counting the number of repeated phrases in a column To: pgsql-general@lists.postgresql.org References: From: Ivan Panchenko Message-ID: <1859eba7-5615-abbd-3c7f-15f836a7b6d6@postgrespro.ru> Date: Wed, 26 Jan 2022 00:33:03 +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: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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.