public void WordChange_NewText_on_text() { WordChange wc = new WordChange("test.txt", "NewText", "text"); string actual; using (StreamReader sr = new StreamReader("test.txt")) { actual = sr.ReadToEnd(); } string expected = wc.Text; Assert.AreEqual(expected, actual); }
public ActionResult Update(PUpload updata) { List <Words> wordlist = new List <Words>(); Media VB; Media JSONout; Words word; Words wordout; Words oldWord; PWords newWord; var data = new ResponseData(); data.requestStatus = "FAILED"; Transcript transcript = (from s in db.Transcripts where s.mediaId == updata.mediaId select s).FirstOrDefault(); string JSON = transcript.JSON; var json_serializer1 = new JavaScriptSerializer(); var json_serializer2 = new JavaScriptSerializer(); VB = json_serializer1.Deserialize <Media>(JSON); JSONout = json_serializer2.Deserialize <Media>(JSON); JSONout.media.transcripts.latest.words = wordlist.ToArray(); // Get Txt Srt file into memory. List <SrtFile> srtList = new List <SrtFile>(); SrtFile srtIntance = new SrtFile(); SrtFile srtObj = (SrtFile)srtIntance.Clone(); int sPos = 0; using (StringReader reader = new StringReader(transcript.Text_Sort)) { string line = string.Empty; do { line = reader.ReadLine(); if (line != null) { if (line.Length == 0) { srtList.Add(srtObj); srtObj = (SrtFile)srtIntance.Clone(); sPos = 0; srtObj.content = ""; } else { switch (sPos) { case 0: srtObj.pos = line; sPos++; break; case 1: sPos++; string[] separators = { " --> " }; string[] times = line.Split(separators, StringSplitOptions.RemoveEmptyEntries); srtObj.s = StringUtils.timeToMilliseconds(times[0]); srtObj.e = StringUtils.timeToMilliseconds(times[1]); srtObj.time = line; break; default: srtObj.content += line + "\n"; break; } } } } while (line != null); srtList.Add(srtObj); } int numwords = updata.content.Count(); int i = 0; int outcounter = 0; while (outcounter < numwords) { word = VB.media.transcripts.latest.words[i]; newWord = updata.content[outcounter]; // Check if speaker insertion if ((newWord.m != null) && ((word.m == null) || (word.m == "punc"))) { wordout = (Words)word.Clone(); wordout.p = outcounter; wordout.e = wordout.s + 1000; wordout.w = newWord.w.Trim(); wordout.m = "turn"; wordlist.Add(wordout); outcounter++; newWord = updata.content[outcounter]; } // Check if word deleted if (word.s < newWord.s) { i++; } else { wordout = (Words)word.Clone(); wordout.p = outcounter; wordout.w = newWord.w.Trim(); wordlist.Add(wordout); if (word.w != newWord.w.Trim()) // write a record to the change log table. { var wChange = new WordChange(); wChange.changeBy = User.Identity.Name; wChange.changeDate = DateTime.Now; wChange.p = i; wChange.TranscriptId = transcript.Id; wChange.m = wordout.m; wChange.oldWord = word.w; wChange.newWord = newWord.w.Trim(); wChange.s = word.s; db.WordChanges.Add(wChange); db.SaveChanges(); // Update Srt // Find relevant object. int j = 0; do { if ((srtList[j].s <= word.s) && (srtList[j].e >= word.s)) { srtList[j].content = srtList[j].content.Replace(word.w, newWord.w.Trim()); } j++; } while (j < srtList.Count); EventLoad.LogEvent(User.Identity.Name, transcript.Id, "Word_Change", null, wChange.oldWord, wChange.newWord, null); } outcounter++; i++; } } JSONout.media.transcripts.latest.words = wordlist.ToArray(); var sJSONout = new JavaScriptSerializer().Serialize(JSONout); //updated text srt file string srtString = ""; int cnt = 0; do { srtString += srtList[cnt].pos + "\n"; srtString += srtList[cnt].time + "\n"; srtString += srtList[cnt].content + "\n"; cnt++; } while (cnt < srtList.Count); transcript.JSON = sJSONout; transcript.Text_Sort = srtString; db.Entry(transcript).State = EntityState.Modified; db.SaveChanges(); data.requestStatus = "SUCCESS"; data.id = transcript.Id; //return RedirectToAction("Play", new { id = transcript.Id }); return(Json(data)); }