Fixed translate

This commit is contained in:
Master Kwoth 2016-07-03 22:30:35 +02:00
parent 29daf48e4f
commit b30a804123
2 changed files with 18 additions and 94 deletions

View File

@ -2,6 +2,7 @@
// License: Code Project Open License
// http://www.codeproject.com/info/cpol10.aspx
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@ -27,23 +28,6 @@ namespace NadekoBot.Modules.Translator.Helpers
}
}
/// <summary>
/// Gets the time taken to perform the translation.
/// </summary>
public TimeSpan TranslationTime {
get;
private set;
}
/// <summary>
/// Gets the url used to speak the translation.
/// </summary>
/// <value>The url used to speak the translation.</value>
public string TranslationSpeechUrl {
get;
private set;
}
/// <summary>
/// Gets the error.
/// </summary>
@ -69,86 +53,22 @@ namespace NadekoBot.Modules.Translator.Helpers
string targetLanguage)
{
// Initialize
this.Error = null;
this.TranslationSpeechUrl = null;
this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now;
string translation = string.Empty;
string text = string.Empty;
try
// Download translation
string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText));
using (WebClient wc = new WebClient())
{
// Download translation
string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText));
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
text = wc.DownloadString(url);
}
// Get translated text
// Get phrase collection
// string text = File.ReadAllText(outputFile);
int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage)));
if (index == -1)
{
// Translation of single word
int startQuote = text.IndexOf('\"');
if (startQuote != -1)
{
int endQuote = text.IndexOf('\"', startQuote + 1);
if (endQuote != -1)
{
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
}
}
else
{
// Translation of phrase
text = text.Substring(0, index);
text = text.Replace("],[", ",");
text = text.Replace("]", string.Empty);
text = text.Replace("[", string.Empty);
text = text.Replace("\",\"", "\"");
// Get translated phrases
string[] phrases = text.Split(new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; (i < phrases.Count()); i += 2)
{
string translatedPhrase = phrases[i];
if (translatedPhrase.StartsWith(",,"))
{
i--;
continue;
}
translation += translatedPhrase + " ";
}
}
// Fix up translation
translation = translation.Trim();
translation = translation.Replace(" ?", "?");
translation = translation.Replace(" !", "!");
translation = translation.Replace(" ,", ",");
translation = translation.Replace(" .", ".");
translation = translation.Replace(" ;", ";");
// And translation speech URL
this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
HttpUtility.UrlEncode(translation), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), translation.Length);
}
}
catch (Exception ex)
{
this.Error = ex;
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
text = wc.DownloadString(url);
}
// Return result
this.TranslationTime = DateTime.Now - tmStart;
return translation;
return JArray.Parse(text)[0][0][0].ToString();
}
#endregion

View File

@ -27,13 +27,17 @@ namespace NadekoBot.Modules.Translator
await e.Channel.SendIsTyping().ConfigureAwait(false);
string from = e.GetArg("langs").ToLowerInvariant().Split('>')[0];
string to = e.GetArg("langs").ToLowerInvariant().Split('>')[1];
var text = e.GetArg("text")?.Trim();
if (string.IsNullOrWhiteSpace(text))
return;
string translation = t.Translate(e.GetArg("text"), from, to);
string translation = t.Translate(text, from, to);
await e.Channel.SendMessage(translation).ConfigureAwait(false);
}
catch
catch (Exception ex)
{
await e.Channel.SendMessage("Bad input format, or sth went wrong...").ConfigureAwait(false);
Console.WriteLine(ex);
await e.Channel.SendMessage("Bad input format, or something went wrong...").ConfigureAwait(false);
}
};