Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace U4_11_Trumpiausias_sakinys
  10. {
  11. class TaskUtils
  12. {
  13.  
  14. public static bool DontHaveWord(string word, string fileName2)
  15. {
  16. string text2 = File.ReadAllText(fileName2);
  17.  
  18. if (Regex.IsMatch(text2, @"\b" + word + @"\b"))
  19. {
  20. return false;
  21. }
  22. return true;
  23. }
  24.  
  25. public static string LongestWord(string fileName, string fileName2,char[] punctuation)
  26. {
  27. string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  28. string longestWord = "";
  29.  
  30. foreach (string line in lines)
  31. if (line.Length > 0)
  32. {
  33. string[] parts = line.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
  34. foreach (string word in parts)
  35. if (word.Length > longestWord.Length && DontHaveWord(word, fileName2))
  36. longestWord = word;
  37. }
  38.  
  39. return longestWord;
  40. }
  41.  
  42. public static List<string> FindLongestWords(string fileName, char[] punctuation, string fileName2)
  43. {
  44. string[] lines = File.ReadAllLines(fileName, Encoding.UTF8);
  45. List<string> words = new List<string>();
  46. string longestWord = LongestWord(fileName, fileName2, punctuation);
  47.  
  48. //// perkelt i kita metoda
  49. //foreach (string line in lines)
  50. // if (line.Length > 0)
  51. // {
  52. // string[] parts = line.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
  53. // foreach (string zodis in parts)
  54. // if (zodis.Length > ilgiaus.Length && DontHaveWord(zodis, fileName2))
  55. // ilgiaus = zodis;
  56. // }
  57.  
  58. words.Add(longestWord);
  59.  
  60. int lenght = longestWord.Length;
  61.  
  62. while (lenght > 0)
  63. {
  64. foreach (string line in lines)
  65. {
  66. if (line.Length > 0)
  67. {
  68. string[] parts = line.Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
  69. foreach (string zodis in parts)
  70. {
  71. if (zodis.Length == lenght && DontHaveWord(zodis, fileName2) && !words.Contains(zodis) && words.Count <= 10)
  72. {
  73. words.Add(zodis);
  74. }
  75. }
  76. }
  77. }
  78. lenght--;
  79. }
  80.  
  81. return words;
  82. }
  83.  
  84. public static List<string> FindSentences(string fileName, char[] punctuation)
  85. {
  86. string text = File.ReadAllText(fileName);
  87. List<string> sentences = new List<string>();
  88.  
  89. Regex rx = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
  90. foreach (Match match in rx.Matches(text))
  91. {
  92. int i = match.Index; //sito nereik ?
  93. sentences.Add(match.Value);
  94. }
  95.  
  96. return sentences;
  97. //int minWordsCount = sentences[0].Length;
  98.  
  99. //int shortestSentenceIndex = 0;
  100. //for (int i = 0; i < sentences.Count; i++)
  101. //{
  102. // int wordsinsentence = 0;
  103. // if (sentences[i].Length > 0)
  104. // {
  105. // string[] parts = sentences[i].Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
  106. // //if (i == 0)
  107. // //{
  108. // // foreach (string word in parts)
  109. // // {
  110. // // wordsinsentence++;
  111. // // }
  112. // // minWordsCount = wordsinsentence;
  113. // // shortestSentenceIndex = i;
  114. // //}
  115. // //else if(i > 0)
  116. // //{
  117. // foreach (string word in parts)
  118. // {
  119. // wordsinsentence++;
  120. // }
  121.  
  122. // if (minWordsCount > wordsinsentence && wordsinsentence > 3)
  123. // {
  124. // minWordsCount = wordsinsentence;
  125. // shortestSentenceIndex = i+1;
  126. // }
  127. // //}
  128. // }
  129. //}
  130. //Console.WriteLine(sentences[0]);
  131. //Console.WriteLine(minWordsCount);
  132. //Console.WriteLine(shortestSentenceIndex);
  133.  
  134. }
  135.  
  136. public static int FindShortestSentenceIndex(List<string> sentences, char[] punctuation, out int minWordsCount)
  137. {
  138.  
  139. minWordsCount = 100;
  140. int shortestSentenceIndex = 0;
  141. // sukurt metoda kur apskaiciuotu zodziu skaiciu
  142. for (int i = 0; i < sentences.Count; i++)
  143. {
  144. int wordsinsentence = 0;
  145. if (sentences[i].Length > 0)
  146. {
  147. string[] parts = sentences[i].Split(punctuation, StringSplitOptions.RemoveEmptyEntries);
  148. //if (i == 0)
  149. //{
  150. // foreach (string word in parts)
  151. // {
  152. // wordsinsentence++;
  153. // }
  154. // minWordsCount = wordsinsentence;
  155. // shortestSentenceIndex = i;
  156. //}
  157. //else if(i > 0)
  158. //{
  159. foreach (string word in parts)
  160. {
  161. wordsinsentence++;
  162. }
  163.  
  164. //if(wordsinsentence < 3 && sentences.Count == 1)
  165. //{
  166. // minWordsCount = wordsinsentence;
  167. // return shortestSentenceIndex;
  168.  
  169. //}
  170.  
  171. if (minWordsCount > wordsinsentence && wordsinsentence >= 3)
  172. {
  173. minWordsCount = wordsinsentence;
  174. shortestSentenceIndex = i;
  175. }
  176. //}
  177. }
  178. }
  179. //Console.WriteLine(sentences[0]);
  180. //Console.WriteLine(minWordsCount);
  181. //Console.WriteLine(shortestSentenceIndex);
  182.  
  183. if (minWordsCount == 100)
  184. {
  185. minWordsCount = 0;
  186. }
  187.  
  188. return shortestSentenceIndex;
  189. }
  190.  
  191. public static string FirstWordToUpperLetter(string file)
  192. {
  193. Regex rx = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
  194.  
  195. string newfile = "";
  196. foreach (Match match in rx.Matches(file))
  197. {
  198. string sentence = match.Value;
  199. newfile += sentence.First().ToString().ToUpper() + sentence.Substring(1);
  200. newfile += " ";
  201. }
  202.  
  203. return newfile;
  204. }
  205.  
  206. public static void CombineText(string fileName1, string fileName2, char[] punctuation)
  207. {
  208. // get encoding utf-8 arba
  209. string text1 = File.ReadAllText(fileName1);
  210. string text2 = File.ReadAllText(fileName2);
  211. string file = "";
  212.  
  213. //bool isDone = false;
  214.  
  215. while (text1.Length != 0 && text2.Length != 0)
  216. {
  217.  
  218. //string firstWord = text2.Split(punctuation).First();
  219.  
  220.  
  221.  
  222. string firstWord = Regex.Match(text2, @"\b\w*\b", RegexOptions.IgnoreCase).Value;
  223.  
  224. Console.WriteLine(firstWord);
  225.  
  226. //Regex.IsMatch(text1, @"\b" + firstWord + @"\b") == false pakeist // ir breakus idet
  227.  
  228. if (/*text2.Length == 0 || */Regex.IsMatch(text1, @"\b" + firstWord + @"\b", RegexOptions.IgnoreCase) == false/*.Value != firstWord*/) // cia atskliaust
  229. {
  230. file += text1; /*+ " " + text2;
  231. text1 = /*Regex.Replace(text1, @"[\s\S]+", "")*//*"";*/
  232. file += text2;
  233. text1 = "";
  234. text2 = "";
  235.  
  236.  
  237. //Console.WriteLine(text2.Length);
  238. break;
  239.  
  240. }
  241. else
  242. {
  243. Match match = Regex.Match(text1, @"^(.*?)\b" + firstWord + @"\b\W*", RegexOptions.IgnoreCase);
  244. file += match.Value;
  245.  
  246. text1 = Regex.Replace(text1, @"^(.*?)\b" + firstWord + @"\b\W*", "", RegexOptions.IgnoreCase);
  247.  
  248. text2 = Regex.Replace(text2, @"^(.*?)\b" + firstWord + @"\b\W*", "", RegexOptions.IgnoreCase);
  249.  
  250. }
  251.  
  252. string firstWord2 = Regex.Match(text1, @"\b\w*\b", RegexOptions.IgnoreCase).Value;
  253. //Console.WriteLine(firstWord2);
  254. //Console.WriteLine();
  255.  
  256. if (/*text1.Length == 0 ||*/Regex.IsMatch(text2, @"\b" + firstWord2 + @"\b", RegexOptions.IgnoreCase) == false/*.Value != firstWord2*/)
  257. {
  258. file += /*text1+ " "+*/text2;
  259. text2 = /*Regex.Replace(text2, @"[\s\S]+", "")*/"";
  260. //text1 = "";
  261. file += text1;
  262. text1 = "";
  263. //Console.WriteLine(text2.Length);
  264. break;
  265. }
  266. else
  267. {
  268. Match match2 = Regex.Match(text2, @"^(.*?)\b" + firstWord2 + @"\b\W*", RegexOptions.IgnoreCase);
  269. file += match2.Value;
  270.  
  271. text2 = Regex.Replace(text2, @"^(.*?)\b" + firstWord2 + @"\b\W*", "", RegexOptions.IgnoreCase);
  272.  
  273. text1 = Regex.Replace(text1, @"^(.*?)\b" + firstWord2 + @"\b\W*", " ", RegexOptions.IgnoreCase);
  274.  
  275. }
  276.  
  277. //Console.WriteLine(text1.Length);
  278. //Console.WriteLine(text2.Length);
  279. if (text2.Length == 0)
  280. {
  281. //firstWord = Regex.Repalce(text2, @"\b\w*\b").Value;
  282. //char.ToUpper(labelName[0]) + labelName.Substring(1)
  283. file += text1;
  284. text1 = "";
  285. break;
  286.  
  287. }
  288. else if (text1.Length == 0)
  289. {
  290. file += text2;
  291. text2 = "";
  292. break;
  293. }
  294.  
  295.  
  296. }
  297.  
  298. file = FirstWordToUpperLetter(file);
  299. Console.WriteLine(file);
  300.  
  301. } //cia returnint string turi
  302.  
  303. }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement