基于公共标签搜索相关项目的算法

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;

这篇关于基于公共标签搜索相关项目的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)