如何从 tsvector 中找到源文档中真正的词位起始位置?

问题描述 投票:0回答:1

样本文档和

tsvector

select
    *
from
    unnest(to_tsvector('english', 'something wide this more wider and wider social-economy wide somethings'))
词义 职位
经济 10
社交 9
社会经济 8
某事 1,12
2,11
更宽 5,7

我如何向客户解释这些立场? docs 说:

位置 通常表示源单词在文档中的位置。

(重点是我的)

这个“正常”部分让我感到困惑。

我理解的位置是文档中的起始

symbol 索引。但这里似乎只是一个lexeme的顺序索引,而不是符号索引,甚至不是词序索引。

我需要突出显示源文档中的词位,就像 Postgres 处理它们一样(没有

ts_headline

)。但使用上面的示例,单词 
wide
 应该可以在 
locations 2
11
 找到。然而,由于 Postgres 使用词干、停用词词典等,
real 单词位置不同。

那么如何将词位的位置“映射”到源文档以突出显示它们?在一个客户端上。我需要 Postgres 只返回它内部使用的数据并以某种方式解释它。

基本上,最后我需要看到这样的东西:

某事 更多更宽更宽 社会-经济 一些东西

我的第一个方法是在客户端上用标记中的空格分割源文档,检索

tsvector

,提取词位并使用 
string.StartsWith
(目前在 
C#
)之类的东西将每个标记与词位进行比较。但问题是有些词位与源文档的单词有点不同。请注意源文档中的“
economi”词素与“economy”标记。此外,在实际项目中,还使用了一些额外的同义词,因此使用 string.StartsWith
 不起作用。

这就是为什么我需要真正的

符号位置。 有可能以某种方式得到它们吗?

更新_01
这是我如何尝试在客户端上拆分源以映射位置的示例(使用

C#

):

var source = "something wide this more wider and wider social-economy wide somethings"; source .Split(new[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select((w, i) => new { Word = w, Pos = i + 1, }) .OrderBy(w => w.Word) ;

词义职位和6经济9更多4社交8某事1一些东西11这个3宽2宽10更宽5更宽7
其中一些确实匹配,其他则大多不匹配。

postgresql full-text-search tsvector
1个回答
0
投票
我看到Postgres首先将social-economi(y)视为一个单词,并将位置设置为8,然后将social设置为9,将economi设置为10(将其作为不同的单词重复并加1),其余单词将+1添加到接下来是经济(y)。 正如 @KasbolatKumakhov 所说,我们可以使用 \W 正则表达式模式将所有非单词字符替换为空格并使用它。

SELECT * FROM unnest(to_tsvector('english', regexp_replace('something;wide {}{}{ this.more wider and\wider social-economy wide/somethings wide;somethings wide&somethings wide|somethings wide+somethings wide"somethings wide.somethings wide=somethings wide#somethings wide@somethings wide*somethings wide~somethings wide$somethings wide:somethings wideËsomethings wide wideËsomethings', E'\\W', ' ', 'g')));
    
© www.soinside.com 2019 - 2024. All rights reserved.