【问题标题】:Group strings by longest common starting substring按最长公共起始子串对字符串进行分组
【发布时间】:2016-03-16 13:39:41
【问题描述】:

这就是问题所在。假设我有这些字符串:

  • 苹果 ipad mini 32gb
  • 苹果 ipad mini 64gb
  • 苹果 ipad air 64gb
  • 苹果 ipad air 32gb
  • 松下gh4
  • 三星 s2 银河
  • 三星s2银河红
  • 三星 s3 银河

我希望这些字符串像这样分组:

  • apple ipad mini:[apple ipad mini 32gb,apple ipad mini 64gb]
  • apple ipad air:[apple ipad air 64gb,apple ipad 32gb]
  • 松下 gh4:[松下 gh4]
  • 三星s2银河:[三星s2银河,三星s2银河红]
  • 三星 s3 银河

重点是将项目名称与其属性(颜色、内存容量等)分开。

我使用这个算法来寻找最长的公共子串: link

你们能分享一下你们的想法吗?无需代码或实现。谢谢。

已编辑:

    this.data = _.sortBy(this.data, function(item) {
        return item.title;
    });

    var i = 0;
    var groups = {};
    var len = this.data.length - 1;
    while(i < len) {
        var key = this.lcs(this.data[i][this.attr], this.data[i+1][this.attr]) || this.data[i][this.attr];
        groups[key] = true;
        i++;
        while(this.data[i][this.attr].startsWith(key) && i < len) {
            i++;
        }
    }
    console.log(groups) 

这很好用(仅测试添加键)。但我也想将三星 s3 Galaxy 添加到列表中。谢谢大家的帮助!

【问题讨论】:

  • 请分享您的代码
  • 您想讨论最长公共子字符串方法相对于其他方法的优点吗?你在寻找什么想法?替代品?
  • 我仍在研究解决方案,将代码更改了 40 次。我要求解决这个问题的一般模式或方法。感谢您的回复。
  • 如果您可以假设第一组文本是make(例如apple、panasonic等),那么您至少可以减少对的依赖最长公共子串 实现一个数量级。如果您可以假设第二组文本是“模型”,那么您可以进一步这样做……当您获得这些不符合“制造、模型、详细信息”顺序的字符串时,就会出现问题。跨度>
  • 我还要重新表述你的问题,因为它相当开放,并没有明确指导那些试图回答问题的人,如果有的话,你的问题到底是什么。

标签: javascript regex algorithm substring grouping


【解决方案1】:

如果您只想按最长的公共前缀简单地分组(这意味着即使“apple ipad”会产生更大的组,也会选择“apple ipad mini”),那么可能是这样的吗?

sort the list
i = 0
while i < end of list:
  key = longest common prefix of list[i] & list[i + 1]
        or list[i] if the common prefix is less than (1?) words or i is the last index
  groups[key] = list[i++]
  while key is prefix of list[i]:
    add list[i++] to groups[key]

【讨论】:

  • 好主意。我没有考虑排序。是否可以将三星 s3 Galaxy 也列为单独的键?我的意思是将所有不匹配的项目添加到列表中(虽然它有前缀“三星”,但它的项目不同)。
  • @AlexShevchenko 根据我建议的算法,“Samsung s3 galaxy”会生成它自己的密钥 - 因为当i 是最后一个索引时,“Samsung s3 galaxy”将没有当前密钥作为前缀(当前键是“Samsung s2 galaxy”)。 (顺便说一句,要投票,请点击投票分数上方或下方的小箭头。)
【解决方案2】:

尝试通过比较具有相同单词的两个字符串并查找单词的长度是否小于前一个路径来解决问题。

function groupObject(i, l) {
    return { item: i, length: l };
}

function group(r, a, i, o) {
    var rr = r.item.split(' '),
        aa = a.split(' '),
        j = 0,
        key, keys = [];

    while (aa[j] === rr[j]) {
        keys.push(aa[j]);
        j++;
    }
    if (keys.length < r.length && i < o.length - 1) {
        return group(groupObject(o[i + 1], 0), a, Number.MAX_VALUE, o);
    }
    key = keys.join(' ');
    if (!key || keys.length < r.length && i === o.length - 1) {
        key = a;
    }
    grouped[key] = grouped[key] || [];
    grouped[key].push(a);
    return groupObject(a, keys.length);
}

var data = ['apple ipad mini 32gb', 'apple ipad mini 64gb', 'apple ipad air 64gb', 'apple ipad air 32gb', 'panasonic gh4', 'samsung s2 galaxy', 'samsung s2 galaxy red', 'samsung s3 galaxy'],
    grouped = {};

data.reduce(group, groupObject(data[1], 0));
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

【讨论】:

  • 谢谢你,尼娜。干得好,帮了大忙。是否可以将三星 s3 Galaxy 也列为单独的键?我的意思是将所有不匹配的项目添加到列表中(尽管它具有前缀“三星”,但它的项目不同)。另一个问题:我怎样才能给你们投票?
  • 哇。你是我的英雄。
  • @AlexShevchenko,不,消防员是英雄。
猜你喜欢
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2019-09-01
  • 2013-09-27
相关资源
最近更新 更多