1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Text.RegularExpressions;
7
8namespace ReadAndWriteFile
9{
10 class Program
11 {
12 /// <summary>
13 /// Đọc toàn bộ nội dung file trong đường dẫn filePath, và in ra màn hình console
14 /// </summary>
15 /// <param name="filePath">Đường dẫn file</param>
16 static string ReadFile(string filePath)
17 {
18 // Sử dụng Try, Catch để bắt các lỗi ngoại lệ...
19 // Tránh làm CRASH chương trình
20 try
21 {
22 // Sử dụng StringBuilder để nối chuỗi lại với nhau
23 // sẽ tối ưu tốc độ hơn so với việc sử dụng toán tử += string
24 StringBuilder sb = new StringBuilder();
25
26 // Sử dụng StreamReader để đọc dữ liệu file "data.txt"
27 using (StreamReader sr = new StreamReader(filePath))
28 {
29 string line;
30
31 // Sử dụng vòng lặp While để đọc từng dòng dữ liệu
32 // đến khi đọc hết file
33 while ((line = sr.ReadLine()) != null)
34 {
35 // In ra màn hình
36 Console.WriteLine(line);
37
38 // Nối chuỗi
39 sb.AppendLine(line);
40 }
41 }
42
43 // Trả về chuỗi tất cả text
44 return sb.ToString();
45 }
46 catch (Exception e)
47 {
48 // Thông báo lỗi ngoại lệ
49 Console.WriteLine("Không thể đọc được file. Lỗi xảy ra:");
50 Console.WriteLine(e.Message);
51 }
52
53 // Nếu chạy đến đây thì có khả năng lỗi đã xảy ra, trả về rỗng
54 return "";
55 }
56
57 /// <summary>
58 /// Hàm ghi nội dung vào filePath
59 /// </summary>
60 /// <param name="filePath">Đường dẫn file cần ghi</param>
61 /// <param name="content">Nội dung</param>
62 /// <returns></returns>
63 static bool WriteFile(string filePath, string content)
64 {
65 // Sử dụng Try, Catch để bắt các lỗi ngoại lệ...
66 // Tránh làm CRASH chương trình
67 try
68 {
69 using (StreamWriter sw = new StreamWriter(filePath))
70 {
71 sw.WriteLine(content);
72 }
73 return true;
74 }
75 catch (Exception e)
76 {
77 // Thông báo lỗi ngoại lệ
78 Console.WriteLine("Không thể ghi được file. Lỗi xảy ra:");
79 Console.WriteLine(e.Message);
80 }
81
82 // Nếu chạy đến đây thì có khả năng lỗi đã xảy ra, trả về false
83 return false;
84 }
85
86 /// <summary>
87 /// Trích xuất nội dung trong file
88 /// </summary>
89 /// <param name="content">Nội dung file</param>
90 static string ExtractDataInContent(string content)
91 {
92 // Sử dụng Try, Catch để bắt các lỗi ngoại lệ...
93 // Tránh làm CRASH chương trình
94 try
95 {
96 // Tạo danh sách chứa các thông tin trích xuất được
97 List<string> lstEmail = new List<string>();
98 List<string> lstName = new List<string>();
99 List<string> lstTel = new List<string>();
100
101 // Trích xuất EMAIL
102 string emailPattern = @"email\:\w+@(nentang\.vn|ctu\.edu\.vn|gmail\.com|cusc\.ctu\.edu\.vn)"; //Mẫu regular pattern kiểm tra EMAIL
103 MatchCollection mcEmail = Regex.Matches(content, emailPattern);
104 foreach (Match m in mcEmail)
105 {
106 lstEmail.Add(m.ToString());
107 }
108
109 // Trích xuất NAME
110 string namePattern = @"name:\w+"; //Mẫu regular pattern kiểm tra NAME
111 MatchCollection mcName = Regex.Matches(content, namePattern);
112 foreach (Match m in mcName)
113 {
114 lstName.Add(m.ToString());
115 }
116
117 // Trích xuất TEL
118 string telPattern = @"tel:\d+"; //Mẫu regular pattern kiểm tra TEL
119 MatchCollection mcTel = Regex.Matches(content, telPattern);
120 foreach (Match m in mcTel)
121 {
122 lstTel.Add(m.ToString());
123 }
124
125 return String.Join(Environment.NewLine, (lstEmail.Concat(lstName).Concat(lstTel)));
126 }
127 catch (Exception e)
128 {
129 // Thông báo lỗi ngoại lệ
130 Console.WriteLine("Không thể đọc được file. Lỗi xảy ra:");
131 Console.WriteLine(e.Message);
132 }
133
134 // Nếu chạy đến đây thì có khả năng lỗi đã xảy ra, trả về content
135 return content;
136 }
137
138 /// <summary>
139 /// Tạo mẫu Regular pattern để lọc các từ bad word
140 /// AA|BB|CC|DD|EE
141 /// </summary>
142 /// <param name="filePath">Đường dẫn file</param>
143 static string GeneratePatternFromBadWordFile(string filePath)
144 {
145 // Sử dụng Try, Catch để bắt các lỗi ngoại lệ...
146 // Tránh làm CRASH chương trình
147 try
148 {
149 // Tạo list lưu trữ Bad word
150 List<string> lstBadWord = new List<string>();
151
152 // Sử dụng StreamReader để đọc dữ liệu file "data.txt"
153 using (StreamReader sr = new StreamReader(filePath))
154 {
155 string line;
156
157 // Sử dụng vòng lặp While để đọc từng dòng dữ liệu
158 // đến khi đọc hết file
159 while ((line = sr.ReadLine()) != null)
160 {
161 // Nối chuỗi
162 lstBadWord.Add(line);
163 }
164 }
165
166 // Ghép các từ trong LIST với ký tự "|"
167 return "(" + String.Join("|", lstBadWord) + ")";
168 }
169 catch (Exception e)
170 {
171 // Thông báo lỗi ngoại lệ
172 Console.WriteLine("Không thể đọc được file. Lỗi xảy ra:");
173 Console.WriteLine(e.Message);
174 }
175
176 // Nếu chạy đến đây thì có khả năng lỗi đã xảy ra, trả về rỗng
177 return "";
178 }
179
180 /// <summary>
181 /// Hàm kiểm tra xem có tồn tại BAD WORD trong CONTENT hay không?
182 /// </summary>
183 /// <param name="content">Nội dung cần kiểm duyệt</param>
184 /// <param name="pattern">Mẫu Regular Expression</param>
185 /// <returns></returns>
186 static int DetectBadWordInContent(string content, string pattern)
187 {
188 MatchCollection mc = Regex.Matches(content, pattern);
189 return mc.Count;
190 }
191
192 /// <summary>
193 /// Hàm loại bỏ các BAD WORD
194 /// </summary>
195 /// <param name="content">Nội dung cần kiểm duyệt</param>
196 /// <param name="pattern">Mẫu Regular Expression</param>
197 /// <returns></returns>
198 static string RemoveBadWordInContent(string content, string pattern)
199 {
200 MatchCollection mc = Regex.Matches(content, pattern);
201 foreach (Match m in mc)
202 {
203 content = content.Replace(m.ToString(), "");
204 }
205
206 return content;
207 }
208
209 static void Main(string[] args)
210 {
211 // Đường dẫn đến file dữ liệu "data.txt"
212 // - Đường dẫn tuyệt đối là được dẫn được chỉ định cụ thể: [Ổ đĩa]:\[Thư mục]\[Tên file]
213 // Ví dụ: D:\DuLieu\data.txt
214 // - Đường dẫn tương đối được tính từ file .EXE của chương trình
215 // Ví dụ: data.txt
216 string filePath = "data.txt";
217
218 // Đọc nội dung file và in ra màn hình
219 Console.WriteLine("Noi dung file \"data.txt\"");
220 string allText = ReadFile(filePath);
221
222 // Tìm và in danh sách các email hợp lệ để gởi mail MARKETING hàng loạt đến người dùng
223 string contentExtracted = ExtractDataInContent(allText);
224
225 // Lưu nội dung được trích xuất ra file
226 string dataExtractedFilePath = "data_extracted.txt";
227 WriteFile(dataExtractedFilePath, contentExtracted);
228 Console.WriteLine("Ket qua trich xuat du lieu duoc luu trong file {0}", dataExtractedFilePath);
229
230 // Tạo mẫu pattern từ các BAD WORD trong file
231 string badWordFilePath = "base-list-of-bad-words_text-file.txt";
232 string badWordPattern = GeneratePatternFromBadWordFile(badWordFilePath);
233
234 // Kiểm tra có BAD WORD không?
235 int badWordCount = DetectBadWordInContent(allText, badWordPattern);
236 bool foundBadWord = badWordCount > 0;
237 Console.WriteLine("Ket qua kiem tra noi dung: {0}", (foundBadWord ? "Phat hien BAD WORD!!!" : "Noi dung OKEY!!!"));
238
239 // Loại bỏ BAD WORD trong nội dung (nếu phát hiện được)
240 if (foundBadWord)
241 {
242 Console.WriteLine("So luong tu BAD WORD phat hien duoc: {0}", badWordCount);
243
244 string contentChecked = RemoveBadWordInContent(allText, badWordPattern);
245 string okeyFilePath = "data_checked.txt";
246 WriteFile(okeyFilePath, contentChecked);
247 Console.WriteLine("Ket qua kiem duyet noi dung duoc luu trong file {0}", okeyFilePath);
248 }
249
250 // Dừng màn hình chờ nhấn Enter để kết thúc
251 Console.ReadLine();
252 }
253 }
254}