gpt4 book ai didi

c# - Microsoft 翻译 API 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 15:58:44 24 4
gpt4 key购买 nike

我正在尝试将英文文本转换为印地文文本。为此,我正在使用 Microsoft Text translator 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

大部分文字未翻译。我需要你的建议才能继续前进。我做的是正确的方式还是我需要改变的任何事情。任何帮助对我都有很大帮助。

提前致谢。

最佳答案

引用NuGet包NequeoNetTranslator ,这包含文本和语音翻译 API。

文本翻译示例使用新的 Cognitive 版本,首先获取您的访问 token ,通过您的翻译 [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();

现在调用翻译方法,这会将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();
}
}

关于c# - Microsoft 翻译 API 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com