Algorithm that searches for related items based on common tags(基于公共标签搜索相关项目的算法)
问题描述
让我们以 StackOverflow 问题为例.他们每个人都分配了多个标签.如何构建一个算法,根据他们有多少常见标签(按常见标签数量排序)找到相关问题?
Lets take StackOverflow questions as example. Each of them has multiple tags assigned. How to build an algorithm that would find related questions based on how many common tags they have (sorted by number of common tags)?
现在我想不出比将所有至少具有一个公共标签的问题放入一个数组中,然后循环遍历它们,为每个项目分配多个公共标签,然后对该数组进行排序更好的方法.
For now I can't think about anything better than just selecting all questions that have at least one common tag into an array and then looping through them all assigning number of common tags to each item, then sorting this array.
还有更聪明的方法吗?完美的解决方案是单个 sql 查询.
Is there more clever way of doing it? Perfect solution would be a single sql query.
推荐答案
这可能和 O(n^2) 一样糟糕,但它确实有效:
This could be as bad as O(n^2), but it works:
create table QuestionTags (questionid int, tag int);
select q1.questionid, q2.questionid, count(*) as commontags
from QuestionTags q1 join QuestionTags q2
where q1.tag = q2.tag and q1.questionid < q2.questionid
group by q1.questionid, q2.questionid order by commontags desc;
这篇关于基于公共标签搜索相关项目的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:基于公共标签搜索相关项目的算法
基础教程推荐
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01