public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string s in lines) { sb.Append(s); } string allText = sb.ToString(); var indexOfMainTag = allText.IndexOf("\"SubtitleLanguages\"", StringComparison.Ordinal); if (indexOfMainTag < 0) { return; } var startIndex = allText.Remove(indexOfMainTag).LastIndexOf('{'); if (startIndex < 0) { return; } var json = allText.Substring(startIndex); var parser = new SeJsonParser(); var items = parser.GetArrayElementsByName(json, "SubtitleItems"); foreach (var item in items) { var textLines = parser.GetArrayElementsByName(item, "TextLines"); var showTime = parser.GetAllTagsByNameAsStrings(item, "ShowTime"); var hideTime = parser.GetAllTagsByNameAsStrings(item, "HideTime"); if (textLines.Count > 0 && showTime.Count == 1 && hideTime.Count == 1 && long.TryParse(showTime[0], out var startMs) && long.TryParse(hideTime[0], out var endMs)) { var text = new StringBuilder(); foreach (var line in textLines) { text.AppendLine(Json.DecodeJsonText(line)); } var p = new Paragraph(text.ToString().Trim(), startMs, endMs); var className = parser.GetAllTagsByNameAsStrings(item, "ClassName"); if (className.Count == 1) { p.Language = className[0]; } subtitle.Paragraphs.Add(p); } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
private static List <ResultText> ParseJsonToResult(string result) { var list = new List <ResultText>(); var jsonParser = new SeJsonParser(); var root = jsonParser.GetArrayElementsByName(result, "result"); foreach (var item in root) { var conf = jsonParser.GetFirstObject(item, "conf"); var start = jsonParser.GetFirstObject(item, "start"); var end = jsonParser.GetFirstObject(item, "end"); var word = jsonParser.GetFirstObject(item, "word"); if (!string.IsNullOrWhiteSpace(word) && decimal.TryParse(conf, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var confidence) && decimal.TryParse(start, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) && decimal.TryParse(end, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSeconds)) { var rt = new ResultText { Confidence = confidence, Text = word, Start = startSeconds, End = endSeconds }; list.Add(rt); } } return(list); }
public void GetArrayElementsByName_Empty_Array() { var parser = new SeJsonParser(); var result = parser.GetArrayElementsByName("{ \"start_time\": \"118.64\", \"items\": [] }", "items"); Assert.AreEqual(1, result.Count); Assert.AreEqual("", result[0].Trim()); }
public void GetArrayElementsByName_Simple() { var parser = new SeJsonParser(); var result = parser.GetArrayElementsByName("{ \"items\": [ { \"name\" : \"Joe1\" },{ \"name\" : \"Joe2\" } ] }", "items"); Assert.AreEqual(2, result.Count); Assert.AreEqual("{ \"name\" : \"Joe1\" }", result[0].Trim()); Assert.AreEqual("{ \"name\" : \"Joe2\" }", result[1].Trim()); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (var s in lines) { sb.Append(s); } var allText = sb.ToString(); if (!allText.Contains("\"lines\"")) { return; } var jp = new SeJsonParser(); var fragments = jp.GetArrayElementsByName(allText, "fragments"); for (var i = 0; i < fragments.Count; i++) { var fragment = fragments[i]; var startTime = jp.GetFirstObject(fragment, "begin"); var endTime = jp.GetFirstObject(fragment, "end"); var texts = jp.GetArrayElementsByName(fragment, "lines"); if (decimal.TryParse(startTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) && decimal.TryParse(endTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSeconds)) { var text = string.Join(Environment.NewLine, texts); var p = new Paragraph(text, (double)startSeconds * 1000.0, (double)endSeconds * 1000.0); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (var s in lines) { sb.Append(s); } var text = sb.ToString().TrimStart(); if (text.IndexOf("captiondata", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var captionDataAr = parser.GetArrayElementsByName(text, "captiondata"); foreach (var captionData in captionDataAr) { var texts = parser.GetArrayElementsByName(captionData, "data"); foreach (var textElement in texts) { var start = parser.GetFirstObject(textElement, "fromms"); var end = parser.GetFirstObject(textElement, "toms"); var caption = parser.GetFirstObject(textElement, "caption"); if (!string.IsNullOrEmpty(caption) && double.TryParse(start, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startMs) && double.TryParse(end, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var endMs)) { caption = Json.DecodeJsonText(caption); caption = caption.Replace("<br />", Environment.NewLine); var p = new Paragraph(caption, startMs, endMs); subtitle.Paragraphs.Add(p); } } } subtitle.Renumber(); }
public void GetArrayElementsByName_Get_Two_Lines() { var json = @"{ 'TextLines': [ 'Aussi dur que ce soit pour nous,', 'tout ce qui nous arrive' ], 'ClassName': 'fr', 'ShowTime': 115801, 'HideTime': 120040" .Replace('\'', '"'); var parser = new SeJsonParser(); var result = parser.GetArrayElementsByName(json, "TextLines"); Assert.AreEqual(2, result.Count); Assert.AreEqual("Aussi dur que ce soit pour nous,", result[0].Trim()); Assert.AreEqual("tout ce qui nous arrive", result[1].Trim()); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string s in lines) { sb.Append(s); } var text = sb.ToString().TrimStart(); if (!text.Contains("\"body\"")) { return; } var parser = new SeJsonParser(); var elements = parser.GetArrayElementsByName(text, "body"); foreach (var element in elements) { var startTimeObject = parser.GetFirstObject(element, "from"); var endTimeObject = parser.GetFirstObject(element, "to"); var texts = parser.GetAllTagsByNameAsStrings(element, "content"); if (texts.Count == 1 && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { var startTime = GetTimeCode(startTimeObject); var endTime = GetTimeCode(endTimeObject); if (Math.Abs(startTime - double.MinValue) < 0.01 || Math.Abs(endTime - double.MinValue) < 0.01) { _errorCount++; } else { var pText = texts[0].Replace("\\n", Environment.NewLine); var p = new Paragraph(pText, startTime, endTime); subtitle.Paragraphs.Add(p); } } } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (var s in lines) { sb.Append(s); } var text = sb.ToString().TrimStart(); if (text.IndexOf("atpjfile_info", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var elements = parser.GetArrayElementsByName(text, "BLOCKS"); foreach (var element in elements) { var startTimeObject = parser.GetFirstObject(element, "time_start"); var endTimeObject = parser.GetFirstObject(element, "time_end"); var textObject = parser.GetFirstObject(element, "text"); if (!string.IsNullOrEmpty(textObject) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { if (double.TryParse(startTimeObject, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startMs) && double.TryParse(endTimeObject, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var endMs)) { var s = Utilities.UrlDecode(Json.DecodeJsonText(textObject)); var p = new Paragraph(s, startMs, endMs); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (var s in lines) { sb.Append(s); } var text = sb.ToString().TrimStart(); if (!text.Contains("\"alternatives\"", StringComparison.Ordinal) || !text.Contains("\"words\"", StringComparison.Ordinal)) { return; } var parser = new SeJsonParser(); var words = parser.GetArrayElementsByName(text, "words"); foreach (var word in words) { var startTimeObject = parser.GetFirstObject(word, "startTime"); var endTimeObject = parser.GetFirstObject(word, "endTime"); var s = parser.GetFirstObject(word, "word"); if (!string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { if (double.TryParse(startTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSecs) && double.TryParse(endTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSecs)) { var pText = Json.DecodeJsonText(s); var p = new Paragraph(pText, startSecs * 1000.0, endSecs * 1000.0); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string s in lines) { sb.Append(s); } var text = sb.ToString(); if (!text.Contains("\"items\"") || !text.Contains("\"alternatives\"", StringComparison.Ordinal)) { return; } var parser = new SeJsonParser(); foreach (string json in parser.GetArrayElementsByName(text, "items")) { var start = parser.GetAllTagsByNameAsStrings(json, "start_time").FirstOrDefault(); var end = parser.GetAllTagsByNameAsStrings(json, "end_time").FirstOrDefault(); var content = parser.GetAllTagsByNameAsStrings(json, "content").FirstOrDefault(); if (start != null && end != null && !string.IsNullOrEmpty(content)) { if (double.TryParse(start, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out var startSeconds) && double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out var endSeconds)) { subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(content), startSeconds * TimeCode.BaseUnit, endSeconds * TimeCode.BaseUnit)); } else { _errorCount++; } } } subtitle.Renumber(); }
public void GetArrayElementsByName_Advanced() { var parser = new SeJsonParser(); var result = parser.GetArrayElementsByName(@"{ 'results': { 'transcripts': [ { 'transcript': 'em águas brasileiras estão achado mais antigo que o homem. Pré sal pré sal começa a se formar há mais de cem milhões de Jonas com Regina. Salas ainda habitavam até os postos são de altíssima produtividade. As grandes reservas de petróleo foram descobertas em dois mil e seis. Dois anos depois, o Brasil já começava a produzir petróleo retirado do pré sal. E hoje, quando a gente olha para trás, era muito desconhecido. Já são dez anos, vem sendo desafios para isso. As equipes contam com tecnologia especialmente desenvolvida para o pré sal. Muitas soluções saíram do Centro de Pesquisas da Petrobras Cenpes, no Rio de Janeiro. Aqui, pesquisadores inventaram técnicas que não existiam no mercado, coisa que o geofísico desenvolveram técnicas de processamento muito especial de matemática pesada. Para tentar mostrar de maneira mais óbvia onde estava roxa de pessoal, a área do pré sal fica a trezentos quilômetros da costa que vai do Espírito Santo a Santa Catarina, uma extensão de oitocentos quilômetros. O petróleo está a sete mil metros de profundidade, abaixo de uma espessa camada de sal, que funciona como um sino. Ao contrário do que muita gente acha que o petróleo tem, piscinas, debaixo do Mar Petróleo se aloja nesses buracos nessas horas, muitas dessas rochas estão no fundo da Bacia de Santos, no litoral de São Paulo. Metade da produção de petróleo que a gente tem aqui no Brasil já vem do pré sal, e um terço do gás que é consumido pelo Brasil também vem do pré sal. Então, se a gente não tivesse descoberto e desenvolvido essa tecnologia toda para poder operar as plataformas, hoje a gente estaria muito mais dependente do mercado externo. A jornada do conhecimento da tecnologia trouxe para a Petrobras a inovação que certamente para nós, hoje são ativos, são mais importantes do que o petróleo.' } ], 'speaker_labels': { 'speakers': 4, 'segments': [ { 'start_time': '5.17', 'speaker_label': 'spk_0', 'end_time': '9.25', 'items': [ { 'start_time': '5.17', 'speaker_label': 'spk_0', 'end_time': '5.37' }, { 'start_time': '105.54', 'speaker_label': 'spk_1', 'end_time': '106.09' } ] } ] }, 'items': [ { 'start_time': '5.17', 'end_time': '5.37', 'alternatives': [ { 'confidence': '0.9999', 'content': 'em' } ], 'type': 'pronunciation' }, { 'start_time': '5.37', 'end_time': '5.67', 'alternatives': [ { 'confidence': '0.9999', 'content': 'águas' } ], 'type': 'pronunciation' } ] }, 'status': 'COMPLETED' }".Replace('\'', '"'), "items"); Assert.AreEqual(4, result.Count); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string s in lines) { sb.Append(s); } string allText = sb.ToString(); var indexOfMainTag = allText.IndexOf("\"timestamp_begin\"", StringComparison.Ordinal); if (indexOfMainTag < 0) { return; } var indexOfSubtitles = allText.IndexOf("subtitles", StringComparison.Ordinal); if (indexOfSubtitles <= 0) { return; } var startText = allText.Substring(0, indexOfSubtitles); var startIndex = startText.LastIndexOf('{'); if (startIndex < 0) { return; } var json = allText.Substring(startIndex); var parser = new SeJsonParser(); var items = parser.GetArrayElementsByName(json, "subtitles"); foreach (var item in items) { var textLines = parser.GetArrayElementsByName(item, "text"); var beginTime = parser.GetAllTagsByNameAsStrings(item, "timestamp_begin"); var endTime = parser.GetAllTagsByNameAsStrings(item, "timestamp_end"); if (textLines.Count > 0 && beginTime.Count == 1 && endTime.Count == 1 && double.TryParse(beginTime[0], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) && double.TryParse(endTime[0], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSeconds)) { var text = new StringBuilder(); foreach (var line in textLines) { text.AppendLine(Json.DecodeJsonText(line)); } var p = new Paragraph(text.ToString().Trim(), startSeconds * 1000.0, endSeconds * 1000.0); subtitle.Paragraphs.Add(p); } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (var s in lines) { sb.Append(s); } var text = sb.ToString().TrimStart(); if (text.IndexOf("canvas_config", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var textDictionary = new Dictionary <string, string>(); var texts = parser.GetArrayElementsByName(text, "texts"); foreach (var textElement in texts) { var id = parser.GetFirstObject(textElement, "id"); var content = parser.GetFirstObject(textElement, "content"); if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(content)) { textDictionary.Add(id, content); } } var timeDictionary = new Dictionary <string, Paragraph>(); var tracks = parser.GetArrayElementsByName(text, "tracks"); foreach (var track in tracks) { var trackType = parser.GetFirstObject(track, "type"); if (trackType == "text") { var segments = parser.GetArrayElementsByName(track, "segments"); foreach (var segment in segments) { var id = parser.GetFirstObject(segment, "material_id"); var targetTimeRage = parser.GetFirstObject(segment, "target_timerange"); if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(targetTimeRage)) { var start = parser.GetFirstObject(targetTimeRage, "start"); var duration = parser.GetFirstObject(targetTimeRage, "duration"); if (double.TryParse(start, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startNumber) && double.TryParse(duration, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var durationNumber)) { var startMs = startNumber / 1_000; var durationMs = durationNumber / 1_000; var endMs = startMs + durationMs; var p = new Paragraph(string.Empty, startMs, endMs); timeDictionary.Add(id, p); } } } } } foreach (var kvp in textDictionary) { if (timeDictionary.TryGetValue(kvp.Key, out var p)) { p.Text = kvp.Value; subtitle.Paragraphs.Add(p); } } subtitle.Sort(Enums.SubtitleSortCriteria.StartTime); subtitle.Renumber(); }