Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在数组中查找重复的短语(不仅仅是单词)_Javascript_Arrays_Regex - Fatal编程技术网

Javascript 在数组中查找重复的短语(不仅仅是单词)

Javascript 在数组中查找重复的短语(不仅仅是单词),javascript,arrays,regex,Javascript,Arrays,Regex,假设我有一个数组: [ "I want **a dog**", "**A dog** is here", "Pet **a dog**", "A **red cat**", "**red cat** is cute" ... ] 我如何找出重复的短语,而不仅仅是单词? 例如,我希望返回“一只狗”和“一只红猫” 我找到的大多数现有帖子都是关于获取单个单词的,而不是短语(多个单词) 这不是javascript函数的最终版本,可以进一步优化。也可能需要很少的更改,但它可以满足您的需求 函数GetP

假设我有一个数组:

[
"I want **a dog**",
"**A dog** is here",
"Pet **a dog**",
"A **red cat**",
"**red cat** is cute"
...
]
我如何找出重复的短语,而不仅仅是单词? 例如,我希望返回“一只狗”和“一只红猫”


我找到的大多数现有帖子都是关于获取单个单词的,而不是短语(多个单词)

这不是javascript函数的最终版本,可以进一步优化。也可能需要很少的更改,但它可以满足您的需求

函数GetPhrases(stringsArray){
//数组将字符串拆分为单词。
var jaggedArray=[];
//数组来保存字符串的索引,其中两个匹配的字一起找到。
var newArray=[];
var短语=[];
//在数组中循环
对于(变量ic=0;ic0){
//如果匹配索引大于0,则查找该单词之前的单词是否也匹配。
变量indexofPrevWord=jaggedArray[il].indexOf(jaggedArray[iLoop][ik-1]);
如果((indexofPrevWord>=0)和&(indexofPrevWord==(indexOfFind-1)))
if(newArray.indexOf(il+“-”+iLoop)<0)
推送(il+“-”+iLoop);
//如果匹配索引大于0,则查找该单词后面的单词是否也匹配。
var indexofNextWord=jaggedArray[il].indexOf(jaggedArray[iLoop][ik+1]);
如果(indexofNextWord>=0&(indexofNextWord==(indexOfFind+1)))
if(newArray.indexOf(il+“-”+iLoop)<0)
推送(il+“-”+iLoop);
}
else if(indexOfFind=0){
//如果匹配索引大于0,则查找该单词后面的单词是否也匹配。
变量indexofNewWord=jaggedArray[il].indexOf(jaggedArray[iLoop][ik+1]);
如果(indexofNewWord>=0&&(indexofNewWord==(indexOfFind+1)))
if(newArray.indexOf(il+“-”+iLoop)<0)
推送(il+“-”+iLoop);
}
}
}
}
//newArray将这些字符串数组的索引存储在交错数组中,该数组具有至少2个单词的匹配序列。
//log(newArray);
//循环通过新数组
对于(var-itl=0;itl=0){
//如果它们确实存在,则获取它们的索引并存储在本地数组中(如果它们不存在),因为我们不希望以后重复单词。
if(短语起始点索引(iy)<0)
短语起始点推送(iy);
if(短语起始点索引(t)<0)
短语起始点推(t);
}
}
var str=“”;
//准备本地数组中的短语,并将其推入短语数组(如果不存在)。
对于(变量ifinalLoop=0;ifinalLoop}
您给我们的信息太少了。我假设你是按空格分开的。ES6的救援:)。当您查找重复的短语时,集合具有O(1)查找

编辑:刚刚意识到你可以通过一些小的修改将空间复杂度降低一吨。如果你想让我那样做,就喊我一声

const buildAllPhrases = sentence => {
    const splitSentence = sentence.split(" ")
    const phraseSize = splitSentence.length
    const allPhrases = []
    for (let i = phraseSize; i > 0; i--) {
        for (let y = 0; y + i <= phraseSize; y++) {
            allPhrases.push(splitSentence.slice(y, y + i))
        }
    }
    return allPhrases.map(phrase => phrase.join(" "))
}

const findRepeats = sentences => {
    const allPhrases = new Set()
    const repeatedPhrases = new Set()
    let phrases
    sentences.forEach(phrase => {
        phrases = buildAllPhrases(phrase)
        phrases.forEach(subPhrase => {
            if (allPhrases.has(subPhrase)) {
                repeatedPhrases.add(subPhrase)
            } else {
                allPhrases.add(subPhrase)
            }
        })
    })
    return [...repeatedPhrases]
}

const sample = [
"I want **a dog**",
"**A dog** is here",
"Pet **a dog**",
"A **red cat**",
"**red cat** is cute"
]

findRepeats(sample)
//['dog**', '**a dog**', '**a', '**red cat**', '**red', 'cat**', 'is']
const buildAllPhrases=句子=>{
常量拆分句子=句子拆分(“”)
const phraseSize=splitsequence.length
常量所有短语=[]
for(设i=phraseSize;i>0;i--){
for(设y=0;y+i短语.join(“”))
}
const findRepeats=句子=>{
const allPhrases=新集合()
const repeatedPhrases=新集合()
让短语
句子。forEach(短语=>{
短语=构建所有短语(短语)
短语.forEach(子短语=>{
if(所有短语.has(子短语)){
重复短语。添加(子短语)
}否则{
所有短语。添加(子短语)
}