public IEnumerator Translate(ITranslationContext context) { var reversedText = new string( context.UntranslatedText.Reverse().ToArray()); context.Complete(reversedText); return(null); }
/// <summary> /// Attempt to translated the provided untranslated text. Will be used in a "coroutine", /// so it can be implemented in an asynchronous fashion. /// </summary> public IEnumerator Translate(ITranslationContext context) { EnsureInitialized(); while (_initializing && !_failed) { yield return(new WaitForSeconds(0.2f)); } if (_failed) { context.Fail("External process failed."); } var result = new ProtocolTransactionHandle(); var id = Guid.NewGuid(); lock ( _sync ) { _transactionHandles[id] = result; } try { var request = new TranslationRequest { Id = id, SourceLanguage = context.SourceLanguage, DestinationLanguage = context.DestinationLanguage, UntranslatedTexts = context.UntranslatedTexts }; var payload = ExtProtocolConvert.Encode(request); _process.StandardInput.WriteLine(payload); } catch (Exception e) { result.SetCompleted(null, e.Message); } // yield-wait for completion var iterator = result.GetSupportedEnumerator(); while (iterator.MoveNext()) { yield return(iterator.Current); } if (!result.Succeeded) { context.Fail("Error occurred while retrieving translation. " + result.Error); } context.Complete(result.Results); }
private void ExtractTranslation(string data, List <UntranslatedTextInfo> untranslatedTextInfos, ITranslationContext context) { var obj = JSON.Parse(data); var translations = obj["result"]["translations"].AsArray; var translatedTexts = new List <string>(); int transIdx = 0; for (int i = 0; i < untranslatedTextInfos.Count; i++) { var parts = untranslatedTextInfos[i].TranslationParts; var fullTranslatedText = new StringBuilder(); foreach (var part in parts) { if (part.IsTranslatable) { var translation = translations[transIdx++]; var beams = translation["beams"].AsArray; if (beams.Count > 0) { var beam = beams[0]; var sentence = beam["postprocessed_sentence"].ToString(); var translatedText = JsonHelper.Unescape(sentence.Substring(1, sentence.Length - 2)); fullTranslatedText.Append(translatedText); } } else { fullTranslatedText.Append(part.Value); } } var t = fullTranslatedText.ToString(); if (string.IsNullOrWhiteSpace(t)) { throw new Exception("Found no valid translations in beam!"); } translatedTexts.Add(t); } context.Complete(translatedTexts.ToArray()); }
private void ExtractTranslation(string data, List <UntranslatedTextInfo> untranslatedTextInfos, ITranslationContext context) { var obj = JsonConvert.DeserializeObject <TranslationResponse>(data); var translatedTexts = new List <string>(); int transIdx = 0; for (int i = 0; i < untranslatedTextInfos.Count; i++) { var parts = untranslatedTextInfos[i].TranslationParts; var fullTranslatedText = new StringBuilder(); foreach (var part in parts) { if (part.IsTranslatable) { var translation = obj.translations[transIdx++].text; fullTranslatedText.Append(translation); } else { fullTranslatedText.Append(part.Value); } } var t = fullTranslatedText.ToString(); if (string.IsNullOrWhiteSpace(t)) { throw new Exception("Found no valid translations in beam!"); } translatedTexts.Add(t); } context.Complete(translatedTexts.ToArray()); }
/// <summary> /// Attempt to translated the provided untranslated text. Will be used in a "coroutine", /// so it can be implemented in an asynchronous fashion. /// </summary> public IEnumerator Translate(ITranslationContext context) { EnsureInitialized(); while (_initializing && !_failed) { var instruction = Features.GetWaitForSecondsRealtime(0.2f); if (instruction != null) { yield return(instruction); } else { yield return(null); } } if (_failed) { context.Fail("External process failed."); } var totalDelay = (float)(Rng.Next((int)((MaxDelay - MinDelay) * 1000)) + (MinDelay * 1000)) / 1000; var timeSinceLast = TimeSupport.Time.realtimeSinceStartup - _lastRequestTimestamp; if (timeSinceLast < totalDelay) { var remainingDelay = totalDelay - timeSinceLast; var instruction = Features.GetWaitForSecondsRealtime(remainingDelay); if (instruction != null) { yield return(instruction); } else { float start = TimeSupport.Time.realtimeSinceStartup; var end = start + remainingDelay; while (TimeSupport.Time.realtimeSinceStartup < end) { yield return(null); } } } _lastRequestTimestamp = TimeSupport.Time.realtimeSinceStartup; var result = new ProtocolTransactionHandle(); var id = Guid.NewGuid(); lock ( _sync ) { _transactionHandles[id] = result; } try { var request = new TranslationRequest { Id = id, SourceLanguage = context.SourceLanguage, DestinationLanguage = context.DestinationLanguage, UntranslatedTextInfos = context.UntranslatedTextInfos.Select(x => x.ToTransmittable()).ToArray() }; var payload = ExtProtocolConvert.Encode(request); _process.StandardInput.WriteLine(payload); } catch (Exception e) { result.SetCompleted(null, e.Message, StatusCode.Unknown); } // yield-wait for completion var iterator = result.GetSupportedEnumerator(); while (iterator.MoveNext()) { yield return(iterator.Current); } if (!result.Succeeded) { context.Fail("Error occurred while retrieving translation. " + result.Error); } context.Complete(result.Results); }
void IWwwTranslationExtractionContext.Complete(string translatedText) { _context.Complete(translatedText); }
public IEnumerator Translate(ITranslationContext context) { context.Complete(context.UntranslatedTexts); return(null); }