Har*_*ech 7 javascript algorithm combinations combinatorics
我正在编写一些需要一段文本的内容,并将其分解为可用于查找类似文本块的可能的数据库查询.(类似于我输入时生成的"类似问题"列表)基本过程:
这是我到目前为止所拥有的:
//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个字左右的文本块上,它会崩溃我的浏览器.我在数学上意识到可能存在大量可能的组合.我想知道的是:
我认为你的逻辑从根本上是有缺陷的,因为你创建了多少组合。
我采取的方法是;
split_words)blocks),其中包含列block_id和word有一个 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顺序取决于块有多少共同的单词。