/// <summary> /// Displays a form that promts user for disambiguation /// if no disambiguation needed, immediately returns /// </summary> /// <param name="articleText">The wiki text of the article.</param> /// <param name="articleTitle">Title of the article</param> /// <param name="dabLink">link to be disambiguated</param> /// <param name="dabVariants">variants of disambiguation</param> /// <param name="contextChars">number of chars each side from link in the context box</param> /// <param name="botMode">whether AWB saves pages automatically</param> /// <param name="skip">returns true when no disambiguation made</param> /// <returns></returns> public string Disambiguate(string articleText, string articleTitle, string dabLink, string[] dabVariants, int contextChars, bool botMode, out bool skip) { Variants.Clear(); Dabs.Clear(); skip = true; foreach (string s in dabVariants) { if (s.Trim().Length > 0) { Variants.Add(s.Trim()); } } if (!Variants.Any()) { return(articleText); } BotMode = botMode; if (dabLink.Contains("|")) { string sum = dabLink.Split(new[] { '|' }) .Where(s => s.Trim().Length != 0) .Aggregate("", (current, s) => current + ("|" + Tools.CaseInsensitive(Regex.Escape(s.Trim())))); if (sum.Length > 0 && sum[0] == '|') { sum = sum.Remove(0, 1); } if (sum.Contains("|")) { sum = "(?:" + sum + ")"; } dabLink = sum; } else { dabLink = Tools.CaseInsensitive(Regex.Escape(dabLink.Trim())); } string newText = articleText; ArticleTitle = articleTitle; Search = new Regex(@"\[\[\s*(" + dabLink + @")\s*(?:|#[^\|\]]*)(|\|[^\]]*)\]\]([\p{Ll}\p{Lu}\p{Lt}\p{Pc}\p{Lm}]*)"); MatchCollection matches = Search.Matches(articleText); if (matches.Count == 0) { return(articleText); } foreach (Match m in matches) { DabControl c = new DabControl(articleText, m, Variants, contextChars); c.Changed += OnUserInput; tableLayout.Controls.Add(c); Dabs.Add(c); } switch (ShowDialog(Variables.MainForm as Form)) { case DialogResult.OK: break; // proceed further case DialogResult.Abort: Abort = true; goto default; default: //DialogResult.Cancel return(articleText); } // https://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Bugs/Archive_22#Errors_disambiguating.2C_possibly_due_to_link_occurring_more_than_once_in_a_line // to perform replace perform each replace via regex, take user's chosen link from the matching dab by index int a = 0; bool dnPunctuation = false; newText = Search.Replace(newText, m2 => { string res = Dabs[a].NoChange ? m2.Value : Dabs[a].Result; if (res.Contains(@"{{Disambiguation needed}}")) { dnPunctuation = true; } a++; return(res); }); if (!newText.Equals(articleText)) { skip = false; } // want ''[[link]]''{{Disambiguation needed}} rather than ''[[link]]{{Disambiguation needed}}'' if (dnPunctuation) { newText = DnPunctuationR.Replace(newText, "$1$3$2"); } return(Parse.Parsers.StickyLinks(newText)); }
/// <summary> /// Displays a form that promts user for disambiguation /// if no disambiguation needed, immediately returns /// </summary> /// <param name="articleText">The wiki text of the article.</param> /// <param name="articleTitle">Title of the article</param> /// <param name="dabLink">link to be disambiguated</param> /// <param name="dabVariants">variants of disambiguation</param> /// <param name="contextChars">number of chars each side from link in the context box</param> /// <param name="botMode">whether AWB saves pages automatically</param> /// <param name="skip">returns true when no disambiguation made</param> /// <returns></returns> public string Disambiguate(string articleText, string articleTitle, string dabLink, string[] dabVariants, int contextChars, bool botMode, out bool skip) { Variants.Clear(); Dabs.Clear(); skip = true; foreach (string s in dabVariants) { if (s.Trim().Length > 0) Variants.Add(s.Trim()); } if (Variants.Count == 0) return articleText; BotMode = botMode; if (dabLink.Contains("|")) { string sum = dabLink.Split(new[] {'|'}) .Where(s => s.Trim().Length != 0) .Aggregate("", (current, s) => current + ("|" + Tools.CaseInsensitive(Regex.Escape(s.Trim())))); if (sum.Length > 0 && sum[0] == '|') sum = sum.Remove(0, 1); if (sum.Contains("|")) sum = "(?:" + sum + ")"; dabLink = sum; } else dabLink = Tools.CaseInsensitive(Regex.Escape(dabLink.Trim())); string newText = articleText; ArticleTitle = articleTitle; Search = new Regex(@"\[\[\s*(" + dabLink + @")\s*(?:|#[^\|\]]*)(|\|[^\]]*)\]\]([\p{Ll}\p{Lu}\p{Lt}\p{Pc}\p{Lm}]*)"); MatchCollection matches = Search.Matches(articleText); if (matches.Count == 0) return articleText; foreach (Match m in matches) { DabControl c = new DabControl(articleText, m, Variants, contextChars); c.Changed += OnUserInput; tableLayout.Controls.Add(c); Dabs.Add(c); } switch (ShowDialog(Variables.MainForm as Form)) { case DialogResult.OK: break; // proceed further case DialogResult.Abort: Abort = true; goto default; default: //DialogResult.Cancel return articleText; } // https://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Bugs/Archive_22#Errors_disambiguating.2C_possibly_due_to_link_occurring_more_than_once_in_a_line // to perform replace perform each replace via regex, take user's chosen link from the matching dab by index int a = 0; bool dnPunctuation = false; newText = Search.Replace(newText, m2 => { string res = Dabs[a].NoChange ? m2.Value : Dabs[a].Result; if (res.Contains(@"{{Disambiguation needed}}")) dnPunctuation = true; a++; return res; }); if (!newText.Equals(articleText)) skip = false; // want ''[[link]]''{{Disambiguation needed}} rather than ''[[link]]{{Disambiguation needed}}'' if (dnPunctuation) newText = DnPunctuationR.Replace(newText, "$1$3$2"); return Parse.Parsers.StickyLinks(newText); }
/// <summary> /// displays form that promts user for disambiguation /// if no disambihuation needed, immediately returns /// </summary> /// <param name="ArticleText">The wiki text of the article.</param> /// <param name="articleTitle"></param> /// <param name="dabLink">link to be disambiguated</param> /// <param name="dabVariants">variants of disambiguation</param> /// <param name="contextChars">number of chars each side from link in the context box</param> /// <param name="botMode">whether AWB saves pages automatically</param> /// <param name="Skip">returns true when no disambiguation made</param> /// <returns></returns> public string Disambiguate(string articleText, string articleTitle, string dabLink, string[] dabVariants, int contextChars, bool botMode, out bool skip) { skip = true; BotMode = botMode; if (dabLink.Contains("|")) { string sum = ""; foreach (string s in dabLink.Split(new char[] { '|' })) { if (s.Trim().Length == 0) continue; sum += "|" + Tools.CaseInsensitive(Regex.Escape(s.Trim())); } if (sum.Length > 0 && sum[0] == '|') sum = sum.Remove(0, 1); if (sum.Contains("|")) sum = "(?:" + sum + ")"; dabLink = sum; } else dabLink = Tools.CaseInsensitive(Regex.Escape(dabLink.Trim())); ArticleText = articleText; ArticleTitle = articleTitle; foreach (string s in dabVariants) { if (s.Trim().Length == 0) continue; Variants.Add(s.Trim()); } if (Variants.Count == 0) return articleText; Search = new Regex(@"\[\[\s*(" + dabLink + @")\s*(?:|#[^\|\]]*)(|\|[^\]]*)\]\]([\p{Ll}\p{Lu}\p{Lt}\p{Pc}\p{Lm}]*)"); Matches = Search.Matches(articleText); if (Matches.Count == 0) return articleText; foreach (Match m in Matches) { DabControl c = new DabControl(articleText, dabLink, m, Variants, contextChars); c.Changed += new EventHandler(OnUserInput); tableLayout.Controls.Add(c); Dabs.Add(c); } DialogResult r = ShowDialog(Variables.MainForm as Form); switch (r) { case DialogResult.OK: break; // proceed further case DialogResult.Abort: Abort = true; return articleText; case DialogResult.Cancel: skip = true; return articleText; //break; default: return articleText; } int adjust = 0; foreach (DabControl d in Dabs) { int start, end1, end2; for (start = 0; (start < Math.Min(d.Surroundings.Length, d.Result.Length)) && (d.Result[start] == d.Surroundings[start]); start++); end1 = d.Surroundings.Length - 1; end2 = d.Result.Length - 1; while ((end1 > start) && (end2 > start) && (d.Result[end2] == d.Surroundings[end1])) { end1--; end2--; } ArticleText = Tools.ReplacePartOfString(ArticleText, d.SurroundingsStart + start + adjust, end1 - start + 1, d.Result.Substring(start, end2 - start + 1)); adjust += d.Result.Length - d.Surroundings.Length; //ArticleText = Tools.ReplacePartOfString(ArticleText, d.SurroundingsStart + adjust, // d.Surroundings.Length, d.Result); //adjust += d.Result.Length - d.Surroundings.Length; } if (ArticleText != articleText) skip = false; return ArticleText; }
/// <summary> /// Displays a form that promts user for disambiguation /// if no disambiguation needed, immediately returns /// </summary> /// <param name="articleText">The wiki text of the article.</param> /// <param name="articleTitle">Title of the article</param> /// <param name="dabLink">link to be disambiguated</param> /// <param name="dabVariants">variants of disambiguation</param> /// <param name="contextChars">number of chars each side from link in the context box</param> /// <param name="botMode">whether AWB saves pages automatically</param> /// <param name="skip">returns true when no disambiguation made</param> /// <returns></returns> public string Disambiguate(string articleText, string articleTitle, string dabLink, string[] dabVariants, int contextChars, bool botMode, out bool skip) { Variants.Clear(); Dabs.Clear(); skip = true; foreach (string s in dabVariants) { if (s.Trim().Length == 0) { continue; } Variants.Add(s.Trim()); } if (Variants.Count == 0) { return(articleText); } BotMode = botMode; if (dabLink.Contains("|")) { string sum = ""; foreach (string s in dabLink.Split(new[] { '|' })) { if (s.Trim().Length == 0) { continue; } sum += "|" + Tools.CaseInsensitive(Regex.Escape(s.Trim())); } if (sum.Length > 0 && sum[0] == '|') { sum = sum.Remove(0, 1); } if (sum.Contains("|")) { sum = "(?:" + sum + ")"; } dabLink = sum; } else { dabLink = Tools.CaseInsensitive(Regex.Escape(dabLink.Trim())); } string newText = articleText; ArticleTitle = articleTitle; Search = new Regex(@"\[\[\s*(" + dabLink + @")\s*(?:|#[^\|\]]*)(|\|[^\]]*)\]\]([\p{Ll}\p{Lu}\p{Lt}\p{Pc}\p{Lm}]*)"); MatchCollection matches = Search.Matches(articleText); if (matches.Count == 0) { return(articleText); } foreach (Match m in matches) { DabControl c = new DabControl(articleText, m, Variants, contextChars); c.Changed += OnUserInput; tableLayout.Controls.Add(c); Dabs.Add(c); } switch (ShowDialog(Variables.MainForm as Form)) { case DialogResult.OK: break; // proceed further case DialogResult.Abort: Abort = true; goto default; default: //DialogResult.Cancel return(articleText); } int adjust = 0; foreach (DabControl d in Dabs) { if (d.NoChange) { continue; } int start; for (start = 0; (start < Math.Min(d.Surroundings.Length, d.Result.Length)) && (d.Result[start] == d.Surroundings[start]); start++) { } int end1 = d.Surroundings.Length - 1; int end2 = d.Result.Length - 1; while ((end1 > start) && (end2 > start) && (d.Result[end2] == d.Surroundings[end1])) { end1--; end2--; } newText = Tools.ReplacePartOfString(newText, d.SurroundingsStart + start + adjust, end1 - start + 1, d.Result.Substring(start, end2 - start + 1)); adjust += d.Result.Length - d.Surroundings.Length; } if (newText != articleText) { skip = false; } return(newText); }