Example #1
0
        public void SubtitleParserTests()
        {
            var directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            // This file contains a segment with invalid timestamp format
            try
            {
                string path        = string.Format("{0}\\{1}", directory, "Content\\timestamperror.srt");
                var    translation = SubtitleParser.Parse(path, "srt");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SubtitleParseException));
            }

            // This file contains 3 segments, the last one has id 9999 and should therefore be cut off
            string path2        = string.Format("{0}\\{1}", directory, "Content\\cutsOff.srt");
            var    translation2 = SubtitleParser.Parse(path2, "srt");

            Assert.IsTrue(translation2.TranslationSegments.Count == 2);


            // This file contains a segment with invalid id
            try
            {
                string path        = string.Format("{0}\\{1}", directory, "Content\\idError.srt");
                var    translation = SubtitleParser.Parse(path, "srt");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SubtitleParseException));
            }
        }
Example #2
0
        public HttpResponseMessage GetParseData()
        {
            string textData = Request.Content.ReadAsStringAsync().Result;

            string[]             parseData;
            List <SplitterModel> parseDataList = new List <SplitterModel>();
            SplitterModel        SM;
            HttpResponseMessage  res;

            try
            {
                SubtitleSplitter.SubtitleParser sp = new SubtitleParser();
                parseData = sp.Parse(textData);
                foreach (string s in parseData)
                {
                    SM             = new SplitterModel();
                    SM.titleString = s;
                    parseDataList.Add(SM);
                }

                res = Request.CreateResponse(HttpStatusCode.OK);
                var json = JsonConvert.SerializeObject(parseDataList);
                res.Content = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
            }
            catch (Exception e)
            {
                res = Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            return(res);
        }
        public void ParseTitle(long titleId)
        {
            DataManager da    = new DataManager();
            Title       title = da.TitlesGetById(titleId);

            if (title != null)
            {
                List <TitleSubtitle> titleSubtitles = da.TitleSubtitleGetByTitleId(title.Id);
                if (titleSubtitles != null && titleSubtitles.Count > 0)
                {
                    foreach (TitleSubtitle titleSubtitle in titleSubtitles)
                    {
                        List <IndexableEntity> parsedData = SubtitleParser.ParseIt(title, titleSubtitle);

                        // insert these into subtitles table
                        if (parsedData != null)
                        {
                            foreach (IndexableEntity indexable in parsedData)
                            {
                                da.SubtitlesTextInsert(indexable);
                            }
                        }
                    }
                }

                da.TitlesIndexStatusUpdate(title.Id, "Parsed");
            }
        }
Example #4
0
        public void BreakSentenceWithIn5Chars()
        {
            string          SAMPLE_TEXT    = "Please write a progr,am 12";
            ISubtitleParser subTitleParser = new SubtitleParser();
            var             expectResult   = subTitleParser.Parse(SAMPLE_TEXT);

            Assert.That(expectResult[1].Length.Equals(5));
        }
Example #5
0
        public void RemoveRedundentWhiteSpaces()
        {
            string          SAMPLE_TEXT    = "Please   write  a program";
            ISubtitleParser subTitleParser = new SubtitleParser();
            var             expectResult   = subTitleParser.Parse(SAMPLE_TEXT);

            Assert.AreNotEqual(expectResult, SAMPLE_TEXT);
        }
Example #6
0
        public void ParseStringWithLessThan25Chars()
        {
            string          SAMPLE_TEXT    = "Please write a program";
            ISubtitleParser subTitleParser = new SubtitleParser();
            var             expectResult   = subTitleParser.Parse(SAMPLE_TEXT);

            Assert.That(expectResult.Count.Equals(1));
        }
        public void Parse()
        {
            string srtContent = File.ReadAllText("./test.srt");
            string lrcContent = File.ReadAllText("./test.lrc");
            var    srt        = SubtitleParser.Parse(srtContent);
            var    lrc        = SubtitleParser.Parse(lrcContent);

            Assert.True(srt != null && lrc != null);
        }
Example #8
0
        public void SubtitleParser_ValidFileName_Parses(string input, string language, bool isDefault, bool isForced)
        {
            var parser = new SubtitleParser(_namingOptions);

            var result = parser.ParseFile(input);

            Assert.Equal(language, result.Language, true);
            Assert.Equal(isDefault, result.IsDefault);
            Assert.Equal(isForced, result.IsForced);
        }
Example #9
0
        public void ParseTest_Blank_Text()
        {
            string SAMPLE_TEXT = "";

            var controller = new SubtitleParser();


            string[] Result = controller.Parse(SAMPLE_TEXT);

            Assert.AreEqual(Result[0].ToString(), "Please insert Some Values for parsing.");
        }
Example #10
0
        public void ParseTest_Comma_RemainlessFiveChar_Text()
        {
            string SAMPLE_TEXT = "Hi,there";

            var controller = new SubtitleParser();


            string[] Result = controller.Parse(SAMPLE_TEXT);
            Assert.IsNotNull(Result);
            Assert.AreEqual(Result.Length, 1);
        }
Example #11
0
        private async void OpenLyricsFile_Button_OnClick(object sender, RoutedEventArgs e)
        {
            var picker = new FileOpenPicker();

            picker.FileTypeFilter.Add(".lrc");
            picker.FileTypeFilter.Add(".srt");
            var file = await picker.PickSingleFileAsync();

            if (file is null)
            {
                return;
            }

            string content = await FileIO.ReadTextAsync(file);

            var subtitleBlock = SubtitleParser.Parse(content);

            Main_ScrollSubtitlePreview.Source = subtitleBlock.Lines.ToLineUiList();
        }
Example #12
0
        /// <summary>
        /// Sends the selected translation to the user as a memorystream
        /// </summary>
        /// <param name="translationId">The translation to download</param>
        /// <param name="mediaId">Unused?</param>
        /// <returns></returns>
        public ActionResult Download(int?translationId, int?mediaId)
        {
            if (translationId == null || mediaId == null)
            {
                throw new MissingParameterException();
            }
            var translation = _unitOfWork.TranslationRepository.GetByID(translationId);

            if (translation == null)
            {
                throw new DataNotFoundException();
            }
            translation.NumberOfDownloads++;
            _unitOfWork.TranslationRepository.Update(translation);
            _unitOfWork.Save();
            string fileName = translation.Media.Title + "(" + translation.Media.ReleaseYear + ")_" + translation.Language.Name + ".srt";

            return(File(SubtitleParser.Output(translation).ToArray(), System.Net.Mime.MediaTypeNames.Application.Octet, fileName));
        }
Example #13
0
        public void ParseTest_Sample_Text()
        {
            string SAMPLE_TEXT =
                "Please write a program that breaks this text into small chucks. Each chunk should have a maximum length of 25 " +
                "characters. The program should try to break on complete sentences or punctuation marks if possible.  " +
                "If a comma or sentence break occurs within 5 characters of the max line length, the line should be broken early.  " +
                "The exception to this rule is when the next line will only contain 5 characters.  Redundant whitespace should " +
                "not be counted between lines, and any duplicate   spaces should be removed.  " +
                "Does this make sense? If not please ask for further clarification, an array containing " +
                "the desired outcome has been provided below. Any text beyond this point is not part of the instructions, " +
                "it's only here to ensure test converge. Finish line. Aaa asdf asdfjk las, asa.";

            var controller = new SubtitleParser();


            string[] Result = controller.Parse(SAMPLE_TEXT);

            Assert.IsNotNull(Result);
            Assert.AreNotEqual(Result[0].ToString(), "Getting some error while parsing.");
        }
Example #14
0
        /* Private members */

        private Subtitles ParsedSubtitles(string path, Encoding fileEncoding, SubtitleFormat format, float inputFrameRate, string text)
        {
            SubtitleCollection collection        = null;
            SubtitleParser     subtitleParser    = new SubtitleParser(includeIncompleteSubtitles);
            ParsingProperties  parsingProperties = subtitleParser.Parse(text, format, inputFrameRate, out collection, out incompleteSubtitles);

            SubtitleProperties subtitleProperties = new SubtitleProperties(parsingProperties);

            collection.SetPropertiesForAll(subtitleProperties);

            Subtitles subtitles = new Subtitles(collection, subtitleProperties);

            CompleteTimingsAfterParsing(subtitles, parsingProperties);

            fileProperties = new FileProperties(path, fileEncoding, format.Type, parsingProperties.TimingMode);

            Logger.Info("[SubtitleFactory] Opened \"{0}\" with encoding \"{1}\", format \"{2}\", timing mode \"{3}\" and frame rate \"{4}\" (input frame rate was \"{5}\")",
                        path, fileEncoding, format.Name, parsingProperties.TimingMode, subtitleProperties.CurrentFrameRate, inputFrameRate);

            return(subtitles);
        }
Example #15
0
 private void AssertThrowsParsingException(SubtitleParser parser)
 {
     Assert.Throws <ParsingException>(
         () => parser.Parse(
             UnvalidatedSubtitleTests.CreateUnvalidatedSubtitle1()));
 }
Example #16
0
 public ActionResult CreateShow(ShowTranslationViewModel showTranslation, HttpPostedFileBase file)
 {
     if (ModelState.IsValid)
     {
         var show           = showTranslation.Show;
         var showToCheckFor = _unitOfWork.ShowRepository.Get()
                              .Where(s => s.Title == show.Title)
                              .Where(s => s.Series == show.Series)
                              .Where(s => s.Episode == show.Episode)
                              .SingleOrDefault();
         if (showToCheckFor == null)
         {
             show.Translations = new List <Translation>();
             if (file != null && file.ContentLength > 0)
             {
                 var fileName = Path.GetFileName(file.FileName);
                 var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                 file.SaveAs(path);
                 var translation = SubtitleParser.Parse(path, "srt");
                 translation.LanguageID = showTranslation.LanguageID;
                 show.Translations.Add(translation);
             }
             else
             {
                 show.Translations.Add(new Translation {
                     LanguageID = showTranslation.LanguageID
                 });
             }
             _unitOfWork.ShowRepository.Insert(show);
             var userid = User.Identity.GetUserId();
             var user   = _unitOfWork.UserRepository.GetByID(userid);
             user.NewTranslations++;
             _unitOfWork.Save();
             var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First();
             return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID }));
         }
         else if (_unitOfWork.TranslationRepository.Get()
                  .Where(t => t.MediaID == showToCheckFor.ID)
                  .Where(t => t.LanguageID == showTranslation.LanguageID)
                  .SingleOrDefault() == null)
         {
             if (file != null && file.ContentLength > 0)
             {
                 var fileName = Path.GetFileName(file.FileName);
                 var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                 file.SaveAs(path);
                 var translation = SubtitleParser.Parse(path, "srt");
                 translation.LanguageID = showTranslation.LanguageID;
                 showToCheckFor.Translations.Add(translation);
             }
             else
             {
                 showToCheckFor.Translations.Add(new Translation {
                     LanguageID = showTranslation.LanguageID
                 });
             }
             _unitOfWork.ShowRepository.Update(showToCheckFor);
             var userid = User.Identity.GetUserId();
             var user   = _unitOfWork.UserRepository.GetByID(userid);
             user.NewTranslations++;
             _unitOfWork.Save();
             HasRequest(showToCheckFor.ID, showTranslation.LanguageID);
             var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First();
             return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID }));
         }
         ViewBag.Errormsg = "Þessi þýðing er nú þegar til.";
     }
     ViewBag.CategoryID = new SelectList(_unitOfWork.CategoryRepository.Get(), "ID", "Name", showTranslation.Show.CategoryID);
     ViewBag.LanguageID = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name");
     return(View("CreateShow"));
 }
Example #17
0
 public ActionResult CreateClip(ClipTranslationViewModel clipTranslation, HttpPostedFileBase file)
 {
     if (ModelState.IsValid)
     {
         var clip           = clipTranslation.Clip;
         var clipToCheckFor = _unitOfWork.ClipRepository.Get()
                              .Where(c => c.Title == clip.Title)
                              .Where(c => c.ReleaseYear == clip.ReleaseYear)
                              .SingleOrDefault();
         if (clipToCheckFor == null)
         {
             clip.Translations = new List <Translation>();
             if (file != null && file.ContentLength > 0)
             {
                 var fileName = Path.GetFileName(file.FileName);
                 var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                 file.SaveAs(path);
                 var translation = SubtitleParser.Parse(path, "srt");
                 translation.LanguageID = clipTranslation.LanguageID;
                 clip.Translations.Add(translation);
             }
             else
             {
                 clip.Translations.Add(new Translation {
                     LanguageID = clipTranslation.LanguageID
                 });
             }
             clip.Link = "//www.youtube.com/embed/" + YoutubeParser.parseLink(clip.Link);
             _unitOfWork.ClipRepository.Insert(clip);
             var userid = User.Identity.GetUserId();
             var user   = _unitOfWork.UserRepository.GetByID(userid);
             user.NewTranslations++;
             _unitOfWork.Save();
             var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First();
             return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID }));
         }
         else if (_unitOfWork.TranslationRepository.Get()
                  .Where(t => t.MediaID == clipToCheckFor.ID)
                  .Where(t => t.LanguageID == clipTranslation.LanguageID)
                  .SingleOrDefault() == null)
         {
             if (file != null && file.ContentLength > 0)
             {
                 var fileName = Path.GetFileName(file.FileName);
                 var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                 file.SaveAs(path);
                 var translation = SubtitleParser.Parse(path, "srt");
                 translation.LanguageID = clipTranslation.LanguageID;
                 clipToCheckFor.Translations.Add(translation);
             }
             else
             {
                 clipToCheckFor.Translations.Add(new Translation {
                     LanguageID = clipTranslation.LanguageID
                 });
             }
             _unitOfWork.ClipRepository.Update(clipToCheckFor);
             var userid = User.Identity.GetUserId();
             var user   = _unitOfWork.UserRepository.GetByID(userid);
             user.NewTranslations++;
             _unitOfWork.Save();
             HasRequest(clipToCheckFor.ID, clipTranslation.LanguageID);
             var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First();
             return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID }));
         }
         ViewBag.Errormsg = "Þessi þýðing er nú þegar til.";
     }
     ViewBag.CategoryID = new SelectList(_unitOfWork.CategoryRepository.Get(), "ID", "Name", clipTranslation.Clip.CategoryID);
     ViewBag.LanguageID = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name");
     return(View("CreateClip"));
 }
Example #18
0
        public ActionResult CreateTranslation(int?id, int?languageID, HttpPostedFileBase file)
        {
            var    media = _unitOfWork.MediaRepository.GetByID(id);
            string type  = "";

            if (languageID == null)
            {
                ViewBag.Errormsg   = "Tungumál verður að vera valið.";
                ViewBag.LanguageID = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name");
                type = media.GetType().BaseType.Name;
                switch (type)
                {
                case "Movie":
                    return(View("CreateMovieTranslation", media));

                case "Show":
                    return(View("CreateShowTranslation", media));

                case "Clip":
                    return(View("CreateClipTranslation", media));

                default:
                    throw new ApplicationException();
                }
            }
            var translationToFind = _unitOfWork.TranslationRepository.Get()
                                    .Where(t => t.MediaID == id)
                                    .Where(t => t.LanguageID == languageID)
                                    .SingleOrDefault();

            if (translationToFind == null)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    var translation = SubtitleParser.Parse(path, "srt");
                    translation.LanguageID = languageID.Value;
                    media.Translations.Add(translation);
                }
                else
                {
                    media.Translations.Add(new Translation {
                        LanguageID = languageID.Value
                    });
                }
                _unitOfWork.MediaRepository.Update(media);
                var userid = User.Identity.GetUserId();
                var user   = _unitOfWork.UserRepository.GetByID(userid);
                user.NewTranslations++;
                _unitOfWork.Save();
                HasRequest(id.Value, languageID.Value);
                var newTranslation = _unitOfWork.TranslationRepository.Get().OrderByDescending(t => t.ID).First();
                return(RedirectToAction("Index", "Translation", new { id = newTranslation.ID }));
            }
            ViewBag.Errormsg   = "Þessi þýðing er nú þegar til.";
            ViewBag.LanguageID = new SelectList(_unitOfWork.LanguageRepository.Get(), "ID", "Name");
            type = media.GetType().BaseType.Name;
            switch (type)
            {
            case "Movie":
                return(View("CreateMovieTranslation", media));

            case "Show":
                return(View("CreateShowTranslation", media));

            case "Clip":
                return(View("CreateClipTranslation", media));

            default:
                throw new ApplicationException();
            }
        }
Example #19
0
 public SubtitleHandler()
 {
     _subtitleParser = new SubtitleParser();
 }
Example #20
0
        static async Task Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            string rootDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\.."));
            var    files   = Directory.GetFiles(rootDir + @"\Data");
            var    context = new TranslationContext();

            ClearDatabase(context);

            // -- Pipeline Components --
            var parser = new SubtitleParser();

            parser.Started += (_) => PrintHeader("Step 1 - Parser");
            //parser.Finished += (_, output) => output.Print();
            var offsetter = new SubtitleOffsetter();

            offsetter.Started += (_) => PrintHeader("Step 2 - Offsetter");
            //offsetter.Finished += (_, _) => offsetter.PrintHistory();
            var mapper = new SubtitleMapper();

            mapper.Started  += (_) => PrintHeader("Step 3 - Mapper");
            mapper.Finished += (_, output) =>
            {
                //output.Print();
                //output.PrintErrorHistogram();
                //output.PrintMismatches();
            };
            var          aggregator     = new SubtitleMappingAggregator();
            const double errorThreshold = 1.0;

            aggregator.WithFilter((conversation) => conversation.Intervals.Count < 5 && conversation.Intervals.Average(i => i.Error) < errorThreshold);
            aggregator.Started  += (_) => PrintHeader("Step 4 - Aggregation");
            aggregator.Finished += (_, output) => {
                //output.Print();
                output.PrintAverageError();
                Save(new TranslationContext(), output); // Create a new context in order to have thread safety
            };

            // -- Pipeline --
            var pipeline = new Pipeline <SubtitleFilePathPair, SubtitleConversations>()
                           .AddStepAndStart(parser)
                           .AddStepAndStart(offsetter)
                           .AddStepAndStart(mapper)
                           .AddStepAndStart(aggregator);

            List <Task> outputTasks = new List <Task>();

            for (int i = 0; i < files.Length; i += 2)
            {
                string native;
                string translated;
                if (files[i].Contains("en"))
                {
                    native     = files[i];
                    translated = files[i + 1];
                }
                else
                {
                    native     = files[i + 1];
                    translated = files[i];
                }
                var input = new SubtitleFilePathPair
                {
                    Native     = native,
                    Translated = translated,
                };
                outputTasks.Add(pipeline.Execute(input));
            }
            await Task.WhenAll(outputTasks);

            pipeline.Stop();
        }
Example #21
0
        public void SubtitleParser_InvalidFileName_ReturnsNull(string input)
        {
            var parser = new SubtitleParser(_namingOptions);

            Assert.Null(parser.ParseFile(input));
        }
        //GET: api/Subtitle
        public List <string> GetSubtitle(string input)
        {
            SubtitleParser subtitleParser = new SubtitleParser();

            return(subtitleParser.Parse(input));
        }