1. 程式人生 > 實用技巧 >【C#】漢語言美化程式

【C#】漢語言美化程式

看到唯美的句子深受感動,於是寫了個漢語言美化程式✧٩(ˊωˋ*)و✧
如果有不妥當的地方,八成怪百度漢語胡亂增添近義詞ヾ( ̄0 ̄; )ノ

  1 using System;
  2 using System.Net;
  3 using System.IO;
  4 using System.Text;
  5 using System.Text.RegularExpressions;
  6 using System.Collections;
  7 
  8 namespace 成語替換
  9 {
 10     class Program
 11     {
 12 
 13
// 用post方法請求http 14 // 暫時無用 15 public static string SendHttpPost(string url, string paraJsonStr) 16 { 17 WebClient webClient = new WebClient(); 18 webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 19 byte
[] postDate = System.Text.Encoding.UTF8.GetBytes(paraJsonStr); 20 ServicePointManager.Expect100Continue = false; // 預設通過 21 string returnStr = ""; 22 try 23 { 24 byte[] responseDate = webClient.UploadData(url, "POST", postDate); 25 returnStr = System.Text.Encoding.UTF8.GetString(responseDate);
26 } 27 catch (Exception e) 28 { 29 Console.WriteLine(e.ToString()); 30 } 31 // if (returnStr == "") 32 returnStr = GetRequest(url); 33 return returnStr; 34 } 35 36 // get方法請求http 37 public static string GetRequest(string url) 38 { 39 Console.WriteLine(url); 40 try 41 { 42 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 43 request.Method = "get"; 44 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 45 using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 46 { 47 string result = sr.ReadToEnd(); 48 return result; 49 } 50 } 51 catch (Exception e) 52 { 53 if (e.ToString().Length == 1) // 實際執行的時候沒打算輸出exception 54 Console.WriteLine(e.ToString()); 55 return ""; 56 } 57 } 58 59 // get方法請求https 60 // 適用於net4.0 61 public static string DoRequest(string Url, string cookieStr) 62 { 63 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 64 request.Timeout = 1000 * 900; 65 request.Headers.Add(HttpRequestHeader.Cookie, cookieStr); 66 request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"; 67 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 68 string back = ""; 69 try 70 { 71 WebResponse response = request.GetResponse(); 72 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); 73 back = reader.ReadToEnd(); 74 reader.Close(); 75 reader.Dispose(); 76 response.Close(); 77 } 78 catch (Exception ex) 79 { 80 back = ex.Message; 81 } 82 return back; 83 } 84 85 // 獲取字串之中兩個字串之間的字串 86 public static string Midstr(string sourse, string s, string e) 87 { 88 Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline); 89 return rg.Match(sourse).Value; 90 } 91 92 // 與java的replaceFirst函式效果相同 93 // 用某字串替換字串中的第一次出現的某字串 94 public static string ReplaceFirst(string value, string oldValue, string newValue) 95 { 96 if (string.IsNullOrEmpty(oldValue)) 97 return value; 98 99 int idx = value.IndexOf(oldValue); 100 if (idx == -1) 101 return value; 102 value = value.Remove(idx, oldValue.Length); 103 return value.Insert(idx, newValue); 104 } 105 106 static ArrayList GetWD(ArrayList WDs, string htm) 107 { 108 int removeIndex = htm.IndexOf("<label>反義詞 </label>"); 109 if (removeIndex > 0) 110 htm = htm.Remove(removeIndex); 111 string WD = ""; 112 string oldWD = WD; 113 while (true) 114 { 115 WD = Midstr(htm, "wd=", "&ptype=zici"); 116 if (WD == "" || WD == oldWD) 117 break; 118 if (WD.Length == 4) 119 WDs.Add(WD); 120 else if (Math.Abs(ReplaceFirst(WD, "&cf=synant", "").Length - 3) == 1) 121 WDs.Add(ReplaceFirst(WD, "&cf=synant", "")); 122 else if (WD.Length > 40 && WD[WD.Length - 5] == '=' && WD[WD.Length - 6] == 'd' && WD[WD.Length - 7] == 'w') 123 WDs.Add(WD.Substring(WD.Length - 4, 4)); 124 oldWD = WD; 125 htm = ReplaceFirst(htm, "wd=" + WD + "&ptype=zici", ""); 126 } 127 return WDs; 128 } 129 130 static void Main(string[] args) 131 { 132 133 string filePath = @"Data"; // 設定資料夾名字為Data 134 if (!Directory.Exists(filePath)) 135 Directory.CreateDirectory(filePath); 136 137 string writeFile = @"Data\last.txt"; 138 string str = File.ReadAllText(@"Data\first.txt", Encoding.UTF8); 139 140 string keyword = ""; 141 string oldKeyword = keyword; 142 string cookie = ""; 143 while (true) 144 { 145 keyword = Midstr(str, "\\[", "\\]"); 146 if (keyword == "" || keyword == oldKeyword) 147 break; 148 oldKeyword = keyword; 149 string keyword2 = keyword; 150 if (keyword2.Length <= 2) 151 keyword2 = keyword2 + "的成語"; 152 string htm = DoRequest("https://dict.baidu.com/s?wd=" + keyword2, cookie); 153 ArrayList WDs = new ArrayList(); 154 WDs = GetWD(WDs, htm); 155 string WD = keyword; 156 Random rnd = new Random(); 157 if (WDs.Count > 0) 158 WD = WDs[rnd.Next(0, WDs.Count)].ToString(); 159 str = ReplaceFirst(str, "[" + keyword + "]", WD); 160 } 161 162 keyword = ""; 163 oldKeyword = keyword; 164 while (true) 165 { 166 keyword = Midstr(str, "<", ">"); 167 if (keyword == "" || keyword == oldKeyword) 168 break; 169 oldKeyword = keyword; 170 string keyword2 = keyword; 171 if (keyword2.Length <= 2) 172 keyword2 = keyword2 + "的成語"; 173 string htm = DoRequest("https://dict.baidu.com/s?wd=" + keyword2, cookie); 174 ArrayList WDs = new ArrayList(); 175 WDs = GetWD(WDs, htm); 176 string WD = keyword; 177 178 if (WDs.Count > 0) 179 { 180 WD = WDs[0].ToString(); 181 for (int i = 1; i < WDs.Count; i++) 182 WD = WD + "" + WDs[i].ToString(); 183 } 184 str = ReplaceFirst(str, "<" + keyword + ">", WD); 185 } 186 187 File.WriteAllText(writeFile, str); 188 Console.WriteLine(str); 189 Console.ReadLine(); 190 } 191 } 192 }
View Code

bin\Debug\Data 下建立first.txt ,將需要美化的句子存入其中,需要單個同義詞替換的詞語用[]括起來,需要多個同義詞替換的詞語用<>括起來,執行程式後,同文件夾下產生last.txt 儲存美化後的句子。

樣例:

first.txt

<勤奮>的[鄙人][不僅]<樂觀>而且[和藹可親]。

last.txt

朝乾夕惕、埋頭苦幹、廢寢忘食、雞鳴而起、將勤補拙、孜孜不倦、夙興夜寐、鑿壁偷光、業精於勤、焚膏繼晷、發憤忘食、懸樑刺股、牛角掛書、臥薪嚐膽、攻苦食淡、夜以繼日、韋編三絕、天道酬勤、勤學苦練、勤學好問的在下不但苦中作樂、喜氣洋洋、喜上眉梢、逍遙自在、時來運轉、妙趣橫生、樂在其中、其樂融融、其樂無窮、意氣風發、威風凜凜、滿面紅光、神采奕奕、神采飛揚、容光煥發、善有善報、美意延年、指日可待而且菩薩低眉。