Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace U4_11_Trumpiausias_sakinys
- {
- class TaskUtils
- {
- public static bool DontHaveWord(string word, string fileName2)
- {
- string text2 = File.ReadAllText(fileName2);
- if (Regex.IsMatch(text2, @"\b" + word + @"\b"))
- {
- return false;
- }
- return true;
- }
- public static string LongestWord(string fileName, string fileName2,char[] punctuation)
- {
- string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
- string longestWord = "";
- foreach (string line in lines)
- if (line.Length > 0)
- {
- string[] parts = line.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
- foreach (string word in parts)
- if (word.Length > longestWord.Length && DontHaveWord(word, fileName2))
- longestWord = word;
- }
- return longestWord;
- }
- public static List<string> FindLongestWords(string fileName, char[] punctuation, string fileName2)
- {
- string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
- List<string> words = new List<string>();
- string longestWord = LongestWord(fileName, fileName2, punctuation);
- //// perkelt i kita metoda
- //foreach (string line in lines)
- // if (line.Length > 0)
- // {
- // string[] parts = line.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
- // foreach (string zodis in parts)
- // if (zodis.Length > ilgiaus.Length && DontHaveWord(zodis, fileName2))
- // ilgiaus = zodis;
- // }
- words.Add(longestWord);
- int lenght = longestWord.Length;
- while (lenght > 0)
- {
- foreach (string line in lines)
- {
- if (line.Length > 0)
- {
- string[] parts = line.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
- foreach (string zodis in parts)
- {
- if (zodis.Length == lenght && DontHaveWord(zodis, fileName2) && !words.Contains(zodis) && words.Count <= 10)
- {
- words.Add(zodis);
- }
- }
- }
- }
- lenght--;
- }
- return words;
- }
- public static List<string> FindSentences(string fileName, char[] punctuation)
- {
- string text = File.ReadAllText(fileName);
- List<string> sentences = new List<string>();
- Regex rx = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
- foreach (Match match in rx.Matches(text))
- {
- int i = match.Index; //sito nereik ?
- sentences.Add(match.Value);
- }
- return sentences;
- //int minWordsCount = sentences[0].Length;
- //int shortestSentenceIndex = 0;
- //for (int i = 0; i < sentences.Count; i++)
- //{
- // int wordsinsentence = 0;
- // if (sentences[i].Length > 0)
- // {
- // string[] parts = sentences[i].Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
- // //if (i == 0)
- // //{
- // // foreach (string word in parts)
- // // {
- // // wordsinsentence++;
- // // }
- // // minWordsCount = wordsinsentence;
- // // shortestSentenceIndex = i;
- // //}
- // //else if(i > 0)
- // //{
- // foreach (string word in parts)
- // {
- // wordsinsentence++;
- // }
- // if (minWordsCount > wordsinsentence && wordsinsentence > 3)
- // {
- // minWordsCount = wordsinsentence;
- // shortestSentenceIndex = i+1;
- // }
- // //}
- // }
- //}
- //Console.WriteLine(sentences[0]);
- //Console.WriteLine(minWordsCount);
- //Console.WriteLine(shortestSentenceIndex);
- }
- public static int FindShortestSentenceIndex(List<string> sentences, char[] punctuation, out int minWordsCount)
- {
- minWordsCount = 100;
- int shortestSentenceIndex = 0;
- // sukurt metoda kur apskaiciuotu zodziu skaiciu
- for (int i = 0; i < sentences.Count; i++)
- {
- int wordsinsentence = 0;
- if (sentences[i].Length > 0)
- {
- string[] parts = sentences[i].Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
- //if (i == 0)
- //{
- // foreach (string word in parts)
- // {
- // wordsinsentence++;
- // }
- // minWordsCount = wordsinsentence;
- // shortestSentenceIndex = i;
- //}
- //else if(i > 0)
- //{
- foreach (string word in parts)
- {
- wordsinsentence++;
- }
- //if(wordsinsentence < 3 && sentences.Count == 1)
- //{
- // minWordsCount = wordsinsentence;
- // return shortestSentenceIndex;
- //}
- if (minWordsCount > wordsinsentence && wordsinsentence >= 3)
- {
- minWordsCount = wordsinsentence;
- shortestSentenceIndex = i;
- }
- //}
- }
- }
- //Console.WriteLine(sentences[0]);
- //Console.WriteLine(minWordsCount);
- //Console.WriteLine(shortestSentenceIndex);
- if (minWordsCount == 100)
- {
- minWordsCount = 0;
- }
- return shortestSentenceIndex;
- }
- public static string FirstWordToUpperLetter(string file)
- {
- Regex rx = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
- string newfile = "";
- foreach (Match match in rx.Matches(file))
- {
- string sentence = match.Value;
- newfile += sentence.First().ToString().ToUpper() + sentence.Substring(1);
- newfile += " ";
- }
- return newfile;
- }
- public static void CombineText(string fileName1, string fileName2, char[] punctuation)
- {
- // get encoding utf-8 arba
- string text1 = File.ReadAllText(fileName1);
- string text2 = File.ReadAllText(fileName2);
- string file = "";
- //bool isDone = false;
- while (text1.Length != 0 && text2.Length != 0)
- {
- //string firstWord = text2.Split(punctuation).First();
- string firstWord = Regex.Match(text2, @"\b\w*\b", RegexOptions.IgnoreCase).Value;
- Console.WriteLine(firstWord);
- //Regex.IsMatch(text1, @"\b" + firstWord + @"\b") == false pakeist // ir breakus idet
- if (/*text2.Length == 0 || */Regex.IsMatch(text1, @"\b" + firstWord + @"\b", RegexOptions.IgnoreCase) == false/*.Value != firstWord*/) // cia atskliaust
- {
- file += text1; /*+ " " + text2;
- text1 = /*Regex.Replace(text1, @"[\s\S]+", "")*//*"";*/
- file += text2;
- text1 = "";
- text2 = "";
- //Console.WriteLine(text2.Length);
- break;
- }
- else
- {
- Match match = Regex.Match(text1, @"^(.*?)\b" + firstWord + @"\b\W*", RegexOptions.IgnoreCase);
- file += match.Value;
- text1 = Regex.Replace(text1, @"^(.*?)\b" + firstWord + @"\b\W*", "", RegexOptions.IgnoreCase);
- text2 = Regex.Replace(text2, @"^(.*?)\b" + firstWord + @"\b\W*", "", RegexOptions.IgnoreCase);
- }
- string firstWord2 = Regex.Match(text1, @"\b\w*\b", RegexOptions.IgnoreCase).Value;
- //Console.WriteLine(firstWord2);
- //Console.WriteLine();
- if (/*text1.Length == 0 ||*/Regex.IsMatch(text2, @"\b" + firstWord2 + @"\b", RegexOptions.IgnoreCase) == false/*.Value != firstWord2*/)
- {
- file += /*text1+ " "+*/text2;
- text2 = /*Regex.Replace(text2, @"[\s\S]+", "")*/"";
- //text1 = "";
- file += text1;
- text1 = "";
- //Console.WriteLine(text2.Length);
- break;
- }
- else
- {
- Match match2 = Regex.Match(text2, @"^(.*?)\b" + firstWord2 + @"\b\W*", RegexOptions.IgnoreCase);
- file += match2.Value;
- text2 = Regex.Replace(text2, @"^(.*?)\b" + firstWord2 + @"\b\W*", "", RegexOptions.IgnoreCase);
- text1 = Regex.Replace(text1, @"^(.*?)\b" + firstWord2 + @"\b\W*", " ", RegexOptions.IgnoreCase);
- }
- //Console.WriteLine(text1.Length);
- //Console.WriteLine(text2.Length);
- if (text2.Length == 0)
- {
- //firstWord = Regex.Repalce(text2, @"\b\w*\b").Value;
- //char.ToUpper(labelName[0]) + labelName.Substring(1)
- file += text1;
- text1 = "";
- break;
- }
- else if (text1.Length == 0)
- {
- file += text2;
- text2 = "";
- break;
- }
- }
- file = FirstWordToUpperLetter(file);
- Console.WriteLine(file);
- } //cia returnint string turi
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement