使用T-SQL从具有特定模式的段落中删除句子

时间:2012-11-09 18:57:52

标签: sql sql-server sql-server-2008 tsql

我有大量的描述,每个描述可以是5到20个句子。我试图将一个脚本放在一起,找到并删除包含带有数字的单词之前或之后的句子。

之前的例子:Hello world。今天的部门有345名员工。祝你有美好的一天。 例子:你好世界。祝你有个美好的一天。

我现在的主要问题是确定违规行为 这里“345名员工”是导致判决被删除的原因。但是,每个描述将具有不同的数字,并且可能是雇员一词的不同变体。 我想避免创建一个包含员工所有不同变体的表格。

JTB

3 个答案:

答案 0 :(得分:3)

这将成为一个很好的SQL拼图。

免责声明:可能会有很多边缘案例会将其搞砸

这将采用一个字符串,将其拆分为一个表,每个句子都有一行,然后删除匹配条件的行,然后最后将它们全部连接回一个字符串。

CREATE FUNCTION dbo.fn_SplitRemoveJoin(@Val VARCHAR(2000), @FilterCond VARCHAR(100))
RETURNS VARCHAR(2000)
AS 
BEGIN
    DECLARE @tbl TABLE (rid INT IDENTITY(1,1), val VARCHAR(2000))
    DECLARE @t VARCHAR(2000)

    -- Split into table @tbl
    WHILE CHARINDEX('.',@Val) > 0
    BEGIN
        SET @t = LEFT(@Val, CHARINDEX('.', @Val))
        INSERT @tbl (val) VALUES (@t)
        SET @Val = RIGHT(@Val, LEN(@Val) - LEN(@t))
    END

    IF (LEN(@Val) > 0)
        INSERT @tbl VALUES (@Val)


    -- Filter out condition 
    DELETE FROM @tbl WHERE val LIKE @FilterCond

    -- Join back into 1 string
    DECLARE @i INT, @rv VARCHAR(2000)
    SET @i = 1
    WHILE @i <= (SELECT MAX(rid) FROM @tbl)
    BEGIN
        SELECT @rv = IsNull(@rv,'') + IsNull(val,'') FROM @tbl WHERE rid = @i
        SET @i = @i + 1
    END
    RETURN @rv

END
go


CREATE TABLE #TMP (rid INT IDENTITY(1,1), sentence VARCHAR(2000))
INSERT #tmp (sentence) VALUES ('Hello world. Todays department has 345 employees. Have a good day.')
INSERT #tmp (sentence) VALUES ('Hello world. Todays department has 15 emps. Have a good day. Oh and by the way there are 12 employees somewhere else')


SELECT 
    rid, sentence, dbo.fn_SplitRemoveJoin(sentence, '%[0-9] Emp%')
FROM #tmp t

返回

rid | sentence |  |
1 | Hello world. Todays department has 345 employees. Have a good day. | Hello world. Have a good day.|
2 | Hello world. Todays department has 15 emps. Have a good day. Oh and by the way there are 12 employees somewhere else | Hello world. Have a good day. |

答案 1 :(得分:2)

我也使用了分割/移除/连接技术。

要点是:

  • 这使用一对recursive CTEs,而不是UDF。
  • 这适用于所有英语句子结尾:.!?
  • 这会删除空格以对“数字然后员工”进行比较,因此您不必担心多个空格等。

这是SqlFiddle demo和代码:

-- Split descriptions into sentences (could use period, exclamation point, or question mark)
-- Delete any sentences that, without whitespace, are like '%[0-9]employ%'
-- Join sentences back into descriptions
;with Splitter as (
    select ID
        , ltrim(rtrim(Data)) as Data
        , cast(null as varchar(max)) as Sentence
        , 0 as SentenceNumber
    from Descriptions -- Your table here
    union all
    select ID
        , case when Data like '%[.!?]%' then right(Data, len(Data) - patindex('%[.!?]%', Data)) else null end
        , case when Data like '%[.!?]%' then left(Data, patindex('%[.!?]%', Data)) else Data end
        , SentenceNumber + 1
    from Splitter
    where Data is not null
), Joiner as (
    select ID
        , cast('' as varchar(max)) as Data
        , 0 as SentenceNumber
    from Splitter
    group by ID
    union all
    select j.ID
        , j.Data +
            -- Don't want "digit+employ" sentences, remove whitespace to search
            case when replace(replace(replace(replace(s.Sentence, char(9), ''), char(10), ''), char(13), ''), char(32), '') like '%[0-9]employ%' then '' else s.Sentence end
        , s.SentenceNumber
    from Joiner j
        join Splitter s on j.ID = s.ID and s.SentenceNumber = j.SentenceNumber + 1
)
-- Final Select
select a.ID, a.Data
from Joiner a
    join (
        -- Only get max SentenceNumber
        select ID, max(SentenceNumber) as SentenceNumber
        from Joiner
        group by ID
    ) b on a.ID = b.ID and a.SentenceNumber = b.SentenceNumber
order by a.ID, a.SentenceNumber

答案 2 :(得分:0)

一种方法。请注意,只有在所有句子中都有一个数字时才有效。

declare @d VARCHAR(1000) = 'Hello world. Todays department has 345 employees. Have a good day.'
declare @dr VARCHAR(1000)

set @dr = REVERSE(@d)

SELECT   REVERSE(RIGHT(@dr,LEN(@dr) - CHARINDEX('.',@dr,PATINDEX('%[0-9]%',@dr))))

 + RIGHT(@d,LEN(@d) - CHARINDEX('.',@d,PATINDEX('%[0-9]%',@d)) + 1)