|
@@ -0,0 +1,377 @@
|
|
|
+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";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译文档Key接口
|
|
|
+ /// </summary>
|
|
|
+ private static string Uri_fileKeytranslate = translate + "/v1/translate/fileUpload";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译文档接口
|
|
|
+ /// </summary>
|
|
|
+ private static string Uri_filetranslate = translate + "/v1/translate/fileTranslateHandle";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译文本接口
|
|
|
+ /// </summary>
|
|
|
+ private static string Uri_texttranslate = translate + "/v1/translate/textTrans";
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// FAQ接口
|
|
|
+ ///// </summary>
|
|
|
+ //private static string Uri_FAQ = host + "/find-faq";
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// 跳转接口
|
|
|
+ ///// </summary>
|
|
|
+ //private static string Uri_GoToView = host + "/recognition";
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// 摘要接口
|
|
|
+ ///// </summary>
|
|
|
+ //private static string Uri_Summary = host + "/summary";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 错别字纠正
|
|
|
+ /// </summary>
|
|
|
+ private static string Uri_Correction = host + "/api/correct-typos";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 错别字纠正
|
|
|
+ /// </summary>
|
|
|
+ private static string Uri_Rewrite = host + "/api/rewrite";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译指定内容,可以指定语言
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="content"></param>
|
|
|
+ /// <param name="language">1=中文 2=英文 3=法语 4=汉语</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async static Task<string> 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译指定内容,可以指定语言
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="fileKey">文本Key</param>
|
|
|
+ /// <param name="fromlanguage">文本语言</param>
|
|
|
+ /// <param name="tolanguage">需要翻译的语言</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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<string>().ToLower() == "200")
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ //return jobject["code"].ToObject<string>().ToLower();
|
|
|
+
|
|
|
+ return "200";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return "300";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 翻译指定内容,可以指定语言
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="content">翻译内容</param>
|
|
|
+ /// <param name="fromlanguage">文本语言</param>
|
|
|
+ /// <param name="tolanguage">需要翻译的语言</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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<string>().ToLower() == "200")
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ //return jobject["code"].ToObject<string>().ToLower();
|
|
|
+
|
|
|
+ return "200";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return "300";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public async static Task<string> 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 纠错
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="content"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async static Task<string> 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 重写
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="content"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async static Task<string> 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// 页面跳转
|
|
|
+ ///// </summary>
|
|
|
+ ///// <param name="content"></param>
|
|
|
+ ///// <returns></returns>
|
|
|
+ //public async static Task<string> GoToView(string content)
|
|
|
+ //{
|
|
|
+ // System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
+ // //需要翻译的内容
|
|
|
+ // namevalue["str"] = content;
|
|
|
+ // return await PostString(Uri_GoToView, namevalue, "flag");
|
|
|
+ //}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从链接中返回存在的链接(仅返回第一个找到的链接)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="content"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<string> GetLinkFromString(string content)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(content))
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 创建正则表达式模式
|
|
|
+ string pattern = @"(https?://[^\s]+)";
|
|
|
+
|
|
|
+ // 获取所有匹配项
|
|
|
+ MatchCollection matches = Regex.Matches(content, pattern);
|
|
|
+
|
|
|
+ List<string> list = new List<string>();
|
|
|
+ // 遍历并打印所有匹配项
|
|
|
+ foreach (Match match in matches)
|
|
|
+ {
|
|
|
+ list.Add(match.Value);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从接口获取信息,获取message对应value值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="url"></param>
|
|
|
+ /// <param name="namevalue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async static Task<string> 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<Dictionary<string, string>>(json);
|
|
|
+
|
|
|
+ string unicode = "";
|
|
|
+ // 遍历字典对象输出键值对
|
|
|
+ foreach (KeyValuePair<string, string> item in values)
|
|
|
+ {
|
|
|
+ if (item.Key == key)
|
|
|
+ {
|
|
|
+ unicode = item.Value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将Unicode格式转换成String
|
|
|
+ repsonseData = Regex.Unescape(unicode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return repsonseData;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|