void ReadJson() { string text = File.ReadAllText(OutFilePath, Encoding.UTF8); JsonTextReader reader = new JsonTextReader(new StringReader(text)); //int initialDepth = -1; var currentPhrase = new Phrase(); var phrases = new List<Phrase>(); var confidence = new Dictionary<string, double>(); while (reader.Read()) { if (reader.Value != null) { //Debug.WriteLine("Token: {0}, Key: {1}, Value: {2}", reader.TokenType, reader.Path, reader.Value); if (IsProperty(reader, "final")) { reader.Read(); if (reader.Value.ToString() == "True" && currentPhrase.Words.Count > 0) { phrases.Add(currentPhrase); } currentPhrase = new Phrase(); } if (IsProperty(reader, "timestamps")) { ReadTimestamps(reader, currentPhrase, confidence); } if (IsProperty(reader, "word_confidence")) { ReadConfidence(reader, confidence); } } else { //Debug.WriteLine("Token: {0}", reader.TokenType); } } Debug.WriteLine(phrases.Count + " phrases"); WriteToSRT(phrases); }
void ReadTimestamps(JsonTextReader reader, Phrase currentPhrase, Dictionary<string, double> confidence) { reader.Read(); reader.Read(); if (reader.TokenType != JsonToken.EndArray) { while (true) { reader.Read(); var wordText = reader.Value.ToString(); var start = reader.ReadAsDouble(); var end = reader.ReadAsDouble(); var word = new Word() { Text = wordText, StartTime = start.Value, EndTime = end.Value, Confidence = GetConfidence(confidence, wordText) }; currentPhrase.Words.Add(word); reader.Read(); var tok1 = reader.TokenType; reader.Read(); var tok2 = reader.TokenType; if (tok1 == JsonToken.EndArray && tok2 == JsonToken.EndArray) { break; } } } }