【问题标题】:Suggestion for a tag cloud algorithm对标签云算法的建议
【发布时间】:2010-08-12 21:02:55
【问题描述】:

我有一个 MSSQL 2005 表:

[Companies](
    [CompanyID] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](128),
    [Description] [nvarchar](256),
    [Keywords] [nvarchar](256)
)

我想为这些公司生成标签云。但我已将所有关键字保存在一列中,以逗号分隔。关于如何通过最常用的关键字生成标签云的任何建议。可能有数百万家公司,每家公司大约有十个关键字。

谢谢。

【问题讨论】:

    标签: c# asp.net sql-server sql-server-2005 tag-cloud


    【解决方案1】:

    第1步:将关键字分成适当的关系(表格)。

    CREATE TABLE Keywords (KeywordID int IDENTITY(1,1) NOT NULL
      , Keyword NVARCHAR(256)
      , constraint KeywordsPK primary key (KeywordID)
      , constraint KeywordsUnique unique (Keyword));
    

    第 2 步:将公司和标签之间的多对多关系映射到一个单独的表中,就像所有多对多关系一样:

    CREATE TABLE CompanyKeywords (
       CompanyID int not null
       , KeywordID int not null
       , constraint CompanyKeywords primary key (KeywordID, CompanyID)
       , constraint CompanyKeyword_FK_Companies
          foreign key (CompanyID)
          references Companies(CompanyID)
       , constraint CompanyKeyword_FK_Keywords
          foreign key (KeywordID)
          references Keywords (KeywordID));
    

    第 3 步:使用简单的 GROUP BY 查询生成“云”(例如,将“云”表示最常见的 100 个标签):

    with cte as (
    SELECT TOP 100 KeywordID, count(*) as Count
    FROM CompanyKeywords
    group by KeywordID
    order by count(*) desc)
    select k.Keyword, c.Count
    from cte c
    join Keyword k on c.KeywordID = k.KeywordID;
    

    第 4 步:缓存结果,因为它很少更改并且计算成本很高。

    【讨论】:

    • 谢谢大家。尽管其他答案都解决了我的问题,但我会进行规范化。
    【解决方案2】:

    我更希望将您的设计规范化为 suggested by Remus,但如果您处于无法更改设计的地步...

    您可以使用解析函数(我将使用的示例取自 here)来解析您的关键字并计算它们。

    CREATE FUNCTION [dbo].[fnParseStringTSQL] (@string NVARCHAR(MAX),@separator NCHAR(1))
    RETURNS @parsedString TABLE (string NVARCHAR(MAX))
    AS 
    BEGIN
       DECLARE @position int
       SET @position = 1
       SET @string = @string + @separator
       WHILE charindex(@separator,@string,@position) <> 0
          BEGIN
             INSERT into @parsedString
             SELECT substring(@string, @position, charindex(@separator,@string,@position) - @position)
             SET @position = charindex(@separator,@string,@position) + 1
          END
         RETURN
    END
    go
    
    create table MyTest (
        id int identity,
        keywords nvarchar(256)
    )
    
    insert into MyTest
        (keywords)
        select 'sql server,oracle,db2'
        union
        select 'sql server,oracle'
        union
        select 'sql server'
    
    select k.string, COUNT(*) as count
        from MyTest mt
            cross apply dbo.fnParseStringTSQL(mt.keywords,',') k
        group by k.string
        order by count desc
    
    drop function dbo.fnParseStringTSQL
    drop table MyTest
    

    【讨论】:

      【解决方案3】:

      Remus 和 Joe 都是正确的,但是就像 Joe 所说的那样,如果你别无选择,那么你必须接受它。我想我可以通过使用 XML 数据类型为您提供一个简单的解决方案。您已经可以通过执行此查询轻松查看已解析的列

      WITH myCommonTblExp AS (
          SELECT CompanyID,
          CAST('<I>' + REPLACE(Keywords, ',', '</I><I>') + '</I>' AS XML) AS Keywords
          FROM Companies
      )
      SELECT CompanyID, RTRIM(LTRIM(ExtractedCompanyCode.X.value('.', 'VARCHAR(256)'))) AS Keywords
      FROM myCommonTblExp
      CROSS APPLY Keywords.nodes('//I') ExtractedCompanyCode(X)
      

      现在知道你可以做到这一点,你所要做的就是对它们进行分组和计数,但你不能对 XML 方法进行分组,所以我的建议是创建上面查询的视图

      CREATE VIEW [dbo].[DissectedKeywords]
      AS
      WITH myCommonTblExp AS (
          SELECT 
          CAST('<I>' + REPLACE(Keywords, ',', '</I><I>') + '</I>' AS XML) AS Keywords
          FROM Companies
      )
      SELECT RTRIM(LTRIM(ExtractedCompanyCode.X.value('.', 'VARCHAR(256)'))) AS Keywords
      FROM myCommonTblExp
      CROSS APPLY Keywords.nodes('//I') ExtractedCompanyCode(X)
      GO
      

      并对该视图进行计数

      SELECT Keywords, COUNT(*) AS KeyWordCount FROM DissectedKeywords
      GROUP BY Keywords
      ORDER BY Keywords
      

      这里是全文-->http://anyrest.wordpress.com/2010/08/13/converting-parsing-delimited-string-column-in-sql-to-rows/

      【讨论】:

        猜你喜欢
        • 2011-01-03
        • 1970-01-01
        • 2011-07-15
        • 2011-12-15
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 2015-07-10
        • 2011-04-22
        相关资源
        最近更新 更多