public void Initialize(Subtitle subtitle, string text) { _subtitle = subtitle; if (!string.IsNullOrEmpty(text)) { textBoxAddName.Text = text.Trim().TrimEnd('.', '!', '?'); if (textBoxAddName.Text.Length > 1) { textBoxAddName.Text = char.ToUpper(textBoxAddName.Text[0]) + textBoxAddName.Text.Substring(1); } } comboBoxDictionaries.Items.Clear(); string languageName = LanguageAutoDetect.AutoDetectLanguageName(Configuration.Settings.General.SpellCheckLanguage, _subtitle); foreach (string name in Utilities.GetDictionaryLanguages()) { comboBoxDictionaries.Items.Add(name); if (name.Contains("[" + languageName + "]")) { comboBoxDictionaries.SelectedIndex = comboBoxDictionaries.Items.Count - 1; } } }
public void Initialize(Subtitle subtitle, string text) { _subtitle = subtitle; if (!string.IsNullOrEmpty(text)) { textBoxAddName.Text = text.Trim().TrimEnd('.', '!', '?'); if (textBoxAddName.Text.Length > 1) { textBoxAddName.Text = char.ToUpper(textBoxAddName.Text[0]) + textBoxAddName.Text.Substring(1); } } comboBoxDictionaries.Items.Clear(); string languageName = LanguageAutoDetect.AutoDetectLanguageName(Configuration.Settings.General.SpellCheckLanguage, _subtitle); int selIndex = -1; var dictionaries = Utilities.GetDictionaryLanguagesCultureNeutral(); for (var index = 0; index < dictionaries.Count; index++) { string name = dictionaries[index]; comboBoxDictionaries.Items.Add(name); if (selIndex == -1 && languageName.Length > 1 && name.Contains("[" + languageName.Substring(0, 2) + "]")) { selIndex = index; } } comboBoxDictionaries.SelectedIndex = selIndex >= 0 ? selIndex : 0; }
public override string ToText(Subtitle subtitle, string title) { string xmlStructure = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + Environment.NewLine + "<tmx version=\"1.4\">" + Environment.NewLine + " <header creationtool=\"Subtitle Edit\" creationtoolversion=\"3.3\" datatype=\"html\" segtype=\"sentence\" adminlang=\"en-us\" srclang=\"EN\" o-encoding=\"utf-8\">" + Environment.NewLine + " <note>This is a subtitle</note>" + Environment.NewLine + " </header>" + Environment.NewLine + " <body />" + Environment.NewLine + "</tmx>"; string lang = LanguageAutoDetect.AutoDetectLanguageName("en_US", subtitle); if (lang.StartsWith("en_")) { lang = "EN"; } else if (lang.Length == 5) { lang = lang.Substring(3); } string paragraphInnerXml = " <prop type=\"start\">[START]</prop>" + Environment.NewLine + " <prop type=\"end\">[END]</prop>" + Environment.NewLine + " <tuv xml:lang=\"" + lang + "\">" + Environment.NewLine + " <seg>[TEXT]</seg>" + Environment.NewLine + " </tuv>"; var xml = new XmlDocument(); xml.LoadXml(xmlStructure); XmlNode body = xml.DocumentElement.SelectSingleNode("body"); int count = 1; foreach (Paragraph p in subtitle.Paragraphs) { XmlNode paragraph = xml.CreateElement("tu"); XmlAttribute tuid = xml.CreateAttribute("tuid"); tuid.InnerText = count.ToString("D4"); paragraph.Attributes.Append(tuid); XmlAttribute datatype = xml.CreateAttribute("datatype"); datatype.InnerText = "html"; paragraph.Attributes.Append(datatype); string innerXml = paragraphInnerXml; innerXml = innerXml.Replace("[START]", p.StartTime.ToString()); innerXml = innerXml.Replace("[END]", p.EndTime.ToString()); innerXml = innerXml.Replace("[TEXT]", p.Text.Replace(Environment.NewLine, "<br />")); paragraph.InnerXml = innerXml; body.AppendChild(paragraph); count++; } return(ToUtf8XmlString(xml)); }
public void DoSpellCheck(bool autoDetect, Subtitle subtitle, string dictionaryFolder, Main mainWindow, int startLine) { _subtitle = subtitle; LanguageStructure.Main mainLanguage = Configuration.Settings.Language.Main; _mainWindow = mainWindow; _skipAllList = new List <string>(); _noOfSkippedWords = 0; _noOfChangedWords = 0; _noOfCorrectWords = 0; _noOfNamesEtc = 0; _noOfAddedWords = 0; _firstChange = true; if (!string.IsNullOrEmpty(Configuration.Settings.General.SpellCheckLanguage) && File.Exists(Path.Combine(dictionaryFolder, Configuration.Settings.General.SpellCheckLanguage + ".dic"))) { _languageName = Configuration.Settings.General.SpellCheckLanguage; } else { string name = Utilities.GetDictionaryLanguages()[0]; int start = name.LastIndexOf('['); int end = name.LastIndexOf(']'); if (start > 0 && end > start) { start++; name = name.Substring(start, end - start); _languageName = name; } else { MessageBox.Show(string.Format(mainLanguage.InvalidLanguageNameX, name)); return; } } if (autoDetect || string.IsNullOrEmpty(_languageName)) { _languageName = LanguageAutoDetect.AutoDetectLanguageName(_languageName, subtitle); } string dictionary = Utilities.DictionaryFolder + _languageName; LoadDictionaries(dictionaryFolder, dictionary, _languageName); _currentIndex = 0; if (startLine >= 0 && startLine < _subtitle.Paragraphs.Count) { _currentIndex = startLine; } _currentParagraph = _subtitle.Paragraphs[_currentIndex]; SetWords(_currentParagraph.Text); _wordsIndex = -1; PrepareNextWord(); }
private void buttonSpellCheckDownload_Click(object sender, EventArgs e) { using (var gd = new GetDictionaries()) { gd.ShowDialog(this); } FillSpellCheckDictionaries(LanguageAutoDetect.AutoDetectLanguageName(null, _subtitle)); if (comboBoxDictionaries.Items.Count > 0 && comboBoxDictionaries.SelectedIndex == -1) { comboBoxDictionaries.SelectedIndex = 0; } ComboBoxDictionariesSelectedIndexChanged(null, null); }
public async Task InitializeLiveSpellCheck(Subtitle subtitle, int lineNumber) { if (lineNumber < 0) { return; } if (_spellCheckWordLists is null && _hunspell is null) { var detectedLanguage = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle); var downloadedDictionaries = Utilities.GetDictionaryLanguagesCultureNeutral(); var isDictionaryAvailable = false; foreach (var downloadedDictionary in downloadedDictionaries) { if (downloadedDictionary.Contains($"[{detectedLanguage}]")) { isDictionaryAvailable = true; break; } } if (isDictionaryAvailable) { IsDictionaryDownloaded = true; var languageName = LanguageAutoDetect.AutoDetectLanguageName(string.Empty, subtitle); if (languageName.Split(new char[] { '_', '-' })[0] != detectedLanguage) { return; } await LoadDictionariesAsync(languageName); IsSpellCheckerInitialized = true; IsSpellCheckRequested = true; TextChangedHighlight(this, EventArgs.Empty); } else { IsDictionaryDownloaded = false; } LanguageChanged = true; CurrentLanguage = detectedLanguage; } }
private void FindAllNames() { string language = LanguageAutoDetect.AutoDetectLanguageName("en_US", _subtitle); if (string.IsNullOrEmpty(language)) { language = "en_US"; } var namesList = new NamesList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNamesEtc, Configuration.Settings.WordLists.NamesEtcUrl); // Will contains both one word names and multi names var namesEtcList = namesList.GetAllNames(); if (language.StartsWith("en", StringComparison.Ordinal)) { namesEtcList.Remove("Black"); namesEtcList.Remove("Bill"); namesEtcList.Remove("Long"); namesEtcList.Remove("Don"); } string text = HtmlUtil.RemoveHtmlTags(_subtitle.GetAllTexts()); string textToLower = text.ToLower(); listViewNames.BeginUpdate(); foreach (string name in namesEtcList) { int startIndex = textToLower.IndexOf(name.ToLower(), StringComparison.Ordinal); if (startIndex >= 0) { while (startIndex >= 0 && startIndex < text.Length && textToLower.Substring(startIndex).Contains(name.ToLower()) && name.Length > 1 && name != name.ToLower()) { bool startOk = (startIndex == 0) || (text[startIndex - 1] == ' ') || (text[startIndex - 1] == '-') || (text[startIndex - 1] == '"') || (text[startIndex - 1] == '\'') || (text[startIndex - 1] == '>') || (Environment.NewLine.EndsWith(text[startIndex - 1].ToString(CultureInfo.InvariantCulture))); if (startOk) { int end = startIndex + name.Length; bool endOk = end <= text.Length; if (endOk) { endOk = end == text.Length || ExpectedEndChars.Contains(text[end]); } if (endOk && text.Substring(startIndex, name.Length) != name) // do not add names where casing already is correct { if (!_usedNames.Contains(name)) { _usedNames.Add(name); AddToListViewNames(name); break; // break while } } } startIndex = textToLower.IndexOf(name.ToLower(), startIndex + 2, StringComparison.Ordinal); } } } listViewNames.EndUpdate(); groupBoxNames.Text = string.Format(Configuration.Settings.Language.ChangeCasingNames.NamesFoundInSubtitleX, listViewNames.Items.Count); }
private void ButtonOkClick(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBoxAddName.Text)) { return; } NewName = textBoxAddName.Text.RemoveControlCharacters().Trim(); string languageName = null; _language = Configuration.Settings.Language.Main; if (!string.IsNullOrEmpty(Configuration.Settings.General.SpellCheckLanguage)) { languageName = Configuration.Settings.General.SpellCheckLanguage; } else { List <string> list = Utilities.GetDictionaryLanguages(); if (list.Count > 0) { string name = list[0]; int start = name.LastIndexOf('['); int end = name.LastIndexOf(']'); if (start > 0 && end > start) { start++; name = name.Substring(start, end - start); languageName = name; } else { MessageBox.Show(string.Format(_language.InvalidLanguageNameX, name)); return; } } } languageName = LanguageAutoDetect.AutoDetectLanguageName(languageName, _subtitle); if (comboBoxDictionaries.Items.Count > 0) { string name = comboBoxDictionaries.SelectedItem.ToString(); int start = name.LastIndexOf('['); int end = name.LastIndexOf(']'); if (start >= 0 && end > start) { start++; name = name.Substring(start, end - start); languageName = name; } } if (string.IsNullOrEmpty(languageName)) { languageName = "en_US"; } var namesList = new NamesList(Configuration.DictionariesFolder, languageName, Configuration.Settings.WordLists.UseOnlineNamesEtc, Configuration.Settings.WordLists.NamesEtcUrl); if (namesList.Add(textBoxAddName.Text)) { DialogResult = DialogResult.OK; } else { DialogResult = DialogResult.Cancel; } }
public override string ToText(Subtitle subtitle, string title) { string language = LanguageAutoDetect.AutoDetectLanguageName("en_US", subtitle); var ci = CultureInfo.GetCultureInfo(language.Replace("_", "-")); string languageTag = $"{language.Replace("_", string.Empty).ToUpper()}CC"; string languageName = ci.Parent.EnglishName; string languageStyle = $".{languageTag} [ name: {languageName}; lang: {language.Replace("_", "-")} ; SAMIType: CC ; ]"; languageStyle = languageStyle.Replace("[", "{").Replace("]", "}"); string header = @"<SAMI> <HEAD> <TITLE>_TITLE_</TITLE> <SAMIParam> Metrics {time:ms;} Spec {MSFT:1.0;} </SAMIParam> <STYLE TYPE=""text/css""> <!-- P { font-family: Arial; font-weight: normal; color: white; background-color: black; text-align: center; } _LANGUAGE-STYLE_ --> </STYLE> </HEAD> <BODY> <-- Open play menu, choose Captions and Subtiles, On if available --> <-- Open tools menu, Security, Show local captions when present -->"; bool useExtra = false; if (!string.IsNullOrEmpty(subtitle.Header) && subtitle.Header.StartsWith("<style", StringComparison.OrdinalIgnoreCase)) { useExtra = true; header = @"<SAMI> <HEAD> <TITLE>_TITLE_</TITLE> <SAMIParam> Metrics {time:ms;} Spec {MSFT:1.0;} </SAMIParam> " + subtitle.Header.Trim() + @" </HEAD> <BODY> <-- Open play menu, choose Captions and Subtiles, On if available --> <-- Open tools menu, Security, Show local captions when present -->"; } // Example text (start numbers are milliseconds) //<SYNC Start=65264><P>Let's go! //<SYNC Start=66697><P><BR> string paragraphWriteFormat = @"<SYNC Start={0}><P Class={3}>{2}" + Environment.NewLine + @"<SYNC Start={1}><P Class={3}> "; string paragraphWriteFormatOpen = @"<SYNC Start={0}><P Class={2}>{1}"; if (Name == new SamiModern().Name) { paragraphWriteFormat = "<SYNC Start=\"{0}\"><P Class=\"{3}\">{2}</P></SYNC>" + Environment.NewLine + "<SYNC Start=\"{1}\"><P Class=\"{3}\"> </P></SYNC>"; paragraphWriteFormatOpen = "<SYNC Start=\"{0}\"><P Class=\"{2}\">{1}</P></SYNC>"; } else if (Name == new SamiYouTube().Name) { paragraphWriteFormat = "<SYNC Start=\"{0}\"><P Class=\"{3}\">{2}</P></SYNC>" + Environment.NewLine + "<SYNC Start=\"{1}\"><P Class=\"{3}\"></P></SYNC>"; paragraphWriteFormatOpen = "<SYNC Start=\"{0}\"><P Class=\"{2}\">{1}</P></SYNC>"; } int count = 1; var sb = new StringBuilder(); sb.AppendLine(header.Replace("_TITLE_", title).Replace("_LANGUAGE-STYLE_", languageStyle)); var totalLine = new StringBuilder(); var partialLine = new StringBuilder(); foreach (Paragraph p in subtitle.Paragraphs) { Paragraph next = subtitle.GetParagraphOrDefault(count); string text = p.Text; if (text.Contains('<') && text.Contains('>')) { bool tagOn = false; for (int i = 0; i < text.Length; i++) { string t = text.Substring(i); if (t.StartsWith('<') && (t.StartsWith("<font", StringComparison.Ordinal) || t.StartsWith("<div", StringComparison.Ordinal) || t.StartsWith("<i", StringComparison.Ordinal) || t.StartsWith("<b", StringComparison.Ordinal) || t.StartsWith("<s", StringComparison.Ordinal) || t.StartsWith("</", StringComparison.Ordinal))) { totalLine.Append(EncodeText(partialLine.ToString())); partialLine.Clear(); tagOn = true; totalLine.Append('<'); } else if (t.StartsWith('>') && tagOn) { tagOn = false; totalLine.Append('>'); } else if (!tagOn) { partialLine.Append(text[i]); } else { totalLine.Append(text[i]); } } totalLine.Append(EncodeText(partialLine.ToString())); text = totalLine.ToString(); totalLine.Clear(); partialLine.Clear(); } else { text = EncodeText(text); } if (Name == new SamiModern().Name) { text = text.Replace(Environment.NewLine, "<br />"); } else { text = text.Replace(Environment.NewLine, "<br>"); } string currentClass = languageTag; if (useExtra && !string.IsNullOrEmpty(p.Extra)) { currentClass = p.Extra; } var startMs = (long)(Math.Round(p.StartTime.TotalMilliseconds)); var endMs = (long)(Math.Round(p.EndTime.TotalMilliseconds)); if (next != null && Math.Abs(((long)Math.Round(next.StartTime.TotalMilliseconds)) - endMs) < 1) { sb.AppendLine(string.Format(paragraphWriteFormatOpen, startMs, text, currentClass)); } else { sb.AppendLine(string.Format(paragraphWriteFormat, startMs, endMs, text, currentClass)); } count++; } sb.AppendLine("</BODY>"); sb.AppendLine("</SAMI>"); return(sb.ToString().Trim()); }