我知道这个帖子已经过时了,但是我上周给了这个试试,这是背面的痛苦。这远非完美,但这是我想出的。
的片段生成:
public static string SelectKeywordSnippets(string StringToSnip, string[] Keywords, int SnippetLength)
{
string snippedString = "";
List<int> keywordLocations = new List<int>();
//Get the locations of all keywords
for (int i = 0; i < Keywords.Count(); i++)
keywordLocations.AddRange(SharedTools.IndexOfAll(StringToSnip, Keywords[i], StringComparison.CurrentCultureIgnoreCase));
//Sort locations
keywordLocations.Sort();
//Remove locations which are closer to each other than the SnippetLength
if (keywordLocations.Count > 1)
{
bool found = true;
while (found)
{
found = false;
for (int i = keywordLocations.Count - 1; i > 0; i--)
if (keywordLocations[i] - keywordLocations[i - 1] < SnippetLength/2)
{
keywordLocations[i - 1] = (keywordLocations[i] + keywordLocations[i - 1])/2;
keywordLocations.RemoveAt(i);
found = true;
}
}
}
//Make the snippets
if (keywordLocations.Count > 0 && keywordLocations[0] - SnippetLength/2 > 0)
snippedString = "... ";
foreach (int i in keywordLocations)
{
int stringStart = Math.Max(0, i - SnippetLength/2);
int stringEnd = Math.Min(i + SnippetLength/2, StringToSnip.Length);
int stringLength = Math.Min(stringEnd - stringStart, StringToSnip.Length - stringStart);
snippedString += StringToSnip.Substring(stringStart, stringLength);
if (stringEnd < StringToSnip.Length) snippedString += " ... ";
if (snippedString.Length > 200) break;
}
return snippedString;
}
它会发现所有的关键字的索引示例文本
private static List<int> IndexOfAll(string haystack, string needle, StringComparison Comparison)
{
int pos;
int offset = 0;
int length = needle.Length;
List<int> positions = new List<int>();
while ((pos = haystack.IndexOf(needle, offset, Comparison)) != -1)
{
positions.Add(pos);
offset = pos + length;
}
return positions;
}
这是在其执行一个有点笨拙的功能。它的工作方式是通过查找字符串中所有关键字的位置。然后检查没有任何关键字比期望的片段长度更接近彼此,以便片段不会重叠(这就是它有点儿可能......)。然后抓住以关键字位置为中心的所需长度的子串并将整个东西拼接在一起。
我知道这是晚年,但如果只是张贴它可能会帮助别人过这个问题来了。
感叹......又浪费了150分...... – 2010-07-27 20:44:26
?那是什么意思? – 2010-08-17 18:57:30
对于任何对这个问题感兴趣的人,都有一个更新的语言不可知的问题,它的回答比任何这个问题都要高:** [给出一个文档,选择一个相关的代码片断](http://stackoverflow.com/questions/2829303)** – hippietrail 2012-10-20 18:02:06