2017-08-25 63 views
0

我想为我的字符串输入创建一个文本框,然后在输入具有“。”的情况下将它分隔成一个数组。例如:输入文本框到数组中

答案在于机器翻译。最好的机器翻译技术并不总是能够为网站或像人类这样的用户提供量身定制的翻译。只需在任意位置复制并粘贴代码段。

在这种情况下,该输入将由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; 
      } 
     } 
    } 
} 
+0

如果在你的一个句子中间有一个'.'会怎么样?这将导致增加一句话。 – Sach

+0

我对“C#”一无所知,但看着你的问题,如果我必须在VB.Net上执行这个操作,我会找到**“。”**的位置。在句子中,将该字符串分解到该点并将其放入数组中。继续这样做直到**“。”之后没有任何文本。**我不知道你是否在上面的代码中做了同样的事情,只是想分享我的想法。现在可以用 – Subaz

+0

,那没关系。如果有'.',那就让它成为另一个句子 – Response

回答

0

这是一些示例代码。将字符串s替换为文本框中的.Text。

string s = @"Changing your development practice to introduce an automated testing strategy can revolutionise your deployments. If you approach the software release date with a sense of dread, this technique is for you. 

Implementing a test-driven development strategy using tSQLt leads to robust, modular code that becomes a pleasure to work with. As more tests are created, trust builds that releases will provide beneficial new functionality with no negative side-effects."; 

var translateArraySourceTexts = s.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToArray(); 
+0

我用空行分割,我想你想按“”分割。 – Derek

1

使用(StringObject).Split("<separator>") 示例代码:

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 array = string.Join(",",translateArraySourceTexts).Split('.'); 
0

你需要String.Split(charArray,stringSplitoptions)得到的只有三根弦的结果数组英寸

在你的榜样

string translatableString = "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."; 

    string[] arr = translatableString.Split(new char[] { '.' }, 
    StringSplitOptions.RemoveEmptyEntries); 

你会得到的4串的阵列 translatableString.Split( ''),因为一个是空的。这就是我提供重载方法的原因。

相关问题