如何编写分组算法,通过具有 3 个或更多相似属性对数据集中的项进行分组



我正在为我的客户构建一个工具,该工具根据前 10 个 Google 搜索网址将关键字分组在一起。关键字表示为包含 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的输出是输出.csv,但我的解决方案要么在组中放置较少的关键字,要么创建错误的组。

其中一个可能的问题可能是您过滤dataset数组的方式,该数组正在循环并通过在其自己的循环中过滤。您可以过滤dataset并删除当前关键字并将其放入另一个变量中,然后对其进行 foreach 而不是修改dataset本身。

查看结果,我认为这是不正确的,因为这会导致 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.forEach函数内改变dataset

我认为这需要

//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);

这可以简化为单个reducefn 调用,以避免副作用和混淆:

const out = data.reduce((acc, current, index) => {
const maybeGroup = acc.findIndex(
el => _.intersection(current.urls, el.urls).length > 3
);
if (maybeGroup !== -1 || maybeGroup === 0) {
if(!acc[maybeGroup].searches.includes(current.keyword)) {
acc[maybeGroup].searches.push(current.keyword);
return acc;
}
return acc;
} else {
acc.push({
mainKeyword: current.keyword,
searches: [],
urls: current.urls
});
return acc;
}
}, []);
console.log(out.map(x => _.pick(x, ['mainKeyword', 'searches'])))

这当然忽略了计数和排序逻辑,但这可以很容易地添加到现有代码中。

最新更新