javascript - 如何编写分组算法,通过具有 3 个或更多相似属性对数据集中的项目进行分组
问题描述
我正在为我的客户构建一个工具,它根据前 10 个 Google 搜索 URL 将关键字分组在一起。关键字表示为包含 URL 数组的 JavaScript 对象。分组标准是,如果两个关键字有3 个或更多的公共 URL,则它们属于同一组。此外,生成的组中不应有关键字重复,并且在分组之前未预定义生成的组的总数。我将不胜感激有关此问题的逻辑部分的任何建议,谢谢!
到目前为止,我设法开发了下面提供的算法,但它仍然会重复并且不会 100% 正确分组关键字(某些关键字应该在同一个组中,但它们不是)。
function makeKeywordGroupsNew(results: Result[], uid: string): Group[] {
let dataset = results;
let groups: any[] = [];
// loop thru all records in dataset
dataset.forEach((current: Result) => {
// initialize the group with the current keyword in it
const group = { volume: 0, items: [current] };
// remove the current keyword from the dataset
dataset = dataset.filter(el => el.keyword !== current.keyword);
// loop thru the new dataset and push the other keyword into the group if it has >=3 urls in common with current keyword
dataset.forEach((other: Result) => {
const urlsInCommon = _.intersection(current.urls, other.urls);
if (urlsInCommon.length >= 3) {
group.items.push(other);
}
});
// sum the keyword volumes to form the group volume - not important for the core logic
// @ts-ignore
group.volume = _.sum(group.items.map(item => item.volume));
// sort keywords in the formed group by volume - not important for the core logic
// @ts-ignore
group.items = group.items
.sort((a, b) => {
if (a.volume < b.volume) return 1;
if (a.volume > b.volume) return -1;
return 0;
})
.map(el => el.keyword);
// add the newly formed group to the groups array (the result)
groups.push(group);
});
// exclude the groups with only one keyword inside
groups = groups.filter(group => group.items.length > 1);
// delete keyword duplicates inside of the group
groups = groups.map(group => ({ ...group, items: _.uniq(group.items) }));
// form the correct result object shape - not important for the core logic
return groups.map(group => ({
uid,
main: group.items[0],
keywords: group.items.slice(1, group.length),
volume: group.volume
}));
}
我希望input.json的输出是output.csv,但我的解决方案要么在组中放置较少的关键字,要么创建错误的组。
解决方案
查看结果,我认为这是不正确的,因为这会导致 135 个“组”,预期的数字是 87,而原始代码产生 88
我认为您的问题就在代码的顶部
let dataset = results;
let groups: any[] = [];
dataset.forEach((current: Result) => {
const group = { volume: 0, items: [current] };
dataset = dataset.filter(el => el.keyword !== current.keyword);
你在函数dataset
内部发生变异dataSet.forEach
我认为这需要
//let dataset = results; remove this
let groups: any[] = [];
results.forEach((current: Result) => {
const group = { volume: 0, items: [current] };
const dataset = results.filter(el => el.keyword !== current.keyword);
推荐阅读
- django - 当 DEBUG 为 True 时,Django 响应字节文字
- c# - 如何阻止“canvas.DrawString”循环?拉绳有替代品吗?
- json - 使用 Axios 在 React 中获取 Reddit 帖子,奇怪的 JSON 路径
- mysql - 如何优化需要大量时间的“优化”MYSQL 查询
- python - python中的排列,但只允许每个元素最多使用n次
- cordova - 我正在创建一个 Ionic 应用程序并在添加 android 平台时遇到 cordova-res 错误
- linux - 如何使 Microphone() 函数在 Linux 的 SpeechRecognition 中工作
- python - 为什么矩阵元素会根据 python numpy 中的前几行发生变化?
- oracle - 使用 @QueryHint 和 spring 数据存储库获取 jpa sql 执行计划
- c# - C#如何将文本框中的数据插入datagridview,然后将datagridview数据保存到数据库中?