简体   繁体   English

获取所有相关标签?

[英]get all related tags?

this is very tricky. 这非常棘手。 i´ve got a table with 2 columns thread_tag_map: thread_id and tag_name. 我有一个包含2列thread_tag_map的表:thread_id和tag_name。

  thread_id tag_name
  1         football
  1         manchester
  2         manchester
  2         england
  3         england
  3         queen
  4         queen
  4         diana

as you can see one thread can have multiple tags, and this give us a linking effect of tags. 如您所见,一个线程可以有多个标签,这给了我们标签的链接效果。

if you type the tag football i want it to show all related tags to football. 如果您键入足球标签,我希望它显示与足球相关的所有标签。 that is to say manchester, england, queen and diana. 也就是说曼彻斯特,英国,皇后和戴安娜。

so here is what ive coded so far: 因此,这是我到目前为止编码的内容:

    // get all thread_id:s for tag_name
    $query = "SELECT *
            FROM thread_tag_map
            WHERE tag_name = 'football'";

    $result1 = mysqli_query($conn, $query) or die ("Couldn't execute query: " . mysqli_error($conn));

    // get all tag_name:s for each thread_id
    while($row = mysqli_fetch_assoc($result1))
    {
        $thread_id = $row['thread_id'];

        $query = "SELECT *
                FROM thread_tag_map
                WHERE thread_id = $thread_id";

        $result2 = mysqli_query($conn, $query) or die ("Couldn't execute query: " . mysqli_error($conn));

    // add each tag to array
    while($row = mysqli_fetch_assoc($result2))
    {
        $tag_array[] = $row['tag_name'];
      }
   }

but this just give me football and manchester. 但这给了我足球和曼彻斯特。 i dont know how i can proceed to make it a good code to loop (for loop?) it through. 我不知道如何继续使它成为循环播放(循环)的好代码。 maybe there is 100 related tags. 也许有100个相关标签。

i think you understand the idea. 我想你明白这个主意。 has someone done this before? 有人做过这个吗?

You can formalize what you're trying to do a bit here with graph theory. 您可以在这里用图论来形式化您要尝试做的事情。 You want all the connected nodes to a given one, given an adjacency list (sort-of). 您希望将所有连接的节点连接到给定的节点,并给出一个邻接列表(排序)。 To do this, you would want to do a breadth first search of the graph. 为此,您需要对图形进行广度优先搜索。 This is important to avoid cycles. 这对于避免循环很重要。

The representation you've chosen isn't entirely efficient, though, but could certainly work. 尽管您选择的表示形式并不完全有效,但是肯定可以。

In pseudo-code, your algorithm should look something like this: 在伪代码中,您的算法应如下所示:

interesting-tags = input-tag
output = empty
for tag in interesting-tags:  (Must be in order)
    select related-tags to tag
    for newtag in related-tags:
        if newtag is not in output:
             append newtag to interesting-tags and output

return output

So here, interesting-tags should be some form of a Queue, since you need to add new items to the back and take them from the front. 因此,在这里,有趣的标签应该是队列的某种形式,因为您需要在后面添加新项目,并从前面获取它们。

Output should be a Set data type, since you need to check if things are already in the set, and add them. 输出应该是Set数据类型,因为您需要检查集合中是否已存在并添加它们。

I am, however, unfamiliar with PHP and so I don't know what is available to you. 但是,我不熟悉PHP,所以我不知道有什么可用。 At very least, you can implement the operations you need with just an array, even if it might not be totally efficient, it'll work for a few tags. 至少,您可以只用一个数组来实现所需的操作,即使它可能不是完全有效的,它也适用于一些标签。

If you're building some kind of forum / discussion board, and if by related tags you mean all the tags that were posted in threads that relate with a board / forum, would't it be easier to just select them all? 如果您要构建某种类型的论坛/讨论板,并且用相关标签来表示所有与委员会/论坛相关的主题中发布的标签,那么将它们全部选中会不会更容易? Since you create tags based on the thread, they should be all correctly placed, so you just have to get them all. 由于您是基于线程创建标签的,因此应该正确放置所有标签,因此您只需要全部获取它们。

Also, I'd suggest you create a temporary table just for that purpose, where you store the "related tags" once they're needed in the first place, saving the array in that table for future search, and updating it when some new word is given as a tag. 另外,我建议您为此目的创建一个临时表,首先将“相关标签”存储在其中,然后将其保存在该表中以供将来搜索,并在需要时对其进行更新单词作为标签给出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM