using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PDF_Master.Properties; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace PDF_Master.Helper { public static class ChatGTPAIHelper { private static string host = "http://api-us-east-1.compdf.com:8082"; private static string translate = "http://101.132.103.13:8030"; /// /// 翻译文档Key接口 /// private static string Uri_fileKeytranslate = translate + "/v1/translate/fileUpload"; /// /// 翻译文档接口 /// private static string Uri_filetranslate = translate + "/v1/translate/fileTranslateHandle"; /// /// 翻译文本接口 /// private static string Uri_texttranslate = translate + "/v1/translate/textTrans"; ///// ///// FAQ接口 ///// //private static string Uri_FAQ = host + "/find-faq"; ///// ///// 跳转接口 ///// //private static string Uri_GoToView = host + "/recognition"; ///// ///// 摘要接口 ///// //private static string Uri_Summary = host + "/summary"; /// /// 错别字纠正 /// private static string Uri_Correction = host + "/api/correct-typos"; /// /// 错别字纠正 /// private static string Uri_Rewrite = host + "/api/rewrite"; /// /// 翻译指定内容,可以指定语言 /// /// /// 1=中文 2=英文 3=法语 4=汉语 /// public async static Task fileKeyTranslate(string content, int language) { System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection(); //需要翻译的内容 namevalue["file"] = content; //1=中文 2=英文 3=法语 4=汉语 namevalue["projectId"] = "2"; namevalue["version"] = "1.0.1"; if (!string.IsNullOrEmpty(Settings.Default.UserDate.id)) { namevalue["userId"] = Settings.Default.UserDate.id; } return await PostString(Uri_fileKeytranslate, namevalue); } /// /// 翻译指定内容,可以指定语言 /// /// 文本Key /// 文本语言 /// 需要翻译的语言 /// public static String fileTranslate(string fileKey, string fromlanguage, string tolanguage) { HttpWebResponse response = null; ServicePointManager.DefaultConnectionLimit = 200; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate); request.Method = "Post"; request.ContentType = "application/json"; //request.Accept = "application/vnd.api+json;version=1"; request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)"; request.Timeout = 20000; request.ServicePoint.Expect100Continue = false; StringWriter sw = new StringWriter(); using (JsonWriter writer = new JsonTextWriter(sw)) { writer.WriteStartObject(); writer.WritePropertyName("fileKey"); writer.WriteValue(fileKey); writer.WritePropertyName("from"); writer.WriteValue(fromlanguage); writer.WritePropertyName("to"); writer.WriteValue(tolanguage); writer.WritePropertyName("projectId"); writer.WriteValue("2"); writer.WritePropertyName("version"); writer.WriteValue("1.0.1"); writer.WritePropertyName("userId"); writer.WriteValue(Settings.Default.UserDate.id); writer.WriteEndObject(); } try { string postBody = sw.ToString(); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postBody); writer.Close(); } response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseData = reader.ReadToEnd(); Console.WriteLine(responseData); reader.Close(); JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } if (jobject["code"].ToObject().ToLower() == "200") { } //return jobject["code"].ToObject().ToLower(); return "200"; } } catch { return "300"; } } /// /// 翻译指定内容,可以指定语言 /// /// 翻译内容 /// 文本语言 /// 需要翻译的语言 /// public static String textTranslate(string content, string fromlanguage, string tolanguage) { HttpWebResponse response = null; ServicePointManager.DefaultConnectionLimit = 200; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate); request.Method = "Post"; request.ContentType = "application/json"; //request.Accept = "application/vnd.api+json;version=1"; request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)"; request.Timeout = 20000; request.ServicePoint.Expect100Continue = false; StringWriter sw = new StringWriter(); using (JsonWriter writer = new JsonTextWriter(sw)) { writer.WriteStartObject(); writer.WritePropertyName("q"); writer.WriteValue(content); writer.WritePropertyName("from"); writer.WriteValue(fromlanguage); writer.WritePropertyName("to"); writer.WriteValue(tolanguage); writer.WritePropertyName("projectId"); writer.WriteValue("2"); writer.WritePropertyName("version"); writer.WriteValue("1.0.1"); writer.WritePropertyName("userId"); writer.WriteValue(Settings.Default.UserDate.id); writer.WriteEndObject(); } try { string postBody = sw.ToString(); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postBody); writer.Close(); } response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseData = reader.ReadToEnd(); Console.WriteLine(responseData); reader.Close(); JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } if (jobject["code"].ToObject().ToLower() == "200") { } //return jobject["code"].ToObject().ToLower(); return "200"; } } catch { return "300"; } } public async static Task textTranslate(string content, int language) { System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection(); //需要翻译的内容 namevalue["q"] = content; //1=中文 2=英文 3=法语 4=汉语 namevalue["from"] = "auto"; namevalue["to"] = "en"; namevalue["projectId"] = "2"; namevalue["version"] = "1.0.1"; if (!string.IsNullOrEmpty(Settings.Default.UserDate.id)) { namevalue["userId"] = Settings.Default.UserDate.id; } return await PostString(Uri_filetranslate, namevalue); } /// /// 纠错 /// /// /// public async static Task Correction(string content) { System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection(); //需要纠错的内容 namevalue["project_id"] = "3"; namevalue["version"] = "1.0.1"; if (!string.IsNullOrEmpty(Settings.Default.UserDate.id)) { namevalue["user_id"] = Settings.Default.UserDate.id; } namevalue["content"] = content; return await PostString(Uri_Correction, namevalue); } /// /// 重写 /// /// /// public async static Task Rewrite(string content) { System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection(); //需要纠错的内容 namevalue["project_id"] = "3"; namevalue["version"] = "1.0.1"; if (!string.IsNullOrEmpty(Settings.Default.UserDate.id)) { namevalue["user_id"] = Settings.Default.UserDate.id; } namevalue["content"] = content; return await PostString(Uri_Rewrite, namevalue); } ///// ///// 页面跳转 ///// ///// ///// //public async static Task GoToView(string content) //{ // System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection(); // //需要翻译的内容 // namevalue["str"] = content; // return await PostString(Uri_GoToView, namevalue, "flag"); //} /// /// 从链接中返回存在的链接(仅返回第一个找到的链接) /// /// /// public static List GetLinkFromString(string content) { if (string.IsNullOrEmpty(content)) { return null; } // 创建正则表达式模式 string pattern = @"(https?://[^\s]+)"; // 获取所有匹配项 MatchCollection matches = Regex.Matches(content, pattern); List list = new List(); // 遍历并打印所有匹配项 foreach (Match match in matches) { list.Add(match.Value); } return list; } /// /// 从接口获取信息,获取message对应value值 /// /// /// /// private async static Task PostString(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message") { string repsonseData = ""; try { using (var client = new WebClient()) { byte[] bytes = await client.UploadValuesTaskAsync(url, namevalue); //转换成字符串类型 var json = Encoding.Default.GetString(bytes); //将Json格式字符串转换成键值对 var values = JsonConvert.DeserializeObject>(json); string unicode = ""; // 遍历字典对象输出键值对 foreach (KeyValuePair item in values) { if (item.Key == key) { unicode = item.Value; } } //将Unicode格式转换成String repsonseData = Regex.Unescape(unicode); } } catch (Exception ex) { return null; } return repsonseData; } } }