예제 #1
0
 public RESTClientDataC TranslateRESTClientData1by1(RESTClientDataC RESTClientDATA)
 {
     // Instead of run all sentences at one, we call TranslateRESTClientData
     // with just one sentence, and the response is add manually to the
     // RESTClientDATA.
     string[] sentence = { "" };  // I will pass just one sentence
     Console.WriteLine(string.Format("Connecting {0}:{1}. Warning, first call takes some time. One dot per API REST sentence call. ", host, port));
     foreach (string s in RESTClientDATA.rawsentences)
     {
         sentence[0] = s;
         RESTClientDataC auxRESTClientData = new RESTClientDataC(sentence);
         auxRESTClientData = TranslateRESTClientData(auxRESTClientData);
         Console.Write(".");
         if (auxRESTClientData.todoOKREST)
         {
             RESTClientDATA.todoOKREST = auxRESTClientData.todoOKREST;
             RESTClientDATA.infoREST  += auxRESTClientData.infoREST;
             // we will add just the first and only item of the list to
             // the source and target lists
             RESTClientDATA.ListSourceONMT.Add(auxRESTClientData.ListSourceONMT[0]);
             RESTClientDATA.ListTargetONMT.Add(auxRESTClientData.ListTargetONMT[0]);
         }
         else         // abort
         {
             RESTClientDATA.todoOKREST = false;
             RESTClientDATA.infoREST  += auxRESTClientData.infoREST;
             break;
         }
     }
     Console.WriteLine();
     return(RESTClientDATA);
 }
예제 #2
0
 public RESTClientDataC TranslateRESTClientData(RESTClientDataC RESTClientDATA)
 {
     // we need to iniatalize the RESTClientData classs
     foreach (string s in RESTClientDATA.rawsentences)
     {
         var tgt = new List <TargetONMT>();
         var src = new SourceONMT();
         src.src = s;                            // to be translated
         RESTClientDATA.ListSourceONMT.Add(src); // now with the sentence
         RESTClientDATA.ListTargetONMT.Add(tgt); // empty
     }
     // lets translate we will create the rest call
     try
     {
         var    serializer = new JavaScriptSerializer();
         string json       = serializer.Serialize(RESTClientDATA.ListSourceONMT);
         RESTClientDATA.infoREST = string.Format("Json2Rest -> {0}" + "<br>", json);
         var SC = new StringContent(json, Encoding.UTF8, "application/json");
         //Console.Write("P");
         HttpResponseMessage response = client.PostAsync(
             "translator/translate", SC).GetAwaiter().GetResult();
         if (response.IsSuccessStatusCode)
         {
             // Console.Write("R");
             RESTClientDATA.infoREST += "Response OK" + "<br>";
             string data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
             RESTClientDATA.infoREST += string.Format("Json2Rest -> {0}" + "<br>", data);
             JavaScriptSerializer JSserializer = new JavaScriptSerializer();
             // var nose = new List<List<TargetONMT>>(); //
             // nose =  JSserializer.Deserialize<List<List<TargetONMT>>>(data);
             RESTClientDATA.ListTargetONMT = JSserializer.Deserialize <List <List <TargetONMT> > >(data);
             RESTClientDATA.infoREST      += string.Format("Target serialized OK!" + "<br>", data);
             RESTClientDATA.todoOKREST     = true;
         }
         else
         {
             RESTClientDATA.infoREST  += "ERROR in response. " + "<br>";
             RESTClientDATA.todoOKREST = false;
         }
     }
     catch (Exception e)
     {
         RESTClientDATA.infoREST  += "<b>FATAL ERROR in response. Check server settings. " + e.Message + "<\b>" + "<br>";
         RESTClientDATA.todoOKREST = false;
     }
     return(RESTClientDATA);
 }
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("OpnNMTConsoleClient (c) 2018 miguel canals -  www.mknals.com - MIT License");
            // string CommandArguments = "-h www.mkanls.com -p 4301 -f c:\tmp\tmp.txt [-EOFT] ";
            classCommandArguments argumentos = new classCommandArguments(args);

            if (argumentos.todoOK == false)
            {
                System.Console.WriteLine("Error parsing arguments");
                System.Console.WriteLine(argumentos.info);
                return;
            }


            string INPfile     = argumentos.inputFile;
            string INPfilePath = Path.GetDirectoryName(INPfile);
            string INPfileFilenameWithoutExtension = Path.GetFileNameWithoutExtension(INPfile);
            // string[] idioma = new string[10];
            string RESThost = argumentos.Host;
            int    RESTport = argumentos.Port;

            StreamReader sr = new StreamReader(INPfile, Encoding.UTF8, true); // input file
            string       OUTfilesrc;

            OUTfilesrc = Path.GetFileNameWithoutExtension(INPfile) + ".src" + Path.GetExtension(INPfile);
            if (Path.GetDirectoryName(INPfile) != "")
            {
                OUTfilesrc = Path.GetDirectoryName(INPfile) + "\\" + OUTfilesrc;
            }

            string OUTfiletgt;

            OUTfiletgt = Path.GetFileNameWithoutExtension(INPfile) + ".tgt" + Path.GetExtension(INPfile);
            if (Path.GetDirectoryName(INPfile) != "")
            {
                OUTfiletgt = Path.GetDirectoryName(INPfile) + "\\" + OUTfiletgt;
            }

            string OUTfilejson;

            OUTfilejson = Path.GetFileNameWithoutExtension(INPfile) + ".json";
            if (Path.GetDirectoryName(INPfile) != "")
            {
                OUTfilejson = Path.GetDirectoryName(INPfile) + "\\" + OUTfilejson;
            }

            List <String> listsentences = new List <String>();

            // apostrophe quotes
            //
            // \u2018  LEFT SINGLE QUOTATION MARK	‘
            // \u2019 ’
            // \u201C LEFT DOUBLE QUOTATION MARK	“
            // \u201D RIGHT DOUBLE QUOTATION MARK	”
            char[] tipochar = new char[] { '\u2018',
                                           '\u2019', '\u201C', '\u201D' };

            try
            {
                int nlines = 0;
                using (sr)
                {
                    while (sr.Peek() != -1)
                    {
                        // Read the stream to a string, and write the string to the console.
                        String line = sr.ReadLine();
                        //Console.WriteLine(line);
                        line = line.Trim();
                        if (line.Length > 0)
                        {                                          // we need a line
                            int index = line.IndexOfAny(tipochar); // quote replacing
                            if (index != -1)                       // cleanup
                            {
                                line = line.Replace("\u2018", "'");
                                line = line.Replace("\u2019", "'");
                                line = line.Replace("\u201C", "\"");
                                line = line.Replace("\u201D", "\"");
                            }
                            listsentences.Add(line); // add the sentence to list of senteces
                            nlines += 1;
                            // Console.WriteLine(line);
                        }
                    }
                    Console.WriteLine(string.Format("File {0} -> {1} lines", INPfile, nlines));

                    sr.Close();
                }
                // Just for info
                Console.WriteLine("Detected encoding ->  {0}.", GetFileEncoding(INPfile));
            }
            catch (Exception e)
            {
                Console.WriteLine("Fatal error.");
                Console.WriteLine(String.Format("The file {0} could not be read:", INPfile));
                Console.WriteLine(e.Message);
                return;
            }
            // I have my list of sentences
            String[] sentences = listsentences.ToArray();            // List of strings to string array

            var RESTClientData = new RESTClientDataC(sentences);     // only parameter senteces
            var rClient        = new RESTClient(RESThost, RESTport); // Rest client

            rClient.TranslateRESTClientData1by1(RESTClientData);

            // Use rClient.TranslateRESTClientData(RESTClientData); not good idea for a text file

            if (RESTClientData.todoOKREST) // translation was fine
            {
                // create a class with all the data I need
                JsonFull outputJson = new JsonFull();

                for (int i = 0; i < RESTClientData.ListSourceONMT.Count; i++)
                {
                    JsonItem ele = new JsonItem();
                    ele.n_best     = RESTClientData.ListTargetONMT[i][0].n_best;
                    ele.pred_score = RESTClientData.ListTargetONMT[i][0].pred_score;
                    ele.src_untok  = RESTClientData.ListSourceONMT[i].src;
                    ele.src        = RESTClientData.ListTargetONMT[i][0].src;
                    ele.tgt        = RESTClientData.ListTargetONMT[i][0].tgt;
                    outputJson.ListJsonItems.Add(ele);
                }

                StreamWriter swrOutjson = new StreamWriter(OUTfilejson);
                try
                {
                    using (swrOutjson)
                    {
                        var json = new JavaScriptSerializer().Serialize(outputJson);
                        swrOutjson.WriteLine(json);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Fatal error with output files.");
                    Console.WriteLine(String.Format("Files {0}", OUTfilejson));
                    Console.WriteLine(e.Message);
                    return;
                }
            }



            if (RESTClientData.todoOKREST) // translation was fine
            {
                StreamWriter swrOutsrc = new StreamWriter(OUTfilesrc);
                StreamWriter swrOuttgt = new StreamWriter(OUTfiletgt);

                try
                {
                    using (swrOutsrc)
                    {
                        using (swrOuttgt)
                        {
                            for (int i = 0; i < RESTClientData.ListSourceONMT.Count; i++)
                            {
                                swrOutsrc.WriteLine(RESTClientData.ListSourceONMT[i].src);
                                swrOuttgt.WriteLine(RESTClientData.ListTargetONMT[i][0].tgt);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Fatal error with output files.");
                    Console.WriteLine(String.Format("Files {0}/{1}", OUTfilesrc, swrOuttgt));
                    Console.WriteLine(e.Message);
                    return;
                }
            }
            else
            {
                Console.WriteLine("Translation ERROR");
                Console.WriteLine("Translation ERROR in RESTClientData.todoOKREST" + "<br>");
                Console.WriteLine(RESTClientData.infoREST + "<br>");
            }


            // Only for debug

            //if (RESTClientData.todoOKREST) // translation was fine
            //{
            ////   Console.WriteLine("OK");
            ////    // Translation inside RESTClientDATA
            //     // we need to search in each RESTSEentence
            //     bool verbose = true;
            //     verbose = false;
            //     for (int i = 0; i < RESTClientData.ListSourceONMT.Count; i++)
            //     {
            //         if (verbose)
            //         {
            //             string auxS = "<br>{1}<br>{0}<br><b>{2}</b> - ({3})<br>";
            //             auxS = string.Format(auxS,
            //                 RESTClientData.ListSourceONMT[i].src,
            //                 RESTClientData.ListTargetONMT[i][0].src,
            //                 RESTClientData.ListTargetONMT[i][0].tgt,
            //                 RESTClientData.ListTargetONMT[i][0].pred_score);
            //             auxS = auxS.Replace("<br>", Environment.NewLine);
            //             auxS = auxS.Replace("<b>", "");
            //             auxS = auxS.Replace("</b>", "");
            //             Console.WriteLine(auxS);
            //
            //         }
            //         else
            //         {
            //             string auxS = "<br>{0}<br><b>{1}</b><br>";
            //             auxS= string.Format(auxS,
            //                 RESTClientData.ListSourceONMT[i].src,
            //                 RESTClientData.ListTargetONMT[i][0].tgt);
            //             auxS = auxS.Replace("<br>", Environment.NewLine);
            //             auxS = auxS.Replace("<b>", "");
            //             auxS = auxS.Replace("</b>", "");
            //             Console.WriteLine(auxS);
            //         }
            //
            //
            //     }
            //
            // }
            // else
            // {
            //     Console.WriteLine("Translation ERROR");
            //     Console.WriteLine("Translation ERROR in RESTClientData.todoOKREST" + "<br>");
            //     Console.WriteLine(RESTClientData.infoREST + "<br>");
            // }
            // Console.ReadLine();
            Console.WriteLine("Done!");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            // before we do antying we will verifiy if we have a valid URL

            try {
                var kk = new Uri(string.Format("http://{0}:{1}/", txtHost.Text, txtPort.Text));
            }
            catch {
                pnlSettings.Visible   = true;
                lnkShowHidePanel.Text = "Click here to hide REST settings";
                lblCookie.Text        = "<font color='red'>Change (and save) connections setings as I cannot create a valid URL</font> ";
                return;
            }



            StringBuilder sbSalida = new StringBuilder("");

            lblOut.Text = "";
            string auxS      = "";
            string inputText = "";

            string[] sentences;
            sbSalida.Append("Input text (uncoded):<br />");
            inputText = txtToT.Text;
            // ...

            char[] delim = ".?!;".ToCharArray(); // delimiters end of sentence
            if (chkSegmentBasedOnNewline.Checked == false)
            {
                foreach (char c in delim)
                {
                    inputText = inputText.Replace(c.ToString(), c.ToString() + Environment.NewLine);
                }
                // remove New line from \d\.Enviroment.Newline\d p.e. 334.\r\n\344

                string pattern = @"\d\.(\n|\r|\r\n)\d";
                var    maches  = Regex.Matches(inputText, pattern);
                foreach (Match mach in maches)
                {
                    auxS = mach.Groups[0].Value;
                    string auxS2 = auxS.Replace("\r\n", "");
                    auxS2     = auxS2.Replace("\n", "");
                    auxS2     = auxS2.Replace("\r", "");
                    inputText = inputText.Replace(auxS, auxS2);
                }
                // elipses
                var   regex = new Regex(@"\.(\n|\r|\r\n)\.");
                Match match = regex.Match(inputText);
                if (match.Success)
                {
                    string strControl;
                    do
                    {
                        strControl = inputText;
                        inputText  = inputText.Replace(".\r\n.", "..");
                    }while (strControl.CompareTo(inputText) != 0);
                }
            }

            // apostrophe quotes
            // \u2018  LEFT SINGLE QUOTATION MARK	‘
            // \u2019 ’
            // \u201C LEFT DOUBLE QUOTATION MARK	“
            // \u201D RIGHT DOUBLE QUOTATION MARK	”
            char[] tipochar = new char[] { '\u2018',
                                           '\u2019', '\u201C', '\u201D' };
            int index = inputText.IndexOfAny(tipochar);

            if (index != -1)
            {
                inputText = inputText.Replace("\u2018", "'");
                inputText = inputText.Replace("\u2019", "'");
                inputText = inputText.Replace("\u201C", "\"");
                inputText = inputText.Replace("\u201D", "\"");
            }

            string[] separators = { Environment.NewLine };                                  // only one
            sentences = inputText.Split(separators, StringSplitOptions.RemoveEmptyEntries); // splitting
            sentences = sentences.Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();    // removing blanks

            for (int i = 0; i < sentences.Length; i += 1)
            {
                sentences[i] = sentences[i].Trim();
            }

            var RESTClientData = new RESTClientDataC(sentences);                          // only parameter senteces
            var rClient        = new RESTClient(txtHost.Text, Int32.Parse(txtPort.Text)); // Rest client

            if (chkSendAll.Checked)                                                       // sen all as 1 REST request
            {
                rClient.TranslateRESTClientData(RESTClientData);
            }
            else     // Send senteces 1 by 1
            {
                rClient.TranslateRESTClientData1by1(RESTClientData);
            }


            if (RESTClientData.todoOKREST) // translation was fine
            {
                Console.WriteLine("OK");
                // Translation inside RESTClientDATA
                // we need to search in each RESTSEentence

                for (int i = 0; i < RESTClientData.ListSourceONMT.Count; i++)
                {
                    if (chkAddInfo.Checked)
                    {
                        auxS = "<br>{1}<br>{0}<br><b>{2}</b> - ({3})<br />";
                        sbSalida.Append(string.Format(auxS,
                                                      RESTClientData.ListSourceONMT[i].src,
                                                      RESTClientData.ListTargetONMT[i][0].src,
                                                      RESTClientData.ListTargetONMT[i][0].tgt,
                                                      RESTClientData.ListTargetONMT[i][0].pred_score));
                    }
                    else
                    {
                        auxS = "<br>{0}<br><b>{1}</b><br>";
                        sbSalida.Append(string.Format(auxS,
                                                      RESTClientData.ListSourceONMT[i].src,
                                                      RESTClientData.ListTargetONMT[i][0].tgt));
                    }
                }
            }
            else
            {
                sbSalida.Append("Translation ERROR");
                sbSalida.Append("Translation ERROR in RESTClientData.todoOKREST" + "<br>");
                sbSalida.Append(RESTClientData.infoREST + "<br>");
            }


            lblOut.Text = sbSalida.ToString() + "<br>";
            // Aquí el código para llamar
        }