public bool IsDialogue(string String, int?Caution = null, bool UseAcceptableRange = true) { try { if (string.IsNullOrWhiteSpace(String)) { return(false); } string Str = String.Trim(); Str = Str.Replace(Breakline, "\n"); foreach (string Ignore in IgnoreList) { Str = Str.Replace(Ignore, ""); } foreach (string Deny in DenyList) { if (Str.ToLower().Contains(Deny.ToLower())) { return(false); } } if (string.IsNullOrWhiteSpace(Str)) { return(false); } if (UseAcceptableRange && CharacterRanges.TotalMissmatch(Str, AcceptableRanges) > 0) { return(false); } string[] Words = Str.Split(' '); int Spaces = Str.Where(x => x == ' ' || x == '\t').Count(); int Pontuations = Str.Where(x => PontuationList.Contains(x)).Count(); int WordCount = Words.Where(x => x.Length >= 2 && !string.IsNullOrWhiteSpace(x)).Count(); int Specials = Str.Where(x => char.IsSymbol(x)).Count(); Specials += Str.Where(x => char.IsPunctuation(x)).Count() - Pontuations; int SpecialsStranges = Str.Where(x => SpecialList.Contains(x)).Count(); int Uppers = Str.Where(x => char.IsUpper(x)).Count(); int Latim = Str.Where(x => x >= 'A' && x <= 'z').Count(); int Numbers = Str.Where(x => x >= '0' && x <= '9').Count(); int NumbersJap = Str.Where(x => x >= '0' && x <= '9').Count(); int JapChars = Str.Where(x => (x >= '、' && x <= 'ヿ') || (x >= '。' && x <= 'ン')).Count(); int Kanjis = Str.Where(x => x >= '一' && x <= '龯').Count(); bool IsCaps = Str.ToUpper() == Str; bool IsJap = JapChars + Kanjis > Latim / 2; //More Points = Don't Looks a Dialogue //Less Points = Looks a Dialogue int Points = 0; if (Str.Length > 4) { string ext = Str.Substring(Str.Length - 4, 4); try { if (System.IO.Path.GetExtension(ext).Trim('.').Length == 3) { Points += 2; } } catch { } } bool BeginQuote = false; Quote?LineQuotes = null; foreach (Quote Quote in Quotes) { BeginQuote |= Str.StartsWith(Quote.Start.ToString()); if (Str.StartsWith(Quote.Start.ToString()) && Str.EndsWith(Quote.End.ToString())) { Points -= 3; LineQuotes = Quote; break; } else if (Str.StartsWith(Quote.Start.ToString()) || Str.EndsWith(Quote.End.ToString())) { Points--; LineQuotes = Quote; break; } } try { char Last = (LineQuotes == null ? Str.Last() : Str.TrimEnd(LineQuotes.Value.End).Last()); if (IsJap && PontuationJapList.Contains(Last)) { Points -= 3; } if (!IsJap && (PontuationList).Contains(Last)) { Points -= 3; } } catch { } try { char First = (LineQuotes == null ? Str.First() : Str.TrimEnd(LineQuotes.Value.Start).First()); if (IsJap && PontuationJapList.Contains(First)) { Points -= 3; } if (!IsJap && (PontuationList).Contains(First)) { Points -= 3; } } catch { } if (!IsJap) { foreach (string Word in Words) { int WNumbers = Word.Where(c => char.IsNumber(c)).Count(); int WLetters = Word.Where(c => char.IsLetter(c)).Count(); if (WLetters > 0 && WNumbers > 0) { Points += 2; } if (Word.Trim(PontuationList).Where(c => PontuationList.Contains(c)).Count() != 0) { Points += 2; } } } if (!BeginQuote && !char.IsLetter(Str.First())) { Points += 2; } if (Specials > WordCount) { Points++; } if (Specials > Latim + JapChars) { Points += 2; } if (SpecialsStranges > 0) { Points += 2; } if (SpecialsStranges > 3) { Points++; } if ((Pontuations == 0) && (WordCount <= 2) && !IsJap) { Points++; } if (Uppers > Pontuations + 2 && !IsCaps) { Points++; } if (Spaces > WordCount * 2) { Points++; } if (Uppers > Spaces + 1 && !IsCaps) { Points++; } if (IsJap && Spaces == 0) { Points--; } if (!IsJap && Spaces == 0) { Points += 2; } if (WordCount <= 2 && Numbers != 0 && !AllowNumbers) { Points += (int)(PercentOf(Str, Numbers) / 10); } if (Str.Length <= 3 && !IsJap) { Points++; } if (Numbers >= (IsJap ? Kanjis + JapChars : Latim)) { Points += 3; } if (IsJap && Kanjis / 2 > JapChars) { Points--; } if (IsJap && JapChars > Kanjis) { Points--; } if (IsJap && Latim != 0) { Points += (int)(PercentOf(Str, Latim) / 10) + 2; } if (IsJap && NumbersJap != 0) { Points += (int)(PercentOf(Str, NumbersJap) / 10) + 2; } if (IsJap && Numbers != 0) { Points += (int)(PercentOf(Str, Numbers) / 10) + 3; } if (IsJap && Pontuations != 0) { Points += (int)(PercentOf(Str, Pontuations) / 10) + 2; } if (Str.Trim() == string.Empty) { return(false); } if (Str.Trim().Trim(Str.Trim().First()) == string.Empty) { Points += 2; } if (IsJap != FromAsian) { return(false); } bool Result = Points < (Caution ?? Sensitivity); return(Result); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); return(false); } }
/// <summary> /// Check if the string looks a dialog line /// </summary> /// <param name="Str">The String</param> /// <param name="Trim">Internal Parameter, don't change it.</param> /// <returns>If looks a dialog, return true, else return false.</returns> public static bool IsDialog(this string String) { if (string.IsNullOrWhiteSpace(String)) { return(false); } if (UseDatabase && ContainsKey(String)) { return(true); } if (!DialogCheck) { return(true); } string Str = String.Trim(); if (ForceTrim) { Str = TrimString(Str, true); } foreach (string Ignore in IgnoreList) { if (!string.IsNullOrEmpty(Ignore)) { Str = Str.Replace(Ignore, ""); } } foreach (string Deny in DenyList) { if (!string.IsNullOrEmpty(Deny) && Str.ToLower().Contains(Deny.ToLower())) { return(false); } } Str = Str.Replace(GameLineBreaker, "\n"); if (string.IsNullOrWhiteSpace(Str)) { return(false); } string[] Words = Str.Split(' '); char[] PontuationJapList = new char[] { '。', '?', '!', '…', '、', '―' }; char[] SpecialList = new char[] { '_', '=', '+', '#', ':', '$', '@' }; char[] PontuationList = new char[] { '.', '?', '!', '…', ',' }; int Spaces = Str.Where(x => x == ' ' || x == '\t').Count(); int Pontuations = Str.Where(x => PontuationList.Contains(x)).Count(); int WordCount = Words.Where(x => x.Length >= 2 && !string.IsNullOrWhiteSpace(x)).Count(); int Specials = Str.Where(x => char.IsSymbol(x)).Count(); Specials += Str.Where(x => char.IsPunctuation(x)).Count() - Pontuations; int SpecialsStranges = Str.Where(x => SpecialList.Contains(x)).Count(); int Uppers = Str.Where(x => char.IsUpper(x)).Count(); int Latim = Str.Where(x => x >= 'A' && x <= 'z').Count(); int Numbers = Str.Where(x => x >= '0' && x <= '9').Count(); int NumbersJap = Str.Where(x => x >= '0' && x <= '9').Count(); int JapChars = Str.Where(x => (x >= '、' && x <= 'ヿ') || (x >= '。' && x <= 'ン')).Count(); int Kanjis = Str.Where(x => x >= '一' && x <= '龯').Count(); bool IsCaps = GetLineCase(Str) == Case.Upper; bool IsJap = JapChars + Kanjis > Latim / 2; //More Points = Don't Looks a Dialogue //Less Points = Looks a Dialogue int Points = 0; if (Str.Length > 4) { string ext = Str.Substring(Str.Length - 4, 4); try { if (System.IO.Path.GetExtension(ext).Trim('.').Length == 3) { Points += 2; } } catch { } } bool BeginQuote = false; Quote?LineQuotes = null; foreach (Quote Quote in QuoteList) { BeginQuote |= Str.StartsWith(Quote.Start.ToString()); if (Str.StartsWith(Quote.Start.ToString()) && Str.EndsWith(Quote.End.ToString())) { Points -= 3; LineQuotes = Quote; break; } else if (Str.StartsWith(Quote.Start.ToString()) || Str.EndsWith(Quote.End.ToString())) { Points--; LineQuotes = Quote; break; } } try { char Last = (LineQuotes == null ? Str.Last() : Str.TrimEnd(LineQuotes.Value.End).Last()); if (IsJap && PontuationJapList.Contains(Last)) { Points -= 3; } if (!IsJap && (PontuationList).Contains(Last)) { Points -= 3; } } catch { } try { char First = (LineQuotes == null ? Str.First() : Str.TrimEnd(LineQuotes.Value.Start).First()); if (IsJap && PontuationJapList.Contains(First)) { Points -= 3; } if (!IsJap && (PontuationList).Contains(First)) { Points -= 3; } } catch { } if (!IsJap) { foreach (string Word in Words) { int WNumbers = Word.Where(c => char.IsNumber(c)).Count(); int WLetters = Word.Where(c => char.IsLetter(c)).Count(); if (WLetters > 0 && WNumbers > 0) { Points += 2; } if (Word.Trim(PontuationList).Where(c => PontuationList.Contains(c)).Count() != 0) { Points += 2; } } } if (!BeginQuote && !char.IsLetter(Str.First())) { Points += 2; } if (Specials > WordCount) { Points++; } if (Specials > Latim + JapChars) { Points += 2; } if (SpecialsStranges > 0) { Points += 2; } if (SpecialsStranges > 3) { Points++; } if ((Pontuations == 0) && (WordCount <= 2) && !IsJap) { Points++; } if (Uppers > Pontuations + 2 && !IsCaps) { Points++; } if (Spaces > WordCount * 2) { Points++; } if (IsJap && Spaces == 0) { Points--; } if (!IsJap && Spaces == 0) { Points += 2; } if (WordCount <= 2 && Numbers != 0) { Points += (int)(Str.PercentOf(Numbers) / 10); } if (Str.Length <= 3 && !IsJap) { Points++; } if (Numbers >= Str.Length) { Points += 3; } if (IsJap && Kanjis / 2 > JapChars) { Points--; } if (IsJap && JapChars > Kanjis) { Points--; } if (IsJap && Latim != 0) { Points += (int)(Str.PercentOf(Latim) / 10) + 2; } if (IsJap && NumbersJap != 0) { Points += (int)(Str.PercentOf(NumbersJap) / 10) + 2; } if (IsJap && Numbers != 0) { Points += (int)(Str.PercentOf(Numbers) / 10) + 3; } if (IsJap && Pontuations != 0) { Points += (int)(Str.PercentOf(Pontuations) / 10) + 2; } if (Str.Trim() == string.Empty) { return(false); } if (Str.Trim().Trim(Str.Trim().First()) == string.Empty) { Points += 2; } if (IsJap != AsianInput) { return(false); } bool Result = Points < Sensitivity; return(Result); }
public static bool IsDialogue(this string String, int? Caution = null) { try { if (string.IsNullOrWhiteSpace(String)) return false; if (Program.ForceDialogues.ContainsKey(String)) return Program.ForceDialogues[String]; if (Program.FilterSettings.UseDB && Program.Cache.ContainsKey(String)) return true; string[] DenyList = Program.FilterSettings.DenyList.Unescape().Split('\n').Where(x => !string.IsNullOrEmpty(x)).ToArray(); string[] IgnoreList = Program.FilterSettings.IgnoreList.Unescape().Split('\n').Where(x => !string.IsNullOrEmpty(x)).ToArray(); Quote[] Quotes = Program.FilterSettings.QuoteList.Unescape().Split('\n') .Where(x => x.Length == 2) .Select(x => { return new Quote() { Start = x[0], End = x[1] }; }).ToArray(); string Str = String.Trim(); foreach (string Ignore in IgnoreList) Str = Str.Replace(Ignore, ""); if (!VerifingDialog) foreach (var Otimizator in Program.ExternalPlugins) { try { Otimizator.BeforeTranslate(ref Str, uint.MaxValue); } catch { } } VerifingDialog = true; foreach (string Deny in DenyList) if (Str.ToLower().Contains(Deny.ToLower())) { VerifingDialog = false; return false; } Str = Str.Replace(Program.WordwrapSettings.LineBreaker, "\n"); if (string.IsNullOrWhiteSpace(Str)) return false; string[] Words = Str.Split(' '); char[] PontuationJapList = new char[] { '。', '?', '!', '…', '、', '―' }; char[] SpecialList = new char[] { '_', '=', '+', '#', ':', '$', '@' }; char[] PontuationList = new char[] { '.', '?', '!', '…', ',' }; int Spaces = Str.Where(x => x == ' ' || x == '\t').Count(); int Pontuations = Str.Where(x => PontuationList.Contains(x)).Count(); int WordCount = Words.Where(x => x.Length >= 2 && !string.IsNullOrWhiteSpace(x)).Count(); int Specials = Str.Where(x => char.IsSymbol(x)).Count(); Specials += Str.Where(x => char.IsPunctuation(x)).Count() - Pontuations; int SpecialsStranges = Str.Where(x => SpecialList.Contains(x)).Count(); int Uppers = Str.Where(x => char.IsUpper(x)).Count(); int Latim = Str.Where(x => x >= 'A' && x <= 'z').Count(); int Numbers = Str.Where(x => x >= '0' && x <= '9').Count(); int NumbersJap = Str.Where(x => x >= '0' && x <= '9').Count(); int JapChars = Str.Where(x => (x >= '、' && x <= 'ヿ') || (x >= '。' && x <= 'ン')).Count(); int Kanjis = Str.Where(x => x >= '一' && x <= '龯').Count(); bool IsCaps = Optimizator.CaseFixer.GetLineCase(Str) == Optimizator.CaseFixer.Case.Upper; bool IsJap = JapChars + Kanjis > Latim / 2; //More Points = Don't Looks a Dialogue //Less Points = Looks a Dialogue int Points = 0; if (Str.Length > 4) { string ext = Str.Substring(Str.Length - 4, 4); try { if (System.IO.Path.GetExtension(ext).Trim('.').Length == 3) Points += 2; } catch { } } bool BeginQuote = false; Quote? LineQuotes = null; foreach (Quote Quote in Quotes) { BeginQuote |= Str.StartsWith(Quote.Start.ToString()); if (Str.StartsWith(Quote.Start.ToString()) && Str.EndsWith(Quote.End.ToString())) { Points -= 3; LineQuotes = Quote; break; } else if (Str.StartsWith(Quote.Start.ToString()) || Str.EndsWith(Quote.End.ToString())) { Points--; LineQuotes = Quote; break; } } try { char Last = (LineQuotes == null ? Str.Last() : Str.TrimEnd(LineQuotes?.End ?? ' ').Last()); if (IsJap && PontuationJapList.Contains(Last)) Points -= 3; if (!IsJap && (PontuationList).Contains(Last)) Points -= 3; } catch { } try { char First = (LineQuotes == null ? Str.First() : Str.TrimEnd(LineQuotes?.Start ?? ' ').First()); if (IsJap && PontuationJapList.Contains(First)) Points -= 3; if (!IsJap && (PontuationList).Contains(First)) Points -= 3; } catch { } if (!IsJap) { foreach (string Word in Words) { int WNumbers = Word.Where(c => char.IsNumber(c)).Count(); int WLetters = Word.Where(c => char.IsLetter(c)).Count(); if (WLetters > 0 && WNumbers > 0) { Points += 2; } if (Word.Trim(PontuationList).Where(c => PontuationList.Contains(c)).Count() != 0) { Points += 2; } } } if (!BeginQuote && !char.IsLetter(Str.First())) Points += 2; if (Specials > WordCount) Points++; if (Specials > Latim + JapChars) Points += 2; if (SpecialsStranges > 0) Points += 2; if (SpecialsStranges > 3) Points++; if ((Pontuations == 0) && (WordCount <= 2) && !IsJap) Points++; if (Uppers > Pontuations + 2 && !IsCaps) Points++; if (Spaces > WordCount * 2) Points++; if (IsJap && Spaces == 0) Points--; if (!IsJap && Spaces == 0) Points += 2; if (WordCount <= 2 && Numbers != 0) Points += (int)(Str.PercentOf(Numbers) / 10); if (Str.Length <= 3 && !IsJap) Points++; if (Numbers >= Str.Length) Points += 3; if (!IsJap && WordCount == 1 && char.IsUpper(Str.First()) && !char.IsPunctuation(Str.TrimEnd().Last())) Points++; if (!IsJap && WordCount == 1 && !char.IsUpper(Str.First())) Points++; if (Words.Where(x => x.Where(y => char.IsUpper(y)).Count() > 1 && x.Where(y => char.IsLower(y)).Count() > 1).Any()) Points += 2; if (!IsJap && char.IsUpper(Str.TrimStart().First()) && char.IsPunctuation(Str.TrimEnd().Last())) Points--; if (!char.IsPunctuation(Str.TrimEnd().Last())) Points++; if (IsJap && Kanjis / 2 > JapChars) Points--; if (IsJap && JapChars > Kanjis) Points--; if (IsJap && Latim != 0) Points += (int)(Str.PercentOf(Latim) / 10) + 2; if (IsJap && NumbersJap != 0) Points += (int)(Str.PercentOf(NumbersJap) / 10) + 2; if (IsJap && Numbers != 0) Points += (int)(Str.PercentOf(Numbers) / 10) + 3; if (IsJap && Pontuations != 0) Points += (int)(Str.PercentOf(Pontuations) / 10) + 2; if (Str.Trim() == string.Empty) return false; if (Str.Trim().Trim(Str.Trim().First()) == string.Empty) Points += 2; if (IsJap != Program.FromAsian) return false; VerifingDialog = false; bool Result = Points < (Caution ?? Program.FilterSettings.Sensitivity); return Result; } catch (Exception ex){ #if DEBUG throw ex; #else return false; #endif } }