我想爲我的字符串輸入創建一個文本框,然後在輸入具有「。」的情況下將它分隔成一個數組。例如:輸入文本框到數組中
答案在於機器翻譯。最好的機器翻譯技術並不總是能夠爲網站或像人類這樣的用戶提供量身定製的翻譯。只需在任意位置複製並粘貼代碼段。
在這種情況下,該輸入將由3個數組組成。
請看看下面的Microsoft代碼。我想使用文本框從輸入中更改硬代碼。然後傳遞每個數組以進行翻譯。
class TranslateArraySample
{
public static async Task Run(string authToken)
{
var from = "en";
var to = "es";
** var translateArraySourceTexts = new []
{
"The answer lies in machine translation.",
"the best machine translation technology cannot always provide translations tailored to a site or users like a human ",
"Simply copy and paste a code snippet anywhere "
};
var uri = "https://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
var body = "<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</From>" +
"<Options>" +
" <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{1}</ContentType>" +
"<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
"</Options>" +
"<Texts>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{2}</string>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{3}</string>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{4}</string>" +
"</Texts>" +
"<To>{5}</To>" +
"</TranslateArrayRequest>";
string requestBody = string.Format(body, from, "text/plain", translateArraySourceTexts[0], translateArraySourceTexts[1], translateArraySourceTexts[2], to);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(uri);
request.Content = new StringContent(requestBody, Encoding.UTF8, "text/xml");
request.Headers.Add("Authorization", authToken);
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
switch (response.StatusCode)
{
case HttpStatusCode.OK:
Console.WriteLine("Request status is OK. Result of translate array method is:");
var doc = XDocument.Parse(responseBody);
var ns = XNamespace.Get("http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2");
var sourceTextCounter = 0;
foreach (XElement xe in doc.Descendants(ns + "TranslateArrayResponse"))
{
foreach (var node in xe.Elements(ns + "TranslatedText"))
{
** Console.WriteLine("\n\nSource text: {0}\nTranslated Text: {1}", translateArraySourceTexts[sourceTextCounter], node.Value);
}
sourceTextCounter++;
}
break;
default:
Console.WriteLine("Request status code is: {0}.", response.StatusCode);
Console.WriteLine("Request error message: {0}.", responseBody);
break;
}
}
}
}
如果在你的一個句子中間有一個'.'會怎麼樣?這將導致增加一句話。 – Sach
我對「C#」一無所知,但看着你的問題,如果我必須在VB.Net上執行這個操作,我會找到**「。」**的位置。在句子中,將該字符串分解到該點並將其放入數組中。繼續這樣做直到**「。」之後沒有任何文本。**我不知道你是否在上面的代碼中做了同樣的事情,只是想分享我的想法。現在可以用 – Subaz
,那沒關係。如果有'.',那就讓它成爲另一個句子 – Response