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 1nD90H-00085s-Vf for pgsql-general@arkaria.postgresql.org; Thu, 27 Jan 2022 17:56:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nD90G-0006Bh-Sw for pgsql-general@arkaria.postgresql.org; Thu, 27 Jan 2022 17:56:12 +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 1nD90G-0006BS-Gl for pgsql-general@lists.postgresql.org; Thu, 27 Jan 2022 17:56:12 +0000 Received: from smtp-outgoing-1602.laposte.net ([160.92.124.97]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD90B-00065x-Sh for pgsql-general@lists.postgresql.org; Thu, 27 Jan 2022 17:56:11 +0000 X-mail-filterd: {"version":"1.3.4","queueID":"4Jl7YY74zSzKm43","contextId":"450ff6d3-df83-4fb7-9032-2e0c90e0742c"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0115.laposte.net (SMTP Server) with ESMTP id 4Jl7YY74zSzKm43; Thu, 27 Jan 2022 18:56:01 +0100 (CET) X-mail-filterd: {"version":"1.3.4","queueID":"4Jl7YY6S6czKm42","contextId":"476750fa-9883-4044-b2a2-1f78cbd3be31"} X-lpn-mailing: LEGIT X-lpn-spamrating: 46 X-lpn-spamlevel: not-spam X-lpn-spamcause: OK, (-100)(0000)gggruggvucftvghtrhhoucdtuddrgedvvddrfeefgddutdeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecunfetrffquffvgfdpggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepuffvfhfhkffffgggjggtsehgtderofdtfeejnecuhfhrohhmpegsvghnjhdruggvvheslhgrphhoshhtvgdrnhgvthenucggtffrrghtthgvrhhnpeetgeeujedukedukeejkeeujedvtdejjeevuddvffdtueduteffhfeugffhgfffffenucffohhmrghinhepghgvvghkshhfohhrghgvvghkshdrohhrghenucfkphepjeekrdduleekrddvgedvrdduvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepjeekrdduleekrddvgedvrdduvdejpdhhvghloheplgduledvrdduieekrddtrddutdgnpdhmrghilhhfrhhomhepsggvnhhjrdguvghvsehlrghpohhsthgvrdhnvghtpdhnsggprhgtphhtthhopeefpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtoheprhhosghjshgrrhhgvghnthesghhmrghilhdrtghomhdprhgtphhtthhopehmmhhonhgtuhhrvgesghhmrghilhdrtghomhdpmhhouggvpehsmhhtphhouhht Received: from [192.168.0.10] (unknown [78.198.242.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mlpnf0115.laposte.net (SMTP Server) with ESMTPSA id 4Jl7YY6S6czKm42; Thu, 27 Jan 2022 18:56:01 +0100 (CET) Subject: Re: Counting the number of repeated phrases in a column To: Merlin Moncure , Rob Sargent Cc: pgsql-general References: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com> From: benj.dev@laposte.net Message-ID: <9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net> Date: Thu, 27 Jan 2022 18:56:02 +0100 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/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="vcITbAZ9qZMwzRyCjJmz9STiuKg8vCAZC" DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=laposte.net; s=lpn-wlmd; t=1643306164; bh=Ic/W1y4cYXzgjWv0ce0KWwCmmhlL730wJD7GYe62ook=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version:In-Reply-To:Content-Type; b=Hspsbpd/gp72DcKceHE/FimVnaBu6gCdQdtybdDd0WJoSnOiczAJHsA1i5Oif1wcdZYOAh6ceABVx80Zv2n0YhGXvy4YDL8WYqUQkOPbxAFGbo+cWUvtLx6RCYY62L8eF0Nq1HjvmKqd8ReaX6ltb6RoCSC8fXOxN8+YnLkWuUoBkvgOXOVNYRm3xo4LHjaJTgtZFvJSY3mFo2qLip73aLWeVDHEF+H8K0/AU57sQKnO9xZfVnW1EOd+3TSVkqp0Rp41Z412GB85pdKsekAZjzPZiR7C4gATYoeR1Ubp74Uf9bN+xyg33+uNiwMiO+ev/ZZunY8zSmebgaRWM5j31A==; List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --vcITbAZ9qZMwzRyCjJmz9STiuKg8vCAZC Content-Type: multipart/mixed; boundary="5dNIqyG9xG7tuNRx0TAEbfhCJi1cIgfLq"; protected-headers="v1" From: benj.dev@laposte.net To: Merlin Moncure , Rob Sargent Cc: pgsql-general Message-ID: <9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net> Subject: Re: Counting the number of repeated phrases in a column References: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com> In-Reply-To: --5dNIqyG9xG7tuNRx0TAEbfhCJi1cIgfLq Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: fr Content-Transfer-Encoding: quoted-printable Le 27/01/2022 =C3=A0 18:35, Merlin Moncure a =C3=A9crit=C2=A0: > On Thu, Jan 27, 2022 at 11:09 AM Rob Sargent wr= ote: >> >> On 1/27/22 10:03, Merlin Moncure wrote: >> >> On Wed, Jan 26, 2022 at 5:23 PM Merlin Moncure wr= ote: >> >> with s as (select 'Hello World Hello World' as sentence) >> select >> phrase, >> array_upper(string_to_array((select sentence from s), phrase), 1) -= >> 1 as occurrances >> from >> ( >> select array_to_string(x, ' ') as phrase >> from >> ( >> select distinct v[a:b] x >> from regexp_split_to_array((select sentence from s), ' ') v >> cross join lateral generate_series(1, array_upper(v, 1)) a >> cross join lateral generate_series(a + 1, array_upper(v, 1)) b >> ) q >> ) q; >> >> Simplified to: >> select distinct array_to_string(v[a:b], ' ') phrase, count(*) as occur= rences >> from regexp_split_to_array('Hello World Hello World', ' ') v >> cross join lateral generate_series(1, array_upper(v, 1)) a >> cross join lateral generate_series(a + 1, array_upper(v, 1)) b >> group by 1; >> >> phrase =E2=94=82 occurances >> =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >> World Hello =E2=94=82 1 >> Hello World Hello =E2=94=82 1 >> Hello World =E2=94=82 2 >> Hello World Hello World =E2=94=82 1 >> World Hello World =E2=94=82 1 >> >> merlin >> >> >> And since we're looking for repeated phrases maybe add >> >> having count(*) > 1 >=20 > thanks. also, testing on actual data, I noticed that a couple other > things are mandatory, mainly doing a bit of massaging before > tokenizing: >=20 > select distinct array_to_string(v[a:b], ' ') phrase, count(*) as occurr= ences > from > ( > select array_agg(t) v > from > ( > select trim(replace(unnest(v), E'\n', '')) t > from regexp_split_to_array(, ' ') v > ) q > where length(t) > 1 > ) q > cross join lateral generate_series(1, array_upper(v, 1)) a > cross join lateral generate_series(a + 1, array_upper(v, 1)) b > group by 1 > having count(*) > 1; >=20 > We are definitely in N^2 space here, so look for things to start > breaking down for sentences > 1000 words. >=20 > merlin >=20 (for better complexity) you may search about "Ukkonen suffix tree" Similar problem as yours :=20 https://www.geeksforgeeks.org/suffix-tree-application-3-longest-repeated-= substring/?ref=3Dlbp --5dNIqyG9xG7tuNRx0TAEbfhCJi1cIgfLq-- --vcITbAZ9qZMwzRyCjJmz9STiuKg8vCAZC Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wmMEABEIACMWIQSkS3tpag0IFuyrNn7u5vY6QwMtDgUCYfLcsgUDAAAAAAAKCRDu5vY6QwMtDqCM AJ9LhiW1Qx3NQjqkHjttqf0Qa/uIwQCfWGbJMWdWKclrJdGS6tCfFxhbHSw= =xx2U -----END PGP SIGNATURE----- --vcITbAZ9qZMwzRyCjJmz9STiuKg8vCAZC--