gpt4 book ai didi

从文本字段中提取单词的 SQL 查询

转载 作者:行者123 更新时间:2023-12-02 00:07:44 29 4
gpt4 key购买 nike

我正在创建一个 SQL 脚本,其中我必须从文本字段 (varchar(max)) 中输出 n(用户输入)个单词,从另一个表中指定的单词(搜索词)开始。因此,例如,我的文本字符串是“The quick brown fox jumps over the lazy dog”,我想从单词“brown”中输出 3 个单词。所以,我的输出应该是“brown fox jumps over”。

此操作需要在一组两个不同的表上运行。第一个表将包含来自多个文档的文本字段。第二个表将包含一些搜索词(上面场景中的“棕色”一词),我们将从这些词开始生成输出。 (如果我对这部分不清楚,请告诉我)。

我编写了一个代码,但在语法或对 SQL 位的理解方面出了问题。以下是生成错误的代码片段。

        SELECT @Loopcounter = 0,
@Termcount = Count([termName])
FROM #temp_table -- temp_table contains the list of search terms

WHILE ( @Loopcounter < @Termcount )
BEGIN

SELECT @SearchTerm = [termName] FROM #temp_table ORDER BY RowNum
OFFSET @Loopcounter ROWS
FETCH NEXT 1 ROWS ONLY -- to iterate all the search terms in temp_table on all documents

SET @Spacecounter = 0;
SET @Lastword = 0;

SELECT
[DocID],
[ExtractedText],
(
SELECT @Lastword = CHARINDEX( @SearchTerm, ExtractedText ) --position of search term in text field of a document
FROM [EDDSDBO].[Document] t1 --contains the list of all documents and their text fields
WHERE d.ArtifactID = t1.ArtifactID -- to match the document id outside of the loop

WHILE ( @Spacecounter <= @Proxnum ) --@Proxnum is the number of words required by the user
-- this loop will find spaces after the search term and will give the position of @proxnum space after the search term
BEGIN

SELECT @Lastword = CHARINDEX( ' ', ExtractedText, @Lastword ) --to find the first space after the search term
FROM [EDDSDBO].[Document] t2
WHERE d.ArtifactID = t2.ArtifactID

SET @Lastword = @Lastword + 1
SET @Spacecounter = @Spacecounter + 1

END

SELECT SUBSTRING ( ExtractedText, CHARINDEX( @SearchTerm, ExtractedText ), @Lastword - CHARINDEX( @SearchTerm, ExtractedText ) )
FROM [EDDSDBO].[Document] t3 --to extract the words from starting of search term till the numbers of words required
WHERE d.ArtifactID = t3.ArtifactID
)
AS [After Hit]
FROM [EDDSDBO].[Document] d
WHERE CONTAINS ( ExtractedText, @SearchTerm) --to only search the document that contains that search term

SET @Loopcounter = @Loopcounter + 1

END

我知道那里有很多脚本,但上下文不多,但是如果有人可以帮助我解决这个问题,请发表您的答案。我假设我在 select 语句中调用循环时出错了,但我没有看到替代方法。

如果您需要更多上下文来理解此 SQL 脚本的要求,请告诉我。谢谢!

最佳答案

这并不容易。我在这上面花了太多时间,我相信 sql-server 有一些你可以安装或打开的文本搜索功能。尽管如此,这里的方法在很大程度上应该可以满足您的需要。您必须到处调整它,但它适用于我在下面提供的示例数据。

设置:

您永远不会知道您 future 的需求,因此您不太可能构建您的系统以实现灵 active 。所以这里有一个搜索表,它不仅有搜索词,还有一个列,用于显示要提取的词后的单词数:

declare @searches table (
termName nvarchar(50),
wordsAfter int,
rowNum int identity(1,1)
);

insert @searches values
('brown', 3),
('green', 2);

然后这是一个文档表,它对我认为您的 eddsdbo.document 表所做的事情进行了采样:

declare @documents table (
docId int identity(1,1),
contents nvarchar(max)
);

insert @documents values
('The quick brown fox jumps over the lazy dog'),
('The slow green turtle crawls under the brown and yellow giraffe');

解决方案:

好的,首先您要将文档内容拆分为单个单词:

declare @splittedWords table (
docId int,
wordNum int,
word nvarchar(50)
);

with

splitWords as (

select docId,
contents,
start = charindex(' ', contents) + 1,
conLen = len(contents),
wordNum = 1
from @documents

union all
select docId,
ap.contents,
start = charindex(' ', ap.contents) + 1,
conLen = len(ap.contents),
wordNum = wordNum + 1
from splitWords
cross apply (select contents =
substring(contents, start, conLen - 1)
) ap
where start > 1

)

insert @splittedWords
select docId,
wordNum,
word = iif(
wordNum = max(wordNum) over(partition by docId),
contents,
substring(contents, 0, start - 1)
)
from splitWords;

现在,对于每个搜索词,您想要获取该词在内容中的位置,以及后面的词:

declare @filteredSplits table (
search nvarchar(50),
docId int,
wordNum int,
word nvarchar(50)
);

insert @filteredSplits
select search = finds.word,
w.docId,
w.wordNum,
w.word
from @searches s
join @splittedWords finds on s.termName = finds.word
join @splittedWords w
on finds.docId = w.docId
and w.wordNum between finds.wordNum and finds.wordNum + s.wordsAfter;

最后,连接:

select      fs.search,
fs.docId,
extract = stuff((
select ' ' + sub.word
from @filteredSplits sub
where sub.docId = fs.docId
and sub.search = fs.search
order by sub.wordNum
for xml path('')
), 1, 1, '')
from @filteredSplits fs
group by fs.search, fs.docId

结果:

+-------------------------------------------+
| search | docId | extract |
+-------------------------------------------+
| brown | 1 | brown fox jumps over |
| brown | 2 | brown and yellow giraffe |
| green | 2 | green turtle crawls |
+-------------------------------------------+

关于从文本字段中提取单词的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60103313/

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