private Dictionary <LineInfo, LineInfo> FindMatch(SubtitleInfo langSub, SubtitleInfo timingSub, int matchLinesToSearchForward, int matchMinimumLettersForMatch, double matchSimilarityThreshold) { Dictionary <LineInfo, LineInfo> matchedLangLines2timingLines = new Dictionary <LineInfo, LineInfo>(); int timingSrtPos = 0; //start a bit after the beginning to skip unwanted chatter lines for (int i = 0; i < langSub.Lines.Count(); ++i) { var sline = langSub.Lines[i]; int endSearch = Math.Min(timingSub.Lines.Count(), timingSrtPos + matchLinesToSearchForward); for (int j = timingSrtPos; j < endSearch; ++j) { var tline = timingSub.Lines[j]; //if we have a match if (tline.CalcIsWordMatch(sline, matchMinimumLettersForMatch, matchSimilarityThreshold)) { if (!matchedLangLines2timingLines.ContainsKey(sline)) { matchedLangLines2timingLines.Add(sline, tline); } timingSrtPos = j + 1; } } } return(matchedLangLines2timingLines); }
public SrtParserTranslator(string translation) { SubtitleInfo inf = new SubtitleInfo(null); inf.LoadSrtFileContent(translation); _translation = inf.Lines.Select(l => l.Line).ToList(); }
public SubtitleInfo CloneSub() { SubtitleInfo clone = new SubtitleInfo(this._translator); clone._language = this._language; clone._listOfLines = new List <LineInfo>(); clone._listOfTimeMarkers = new List <TimeStamp>(); foreach (var ts in _listOfTimeMarkers) { TimeStamp tsn = new TimeStamp() { Correction = ts.Correction, Duration = ts.Duration, FromTime = ts.FromTime }; foreach (var line in ts.Lines) { var nline = new LineInfo() { Line = line.Line, TimeStamp = tsn, TranslatedLine = line.TranslatedLine }; tsn.Lines.Add(nline); clone._listOfLines.Add(nline); } clone._listOfTimeMarkers.Add(tsn); } return(clone); }
private SubtitleInfo xCreateFixedSubtitle(SubtitleInfo subtitle) { var resultSub = subtitle.CloneSub(); var correctionsByTimePos = new Dictionary <double, double>(); _points.ForEach(p => correctionsByTimePos.Add(p.X, p.Y)); //place all known corrections on the subtitle resultSub.TimeMarkers.ForEach(t => { if (correctionsByTimePos.ContainsKey((double)t.FromTime)) { var correction = correctionsByTimePos[(double)t.FromTime]; t.Correction = (long)correction; t.IsOffsetCorrected = true; } }); //spread correction to all timestamps (including the ones not attached) long prevOffset = (long)_points[0].Y; TimeStamp prev = null; foreach (var time in resultSub.TimeMarkers) { if (!time.IsOffsetCorrected) { var next = subtitle.Lines.Where(p => p.TimeStamp.FromTime > time.FromTime).FirstOrDefault(x => x.TimeStamp.IsOffsetCorrected); var currTime = time; var prevTime = prev; double newOffset = prevOffset; if (prevTime != null && next != null) { var nextTime = next.TimeStamp; //timeAfterPrev / timeInterval (=next-prev) = the precentage of movement in the X axis between the two points double part = ((double)currTime.FromTime - (double)prevTime.FromTime) / ((double)nextTime.FromTime - (double)prevTime.FromTime); //(change in corrections between prev -> next) * (calculated place between them =part) + (the base correction of prev =the stating point of this correction) newOffset = (((double)nextTime.Correction - (double)prev.Correction) * part) + (double)prevTime.Correction; } time.IsOffsetCorrected = true; time.Correction = (long)newOffset; } else { prevOffset = time.Correction; prev = time; } //extend duration for all subs time.Duration = (long)((double)time.Duration); } return(resultSub); }
public SubtitleInfo CreateFixedSubtitle(SubtitleInfo subtitle) { var resultSub = subtitle.CloneSub(); foreach (var time in resultSub.TimeMarkers) { time.IsOffsetCorrected = true; time.Correction = (long)ComputeYforXbyInterpolation(time.FromTime); } return(resultSub); }
private void PrepareInputSubs(out SubtitleInfo langSub, out SubtitleInfo timingSub) { ITranslator translator; string translationFileName = LanguageSrtFile + ".trans"; bool transFileExists = (File.Exists(translationFileName)); if (!string.IsNullOrWhiteSpace(_translationText) || transFileExists) { if (transFileExists) { _translationText = File.ReadAllText(translationFileName); } translator = new SrtParserTranslator(_translationText); } else { if (_bingTranslator == null && !string.IsNullOrWhiteSpace(MSSearchSecret)) { _bingTranslator = new BingTranslator(MSSearchClientID, MSSearchSecret); } _bingTranslator.ClientSecret = MSSearchSecret; _bingTranslator.ClientId = MSSearchClientID; translator = _bingTranslator; } langSub = new SubtitleInfo(translator); timingSub = new SubtitleInfo(translator); langSub.LoadSrtFile(LanguageSrtFile, SelectedEncoding); timingSub.LoadSrtFile(TimingSrtFile, Encoding.ASCII); try { langSub.Translate(); TranslationText = langSub.GetTranslatedSrtString(); if (!transFileExists) { File.WriteAllText(LanguageSrtFile + ".trans", TranslationText); } } catch (Exception ex) { MessageBox.Show("Translation error: " + ex.Message); } }
private Dictionary <LineInfo, LineInfo> FindBestMatch(SubtitleInfo langSub, SubtitleInfo timingSub, int matchLinesToSearchForward, int matchMinimumLettersForMatch, double matchSimilarityThreshold) { Dictionary <LineInfo, LineInfo> matchedLangLines2timingLines1 = FindMatch(langSub, timingSub, matchLinesToSearchForward, matchMinimumLettersForMatch, matchSimilarityThreshold); Dictionary <LineInfo, LineInfo> matchedLangLines2timingLines2 = ReverseKeyVal(FindMatch(timingSub, langSub, matchLinesToSearchForward, matchMinimumLettersForMatch, matchSimilarityThreshold)); Dictionary <LineInfo, LineInfo> matchedLangLines2timingLines; if (matchedLangLines2timingLines1.Count() > matchedLangLines2timingLines2.Count()) { matchedLangLines2timingLines = matchedLangLines2timingLines1; } else { matchedLangLines2timingLines = matchedLangLines2timingLines2; } return(matchedLangLines2timingLines); }