Javascript 如何从关键字搜索返回最短的代码段?

Javascript 如何从关键字搜索返回最短的代码段?,javascript,Javascript,我有以下任务要解决: 给定一页包含字母数字单词和搜索短语的内容 在N个单词中,编写一个返回最短代码段的算法 按任意顺序包含所有N个单词的内容的集合 这就是我目前所拥有的。我将页面上的文本放入一个没有标点的数组中: var allText = $('#all-text').text(); var punctuationless = allText.replace(/[^A-Za-z0-9_]/g," ").toLowerCase(); var lessSpaces = punctuationles

我有以下任务要解决:

给定一页包含字母数字单词和搜索短语的内容 在N个单词中,编写一个返回最短代码段的算法 按任意顺序包含所有N个单词的内容的集合

这就是我目前所拥有的。我将页面上的文本放入一个没有标点的数组中:

var allText = $('#all-text').text();
var punctuationless = allText.replace(/[^A-Za-z0-9_]/g," ").toLowerCase();
var lessSpaces = punctuationless.replace(/\s{2,}/g," ");
var allTextArray = lessSpaces.split(" ");
var keywords = [];
我想我可能可以使用.filter方法,但我不确定如何比较两个数组

allTextArray.filter(keywords)
//find the indexes of the matches to the keywords
//compare how far apart the indexes are to each other and return the shortest length

因此,如果页面文本为:

One for the money,
two for the show.
  • 搜索词
    “一”、“钱”
    应产生
    “一换钱”
  • 搜索词“一”、“二”、“for”应产生“一个for The money,两个”
  • 搜索词
    “for”、“show”
    应为show“

    • 。。。但是不是为了钱,而是为了演出
我加上最后一点,因为这是这样一个函数获得很多复杂性的地方,因为它现在必须找到所有组合中最小的搜索词组合

我对此进行了计算,但我不会声称这是最有效的答案(因为时间已经晚了,我的部分大脑可能已经睡觉了)

基本上,这是一个迭代和扫描的解决方案,这可能是一个人用眼球做的:

  • 传递搜索文本,并为每个关键字及其索引的每次出现构建一个数组
  • 确保数组的顺序基于关键字在文本中出现的顺序
  • 从(几乎)有序数组中的每个条目开始,向右扫描,直到找到每个关键字的至少一个匹配项——在这一点上,我们可以看到它是否是迄今为止得到的最小片段
  • 在说了所有的话和做了所有的事之后,所有可能的片段包括所有的关键字都被检查了

    需要注意的是我创建的解决方案不区分大小写,它匹配部分单词,而不仅仅是整个单词,OP表示不区分大小写和整个单词匹配

    不区分大小写(将所有输入文本转换为小写)和全字匹配(去掉标点符号和额外空白,然后拆分为数组)的策略应该很容易应用于解决方案,特别是因为
    indexOf
    对字符串和数组的作用是相同的

    getSnippet = function(keywords, fullText) {
      var keywordCount = keywords.length,
          keywordIndexes = [];
    
      // Find each occurrence of every word
      for(var i=0; i < keywordCount; i++) {
        var searchPos = 0;
        var word = keywords[i];
        var index = -1;
        do {
          index = fullText.indexOf(keywords[i],searchPos);
          if (index >= 0) {
            keywordIndexes.push({i:index, word:word});
          }
          searchPos = index + 1;
        } while (index >= 0);
      }
    
      keywordIndexes.sort(function(a, b) { return a.i == b.i ? 0 : a.i < b.i ? -1 : 1; });
    
      // Find the shortest run by starting at each array index and scanning to the
      // right until we have encountered each word in the list.
      for (i=0, n=keywordIndexes.length-keywordCount; i<=n; i++) {
        // NOTE: We actually can actually stop once there are fewer keyword
        // indexes than keywords, since we know we won't find all the keywords (hence the subtraction of keywordCount)
        var foundWords = {},
            foundCount = 0;
        snippetStart = keywordIndexes[i].i;
    
        for (j=i; j < keywordIndexes.length; j++) {
          var word = keywordIndexes[j].word;
          if (!foundWords[word]) {
            foundWords[word] = true;
            foundCount++;
          }
          if (foundCount == keywordCount) {
            // We've found all the words
            snippetEnd = keywordIndexes[j].i + word.length;
            if (minSnippet.end - minSnippet.start > snippetEnd - snippetStart) {
              minSnippet.end = snippetEnd;
              minSnippet.start = snippetStart;
            }
            break;
          }
        }
      }
      return fullText.substring(minSnippet.start, minSnippet.end);
    }
    
    getSnippet=function(关键字,全文){
    var keywordCount=keywords.length,
    关键词索引=[];
    //找出每个单词的每一次出现
    对于(var i=0;i=0){
    push({i:index,word:word});
    }
    searchPos=index+1;
    }而(指数>=0);
    }
    关键字index.sort(函数(a,b){返回a.i==b.i?0:a.i

    有关更多信息,请参见

    此解决方案使用两个数组,
    q
    数组可获得所需单词及其位置的排序列表:

    [
        {
            "word": "bridge",
            "pos": 46
        },
        {
            "word": "city",
            "pos": 65
        },
        {
            "word": "bridge",
            "pos": 155
        },
        ...
    ]
    
    qq
    数组,用于直接邻域中所有可能单词的组。它包括所需部件的开始和结束。该列表基本上被排序以获得最小长度,并作为结果

    [
        {
            "start": 155,
            "end": 181
        },
        {
            "start": 177,
            "end": 220
        },
        ...
    ]
    
    该算法只在应用了
    toLowerCase
    的情况下,在原始文本中查找完整的单词

    函数getWords(){ var text=document.getElementById('text').textContent, lowerText=text.toLowerCase(), wordsO={}, words=document.getElementById('words').value.split('').map(函数(w){wordsO[w.toLowerCase()]=true;返回w.toLowerCase();}), 模式=/\w+/g, 执行结果, 结果=[], q=[],qq=[],i,最大值; while((execResult=pattern.exec(lowerText))!==null){ wordsO[execResult[0]]和&q.push({word:execResult[0],pos:execResult.index}); } 对于(i=0;ir?j:r; },i); ~max&&qq.push({start:q[i].pos,end:q[max].pos+q[max].word.length}); } sort(函数(a,b){返回a.end-a.start-b.end+b.start;}); qq.every(函数(a,uu,aa){ 返回aa[0].end-aa[0].start==a.end-a.start&&result.push(text.substring(a.start,a.end)+'['+a.start+']); }); document.getElementById('result').innerHTML=result.length&&'
      '+result.map(函数(a){return'
    • '+a++
    • '}).join('''+'
    '.| |###未找到匹配项##; }
    
    纽约市的乔治华盛顿大桥是有史以来建造的最古老的桥梁之一。由于这座桥是一座地标性建筑,所以现在正在对其进行改造。市政府官员表示,这座具有里程碑意义的大桥将为该市创造大量新的就业机会。
    搜索 已找到搜索:

    不确定是否正确解释“编写一个算法,返回包含所有N个单词的最短内容片段”?什么是“内容的最短片段”?例如:纽约市的乔治华盛顿大桥是有史以来建造的最古老的桥梁之一。现在是