Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace U4L_2_Lygiavimas
- {
- public class WordMatch
- {
- public string Word { get; set; }
- public int Count { get; set; }
- }
- public class WordMatchSorter : IComparer<WordMatch>
- {
- /// <summary>
- /// compares list elements by amount and if amount is the same, by the alphabet used for sort
- /// </summary>
- /// <param name="a">first WordMatch object</param>
- /// <param name="b">second WordMatch object</param>
- /// <returns>compared data</returns>
- public int Compare(WordMatch a, WordMatch b)
- {
- if (a.Count == b.Count)
- {
- return a.Word.CompareTo(b.Word);
- }
- return b.Count.CompareTo(a.Count);
- }
- }
- public class Program
- {
- private static string vowelLetters = "aąeęėiįyouųū";
- private static Char[] punctuation = new Char[] { ' ', ',', '.', '!', '?', '(', ')', ':', ';', '\t', '\n' };
- private static string dataFile = @"../../Knyga.txt";
- private static string resultFile = @"../../Rodikliai.txt";
- private static List<WordMatch> resultingVowelWords = new List<WordMatch>();
- private static List<WordMatch> resultingLongestWords = new List<WordMatch>();
- public static void Main()
- {
- ReadFromFile(dataFile);
- IComparer<WordMatch> comparer = new WordMatchSorter();
- resultingVowelWords.Sort(comparer);
- resultingLongestWords.Sort(comparer);
- PrintVowelWords(resultFile);
- PrintLongestWords(resultFile);
- }
- /// <summary>
- /// Reads data from file.
- /// </summary>
- /// <param name="file">file</param>
- public static void ReadFromFile(string file)
- {
- string[] lines = File.ReadAllLines(file);
- int longestWordLength = 0;
- foreach (string line in lines)
- {
- string[] lineWords = line.Split(punctuation);
- WordVowelMatchInLine(lineWords);
- FindLongestWordLengthInText(lineWords, ref longestWordLength);
- }
- foreach(string line in lines)
- {
- string[] lineWords = line.Split(punctuation);
- SearchForLongestWordsInLine(lineWords, longestWordLength);
- }
- }
- /// <summary>
- /// Finds words that starts and ends with vowel
- /// </summary>
- /// <param name="lineWords">words in line</param>
- public static void WordVowelMatchInLine(string[] lineWords)
- {
- string pattern = String.Format(@"\b[{0}].*[{0}]\b", vowelLetters);
- Regex regex = new Regex(pattern);
- foreach (string word in lineWords)
- {
- if (regex.IsMatch(word.ToLower()))
- {
- AddWordToResultList(word, resultingVowelWords);
- }
- }
- }
- public static void FindLongestWordLengthInText(string[] lineWords, ref int length)
- {
- foreach(string word in lineWords)
- {
- if (word.Length > length)
- {
- length = word.Length;
- }
- }
- }
- public static void SearchForLongestWordsInLine(string[] lineWords, int length)
- {
- foreach(string word in lineWords)
- {
- if (word.Length == length)
- {
- AddWordToResultList(word, resultingLongestWords);
- }
- }
- }
- /// <summary>
- /// Adds same words to a list
- /// </summary>
- /// <param name="word">words</param>
- public static void AddWordToResultList(string word, List<WordMatch> list)
- {
- int id = -1;
- for (int i = 0; i < list.Count; i++)
- {
- if (String.Compare(list[i].Word, word.ToLower()) == 0)
- {
- id = i;
- break;
- }
- }
- if (id != -1)
- {
- list[id].Count += 1;
- }
- else
- {
- list.Add(new WordMatch { Word = word.ToLower(), Count = 1 });
- }
- }
- /// <summary>
- /// Prints words that starts and ends with vowel to .txt file
- /// </summary>
- /// <param name="fileName">name of the file</param>
- public static void PrintVowelWords(string fileName)
- {
- int indexCount = 10;
- if(resultingVowelWords.Count < indexCount)
- {
- indexCount = resultingVowelWords.Count;
- }
- string[] lines = new string[resultingVowelWords.Count + 3];
- lines[0] = String.Format("Žodžių kiekis, kurie prasideda ir pasibaige balse yra {0}", GetVowelWordsCount());
- lines[1] = String.Format(new string('-',39));
- lines[2] = String.Format($"| {"Žodis", -15} | {"Pasikartojimų sk.", -15} |");
- lines[3] = String.Format(new string('-', 39));
- for (int i = 0; i < indexCount; i++)
- {
- lines[i + 4] = String.Format($"| {resultingVowelWords[i].Word, -15} | {resultingVowelWords[i].Count, 17} |");
- }
- lines[resultingVowelWords.Count + 1] = String.Format(new string('-', 39));
- File.WriteAllLines(fileName, lines, Encoding.UTF8);
- }
- public static void PrintLongestWords(string fileName)
- {
- int indexCount = 10;
- if (resultingLongestWords.Count < indexCount)
- {
- indexCount = resultingLongestWords.Count;
- }
- string[] lines = new string[resultingLongestWords.Count + 3];
- lines[0] = String.Format(new string('-',39));
- lines[1] = String.Format($"| {"Žodis", -15} | {"Pasikartojimų sk.", -15} |");
- lines[2] = String.Format(new string('-', 39));
- for (int i = 0; i < indexCount; i++)
- {
- lines[i + 3] = String.Format($"| {resultingLongestWords[i].Word, -15} | {resultingLongestWords[i].Count, 17} |");
- }
- lines[resultingLongestWords.Count + 1] = String.Format(new string('-', 39));
- File.WriteAllLines(fileName, lines, Encoding.UTF8);
- }
- /// <summary>
- /// Gets count of words that starts and ends with vowel
- /// </summary>
- /// <returns>count</returns>
- public static int GetVowelWordsCount()
- {
- int result = 0;
- foreach (var word in resultingVowelWords)
- {
- result += word.Count;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement