Given the text, it must be divided into sentences, and sentences into words. So, you need to create a List sentences and place sentences there, and then create a List> words and put the words there, right? Or sentences should be a sheet of sheets, and the words just a sheet. So, how to add elements to the sheet of sheets.

Please help me, I don’t understand how to work with lists at all.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

And they say that recursion is divine!

Let's start with the biggest nesting, with the words that make up the sentence.

var sentence = new List<string>(); sentence.Add("Hello"); sentence.Add("world"); 

Words are combined into sentences that make up the text:

 var text = new List<List<string>>(); var sentence = new List<string>(); sentence.Add("Hello"); sentence.Add("world"); text.Add(sentence); 

The last code can be rewritten as follows:

 var text = new List<List<string>>(); text.Add(new List<string>()); text[text.Count - 1].Add("Hello"); text[text.Count - 1].Add("world"); 

Here we first add an empty sentence to the text, and then add words to the last ( text[text.Count - 1] ) text sentence.

If we had another level of nesting, for example, a card file, which consists of texts, it would look like this:

 var directory = new List<List<List<string>>>(); var text = new List<List<string>>(); var sentence = new List<string>(); sentence.Add("Hello"); sentence.Add("world"); text.Add(sentence); directory.Add(text); 

    As an option:

     var sentenseList = inputText .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Split(new[] { "-", " ", "," }, StringSplitOptions.RemoveEmptyEntries).ToList()) .ToList(); 

    Where inputText is a string variable containing the input text to parse.