pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: benj.dev@laposte.net
To: Merlin Moncure <mmoncure@gmail.com>
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Counting the number of repeated phrases in a column
Date: Thu, 27 Jan 2022 18:56:02 +0100
Message-ID: <9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net> (raw)
In-Reply-To: <CAHyXU0x=EbguRAuBPj++gR_RWvWsdwRTsm4s5+8omEZy9wy23Q@mail.gmail.com>
References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com>
	<CAHyXU0x501igQ2x_83wTfsL1pg0e9aRYwr0ScxSR+w95r8CJPg@mail.gmail.com>
	<CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com>
	<22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com>
	<CAHyXU0x=EbguRAuBPj++gR_RWvWsdwRTsm4s5+8omEZy9wy23Q@mail.gmail.com>



Le 27/01/2022 à 18:35, Merlin Moncure a écrit :
> On Thu, Jan 27, 2022 at 11:09 AM Rob Sargent <robjsargent@gmail.com> wrote:
>>
>> On 1/27/22 10:03, Merlin Moncure wrote:
>>
>> On Wed, Jan 26, 2022 at 5:23 PM Merlin Moncure <mmoncure@gmail.com> wrote:
>>
>> 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 occurrences
>> 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          │ occurances
>> ─────────────────────────┼────────────
>>   World Hello             │          1
>>   Hello World Hello       │          1
>>   Hello World             │          2
>>   Hello World Hello World │          1
>>   World Hello World       │          1
>>
>> merlin
>>
>>
>> And since we're looking for repeated phrases maybe add
>>
>> having count(*) > 1
> 
> thanks.  also, testing on actual data, I noticed that a couple other
> things are mandatory, mainly doing a bit of massaging before
> tokenizing:
> 
> select distinct array_to_string(v[a:b], ' ') phrase, count(*) as occurrences
> from
> (
>    select array_agg(t) v
>    from
>    (
>      select trim(replace(unnest(v), E'\n', '')) t
>      from regexp_split_to_array(<sentence>, ' ') 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;
> 
> We are definitely in N^2 space here, so look for things to start
> breaking down for sentences > 1000 words.
> 
> merlin
> 

(for better complexity) you may search about "Ukkonen suffix tree"
Similar problem as yours : 
https://www.geeksforgeeks.org/suffix-tree-application-3-longest-repeated-substring/?ref=lbp

Attachments:

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

view thread (23+ messages)  latest in thread

Message-ID: <9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net>
Permalink:  ../9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net/
Also on:    postgresql.org/message-id/9ae7115c-89c8-1960-6ca5-d899f710aa0b@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, mmoncure@gmail.com, robjsargent@gmail.com
  Subject: Re: Counting the number of repeated phrases in a column
  In-Reply-To: <9ae7115c-89c8-1960-6ca5-d899f710aa0b@laposte.net>
  References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com> <CAHyXU0x501igQ2x_83wTfsL1pg0e9aRYwr0ScxSR+w95r8CJPg@mail.gmail.com> <CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com> <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com> <CAHyXU0x=EbguRAuBPj++gR_RWvWsdwRTsm4s5+8omEZy9wy23Q@mail.gmail.com> <9ae7115c-89c8-1960-6ca5-d899f710aa0b@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