diff --git a/Lab4.ChainedWords/InOut.cs b/Lab4.ChainedWords/InOut.cs new file mode 100644 index 0000000..84f47ca --- /dev/null +++ b/Lab4.ChainedWords/InOut.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab4.ChainedWords +{ + static class InOut + { + /// + /// Read a file line-by-line using an enumarable + /// + /// Target file + /// Enumerable + public static IEnumerable ReadByLines(string filename) + { + string line; + using (StreamReader reader = new StreamReader(filename)) + { + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } + } +} diff --git a/Lab4.ChainedWords/Lab4.ChainedWords.csproj b/Lab4.ChainedWords/Lab4.ChainedWords.csproj index 23df604..8a5bcb0 100644 --- a/Lab4.ChainedWords/Lab4.ChainedWords.csproj +++ b/Lab4.ChainedWords/Lab4.ChainedWords.csproj @@ -5,4 +5,10 @@ netcoreapp2.1 + + + Always + + + diff --git a/Lab4.ChainedWords/TaskUtils.cs b/Lab4.ChainedWords/TaskUtils.cs index 120fa75..7cfb8c8 100644 --- a/Lab4.ChainedWords/TaskUtils.cs +++ b/Lab4.ChainedWords/TaskUtils.cs @@ -9,17 +9,35 @@ namespace Lab4.ChainedWords { static class TaskUtils { + /// + /// Return anything that matches a word in given text, depending on punctuation + /// + /// Target text + /// Target punctuation + /// Word matches private static MatchCollection MatchByWords(string text, string punctuation) { string pattern = string.Format(@"[^{0}\n]+", Regex.Escape(punctuation)); return Regex.Matches(text, pattern, RegexOptions.IgnoreCase); } + /// + /// Return anything that matches a positive integer in text + /// + /// Target text + /// Integer matches private static MatchCollection MatchByIntegers(string text) { return Regex.Matches(text, @"\d+"); } + /// + /// Find all chains of words in given text. A chain is a sequence of words where every words + /// last letter matches the next words first letter. And split the text into words using punctuation + /// + /// Target text + /// Target punctuation + /// A list of chains with line number where it was found public static List> FindChains(string text, string punctuation) { List> chains = new List>(); @@ -59,6 +77,11 @@ namespace Lab4.ChainedWords return chains; } + /// + /// Find all positive integers in given text + /// + /// Target text + /// List of positive integers public static List FindIntegers(string text) { List integers = new List(); @@ -72,6 +95,12 @@ namespace Lab4.ChainedWords return integers; } + /// + /// Finds the longest chain in the given text, while using given punctuation to determine words. + /// + /// Target text + /// Target punctuation + /// public static Tuple FindLongestChain(string text, string punctuation) { List> chains = FindChains(text, punctuation); @@ -93,6 +122,14 @@ namespace Lab4.ChainedWords return longestChain; } + /// + /// Ouput to file: + /// * longest chain and where it was found + /// * sum of all positive integers + /// + /// Input file + /// Output file + /// Target punctuation public static void ProcessChains(string inputFile, string outputFile, string punctuation) { string text = File.ReadAllText(inputFile, Encoding.UTF8); @@ -114,18 +151,12 @@ namespace Lab4.ChainedWords } } - private static IEnumerable ReadByLines(string filename) - { - string line; - using (StreamReader reader = new StreamReader(filename)) - { - while((line = reader.ReadLine()) != null) - { - yield return line; - } - } - } - + /// + /// Align every word in a line by columns + /// + /// Input file + /// Output file + /// Target punctuation public static void ProcessAligned(string inputFile, string outputFile, string punctuation) { string pattern = string.Format(@"[^{0}]+[{0}]*", Regex.Escape(punctuation)); @@ -133,7 +164,7 @@ namespace Lab4.ChainedWords Dictionary columns = new Dictionary(); List> words = new List>(); - foreach (string line in ReadByLines(inputFile)) + foreach (string line in InOut.ReadByLines(inputFile)) { List row = new List(); words.Add(row);