Exemplo n.º 1
0
        private static IEnumerable <TranscriptionParagraph> MakeParagraphs(IEnumerable <XElement> elements, TimeSpan timeunit)
        {
            foreach (var e in elements)
            {
                var p = new TranscriptionParagraph()
                {
                    Begin = new TimeSpan(timeunit.Ticks * XmlConvert.ToInt64(e.Attribute("bt").Value)),
                    End   = new TimeSpan(timeunit.Ticks * XmlConvert.ToInt64(e.Attribute("et").Value)),
                };

                var ph = new TranscriptionPhrase()
                {
                    Begin = p.Begin,
                    End   = p.End,
                    Text  = "",
                };


                var idel = e.Element("Id");
                if (idel != null)
                {
                    p.InternalID = XmlConvert.ToInt32(idel.Attribute("id").Value);
                    ph.Text      = "score: " + idel.Attribute("score").Value;
                }
                p.Phrases.Add(ph);
                yield return(p);
            }
        }
Exemplo n.º 2
0
        public static bool Import(Stream input, Transcription storage)
        {
            var groups = ReadLines(input).SplitLines(x => x == "");

            //group[0] .. line index
            //group[1] .. time --> time SomeCustomStuff...
            //group[2...] .. string

            //group[lenght-1] .. empty line ignored


            var paragraphs = groups.Where(g => g.Length > 0).Select(g =>
            {
                TranscriptionPhrase p = new TranscriptionPhrase();
                var time = g[1].Split(' ');
                p.Begin  = TimeSpan.Parse(time[0], FRculture);
                // -->
                p.End = TimeSpan.Parse(time[2], FRculture);

                if (time.Length > 3)     //some position data
                {
                    p.Phonetics = string.Join(" ", time.Skip(3));
                }

                p.Text = string.Join("\r\n", g.Skip(2));

                return(new TranscriptionParagraph(p));
            });

            foreach (var p in paragraphs)
            {
                storage.Add(p);
            }

            return(true);
        }
        void l_SplitRequest(object sender, EventArgs e)
        {

            Element el = (Element)sender;
            if (el.ValueElement is TranscriptionParagraph)
            {

                TranscriptionParagraph par = (TranscriptionParagraph)el.ValueElement;
                par.Parent.BeginUpdate();
                TimeSpan end = par.End;
                TranscriptionParagraph par2 = new TranscriptionParagraph();
                TranscriptionParagraph par1 = new TranscriptionParagraph();

                par1.AttributeString = par2.AttributeString = par.AttributeString;

                par1.Speaker = par2.Speaker = par.Speaker;
                par1.Begin = par.Begin;
                par2.End = end;
                int where = el.editor.CaretOffset;

                int sum = 0;
                for (int i = 0; i < par.Phrases.Count; i++)
                {
                    TranscriptionPhrase p = par.Phrases[i];

                    if (sum + p.Text.Length <= where) //patri do prvniho
                    {
                        par1.Add(new TranscriptionPhrase(p));
                    }
                    else if (sum >= where)
                    {
                        par2.Add(new TranscriptionPhrase(p));
                    }
                    else if (sum <= where && sum + p.Text.Length > where) //uvnitr fraze
                    {
                        int offs = where - sum;
                        double ratio = offs / (double)p.Text.Length;


                        TimeSpan length = p.End - p.Begin;
                        TimeSpan l1 = new TimeSpan((long)(ratio * length.Ticks));

                        TranscriptionPhrase p1 = new TranscriptionPhrase();
                        p1.Text = p.Text.Substring(0, offs);

                        p1.Begin = p.Begin;
                        p1.End = p1.Begin + l1;
                        //if (p1.End <= par.Begin)
                        //    p1.End = par.Begin + TimeSpan.FromMilliseconds(100); //pojistka kvuli nezarovnanejm textum
                        int idx = i;
                        par1.Add(p1);

                        TranscriptionPhrase p2 = new TranscriptionPhrase();
                        p2.Text = p.Text.Substring(offs);
                        p2.Begin = p1.End;
                        p2.End = p.End;


                        par2.Add(p2);
                        par2.Begin = p2.Begin;

                        par1.End = p1.End;

                    }
                    sum += p.Text.Length;
                }//for

                if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control))//TODO: the keyboard check should be somewhere else?
                {

                    if (RequestTimePosition != null)
                    {
                        TimeSpan pos;
                        RequestTimePosition(out pos);

                        par1.End = pos;
                        par2.Begin = pos;
                    }

                }

                var parent = par.Parent;
                int indx = par.ParentIndex;

                FixPosSetFor(par.Previous());

                parent.Remove(par);
                parent.Insert(indx, par2);
                parent.Insert(indx, par1);
                parent.EndUpdate();
                ActiveTransctiption = par2;

                this.Dispatcher.Invoke(() =>
                {
                    FixPosTryRestore();
                    SetCaretIn(par2, 0);
                }, DispatcherPriority.Background);

            }
        }
Exemplo n.º 4
0
        public void GetTranscription(bool useOrtoTP, bool removeNonPhonemes, Transcription storage)
        {
            Transcription data = storage;
            List <TranscriptionPhrase> phrazes = new List <TranscriptionPhrase>();

            string[] orto = useOrtoTP ? ORTOTP : ORTOT;

            for (int i = 0; i < ORTOT.Length; i++)
            {
                TranscriptionPhrase ph = new TranscriptionPhrase();
                ph.Text = orto[i].Trim() + " ";
                if (orto[i].Length == 0) //empty words
                {
                    ph.Text = "";
                }
                ph.Phonetics = PRON[i];
                ph.Begin     = START[i];
                ph.End       = STOP[i];
                phrazes.Add(ph);
            }

            TranscriptionChapter c   = new TranscriptionChapter();
            TranscriptionSection sec = new TranscriptionSection();

            var second = TimeSpan.FromSeconds(0.5);
            TranscriptionParagraph     pah     = new TranscriptionParagraph();
            List <TranscriptionPhrase> silence = new List <TranscriptionPhrase>();
            TimeSpan sec20 = TimeSpan.FromSeconds(20);

            #region sileny splitovaci algoritmus
            while (phrazes.Count > 0)
            {
                string tt = phrazes[0].Text.Trim();
                if (string.IsNullOrWhiteSpace(tt) || (tt.First() == '[' && tt.Last() == ']'))//nerecova udalost
                {
                    silence.Add(phrazes[0]);
                    TimeSpan begin;
                    TimeSpan end;

                    if (pah.Phrases.Count > 0)//pokud uz jsou sestavene nejake fraze spocitam jestli presahnou spolu s tichy 20s
                    {
                        begin = pah.Phrases.First().Begin;
                        end   = silence.Last().End;
                        if (end - begin >= sec20)//pokud ano reknu, ze pred tichy mel odstavec zkoncit
                        {
                            pah.Begin = pah.Phrases.First().Begin;
                            pah.End   = pah.Phrases.Last().End;
                            sec.Paragraphs.Add(pah);
                            pah = new TranscriptionParagraph();
                        }
                    }
                }
                else if (silence.Count > 0)                                                    //mam nejaky nerecovy udalosti a prislo neco jinyho
                {
                    if (silence.Last().End - silence.First().Begin >= TimeSpan.FromSeconds(2)) //mam vic nereci nez 2 sekundy udelat z ni samostatnej segment
                    {
                        if (pah.Phrases.Count > 0)
                        {
                            pah.Begin = pah.Phrases.First().Begin;
                            pah.End   = pah.Phrases.Last().End;
                            sec.Paragraphs.Add(pah);
                            pah = new TranscriptionParagraph();
                        }


                        foreach (var ss in silence)
                        {
                            pah.Phrases.Add(ss);
                        }

                        silence.Clear();

                        pah.Begin = pah.Phrases.First().Begin;
                        pah.End   = pah.Phrases.Last().End;
                        sec.Paragraphs.Add(pah);
                        pah = new TranscriptionParagraph();


                        pah.Phrases.Add(phrazes[0]);
                    }
                    else//mam ji malo -  prilepit nerecovy udalosti k odstavci
                    {
                        foreach (var ss in silence)
                        {
                            pah.Phrases.Add(ss);
                        }

                        silence.Clear();
                        pah.Phrases.Add(phrazes[0]);
                    }
                }
                else//prisla recova fraze a nemam nic v tichu pridat do paragrafu
                {
                    pah.Phrases.Add(phrazes[0]);
                }

                phrazes.RemoveAt(0);
            }

            //dosyp zbytek do prepisu

            while (silence.Count > 0)
            {
                pah.Phrases.Add(silence[0]);
                silence.RemoveAt(0);
            }
            #endregion

            pah.Begin = pah.Phrases.First().Begin;
            pah.End   = pah.Phrases.Last().End;
            sec.Paragraphs.Add(pah);
            c.Sections.Add(sec);
            data.Chapters.Add(c);

            if (removeNonPhonemes)
            {
                {
                    TranscriptionPhrase ph = (TranscriptionPhrase)sec[0][0];
                    while (ph != null)
                    {
                        var parent = ph.Parent;

                        var ph2 = (TranscriptionPhrase)ph.NextSibling();

                        string t = ph.Text.Trim();
                        if (string.IsNullOrWhiteSpace(t) || (t.StartsWith("[") && t.EndsWith("]")))
                        {
                            ph.Parent.Remove(ph);
                        }

                        ph = ph2;
                    }
                }
            }

            data.MediaURI = FileName;
        }
Exemplo n.º 5
0
        /// <summary>
        /// exportuje dokument do vybraneho formatu - pokud je cesta null, zavola savedialog
        /// </summary>
        /// <param name="aDokument"></param>
        /// <param name="vystup"></param>
        /// <param name="aFormat"></param>
        /// <returns></returns>
        public void ExportovatDokument(string vstup, string vystup, bool times, bool nonoises)
        {
            Transcription data = null;

            using (var file = File.OpenRead(vstup))
                data = Transcription.Deserialize(file);

            if (times)
            {
                FileStream   fs        = new FileStream(vystup, FileMode.Create);
                StreamWriter sw        = new StreamWriter(fs, Encoding.GetEncoding("windows-1250"));
                FileInfo     fi        = new FileInfo(vystup);
                string       pNazev    = fi.Name.ToUpper().Remove(fi.Name.Length - fi.Extension.Length);
                string       pHlavicka = "<" + pNazev + ";";
                for (int i = 0; i < data.Speakers.Count; i++)
                {
                    if (i > 0)
                    {
                        pHlavicka += ",";
                    }
                    pHlavicka += " " + data.Speakers[i].FirstName;
                }
                pHlavicka += ">";
                sw.WriteLine(pHlavicka);
                sw.WriteLine();
                for (int i = 0; i < data.Chapters.Count; i++)
                {
                    for (int j = 0; j < data.Chapters[i].Sections.Count; j++)
                    {
                        for (int k = 0; k < data.Chapters[i].Sections[j].Paragraphs.Count; k++)
                        {
                            TranscriptionParagraph pP = data.Chapters[i].Sections[j].Paragraphs[k];
                            //zapsani jednotlivych odstavcu
                            string pRadek = "<" + (pP.InternalID - 1).ToString() + "> ";
                            for (int l = 0; l < pP.Phrases.Count; l++)
                            {
                                TranscriptionPhrase pFraze = pP.Phrases[l];
                                pRadek += "[" + (pFraze.Begin).ToString() + "]" + pFraze.Text;
                            }
                            sw.WriteLine(pRadek);
                        }
                    }
                }

                sw.Close();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in data.EnumerateParagraphs())
                {
                    if (nonoises)
                    {
                        var alltext = ignoredGroup.Replace(item.Text, "");
                        alltext = whitespaceGroup.Replace(alltext, " ");
                        if (!string.IsNullOrWhiteSpace(alltext))
                        {
                            sb.AppendLine(alltext);
                        }
                    }
                    else
                    {
                        sb.AppendLine(item.Text);
                    }
                }

                File.WriteAllText(vystup, sb.ToString());
            }
        }