Hello. There is a text, how to make it register in the List<List<string>> , where List<List<string>> is the Sentence, and List<string> Words?
|
1 answer
If the sentence is not overloaded with abbreviations, then this code will work. Which characters to cut from the source line - choose your own.
// Исходная строка string source = @"В список отлей, завышающих цены, попал мини-отель без звезд «Люкс», расположенный на проспекте Королева, 1/9. Стоимость бронирования на период проведения чемпионата мира по футболу отеля на сайте Booking составляет 15 тысяч рублей, что превышает норматив на 514%."; // Убираем ненужные символы (в данном случае только запятые) string clearedSource = source.Replace(@",", ""); // Делим текст на предложения List<string> cuttedSentence = source.Split('.').ToList(); // Готовим место под финальный список List<List<string>> cuttedWords = new List<List<string>>(); // Перебираем список предложений и каждое предложение делим на слова foreach (string item in cuttedSentence) { cuttedWords.Add(item.Split(' ').ToList()); } The result is:
- Although the solution with LINQ is more elegant. - dTi
|

var list = text.Split('.', '!', '?').Select(sentence => sentence.Split(' ').ToList()).ToList();- Andrey NOP