123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- 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;
- }
- }
- }
|