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;
这篇关于基于公共标签搜索相关项目的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:基于公共标签搜索相关项目的算法


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01