在javascript中,我有一个字符串数组。
[
"test",
"tests",
"abc",
"abcdef"
]
我想创建一个只包含字符串唯一词干的新数组。例如,上面的数组会减少到......
[
"test",
"abc"
]
...因为“test”是“测试”的词干,“abc”是“abcdef”的词干。
最简单的方法是什么?
答案 0 :(得分:0)
最简单的方法是循环。首先,我建议按字母顺序排序,所以你会这样做:
var myArray = ["test", "tests", "abc", "abcdef"];
//this sorts from smallest to largest
myArray.sort(function(a,b){return a.length - b.length});
所以,现在myArray从最小到最大排序。现在,您将遍历每个元素以检查它是否是下一个元素的词干。
//this is the array where we will store the stems
var stemArray = [];
//the temporary stem goes here
var stem;
//this variable is used to capture a substring from each string
// to check against the stem variable
var check;
//loop over all the variables except the last
//since they are ordered from smallest to last, we are guaranteed that
//the last object wont be much of a stem
//and thus we can avoid that check
for (var i = 0; i < myArray.length - 1; i++){
//set the current stem
stem = myArray[i];
//then loop over the remainding objects
for (var j = i+1; j < myArray.length; j++){
//capture a substring
//so for example, stem = "abc" and the element we're testing against
//is "test", check will be equal to "tes" so the size matches "abc"
check = myArray[j].substring(0,stem.length);
//if stem and check are the same
//and since you wanted unique we check if it is unique
//alternatively, we could just break inside the next
//conditional statement and it would be more efficient
//and thus remove the indexOf test
//but i include it to explain the logic of how the algorithm works
if (stem === check){
if (stemArray.indexOf(stem) === -1){
//add the verified stem to the array of stems
stemArray.push(stem);
}
//In the case of ["t", "te", "test"], the above code
//will output ["t", "te"]
//if you want it to output just ["t"], uncomment the following
//myArray.splice(j,1);
//j--;
//these lines will remove the duplicate from myArray
//and correct the iteration due to the deletion
}
}
}
答案 1 :(得分:0)
“简单”当然是相对的。
采用一种简单的观点,即词干将始终与较长词的第一个字符匹配,对数组进行排序,使词干位于较长的词之前(例如“测试”之前的“测试”),然后迭代数组并测试每个成员针对以下成员,删除那些作为词干扩展的成员,例如
function getStems(arr) {
var a = arr.slice().sort();
var stem, len;
for (var i=0, iLen=a.length - 1; i<iLen; i++) {
stem = a[i];
len = stem.length;
while (i<iLen && stem == a[i+1].substring(0,len)) {
a.splice(i+1, 1);
--iLen;
}
}
return a;
}
var a = ["test","tests","abcdef","abcf","abcdefqw","abc"];
alert(getStems(a)); // abc, test
但是,如果给出“a”,“any”,“any”,上面的内容将返回“a”,这可能不是您想要的。
答案 2 :(得分:0)
您还需要使用下划线字符串。 [https://github.com/epeli/underscore.string]
// make a new array that is the old array without...
var myArrayWithNoStems =_.without(myArray,function(word) {
// anything that matches the filter
return _.find(myArray, function(word2) {
// if this word wholly includes another word, return true
if (word != word2 && _.str.include(word, word2)) return true;
})
});