pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Detecting repeated phrase in a string
5+ messages / 3 participants
[nested] [flat]

* Detecting repeated phrase in a string
@ 2021-12-09 12:38  Shaozhong SHI <shishaozhong@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Shaozhong SHI @ 2021-12-09 12:38 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

Does anyone know how to detect repeated phrase in a string?

Is there any such function?

Regards,

David

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Detecting repeated phrase in a string
@ 2021-12-09 13:35  Peter J. Holzer <hjp-pgsql@hjp.at>
  parent: Shaozhong SHI <shishaozhong@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Peter J. Holzer @ 2021-12-09 13:35 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2021-12-09 12:38:15 +0000, Shaozhong SHI wrote:
> Does anyone know how to detect repeated phrase in a string?

Use regular expressions with backreferences:

bayes=> select regexp_match('foo wikiwiki bar', '(.+)\1');
╔══════════════╗
║ regexp_match ║
╟──────────────╢
║ {o}          ║
╚══════════════╝
(1 row)

"o" is repeated in "foo".

bayes=> select regexp_match('fo wikiwiki bar', '(.+)\1');
╔══════════════╗
║ regexp_match ║
╟──────────────╢
║ {wiki}       ║
╚══════════════╝
(1 row)

"wiki" is repeated in "wikiwiki".

bayes=> select regexp_match('fo wikiwi bar', '(.+)\1');
╔══════════════╗
║ regexp_match ║
╟──────────────╢
║ (∅)          ║
╚══════════════╝
(1 row)

nothing is repeated.

Adjust the expression within parentheses if you want to match somethig
more specific than any sequence of one or more characters.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Detecting repeated phrase in a string
@ 2021-12-09 14:46  Shaozhong SHI <shishaozhong@gmail.com>
  parent: Peter J. Holzer <hjp-pgsql@hjp.at>
  0 siblings, 1 reply; 5+ messages in thread

From: Shaozhong SHI @ 2021-12-09 14:46 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

Hi, Peter,

How to define word boundary as either by using
^  , space, or $

So that the following can be done

fox fox is a repeat

foxfox is not a repeat but just one word.

Regards,

David

On Thu, 9 Dec 2021 at 13:35, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:

> On 2021-12-09 12:38:15 +0000, Shaozhong SHI wrote:
> > Does anyone know how to detect repeated phrase in a string?
>
> Use regular expressions with backreferences:
>
> bayes=> select regexp_match('foo wikiwiki bar', '(.+)\1');
> ╔══════════════╗
> ║ regexp_match ║
> ╟──────────────╢
> ║ {o}          ║
> ╚══════════════╝
> (1 row)
>
> "o" is repeated in "foo".
>
> bayes=> select regexp_match('fo wikiwiki bar', '(.+)\1');
> ╔══════════════╗
> ║ regexp_match ║
> ╟──────────────╢
> ║ {wiki}       ║
> ╚══════════════╝
> (1 row)
>
> "wiki" is repeated in "wikiwiki".
>
> bayes=> select regexp_match('fo wikiwi bar', '(.+)\1');
> ╔══════════════╗
> ║ regexp_match ║
> ╟──────────────╢
> ║ (∅)          ║
> ╚══════════════╝
> (1 row)
>
> nothing is repeated.
>
> Adjust the expression within parentheses if you want to match somethig
> more specific than any sequence of one or more characters.
>
>         hp
>
> --
>    _  | Peter J. Holzer    | Story must make more sense than reality.
> |_|_) |                    |
> | |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
> __/   | http://www.hjp.at/ |       challenge!"
>


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Detecting repeated phrase in a string
@ 2021-12-09 15:11  Andreas Joseph Krogh <andreas@visena.com>
  parent: Shaozhong SHI <shishaozhong@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Andreas Joseph Krogh @ 2021-12-09 15:11 UTC (permalink / raw)
  To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>


På torsdag 09. desember 2021 kl. 15:46:05, skrev Shaozhong SHI <
shishaozhong@gmail.com <mailto:shishaozhong@gmail.com>>: 


Hi, Peter, 

How to define word boundary as either by using 
^ , space, or $ 

So that the following can be done 

fox fox is a repeat 

foxfox is not a repeat but just one word. 

Do you want repeated phrase (list of words) ore repeated words? 
For repeated words (including unicode-chars) you can do: 

(\b\p{L}+\b)(?:\s+\1)+ 

I'm not quite sure how to translate this to PG, but in JAVA it works. 



-- 
Andreas Joseph Krogh 
CTO / Partner - Visena AS 
Mobile: +47 909 56 963 
andreas@visena.com <mailto:andreas@visena.com> 
www.visena.com <https://www.visena.com; 
 <https://www.visena.com; 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Detecting repeated phrase in a string
@ 2021-12-09 19:52  Peter J. Holzer <hjp-pgsql@hjp.at>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Peter J. Holzer @ 2021-12-09 19:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2021-12-09 16:11:31 +0100, Andreas Joseph Krogh wrote:
> For repeated words (including unicode-chars) you can do:
>  
> (\b\p{L}+\b)(?:\s+\1)+
>  
> I'm not quite sure how to translate this to PG, but in JAVA it works.

See https://www.postgresql.org/docs/11/functions-matching.html#POSIX-CONSTRAINT-ESCAPES-TABLE

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2021-12-09 19:52 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09 12:38 Detecting repeated phrase in a string Shaozhong SHI <shishaozhong@gmail.com>
2021-12-09 13:35 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2021-12-09 14:46   ` Shaozhong SHI <shishaozhong@gmail.com>
2021-12-09 15:11     ` Andreas Joseph Krogh <andreas@visena.com>
2021-12-09 19:52       ` Peter J. Holzer <hjp-pgsql@hjp.at>

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