【问题标题】:Word Co-occurrences in SQL - is this even possible?SQL中的单词共现 - 这甚至可能吗?
【发布时间】:2013-09-20 19:18:03
【问题描述】:

我有一个看起来像这样的数据集

id | sentence                       | tags
1  | "people walk dogs in the park" | "pet park health"
2  | "I am allergic to dogs"        | "allergies health"

是否可以使用sql查询找到每个标记词和每个句子词之间的数量共现?这会很困难,因为您必须解析每个标签和句子条目。

它可能看起来像

select sentence_word,tag_word,count(id)
from
(select id,sentence_word
from table)A

join

(select id, tag_word
from table)B

on A.id=B.id
group by sentence_word,tag_word

除非我知道这两个子查询不正确

以下是一些示例结果

 tag_word   | sentence_word  | count(id)
"walk"      |"pet"           |1
"health"    |"dogs"          |2
"allergies" |"dogs"          |1

【问题讨论】:

  • 请发布样本结果。
  • 如果你规范化标签,你会帮助自己。
  • 对不起。我没有任何结果,我只是想找到一种策略来解决这个问题。
  • 共现是指具有共同标签的句子数吗?例如。在上面的例子中,它应该返回 2 表示健康,1 表示其余部分?
  • 对不起@danihp,我刚刚意识到我误解了你所说的,我添加了一些示例结果。

标签: sql parsing mysqli split unpivot


【解决方案1】:

我可以提出以下行动计划:

  1. 将两列中的每一列移到各自的临时数据库中

  2. 调用stored procedurelike this one for MySQL)将字符串字段转换为列

  3. CROSS JOIN两个临时表

  4. 在结果数据集上运行COUNT DISTINCT

以上步骤可以组合成一个自己的存储过程。

这里是an article on splitting 代表SQL Server

在某些 SQL 实现中,拆分可以实现为user defined functions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2019-09-08
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多