从字符串数组创建唯一组合的数组

Har*_*ech 7 javascript algorithm combinations combinatorics

我正在编写一些需要一段文本的内容,并将其分解为可用于查找类似文本块的可能的数据库查询.(类似于我输入时生成的"类似问题"列表)基本过程:

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

}
Run Code Online (Sandbox Code Playgroud)

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

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

Mat*_*att 1

我认为你的逻辑从根本上是有缺陷的,因为你创建了多少组合。

我采取的方法是;

  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
    
    Run Code Online (Sandbox Code Playgroud)

然后您将得到一个列表,其中的block_ids顺序取决于块有多少共同的单词。