Advertisement
MartisK

KTU OP L4 - Lygiavimas

Nov 25th, 2020 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. namespace U4L_2_Lygiavimas
  2. {
  3.     public class WordMatch
  4.     {
  5.         public string Word { get; set; }
  6.         public int Count { get; set; }
  7.     }
  8.  
  9.     public class WordMatchSorter : IComparer<WordMatch>
  10.     {
  11.         /// <summary>
  12.         /// compares list elements by amount and if amount is the same, by the alphabet used for sort
  13.         /// </summary>
  14.         /// <param name="a">first WordMatch object</param>
  15.         /// <param name="b">second WordMatch object</param>
  16.         /// <returns>compared data</returns>
  17.         public int Compare(WordMatch a, WordMatch b)
  18.         {
  19.             if (a.Count == b.Count)
  20.             {
  21.                 return a.Word.CompareTo(b.Word);
  22.             }
  23.             return b.Count.CompareTo(a.Count);
  24.         }
  25.     }
  26.  
  27.     public class Program
  28.     {
  29.         private static string vowelLetters = "aąeęėiįyouųū";
  30.         private static Char[] punctuation = new Char[] { ' ', ',', '.', '!', '?', '(', ')', ':', ';', '\t', '\n' };
  31.         private static string dataFile = @"../../Knyga.txt";
  32.         private static string resultFile = @"../../Rodikliai.txt";
  33.  
  34.         private static List<WordMatch> resultingVowelWords = new List<WordMatch>();
  35.         private static List<WordMatch> resultingLongestWords = new List<WordMatch>();
  36.  
  37.         public static void Main()
  38.         {
  39.             ReadFromFile(dataFile);
  40.             IComparer<WordMatch> comparer = new WordMatchSorter();
  41.             resultingVowelWords.Sort(comparer);
  42.             resultingLongestWords.Sort(comparer);
  43.  
  44.             PrintVowelWords(resultFile);
  45.             PrintLongestWords(resultFile);
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Reads data from file.
  50.         /// </summary>
  51.         /// <param name="file">file</param>
  52.         public static void ReadFromFile(string file)
  53.         {
  54.             string[] lines = File.ReadAllLines(file);
  55.             int longestWordLength = 0;
  56.  
  57.             foreach (string line in lines)
  58.             {
  59.                 string[] lineWords = line.Split(punctuation);
  60.  
  61.                 WordVowelMatchInLine(lineWords);
  62.                 FindLongestWordLengthInText(lineWords, ref longestWordLength);
  63.             }
  64.  
  65.             foreach(string line in lines)
  66.             {
  67.                 string[] lineWords = line.Split(punctuation);
  68.  
  69.                 SearchForLongestWordsInLine(lineWords, longestWordLength);
  70.             }
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Finds words that starts and ends with vowel
  75.         /// </summary>
  76.         /// <param name="lineWords">words in line</param>
  77.         public static void WordVowelMatchInLine(string[] lineWords)
  78.         {
  79.             string pattern = String.Format(@"\b[{0}].*[{0}]\b", vowelLetters);
  80.             Regex regex = new Regex(pattern);
  81.  
  82.             foreach (string word in lineWords)
  83.             {
  84.                 if (regex.IsMatch(word.ToLower()))
  85.                 {
  86.                     AddWordToResultList(word, resultingVowelWords);
  87.                 }
  88.             }
  89.         }
  90.  
  91.         public static void FindLongestWordLengthInText(string[] lineWords, ref int length)
  92.         {
  93.             foreach(string word in lineWords)
  94.             {
  95.                 if (word.Length > length)
  96.                 {
  97.                     length = word.Length;
  98.                 }
  99.             }
  100.         }
  101.  
  102.         public static void SearchForLongestWordsInLine(string[] lineWords, int length)
  103.         {
  104.             foreach(string word in lineWords)
  105.             {
  106.                 if (word.Length == length)
  107.                 {
  108.                     AddWordToResultList(word, resultingLongestWords);
  109.                 }
  110.             }
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Adds same words to a list
  115.         /// </summary>
  116.         /// <param name="word">words</param>
  117.         public static void AddWordToResultList(string word, List<WordMatch> list)
  118.         {
  119.             int id = -1;
  120.             for (int i = 0; i < list.Count; i++)
  121.             {
  122.                 if (String.Compare(list[i].Word, word.ToLower()) == 0)
  123.                 {
  124.                     id = i;
  125.                     break;
  126.                 }
  127.             }
  128.  
  129.             if (id != -1)
  130.             {
  131.                 list[id].Count += 1;
  132.             }
  133.             else
  134.             {
  135.                 list.Add(new WordMatch { Word = word.ToLower(), Count = 1 });
  136.             }
  137.         }
  138.  
  139.         /// <summary>
  140.         /// Prints words that starts and ends with vowel to .txt file
  141.         /// </summary>
  142.         /// <param name="fileName">name of the file</param>
  143.         public static void PrintVowelWords(string fileName)
  144.         {
  145.             int indexCount = 10;
  146.  
  147.             if(resultingVowelWords.Count < indexCount)
  148.             {
  149.                 indexCount = resultingVowelWords.Count;
  150.             }
  151.  
  152.             string[] lines = new string[resultingVowelWords.Count + 3];
  153.  
  154.             lines[0] = String.Format("Žodžių kiekis, kurie prasideda ir pasibaige balse yra {0}", GetVowelWordsCount());
  155.             lines[1] = String.Format(new string('-',39));
  156.             lines[2] = String.Format($"| {"Žodis", -15} | {"Pasikartojimų sk.", -15} |");
  157.             lines[3] = String.Format(new string('-', 39));
  158.             for (int i = 0; i < indexCount; i++)
  159.             {
  160.                 lines[i + 4] = String.Format($"| {resultingVowelWords[i].Word, -15} | {resultingVowelWords[i].Count, 17} |");
  161.             }
  162.             lines[resultingVowelWords.Count + 1] = String.Format(new string('-', 39));
  163.             File.WriteAllLines(fileName, lines, Encoding.UTF8);
  164.         }
  165.  
  166.         public static void PrintLongestWords(string fileName)
  167.         {
  168.             int indexCount = 10;
  169.  
  170.             if (resultingLongestWords.Count < indexCount)
  171.             {
  172.                 indexCount = resultingLongestWords.Count;
  173.             }
  174.  
  175.             string[] lines = new string[resultingLongestWords.Count + 3];
  176.  
  177.             lines[0] = String.Format(new string('-',39));
  178.             lines[1] = String.Format($"| {"Žodis", -15} | {"Pasikartojimų sk.", -15} |");
  179.             lines[2] = String.Format(new string('-', 39));
  180.             for (int i = 0; i < indexCount; i++)
  181.             {
  182.                 lines[i + 3] = String.Format($"| {resultingLongestWords[i].Word, -15} | {resultingLongestWords[i].Count, 17} |");
  183.             }
  184.             lines[resultingLongestWords.Count + 1] = String.Format(new string('-', 39));
  185.             File.WriteAllLines(fileName, lines, Encoding.UTF8);
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Gets count of words that starts and ends with vowel
  190.         /// </summary>
  191.         /// <returns>count</returns>
  192.         public static int GetVowelWordsCount()
  193.         {
  194.             int result = 0;
  195.             foreach (var word in resultingVowelWords)
  196.             {
  197.                 result += word.Count;
  198.             }
  199.             return result;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement