我正在試圖找到一個比下面的代碼更優雅的方式來獲取基於其中一個包含單詞的索引的句子列表。例如,如果我給它一個單詞列表,例如用戶名,它會找到所有這些單詞的索引(這已經完成,並且是GetWordsMatches方法),然後使用該單詞的索引,我想要抓住整個句子。從單詞索引獲取完整句子
我有兩個問題,一是我無法弄清楚如何查看前一個單詞的前一個單詞,最後一個單詞,兩個單詞,我無法弄清楚如果最後一個單詞匹配如何阻止它崩潰在文件結束之前沒有一段時間。
public static List<string> GetSentencesFromWords(List<string> Words, string FileContents)
{
List<string> returnList = new List<string>();
MatchCollection mColl = GetWordsMatches(Words,FileContents);
foreach (Match ma in mColl)
{
int tmpInd = ma.Index;
int endInd = FileContents.IndexOf(".", tmpInd);
string tmp = FileContents.Substring(tmpInd,endInd);
returnList.Add(tmp);
}
return returnList;
}
有沒有更好的方法來做到這一點?
是什麼'GetWordsMatches'做什麼呢? – rikitikitik 2012-04-04 00:57:17
它爲列表中的單詞所在的位置提供匹配MatchCollection。 – SpectralEdge 2012-04-04 02:19:59