# 自动根据标题中的关键词重新分类导入的论坛主题

**URL:** https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663
**Category:** Migration
**Created:** [2020年九月20日 23:40 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663 "2020-09-20T23:40:12Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2020年九月20日 23:40 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/1 "2020-09-20T23:40:12Z")

</div>

你好，

我正在导入一个关于独轮车运动的旧的大型论坛。

旧的分类不够理想，许多不同类别的内容混杂在一起。

因此，我正在重新组织分类。

起初，我打算手动重新分类最近几百个主题，而保留旧主题的分类不变。  
我的想法是面向未来，而不是纠结于过去。旧主题分类不够准确其实无关紧要，最重要的是它们仍然可以访问。

但我也在想，是否可以通过关键词自动重新分类主题，实际上能取得不错的效果。

目前，我们绝大多数主题——超过总数的一半！——都集中在一个分类中（😱）。

我可以在标题中针对这些关键词：“学习”、“训练”、“姿势”等……并将所有相关主题归入 **#riding-advice** 分类。

同样的方法也可以应用于“车架”、“轮子”、“轮胎”、“座垫”等关键词，这些可以归入 **#unicycles-and-equipments** 分类。

我将针对被空格包围的单词，并尝试预判多词表达，以减少“误判”。例如：“wheelwalking”是一种独轮车技巧，可能应该归入 **#riding-advice** 分类。如果我只针对“wheel”这个词而不多加考虑，就会产生一些本可以轻松避免的误判（不过，我可以先将包含“wheel”的主题从 A 移动到 B，然后再将包含“wheelwalking”的主题从 B 移动到 C……）。

有人做过类似的事情吗？你们有什么建议或想法可以降低“误判”的风险吗？在这样做之前，有哪些明显（或不明显）的事情是我需要了解的？

大约有 70000 个主题需要处理。

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://meta.discourse.org/u/EricGT)
#### Post date: [2020年九月21日 09:30 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/2 "2020-09-21T09:30:14Z")

</div>

> [@Canapin](#):
>
> 在做这件事之前，有什么明显（或不明显）的事情是我需要知道的吗？

一点建议：不要认为必须一次就做到完美。

你寻找关键词的思路，和我会采取的第一步完全一致。不要害怕把你第一次尝试所做的工作全部推翻。如果结果不是你想要的，就吸取第一次尝试中学到的经验，从头再来。

* * *

_ **编辑** _

在快速搜索一些用于词分析的免费工具时，发现了这个关于 [文本分析](https://monkeylearn.com/text-analysis/) 的信息页面。值得一读。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2020年九月21日 09:57 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/3 "2020-09-21T09:57:44Z")

</div>

我之前在处理类似项目时，曾使用基于 K-means 聚类的无监督学习方法。这将是一个非常有趣的实验，说不定算法还能提出更优的分类方案呢；)

你可以在这里了解这种方法的详情：[https://towardsdatascience.com/applying-machine-learning-to-classify-an-unsupervised-text-document-e7bb6265f52](https://towardsdatascience.com/applying-machine-learning-to-classify-an-unsupervised-text-document-e7bb6265f52)

> [@Canapin](#):
>
> 你有什么建议或想法来降低“误报”的风险吗？

正如 @EricGT 所说：不要害怕迭代，但“差不多”就足够了，同时可以准备一些 TL3 用户，在必要时进行重新分类。

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2020年九月21日 16:45 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/4 "2020-09-21T16:45:40Z")

</div>

这很有趣！

不过我可能既没有时间也没有技能去尝试这种方法（论坛已经宕机一个多月了，我还有很多工作要做！）。

经过初步尝试，手动选择关键词似乎效果相当不错，尽管我尚未重新分类，只是用 SQL 查询做了一些测试。

```sql
select title from topics
where category_id = 10
and lower(title) not like '%saddle%'
and lower(title) not like '%crank%'
and lower(title) not like '%pedal%'
and lower(title) not like '%rim%'
and lower(title) not like '%carbon%'
and lower(title) not like '%spoke%'
and lower(title) not like '%wheel%'
and lower(title) not like '%frame%'
and lower(title) not like '%hub%'
and lower(title) not like '%tubeless%'
and lower(title) not like '%disk%'
and lower(title) not like '%hydraulic%'
and lower(title) not like '%duro%'
and lower(title) not like '%dominator%'
and lower(title) not like '%torker%'
and lower(title) not like '%nimbus%'
and lower(title) not like '%bearing%'
and lower(title) not like '%pad%'
and lower(title) not like '%repair%'
and lower(title) not like '%handlebar%'
and lower(title) not like '%kh%'
and lower(title) not like '%kris holm%'
and lower(title) not like '%coker%'
and lower(title) not like '%tube%'
and lower(title) not like '%build%'
and lower(title) not like '%29er%'
and lower(title) not like '%36er%'

and lower(title) not like '%backwards%'
and lower(title) not like '%riding%'
and lower(title) not like '%foot%'
and lower(title) not like '%train%'
and lower(title) not like '%training%'
and lower(title) not like '%learn%'
and lower(title) not like '%learning%'
and lower(title) not like '%dismount%'
and lower(title) not like '%habit%'
and lower(title) not like '%idle%'
and lower(title) not like '%idling%'
and lower(title) not like '%freemount%'
and lower(title) not like '%free mount%'
and lower(title) not like '%free mounting%'

```

该查询从主类别的 52000 个主题中返回了 33000 个可重新分类的主题。这个数字看起来是合理的，但我可能还需要添加更多关键词。

这种方法似乎足够可靠。

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2021年二月14日 00:05 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/5 "2021-02-14T00:05:18Z")

</div>

你最后在这里做了什么？

如果你的主题中包含足够独特的关键词（我假设你正在遍历所有主题回复并统计每篇帖子中的关键词），那么根据主题中是否包含足够多独特且具体的关键词来自动对主题进行分类，可能是可行的。

（不过，这主要用于迁移场景，因为在活跃的论坛中，你希望主题从一开始就位于正确的分类下。）

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2021年二月14日 00:23 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/6 "2021-02-14T00:23:03Z")

</div>

我通过检查标题中的关键词将主题移动到了其他分类。效果不错，比之前的混乱状态好多了。

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2021年二月14日 00:27 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/7 "2021-02-14T00:27:59Z")

</div>

这是个很好的观点：如果某个特定词汇频繁出现在众多主题标题中，这往往是需要设立新分类的有力证据。🤔

---

<div class="post-metadata">

### Author: ![LotusJeff](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lotusjeff/32/477888_2.png) [@LotusJeff](https://meta.discourse.org/u/LotusJeff)
#### Post date: [2024年十二月20日 01:46 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/8 "2024-12-20T01:46:25Z")

</div>

您是通过查询执行此操作的吗？如果是，查询模板是什么？运行查询后是否需要执行任何其他操作来确保数据库完整性？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年十二月20日 03:46 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/9 "2024-12-20T03:46:21Z")

</div>

这听起来像是在导入脚本上完成的，因此它将被修改为从标题推断类别。

您正在进行导入吗？从什么软件导入？如果它已经在 Discourse 中存在，那么您可以从 Rails 进行。

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [2024年十二月20日 19:00 UTC](https://meta.discourse.org/t/automatically-re-categorize-topics-from-an-imported-forum-by-targeting-keywords-in-titles/164663/10 "2024-12-20T19:00:37Z")

</div>

据我记忆，自从我协助他完成许多与 Discourse 相关的工作以来，我记得他在导入后使用了一个 Rails 脚本。他通过标题中的关键词选择主题，然后使用官方文档记录的命令来移动它们，例如 [Administrative Bulk Operations](https://meta.discourse.org/t/administrative-bulk-operations/118349#p-582358-moving-topics-2%E3%80%82)

我还记得移动带有标签、官方命令和 rake 任务的主题并没有完全更新某些表，相关的周期性 Sidekiq 作业也没有。

我不知道现在是否还是这样，但这可能是需要注意的一点，请参见 [https://meta.discourse.org/t/bulk-tagged-topics-then-moved-topics-into-another-category-but-the-category-tag-selector-doesnt-show-tags/213136/3。](https://meta.discourse.org/t/bulk-tagged-topics-then-moved-topics-into-another-category-but-the-category-tag-selector-doesnt-show-tags/213136/3%E3%80%82)

希望这有帮助！
