コード例 #1
0
        /// <summary>
        /// Distribute the New Text to the utterances that mirrors
        /// the lengths of the original utterances.
        /// </summary>
        /// <param name="newtext">The new text.</param>
        /// <returns>The list of utterances containing the new text.</returns>
        private List <Utterance> Distribute(List <string> newtext)
        {
            //Calculate the ratio of each utterance per group.
            for (int utteranceindex = 0; utteranceindex < UtteranceList.Count; utteranceindex++)
            {
                if ((UtteranceList[utteranceindex].Content.Length < 1) || (groupsumlengths[UtteranceList[utteranceindex].Group] == 0))
                {
                    UtteranceList[utteranceindex].Portion = 0;
                    UtteranceList[utteranceindex].Lines   = 0;
                }
                else
                {
                    UtteranceList[utteranceindex].Portion = (double)UtteranceList[utteranceindex].Content.Length / groupsumlengths[UtteranceList[utteranceindex].Group];
                }
            }

            string remainingstring = string.Empty;

            //split the new text per ratio
            for (int groupindex = 0; groupindex < newtext.Count; groupindex++)
            {
                //get all utterances for this group
                IEnumerable <Utterance> grouplist = UtteranceList.Where(utterance => utterance.Group == groupindex);

                List <double> portions = new List <double>();
                foreach (var item in grouplist)
                {
                    portions.Add(item.Portion);
                }
                List <string>    splitlist    = SplitString(newtext[groupindex], portions);
                List <Utterance> newgrouplist = grouplist.ToList();
                for (int utteranceindex = 0; utteranceindex < grouplist.Count(); utteranceindex++)
                {
                    Utterance utt = newgrouplist[utteranceindex];
                    utt.Content = splitlist[utteranceindex];
                    utt.Content = SplitLines(utt.Content, utt.Lines);
                    UtteranceResultList.Add(utt);
                }
            }
            return(UtteranceResultList);
        }
コード例 #2
0
        /// <summary>
        /// Translate the VTT
        /// </summary>
        /// <param name="tolangcode">Translate to language</param>
        /// <returns>List of translated VTT</returns>
        public async Task <int> Translate(string tolangcode)
        {
            //Read into the Markup and Content arrays
            bool headerended = false;

            using (StreamReader streamReader = new StreamReader(filename))
            {
                int uttindex = 0;
                while (!streamReader.EndOfStream)
                {
                    string line = streamReader.ReadLine();
                    if (filetype == Filetype.srt)
                    {
                        //read the utterance number
                        try
                        {
                            uttindex = System.Convert.ToInt16(line.Trim());
                            if (uttindex > 0)
                            {
                                continue;
                            }
                        }
                        catch (System.FormatException) { }
                    }


                    if (line.Trim().Length > 0 && char.IsDigit(line.Trim()[0]) && line.Contains("-->"))
                    {
                        //this is a time code line.
                        Utterance u = new Utterance(uttindex, string.Empty, string.Empty);
                        uttindex++;
                        u.Timecode = line;
                        utterances.Add(u);
                        headerended = true;
                    }
                    else
                    {
                        if (line.Trim().Length > 0)
                        {
                            //this is a content line
                            if (headerended)
                            {
                                utterances[utterances.Count - 1].Content += line + " ";
                                utterances[utterances.Count - 1].Lines++;
                            }
                            else
                            {
                                line = ReplaceLanguageCode.Replace(line, langcode, tolangcode);
                                Header.Add(line);
                            }
                        }
                    }
                }
                streamReader.Close();
            }

            //Translate the utterances
            Utterances       utt    = new Utterances(utterances);
            List <Utterance> newutt = await utt.Translate(tolangcode);


            //Write out the target file
            using (StreamWriter newVTT = new StreamWriter(filename))
            {
                foreach (var line in Header)
                {
                    newVTT.WriteLine(line);
                }
                newVTT.WriteLine();
                foreach (var u in newutt)
                {
                    if (filetype == Filetype.srt)
                    {
                        newVTT.WriteLine(u.Order);
                    }
                    newVTT.WriteLine(u.Timecode);
                    newVTT.WriteLine(u.Content);
                    newVTT.WriteLine();
                }
                newVTT.Close();
            }

            return(0);
        }