我正在尝试将英文文本转换为印地文文本。为此我使用Microsoft Text翻译API。但是我的一些文字没有翻译。
谷歌搜索后我发现下面的代码。
public string GetHindiText(string textToTranslate)
{
Carrier.ErrorCode = 0;
string TranslatedText = string.Empty;
try
{
Translator.LanguageServiceClient objTranslate = new Translator.LanguageServiceClient();
TranslatedText = objTranslate.Translate("***************", textToTranslate, "en", "hi");
}
catch (WebException ex)
{
Carrier.ErrorCode = 1;
return ex.Message;
}
return TranslatedText;
}
我已添加以下服务参考
http://api.microsofttranslator.com/V1/SOAP.svc
大多数文字都没有翻译。我需要你的建议才能继续前进。 我是以正确的方式做什么还是我需要改变的任何事情。任何帮助对我都很有帮助。
提前致谢。
答案 0 :(得分:3)
请参阅NuGet包NequeoNetTranslator,其中包含文本和语音翻译API。
文本翻译示例使用新的Cognitive
版本,首先通过翻译获取访问令牌[KEY]:
Nequeo.Net.Translator.Microsoft.Cognitive.Api apiat = new Nequeo.Net.Translator.Microsoft.Cognitive.Api(new Uri("https://api.cognitive.microsoft.com/sts/v1.0/"));
apiat.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
string token = apiat.GetAccessToken();
现在调用translate方法,这会将en
翻译为de
:
Nequeo.Net.Translator.Microsoft.Cognitive.Api api = new Nequeo.Net.Translator.Microsoft.Cognitive.Api(new Uri("https://api.microsofttranslator.com/v2/http.svc/"));
api.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
byte[] data = api.Translate("hello", "de", "en", null, token);
Translation[] tran = api.Translate(data);
string tranText = System.Text.Encoding.Default.GetString(data);
如果您正在使用语音文本,请提供示例代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Nequeo.Net.Translator.Microsoft.Cognitive.SpeechApi apiat = null;
private void button1_Click(object sender, EventArgs e)
{
apiat = new Nequeo.Net.Translator.Microsoft.Cognitive.SpeechApi(new Uri("wss://dev.microsofttranslator.com/speech/"));
apiat.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
string token = apiat.GetAccessToken(new Uri("https://api.cognitive.microsoft.com/sts/v1.0/"));
apiat.OnRecording += Apiat_OnRecording;
apiat.OnStopRecording += Apiat_OnStopRecording;
apiat.OnTranslationReceived += Apiat_OnTranslationReceived;
Nequeo.IO.Audio.Device device_in = Nequeo.IO.Audio.Devices.GetDeviceIn(0);
apiat.AudioDevice = device_in;
apiat.WriteStream = new System.IO.MemoryStream();
apiat.Translate("hr-HR", "en-US", token);
}
private void Apiat_OnTranslationReceived(object sender, EventArgs e)
{
System.IO.MemoryStream jj = (System.IO.MemoryStream)apiat.WriteStream;
string gg = Encoding.Default.GetString(jj.ToArray());
Nequeo.Net.Translator.Microsoft.Cognitive.SpeechTranslation dffddf = apiat.GetSpeechTranslation();
}
private void Apiat_OnStopRecording(object sender, EventArgs e)
{
bool kk = true;
}
private void Apiat_OnRecording(object sender, EventArgs e)
{
bool kk = true;
}
private void button2_Click(object sender, EventArgs e)
{
apiat.StopTranslate();
}
}