我正在寫一些需要一段文本並將其分解成可用於查找可能的數據庫查詢的內容,這些查詢可用於查找類似的文本塊。 (產生類似於「類似的問題」清單的東西,而我鍵入此)的基本過程:從字符串數組創建獨特組合陣列
- 從文本中刪除停用詞
- 刪除特殊字符
- 從剩餘的文本創建的獨特數組「莖」
- 創建的陣列的可能組合的陣列的莖(種......在那裏我卡住了)
這是我到目前爲止有:
//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!您的瀏覽器可能會因使用的內存量而崩潰,或者遞歸太多。 – Bojangles 2012-07-10 13:54:54
你是否真的需要所有的組合?你不能在處理它們時立即生成它們而不是積累龐大的數組?也嘗試重寫你的算法迭代而不是遞歸。 – 2012-07-10 13:57:00
謝謝,我已經有很長一段時間了)@ OlegV.Volkov不,我不需要所有組合,我希望能夠爲返回的組合數組定義最小/最大長度。感謝您的迭代建議。 – HartyeTech 2012-07-10 14:06:18