我正在写一些东西,它接受一段文本并将其分解为可能的数据库查询,这些查询可用于查找类似的文本 block 。 (类似于我输入时生成的“类似问题”列表)基本过程:
- 从文本中删除停用词
- 去除特殊字符
- 从剩余的文本中创建一组独特的“词干”
- 创建一个词干数组的可能组合数组(我被卡住了……有点)
这是我目前所拥有的:
//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 上,它会使我的浏览器崩溃。我意识到在数学上可能存在大量可能的组合。我想知道的是:
- 有没有更有效的方法来做到这一点?
- 如何定义最小/最大组合数组长度?
最佳答案
我认为您的逻辑存在根本性缺陷,因为您创建了多少组合。
我会采取的一种方法是;
- 将文本拆分为单个单词(我们将此变量称为
split_words
) - 去除特殊字符
- 删除短词/常用词(and, or, I, a);要么按长度来做,要么更聪明地用黑名单来做
- 有一个表(例如
blocks
),其中包含列block_id
和word
有一个 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/