VitalyD

Untitled

Feb 19th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using static System.IO.File;
  7. using static System.Threading.Tasks.Parallel;
  8.  
  9. namespace potokC
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             const string delimiters = " ,.!?-";
  16.  
  17.             var inputFilename = "text2.txt";
  18.             var outputFilename = "output2.html";
  19.             var dictionaryFilename = "dictionary2.txt";
  20.  
  21.             var dictionary = new HashSet<string>(ReadAllLines(dictionaryFilename));
  22.  
  23.             var textLines = ReadAllLines(inputFilename, Encoding.UTF8);
  24.  
  25.             var wordsToMark = new ConcurrentBag<string>();
  26.             for (var i = 0; i < textLines.Length; i++)
  27.             {
  28.                 var words = textLines[i].Split(delimiters.ToCharArray(), options: StringSplitOptions.RemoveEmptyEntries)
  29.                     .Distinct()
  30.                     .ToArray();
  31.  
  32.                 ForEach(words, (word) =>
  33.                 {
  34.                     if (dictionary.Contains(word))
  35.                         wordsToMark.Add(word);
  36.                 });
  37.  
  38.                 foreach (var word in wordsToMark)
  39.                     textLines[i] = textLines[i].Replace(word, MarkWord(word));
  40.             }
  41.  
  42.             WriteAllLines(outputFilename, textLines, Encoding.UTF8);
  43.         }
  44.  
  45.         static string MarkWord(string word) => $"<b><i>{word}</b></i>";
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment