【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 }
bin\Debug\Data 下建立first.txt ,将需要美化的句子存入其中,需要单个同义词替换的词语用[]括起来,需要多个同义词替换的词语用<>括起来,运行程序后,同文件夹下产生last.txt 保存美化后的句子。
样例:
first.txt
<勤奋>的[鄙人][不仅]<乐观>而且[和蔼可亲]。
last.txt
朝乾夕惕、埋头苦干、废寝忘食、鸡鸣而起、将勤补拙、孜孜不倦、夙兴夜寐、凿壁偷光、业精于勤、焚膏继晷、发愤忘食、悬梁刺股、牛角挂书、卧薪尝胆、攻苦食淡、夜以继日、韦编三绝、天道酬勤、勤学苦练、勤学好问的在下不但苦中作乐、喜气洋洋、喜上眉梢、逍遥自在、时来运转、妙趣横生、乐在其中、其乐融融、其乐无穷、意气风发、威风凛凛、满面红光、神采奕奕、神采飞扬、容光焕发、善有善报、美意延年、指日可待而且菩萨低眉。