Re: Counting the number of repeated phrases in a column

From: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Counting the number of repeated phrases in a column
Date: 2022-02-01 18:49:34
Message-ID: CA+i5JwbsgTm0ufaYvr=eXhkWM=BhpikA3ViUk8gCfk4BuwHqww@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thursday, 27 January 2022, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:

> On Wed, Jan 26, 2022 at 5:23 PM Merlin Moncure <mmoncure(at)gmail(dot)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
>

How about knock unique words into discrete joint up strings? Then check
whether there is any repeated words?
Regards,
David

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Shaozhong SHI 2022-02-01 23:29:50 Re: Counting the number of repeated phrases in a column
Previous Message Tom Lane 2022-02-01 15:03:33 Re: Extension has owner