javascript - 从字符串数组创建唯一组合数组

标签 javascript algorithm combinations combinatorics

我正在写一些东西,它接受一段文本并将其分解为可能的数据库查询,这些查询可用于查找类似的文本 block 。 (类似于我输入时生成的“类似问题”列表)基本过程:

  1. 从文本中删除停用词
  2. 去除特殊字符
  3. 从剩余的文本中创建一组独特的“词干”
  4. 创建一个词干数组的可能组合数组(我被卡住了……有点)

这是我目前所拥有的:

    //baseList starts with an empty array
    //candList starts with the array of unique stems
    //target is where the arrays of unique combinations are stored

    function createUniqueCombos(baseList,candList,target){

    for(var i=0;i<candList.length;i++){         

        //copy the base List
        var newList = baseList.slice(0);

        //add the candidate list item to the base list copy
        newList.push(candList[i]);

        //add the new array to the target array
        target.push(newList);   

        //re-call function using new array as baseList
        //and remaining candidates as candList
        var nextCandList = candList.slice(i + 1);       
        createUniqueCombos(newList,nextCandList,target);
    }

}

这可行,但在大于 25 个单词左右的文本 block 上,它会使我的浏览器崩溃。我意识到在数学上可能存在大量可能的组合。我想知道的是:

  1. 有没有更有效的方法来做到这一点?
  2. 如何定义最小/最大组合数组长度?

最佳答案

我认为您的逻辑存在根本性缺陷,因为您创建了多少组合。

我会采取的一种方法是;

  1. 将文本拆分为单个单词(我们将此变量称为 split_words)
  2. 去除特殊字符
  3. 删除短词/常用词(and, or, I, a);要么按长度来做,要么更聪明地用黑名单来做
  4. 有一个表(例如 blocks),其中包含列 block_idword
  5. 有一个 SQL 查询,例如

    SELECT block_id FROM blocks 
    WHERE word IN (split_words) GROUP BY block_id 
    ORDER BY COUNT(*) DESC
    

然后您将得到一个 block_ids 列表,这些列表根据 block 中共有的单词数量进行排序。

关于javascript - 从字符串数组创建唯一组合数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414843/

相关文章:

javascript - 如何使用 Angular 资源在每个请求上发送自定义 header ?

Javascript jQuery 右括号错误

javascript - 使用 JavaScript 检索 css 元素而不是 html

arrays - 对字符数组进行排序

python - 对任意数量的数组的所有可能组合求和并应用限制并返回索引

javascript - 使用js回调函数更新json文件内容

algorithm - 生成连接的凸多边形图

javascript - 如何在 Javascript 中检查每个对象字段的深度

arrays - 数组所有可能的组合

algorithm - 如何随机生成满足P[i] != i的前n个自然数的排列P?