【发布时间】:2012-07-10 00:28:27
【问题描述】:
我正在写一些东西,它需要一段文本并将其分解为可能的数据库查询,这些查询可用于查找相似的文本块。 (类似于我输入时生成的“类似问题”列表)基本过程:
- 从文本中删除停用词
- 删除特殊字符
- 从剩余的文本中创建一组独特的“词干”
- 创建一系列可能的茎数组组合(我被卡住的地方......有点)
这是我目前所拥有的:
//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 个字左右的文本块上,它会使我的浏览器崩溃。我意识到在数学上可能存在大量可能的组合。我想知道的是:
- 有没有更有效的方法来做到这一点?
- 如何定义最小/最大组合数组长度?
【问题讨论】:
-
这是一个很棒的第一个问题。欢迎来到 StackOverflow!您的浏览器可能会因使用的内存量而崩溃,或者递归过多。
-
你真的需要一次所有的组合吗?你不能在生成它们时立即处理它们而不是积累巨大的数组吗?还尝试将您的算法重写为迭代而不是递归。
-
谢谢,我作为旁观者已经有一段时间了 ;) @OlegV.Volkov 不,我不需要所有组合,我希望能够定义最小/最大长度返回的组合数组。感谢您的迭代建议。
标签: javascript algorithm combinations combinatorics