gpt4 book ai didi

sql - 使用 PostgreSQL 定位流行的字符串

转载 作者:行者123 更新时间:2023-11-29 12:09:44 24 4
gpt4 key购买 nike

我在 PostgreSQL 表中有一堆文本行,我正在尝试查找常见的字符串。

例如,假设我有一个基本表:

CREATE TABLE a (id serial, value text);
INSERT INTO a (value) VALUES
('I go to the movie theater'),
('New movie theater releases'),
('Coming out this week at your local movie theater'),
('New exposition about learning disabilities at the children museum'),
('The genius found in learning disabilities')
;

我试图在所有行中找到流行的字符串,如 movie theaterlearning disabilities(目标是显示“趋势”字符串列表推特“趋势”)

我使用全文搜索,并尝试将 ts_statts_headline 结合使用,但结果非常令人失望。

有什么想法吗?谢谢!

最佳答案

没有现成可用的 Posgres 文本搜索功能来查找最流行的短语。对于双词短语,您可以使用 ts_stat() 找到最流行的词,消除粒子、介词等,并交叉连接这些词以找到最流行的词对。

对于实际数据,您可能希望更改标记为 --> 参数的值。 在较大的数据集上查询可能会非常昂贵。

with popular_words as (
select word
from ts_stat('select value::tsvector from a')
where nentry > 1 --> parameter
and not word in ('to', 'the', 'at', 'in', 'a') --> parameter
)
select concat_ws(' ', a1.word, a2.word) phrase, count(*)
from popular_words as a1
cross join popular_words as a2
cross join a
where value ilike format('%%%s %s%%', a1.word, a2.word)
group by 1
having count(*) > 1 --> parameter
order by 2 desc;


phrase | count
-----------------------+-------
movie theater | 3
learning disabilities | 2
(2 rows)

关于sql - 使用 PostgreSQL 定位流行的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42702888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com