Javascript 如何从关键字搜索返回最短的代码段?
我有以下任务要解决: 给定一页包含字母数字单词和搜索短语的内容 在N个单词中,编写一个返回最短代码段的算法 按任意顺序包含所有N个单词的内容的集合 这就是我目前所拥有的。我将页面上的文本放入一个没有标点的数组中:Javascript 如何从关键字搜索返回最短的代码段?,javascript,Javascript,我有以下任务要解决: 给定一页包含字母数字单词和搜索短语的内容 在N个单词中,编写一个返回最短代码段的算法 按任意顺序包含所有N个单词的内容的集合 这就是我目前所拥有的。我将页面上的文本放入一个没有标点的数组中: var allText = $('#all-text').text(); var punctuationless = allText.replace(/[^A-Za-z0-9_]/g," ").toLowerCase(); var lessSpaces = punctuationles
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,两个”
- 搜索词
应为show““for”、“show”
- 。。。但是不是为了钱,而是为了演出
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;i- '+result.map(函数(a){return'
- '+a++ '}).join('''+'
纽约市的乔治华盛顿大桥是有史以来建造的最古老的桥梁之一。由于这座桥是一座地标性建筑,所以现在正在对其进行改造。市政府官员表示,这座具有里程碑意义的大桥将为该市创造大量新的就业机会。
搜索
已找到搜索:
不确定是否正确解释“编写一个算法,返回包含所有N个单词的最短内容片段”?什么是“内容的最短片段”?例如:纽约市的乔治华盛顿大桥是有史以来建造的最古老的桥梁之一。现在是