diff --git a/IntentoSDK/Handlers/BaseSymbolHandler.cs b/IntentoSDK/Handlers/BaseSymbolHandler.cs new file mode 100644 index 0000000..605f527 --- /dev/null +++ b/IntentoSDK/Handlers/BaseSymbolHandler.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IntentoSDK.Handlers +{ + /// + /// Base symbol handler + /// + public abstract class BaseSymbolHandler : ISymbolHandler + { + protected abstract IReadOnlyDictionary SpecialCodesIn { get; } + + protected abstract IReadOnlyDictionary SpecialCodesOut { get; } + + public abstract string Format { get; } + + public virtual string OnResponsing(string text) + { + return PrepareResult(text); + } + + public virtual string OnSending(string text) + { + return PrepareText(text); + } + + protected virtual string PrepareText(string data) + { + // Remove parasite character for memoq + data = new string(data.Where(c => (int)c != 9727).ToArray()); + + // Replacing some HTML codes with special tags + foreach (KeyValuePair pair in SpecialCodesIn) + { + data = data.Replace(pair.Key, pair.Value); + } + return data; + } + + protected virtual string PrepareResult(string text) + { + // Return HTML codes instead of special tags + foreach (KeyValuePair pair in SpecialCodesOut) + { + text = text.Replace(pair.Key, pair.Value); + } + + return text; + } + + } +} diff --git a/IntentoSDK/Handlers/HtmlSymbolHandler.cs b/IntentoSDK/Handlers/HtmlSymbolHandler.cs new file mode 100644 index 0000000..fddda60 --- /dev/null +++ b/IntentoSDK/Handlers/HtmlSymbolHandler.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IntentoSDK.Handlers +{ + /// + /// Prepare HTML text + /// + internal class HtmlSymbolHandler : BaseSymbolHandler + { + protected override IReadOnlyDictionary SpecialCodesIn => new Dictionary(); + + protected override IReadOnlyDictionary SpecialCodesOut => + new Dictionary + { + { "<", "<" }, + { ">", ">" } + }; + + public override string Format => "html"; + + protected override string PrepareResult(string text) + { + text = base.PrepareResult(text); + + // Remove and tags + int n1 = text.IndexOf(""); + text2 = text.Substring(n2 + 1); + } + return text2; + } + } +} diff --git a/IntentoSDK/Handlers/ISymbolHandler.cs b/IntentoSDK/Handlers/ISymbolHandler.cs new file mode 100644 index 0000000..31d3f8f --- /dev/null +++ b/IntentoSDK/Handlers/ISymbolHandler.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IntentoSDK.Handlers +{ + /// + /// Process text before and after sending + /// + public interface ISymbolHandler + { + /// + /// Type of text format + /// + string Format { get; } + + /// + /// Prepare text before send + /// + /// Text + /// + string OnSending(string text); + + /// + /// Prepare text after send + /// + /// + /// + string OnResponsing(string text); + } +} diff --git a/IntentoSDK/Handlers/SymbolHandlersFactory.cs b/IntentoSDK/Handlers/SymbolHandlersFactory.cs new file mode 100644 index 0000000..291f441 --- /dev/null +++ b/IntentoSDK/Handlers/SymbolHandlersFactory.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IntentoSDK.Handlers +{ + /// + /// Factory for handlers + /// + public class SymbolHandlersFactory + { + private readonly ISymbolHandler[] symbolHandlers; + + /// + /// Ctor + /// + public SymbolHandlersFactory() + { + symbolHandlers = new ISymbolHandler[] { + new XmlSymbolHandler(), + new HtmlSymbolHandler() + }; + } + + /// + /// Prepare source + /// + /// + /// + /// + public string HandleSource(string text, string format) + { + foreach (ISymbolHandler handler in symbolHandlers.Where(h => h.Format == format)) + { + text = handler.OnSending(text); + } + return text; + } + + /// + /// Prepare result + /// + /// + /// + /// + public string HandleResult(string text, string format) + { + foreach (ISymbolHandler handler in symbolHandlers.Where(h => h.Format == format)) + { + text = handler.OnResponsing(text); + } + return text; + } + + private static readonly SymbolHandlersFactory current = new SymbolHandlersFactory(); + + /// + /// Current instance of factory + /// + public static SymbolHandlersFactory Current => current; + + } +} diff --git a/IntentoSDK/Handlers/XmlSymbolHandler.cs b/IntentoSDK/Handlers/XmlSymbolHandler.cs new file mode 100644 index 0000000..35c6abc --- /dev/null +++ b/IntentoSDK/Handlers/XmlSymbolHandler.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IntentoSDK.Handlers +{ + /// + /// Xml symbols prepare + /// + internal class XmlSymbolHandler : BaseSymbolHandler + { + protected override IReadOnlyDictionary SpecialCodesIn => new Dictionary + { + { "&gt;" , "" }, + { "&lt;" , "" }, + { "<" , "" }, + { ">" , "" } + }; + + protected override IReadOnlyDictionary SpecialCodesOut => new Dictionary + { + { "" , "&gt;" }, + { "" , "&lt;" }, + { "" , "<" }, + { "" , ">" }, + { "", "&gt;" }, + { "", "&lt;" }, + { "", "<" }, + { "", ">" }, + { "", "&gt;" }, + { "", "&lt;" }, + { "", "<" }, + { "", ">" }, + { "", "" }, + { "", "" }, + { "", "" }, + { "", "" } + }; + + public override string Format => "xml"; + + protected override string PrepareResult(string text) + { + text = base.PrepareResult(text); + + // Remove tag + int n1 = text.IndexOf(""); + text2 = text.Substring(n2 + 1); + } + + // Remove and tags + string text3 = text2.Replace("", "").Replace("", ""); + return text3; + } + + protected override string PrepareText(string data) + { + data = base.PrepareText(data); + return string.Format("{0}", data); + } + } +} diff --git a/IntentoSDK/HttpConnector.cs b/IntentoSDK/HttpConnector.cs index 73af453..fd3f559 100644 --- a/IntentoSDK/HttpConnector.cs +++ b/IntentoSDK/HttpConnector.cs @@ -46,14 +46,15 @@ public HttpConnector(Intento _intento) client.DefaultRequestHeaders.Add("User-Agent", userAgent); } - async public Task PostAsync(string path, dynamic json, Dictionary special_headers = null, Dictionary additionalParams = null) + async public Task PostAsync(string path, dynamic json, Dictionary special_headers = null, + Dictionary additionalParams = null, bool useSyncwrapper = false) { try { string jsonData = JsonConvert.SerializeObject(json); using (HttpContent httpContent = new StringContent(jsonData)) { - var url = MakeUrl(path, additionalParams); + var url = MakeUrl(path, additionalParams, useSyncwrapper); if (special_headers != null && special_headers.Count != 0) { foreach (KeyValuePair pair in special_headers) @@ -112,12 +113,13 @@ async public Task GetAsync(string path, Dictionary addi } } - private string MakeUrl(string path, Dictionary additionalParams) + private string MakeUrl(string path, Dictionary additionalParams, bool useSyncwrapper = false) { - if (additionalParams == null) - return intento.serverUrl + path; + string url = useSyncwrapper ? intento.syncwrapperUrl : intento.serverUrl; + if (additionalParams == null) + return url + path; - UriBuilder uri = new UriBuilder(intento.serverUrl + path); + UriBuilder uri = new UriBuilder(url + path); System.Collections.Specialized.NameValueCollection query = HttpUtility.ParseQueryString(uri.Query); foreach (KeyValuePair pair in additionalParams) query[pair.Key] = pair.Value; diff --git a/IntentoSDK/Intento.cs b/IntentoSDK/Intento.cs index c6e4049..78af3d8 100644 --- a/IntentoSDK/Intento.cs +++ b/IntentoSDK/Intento.cs @@ -54,8 +54,9 @@ public class Intento { internal string apiKey; internal Dictionary auth; - internal string serverUrl; - internal string otherUserAgent; + internal string serverUrl; + internal string syncwrapperUrl; + internal string otherUserAgent; internal string version; internal Action loggingCallback; internal int waitAsyncDelay = 0; @@ -81,7 +82,10 @@ private Intento( this.apiKey = apiKey; this.auth = auth != null ? new Dictionary(auth) : null; this.serverUrl = string.IsNullOrEmpty(path) ? "https://api.inten.to/" : path; - otherUserAgent = userAgent; + // path can be https://api2.inten.to/ + // this.syncwrapperUrl = this.serverUrl.Replace("https://api", "https://syncwrapper-memoq"); + this.syncwrapperUrl = "https://syncwrapper-memoq.inten.to/"; + otherUserAgent = userAgent; System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; this.waitAsyncDelay = waitAsyncDelay; diff --git a/IntentoSDK/IntentoAiTextTranslate.cs b/IntentoSDK/IntentoAiTextTranslate.cs index 882c09d..3af9a85 100644 --- a/IntentoSDK/IntentoAiTextTranslate.cs +++ b/IntentoSDK/IntentoAiTextTranslate.cs @@ -30,13 +30,14 @@ public dynamic Fulfill(object text, string to, string from = null, string provid string custom_model = null, string glossary = null, object pre_processing = null, object post_processing = null, bool failover = false, object failover_list = null, string routing = null, bool trace = false, - Dictionary special_headers = null) + Dictionary special_headers = null, bool useSyncwrapper = false) { Task taskReadResult = Task.Run(async () => await this.FulfillAsync(text, to, from: from, provider: provider, async: async, wait_async: wait_async, format: format, auth: auth, custom_model: custom_model, glossary: glossary, pre_processing: pre_processing, post_processing: post_processing, - failover: failover, failover_list: failover_list, routing: routing, trace: trace, special_headers: special_headers)); + failover: failover, failover_list: failover_list, routing: routing, trace: trace, + special_headers: special_headers, useSyncwrapper: useSyncwrapper)); return taskReadResult.Result; } @@ -45,7 +46,7 @@ async public Task FulfillAsync(object text, string to, string from = nu string custom_model = null, string glossary = null, object pre_processing = null, object post_processing = null, bool failover = false, object failover_list = null, string routing = null, bool trace = false, - Dictionary special_headers = null) + Dictionary special_headers = null, bool useSyncwrapper = false) { dynamic preProcessingJson = GetJson(pre_processing, "pre_processing"); dynamic postProcessingJson = GetJson(post_processing, "post_processing"); @@ -55,16 +56,33 @@ async public Task FulfillAsync(object text, string to, string from = nu // ------ context section dynamic context = new JObject(); - // text - if (text == null) - context.text = ""; - else if (text is IEnumerable) - context.text = GetJson(((IEnumerable)text).Select(i => i == null ? "" : i), "text"); - else - context.text = GetJson(text.ToString(), "text") ?? ""; + int textLength = 0; + // text + if (text == null) + context.text = ""; + else if (text is IEnumerable) + { + context.text = GetJson(((IEnumerable)text).Select(i => i == null ? "" : i), "text"); + textLength = ((IEnumerable)text).Where(x => x != null).Sum(i => i.Length); + } + else + { + context.text = GetJson(text.ToString(), "text") ?? ""; + textLength = ((string)text).Length; + } - // to - context.to = to; + // determination of the possibility of using the syncwrapper + // maximum 10k characters + if (useSyncwrapper) + { + if (textLength < 10000) + async = !useSyncwrapper; + else + useSyncwrapper = false; + } + + // to + context.to = to; // from if (!string.IsNullOrWhiteSpace(from)) @@ -91,9 +109,9 @@ async public Task FulfillAsync(object text, string to, string from = nu if (!string.IsNullOrWhiteSpace(provider)) service.provider = provider; - // async parameter - if (async) - service.async = true; + // async parameter + if (async) + service.async = true; // auth parameter service.auth = GetJson(auth, "auth"); @@ -141,7 +159,7 @@ async public Task FulfillAsync(object text, string to, string from = nu // Call to Intento API and get json result using (HttpConnector conn = new HttpConnector(Intento)) { - jsonResult = await conn.PostAsync(url, json); + jsonResult = await conn.PostAsync(url, json, useSyncwrapper: useSyncwrapper); } if (async && wait_async) @@ -158,8 +176,18 @@ async public Task FulfillAsync(object text, string to, string from = nu jsonResult = await Intento.WaitAsyncJobAsync(id); } + if (useSyncwrapper) + { + JObject response = new JObject(); + response["results"] = jsonResult.results; + response["meta"] = jsonResult.meta; + response["service"] = jsonResult.service; + ((JObject)jsonResult)["response"] = new JArray() { response }; + jsonResult.meta["providers"] = new JArray() { jsonResult.service.provider }; - return jsonResult; + } + + return jsonResult; } private dynamic GetJson(object data, string name) diff --git a/IntentoSDK/IntentoSDK.csproj b/IntentoSDK/IntentoSDK.csproj index 9e81294..dcade88 100644 --- a/IntentoSDK/IntentoSDK.csproj +++ b/IntentoSDK/IntentoSDK.csproj @@ -1,94 +1,99 @@ - - - - - Debug - AnyCPU - {14D9DA22-F8D4-4F62-975A-7BD821807528} - Library - Properties - IntentoSDK - IntentoSDK - v4.5 - 512 - 0 - - - - - true - full - false - bin\ - DEBUG;TRACE - prompt - 4 - false - - - pdbonly - true - bin\ - TRACE - prompt - 4 - false - - - true - - - intento_sn.snk - - - false - - - - ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - powershell -ExecutionPolicy RemoteSigned $(ProjectDir)postbuild.ps1 '$(TargetDir)$(TargetName).dll' - - - $(ProjectDir)githashForm.bat >$(ProjectDir)GitHash.cs - + + + + + Debug + AnyCPU + {14D9DA22-F8D4-4F62-975A-7BD821807528} + Library + Properties + IntentoSDK + IntentoSDK + v4.5 + 512 + 0 + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\ + TRACE + prompt + 4 + false + + + true + + + intento_sn.snk + + + false + + + + ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + powershell -ExecutionPolicy RemoteSigned $(ProjectDir)postbuild.ps1 '$(TargetDir)$(TargetName).dll' + + + $(ProjectDir)githashForm.bat >$(ProjectDir)GitHash.cs + + --> \ No newline at end of file diff --git a/IntentoSDK/Properties/AssemblyInfo.cs b/IntentoSDK/Properties/AssemblyInfo.cs index 55cda31..2e7096d 100644 --- a/IntentoSDK/Properties/AssemblyInfo.cs +++ b/IntentoSDK/Properties/AssemblyInfo.cs @@ -33,7 +33,7 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.3.0")] -[assembly: AssemblyFileVersion("1.5.3.0")] +[assembly: AssemblyVersion("1.5.4.0")] +[assembly: AssemblyFileVersion("1.5.4.0")] [assembly: AssemblyInformationalVersion(GitHash.hash)] [assembly: AssemblyGitHash(GitHash.hash)] diff --git a/IntentoSDK/Properties/IntentoSDK.nuspec b/IntentoSDK/Properties/IntentoSDK.nuspec index 9e548fe..52b9501 100644 --- a/IntentoSDK/Properties/IntentoSDK.nuspec +++ b/IntentoSDK/Properties/IntentoSDK.nuspec @@ -2,7 +2,7 @@ IntentoSDK - 1.5.3.0 + 1.5.4.0 Intento Intento false