public MyUIElement(RPARemoteObj context) { this.context = context; node = new UiElement(SelectorStrategy.DEFAULT); node.Timeout = 30000; node.WaitForReadyLevel = WaitForReady.INTERACTIVE; var selector = context.selector; if (!string.IsNullOrEmpty(selector)) { Selector filter = new Selector(selector); FindScope scope = filter.IsTopLevel() ? FindScope.FIND_TOP_LEVELS : FindScope.FIND_DESCENDANTS; node = node.FindFirst(scope, filter); } }
// find by text - Exact public static List<Phrase> FindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string text, LanguageType language_type, string translation, TextLocation text_location, bool case_sensitive, TextWordness wordness, int multiplicity, bool at_word_start, bool with_diacritics) { List<Phrase> result = new List<Phrase>(); if (language_type == LanguageType.Arabic) { result = DoFindPhrases(book, find_scope, current_selection, previous_result, text, language_type, translation, text_location, case_sensitive, wordness, multiplicity, at_word_start, with_diacritics, true); } else if (language_type == LanguageType.Translation) { if (book.Verses != null) { if (book.Verses.Count > 0) { foreach (string key in book.Verses[0].Translations.Keys) { List<Phrase> new_phrases = DoFindPhrases(book, find_scope, current_selection, previous_result, text, language_type, key, text_location, case_sensitive, wordness, multiplicity, at_word_start, with_diacritics, false); result.AddRange(new_phrases); } } } } return result; }
// find by revelation place public static List<Chapter> FindChapters(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, RevelationPlace revelation_place) { return DoFindChapters(book, find_scope, current_selection, previous_result, revelation_place); }
private static List<Chapter> DoFindChapters(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, RevelationPlace revelation_place) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindChapters(source, revelation_place); }
// helper methods for finds public static List<Verse> GetSourceVerses(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result) { List<Verse> result = new List<Verse>(); if (book != null) { if (find_scope == FindScope.Book) { result = book.Verses; } else if (find_scope == FindScope.Selection) { result = current_selection.Verses; } else if (find_scope == FindScope.Result) { if (previous_result != null) { result = new List<Verse>(previous_result); } } } return result; }
// find by numbers - Words public static List<Word> FindWords(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, NumberQuery query) { return DoFindWords(book, find_scope, current_selection, previous_result, query); }
// find by similarity - verse similar to given verse public static List<Verse> FindVerses(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, Verse verse, SimilarityMethod similarity_method, double similarity_percentage) { return DoFindVerses(book, find_scope, current_selection, previous_result, verse, similarity_method, similarity_percentage); }
// find by text - Root public static List<Phrase> FindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string roots, int multiplicity, bool with_diacritics) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = null; while (roots.Contains(" ")) { roots = roots.Replace(" ", " "); } string[] parts = roots.Split(); if (parts.Length == 0) { return result; } else if (parts.Length == 1) { return DoFindPhrases(book, find_scope, current_selection, previous_result, roots, multiplicity, with_diacritics); } else if (parts.Length > 1) // enable nested searches { if (roots.Length > 1) // enable nested searches { List<Phrase> phrases = null; List<string> negative_words = new List<string>(); List<string> positive_words = new List<string>(); List<string> neutral_words = new List<string>(); foreach (string part in parts) { if ((part.StartsWith("-")) || (part.EndsWith("-"))) { int index = part.IndexOf("-"); negative_words.Add(part.Remove(index, 1)); } else if ((part.StartsWith("+")) || (part.EndsWith("+"))) { int index = part.IndexOf("+"); positive_words.Add(part.Remove(index, 1)); } else { neutral_words.Add(part); } } foreach (string negative_word in negative_words) { phrases = DoFindPhrases(book, find_scope, current_selection, found_verses, negative_word, 0, with_diacritics); // multiplicity = 0 for exclude AddToResult(phrases, ref found_verses, ref find_scope, ref result); } foreach (string positive_word in positive_words) { phrases = DoFindPhrases(book, find_scope, current_selection, found_verses, positive_word, multiplicity, with_diacritics); AddToResult(phrases, ref found_verses, ref find_scope, ref result); } foreach (string neutral_word in neutral_words) { phrases = DoFindPhrases(book, find_scope, current_selection, found_verses, neutral_word, multiplicity, with_diacritics); AddToResult(phrases, ref found_verses, ref find_scope, ref result); } } } return result; }
private static List<Phrase> DoFindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string word_text, double similarity_percentage) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindPhrases(source, find_scope, current_selection, previous_result, word_text, similarity_percentage); }
private static List<Phrase> DoFindPhrases(List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string root, int multiplicity, bool with_diacritics) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = new List<Verse>(); if (source != null) { if (source.Count > 0) { Book book = source[0].Book; if (root.Length > 0) { try { Dictionary<string, List<Word>> root_words_dictionary = book.RootWords; if (root_words_dictionary != null) { List<Word> root_words = null; if (root_words_dictionary.ContainsKey(root)) { // get all pre-identified root_words root_words = root_words_dictionary[root]; } else // if no such root, search for the matching root_word by its verse position and get its root and then get all root_words { string new_root = book.GetBestRoot(root, with_diacritics); if (!String.IsNullOrEmpty(new_root)) { // get all pre-identified root_words for new root root_words = root_words_dictionary[new_root]; } } if (root_words != null) { result = GetPhrasesWithRootWords(source, root_words, multiplicity, with_diacritics); foreach (Phrase phrase in result) { if (phrase != null) { if (!found_verses.Contains(phrase.Verse)) { found_verses.Add(phrase.Verse); } } } } } } catch { // log exception } } } } return result; }
private static List<Phrase> DoFindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string root, int multiplicity, bool with_diacritics) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindPhrases(source, find_scope, current_selection, previous_result, root, multiplicity, with_diacritics); }
private static List<Phrase> DoFindPhrases(string translation, List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string text, TextLocation text_location, bool case_sensitive, TextWordness wordness, int multiplicity, bool at_word_start) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = new List<Verse>(); if (source != null) { if (source.Count > 0) { Book book = source[0].Book; if (!String.IsNullOrEmpty(text)) { RegexOptions regex_options = case_sensitive ? RegexOptions.None : RegexOptions.IgnoreCase; if (text.IsArabic()) // Arabic letters in translation (Emlaaei, Urdu, Farsi, etc.) { regex_options |= RegexOptions.RightToLeft; } try { string pattern_empty_line = @"^$"; string pattern_whole_line = "(" + @"^" + text + @"$" + ")"; string pattern_any_with_prefix = "(" + @"\S+?" + text + ")"; string pattern_any_with_prefix_and_suffix = "(" + @"\S+?" + text + @"\S+?" + ")"; string pattern_any_with_suffix = "(" + text + @"\S+?" + ")"; string pattern_word_with_prefix = "(" + pattern_any_with_prefix + @"\b" + ")"; string pattern_word_with_prefix_and_suffix = "(" + pattern_any_with_prefix_and_suffix + ")"; string pattern_word_with_suffix = "(" + @"\b" + pattern_any_with_suffix + ")"; string pattern_word_with_any_fixes = "(" + pattern_word_with_prefix + "|" + pattern_word_with_prefix_and_suffix + "|" + pattern_any_with_suffix + ")"; // Whole word string pattern_whole_word_at_start = "(" + pattern_whole_line + "|" + @"^" + text + @"\b" + ")"; string pattern_whole_word_at_middle = "(" + pattern_whole_line + "|" + @"(?<!^)" + @"\b" + text + @"\b" + @"(?!$)" + ")"; string pattern_whole_word_at_end = "(" + pattern_whole_line + "|" + @"\b" + text + @"$" + ")"; string pattern_whole_word_anywhere = "(" + pattern_whole_line + "|" + @"\b" + text + @"\b" + ")"; // Part of word string pattern_part_word_at_start = "(" + @"^" + pattern_word_with_any_fixes + ")"; string pattern_part_word_at_middle = "(" + @"(?<!^)" + pattern_word_with_any_fixes + @"(?!$)" + ")"; string pattern_part_word_at_end = "(" + pattern_word_with_any_fixes + @"$" + ")"; string pattern_part_word_anywhere = "(" + pattern_part_word_at_start + "|" + pattern_part_word_at_middle + "|" + pattern_part_word_at_end + ")"; // Any == Whole word | Part of word string pattern_any_at_start = "(" + pattern_whole_line + "|" + @"^" + text + ")"; string pattern_any_at_middle = "(" + pattern_whole_line + "|" + @"(?<!^)" + text + @"(?!$)" + ")"; string pattern_any_at_end = "(" + pattern_whole_line + "|" + text + @"$" + ")"; string pattern_any_anywhere = text; string pattern = null; List<string> negative_words = new List<string>(); List<string> positive_words = new List<string>(); List<string> unsigned_words = new List<string>(); if (at_word_start) { pattern = @"(?<=\b)(" + pattern + @")"; // positive lookbehind } switch (text_location) { case TextLocation.Anywhere: { if (wordness == TextWordness.WholeWord) { pattern += pattern_whole_word_anywhere; } else if (wordness == TextWordness.PartOfWord) { pattern += pattern_part_word_anywhere; } else if (wordness == TextWordness.Any) { pattern += pattern_any_anywhere; } else { pattern += pattern_empty_line; } } break; case TextLocation.AtStart: { if (wordness == TextWordness.WholeWord) { pattern += pattern_whole_word_at_start; } else if (wordness == TextWordness.PartOfWord) { pattern += pattern_part_word_at_start; } else if (wordness == TextWordness.Any) { pattern += pattern_any_at_start; } else { pattern += pattern_empty_line; } } break; case TextLocation.AtMiddle: { if (wordness == TextWordness.WholeWord) { pattern += pattern_whole_word_at_middle; } else if (wordness == TextWordness.PartOfWord) { pattern += pattern_part_word_at_middle; } else if (wordness == TextWordness.Any) { pattern += pattern_any_at_middle; } else { pattern += pattern_empty_line; } } break; case TextLocation.AtEnd: { if (wordness == TextWordness.WholeWord) { pattern += pattern_whole_word_at_end; } else if (wordness == TextWordness.PartOfWord) { pattern += pattern_part_word_at_end; } else if (wordness == TextWordness.Any) { pattern += pattern_any_at_end; } else { pattern += pattern_empty_line; } } break; case TextLocation.AllWords: case TextLocation.AnyWord: { pattern = Regex.Replace(text.Trim(), @"\s+", " "); // remove double space or higher if any string[] pattern_words = pattern.Split(); foreach (string pattern_word in pattern_words) { if (pattern_word.StartsWith("-")) { negative_words.Add(pattern_word.Substring(1)); } else if (pattern_word.EndsWith("-")) { negative_words.Add(pattern_word.Substring(0, pattern_word.Length - 1)); } else if (pattern_word.StartsWith("+")) { positive_words.Add(pattern_word.Substring(1)); } else if (pattern_word.EndsWith("+")) { positive_words.Add(pattern_word.Substring(0, pattern_word.Length - 1)); } else { unsigned_words.Add(pattern_word); } } } break; default: { return new List<Phrase>(); } } // do actual search foreach (Verse verse in source) { if (text_location == TextLocation.AllWords) { bool found = false; foreach (string negative_word in negative_words) { if (verse.Translations[translation].Contains(negative_word)) { found = true; break; } } if (found) continue; foreach (string positive_word in positive_words) { if (!verse.Translations[translation].Contains(positive_word)) { found = true; break; } } if (found) continue; if ( (unsigned_words.Count == 0) || (verse.Translations[translation].ContainsWordsOf(unsigned_words)) ) { found_verses.Add(verse); result.Add(new Phrase(verse, 0, "")); } } else if (text_location == TextLocation.AnyWord) { bool skip = false; foreach (string negative_word in negative_words) { if (verse.Translations[translation].Contains(negative_word)) { skip = true; break; } } if (skip) continue; foreach (string positive_word in positive_words) { if (!verse.Translations[translation].Contains(positive_word)) { skip = true; break; } } if (skip) continue; if ( (negative_words.Count > 0) || (positive_words.Count > 0) || ( (unsigned_words.Count == 0) || (verse.Translations[translation].ContainsWordOf(unsigned_words)) ) ) { found_verses.Add(verse); result.Add(new Phrase(verse, 0, "")); } } else // at start, middle, end, or anywhere { MatchCollection matches = Regex.Matches(verse.Translations[translation], pattern, regex_options); if (multiplicity != -1) // with multiplicity { if (matches.Count >= multiplicity) { found_verses.Add(verse); if (matches.Count > 0) { result.AddRange(BuildPhrasesAndOriginify(verse, matches)); } else { result.Add(new Phrase(verse, 0, "")); } } } else // without multiplicity { if (matches.Count > 0) { found_verses.Add(verse); result.AddRange(BuildPhrasesAndOriginify(verse, matches)); } } } } // end for } catch { // log exception } } } } return result; }
private static List<Phrase> DoFindPhrases(List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string text, TextLocation text_location, TextWordness wordness, int multiplicity, bool at_word_start, bool with_diacritics, bool try_emlaaei_if_nothing_found) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = new List<Verse>(); if (source != null) { if (source.Count > 0) { Book book = source[0].Book; if (!String.IsNullOrEmpty(text)) { RegexOptions regex_options = RegexOptions.IgnoreCase | RegexOptions.RightToLeft; string pattern = null; List<string> unsigned_words = null; List<string> positive_words = null; List<string> negative_words = null; try { if (with_diacritics) { // search in original text first (without simplification) pattern = BuildPattern(text, text_location, wordness, at_word_start, out unsigned_words, out positive_words, out negative_words); if (!String.IsNullOrEmpty(pattern)) { foreach (Verse verse in source) { ///////////////////////// // process negative_words ///////////////////////// if (negative_words.Count > 0) { bool found = false; foreach (string negative_word in negative_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(negative_word)) { found = true; // next verse break; } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(negative_word)) && (word_text.Length > negative_word.Length)) { found = true; // next verse break; } } else if (wordness == TextWordness.WholeWord) { if (word_text == negative_word) { found = true; // next verse break; } } } if (found) { break; } } if (found) continue; // next verse } ///////////////////////// // process positive_words ///////////////////////// if (positive_words.Count > 0) { int matches = 0; foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { matches++; break; // next positive_word } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { matches++; break; // next positive_word } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { matches++; break; // next positive_word } } } } // verse failed test, so skip it if (matches < positive_words.Count) { continue; // next verse } } ////////////////////////////////////////////////////// // both negative and positive conditions have been met ////////////////////////////////////////////////////// ///////////////////////// // process unsigned_words ///////////////////////// ////////////////////////////////////////////////////////// // FindByText WORDS All ////////////////////////////////////////////////////////// if (text_location == TextLocation.AllWords) { int matches = 0; foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { matches++; break; // no need to continue even if there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { matches++; break; // no need to continue even if there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { matches++; break; // no need to continue even if there are more matches } } } } if (matches == unsigned_words.Count) { /////////////////////////////////////////////////////////////// // all negative, positive and unsigned conditions have been met /////////////////////////////////////////////////////////////// // add positive matches foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } // add unsigned matches foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } } else // verse failed test, so skip it { continue; // next verse } } ////////////////////////////////////////////////////////// // FindByText WORDS Any ////////////////////////////////////////////////////////// else if (text_location == TextLocation.AnyWord) { bool found = false; foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found = true; break; // next unsigned_word } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found = true; break; // next unsigned_word } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found = true; break; // next unsigned_word } } } if (found) { break; } } if (found) // found 1 unsigned word in verse, which is enough { /////////////////////////////////////////////////////////////// // all negative, positive and unsigned conditions have been met /////////////////////////////////////////////////////////////// // add positive matches foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } // add unsigned matches foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { string word_text = word.Text; if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } } else // verse failed test, so skip it { continue; // next verse } } ////////////////////////////////////////////////////////// // FindByText EXACT ////////////////////////////////////////////////////////// else // at start, middle, end, or anywhere { string verse_text = verse.Text; //??? whole_word still needs verification in border cases in all text_modes MatchCollection matches = Regex.Matches(verse_text, pattern, regex_options); if (multiplicity != -1) // with multiplicity { if (matches.Count >= multiplicity) { found_verses.Add(verse); if (matches.Count > 0) { result.AddRange(BuildPhrases(verse, matches)); } else { result.Add(new Phrase(verse, 0, "")); } } } else // without multiplicity { if (matches.Count > 0) { found_verses.Add(verse); result.AddRange(BuildPhrases(verse, matches)); } } } } // end for } } //DON'T use else{} in case with_diacritics didn't find any result, so try simplified text first before emlaaei text if (result.Count == 0) { // simplify all text_modes (Original will be simplified29 automatically) text = text.SimplifyTo(s_numerology_system.TextMode); if (!String.IsNullOrEmpty(text)) // re-test in case text was just harakaat which is simplifed to nothing { pattern = BuildPattern(text, text_location, wordness, at_word_start, out unsigned_words, out positive_words, out negative_words); if (!String.IsNullOrEmpty(pattern)) { foreach (Verse verse in source) { ///////////////////////// // process negative_words ///////////////////////// if (negative_words.Count > 0) { bool found = false; foreach (string negative_word in negative_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(negative_word)) { found = true; // next verse break; } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(negative_word)) && (word_text.Length > negative_word.Length)) { found = true; // next verse break; } } else if (wordness == TextWordness.WholeWord) { if (word_text == negative_word) { found = true; // next verse break; } } } if (found) { break; } } if (found) continue; // next verse } ///////////////////////// // process positive_words ///////////////////////// if (positive_words.Count > 0) { int matches = 0; foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { matches++; break; // next positive_word } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { matches++; break; // next positive_word } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { matches++; break; // next positive_word } } } } // verse failed test, so skip it if (matches < positive_words.Count) { continue; // next verse } } ////////////////////////////////////////////////////// // both negative and positive conditions have been met ////////////////////////////////////////////////////// ///////////////////////// // process unsigned_words ///////////////////////// ////////////////////////////////////////////////////////// // FindByText WORDS All ////////////////////////////////////////////////////////// if (text_location == TextLocation.AllWords) { int matches = 0; foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { matches++; break; // no need to continue even if there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { matches++; break; // no need to continue even if there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { matches++; break; // no need to continue even if there are more matches } } } } if (matches == unsigned_words.Count) { /////////////////////////////////////////////////////////////// // all negative, positive and unsigned conditions have been met /////////////////////////////////////////////////////////////// // add positive matches foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } // add unsigned matches foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } } else // verse failed test, so skip it { continue; // next verse } } ////////////////////////////////////////////////////////// // FindByText WORDS Any ////////////////////////////////////////////////////////// else if (text_location == TextLocation.AnyWord) { bool found = false; foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found = true; break; // next unsigned_word } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found = true; break; // next unsigned_word } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found = true; break; // next unsigned_word } } } if (found) { break; } } if (found) // found 1 unsigned word in verse, which is enough { /////////////////////////////////////////////////////////////// // all negative, positive and unsigned conditions have been met /////////////////////////////////////////////////////////////// // add positive matches foreach (string positive_word in positive_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(positive_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(positive_word)) && (word_text.Length > positive_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == positive_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } // add unsigned matches foreach (string unsigned_word in unsigned_words) { foreach (Word word in verse.Words) { // simplify all text_modes (Original will be simplified29 automatically) string word_text = word.Text.SimplifyTo(s_numerology_system.TextMode); if (wordness == TextWordness.Any) { if (word_text.Contains(unsigned_word)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.PartOfWord) { if ((word_text.Contains(unsigned_word)) && (word_text.Length > unsigned_word.Length)) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } else if (wordness == TextWordness.WholeWord) { if (word_text == unsigned_word) { found_verses.Add(verse); result.Add(new Phrase(verse, word.Position, word.Text)); //break; // no break in case there are more matches } } } } } else // verse failed test, so skip it { continue; // next verse } } ////////////////////////////////////////////////////////// // FindByText EXACT ////////////////////////////////////////////////////////// else // at start, middle, end, or anywhere { // simplify all text_modes (Original will be simplified29 automatically) string verse_text = verse.Text.SimplifyTo(s_numerology_system.TextMode); MatchCollection matches = Regex.Matches(verse_text, pattern, regex_options); if (multiplicity != -1) // with multiplicity { if (matches.Count >= multiplicity) { found_verses.Add(verse); if (matches.Count > 0) { result.AddRange(BuildPhrasesAndOriginify(verse, matches)); } else { result.Add(new Phrase(verse, 0, "")); } } } else // without multiplicity { if (matches.Count > 0) { found_verses.Add(verse); result.AddRange(BuildPhrasesAndOriginify(verse, matches)); } } } } // end for } } } // if nothing found if ((multiplicity != 0) && (result.Count == 0)) { // search in emlaaei if (try_emlaaei_if_nothing_found) { // always simplify29 for emlaaei comparison pattern = pattern.Simplify29(); pattern = pattern.Trim(); while (pattern.Contains(" ")) { pattern = pattern.Replace(" ", " "); } if ((source != null) && (source.Count > 0)) { foreach (Verse verse in source) { // always simplify29 for emlaaei comparison string simplified_emlaaei_text = verse.Translations[DEFAULT_EMLAAEI_TEXT].Simplify29(); simplified_emlaaei_text = simplified_emlaaei_text.Trim(); while (simplified_emlaaei_text.Contains(" ")) { simplified_emlaaei_text = simplified_emlaaei_text.Replace(" ", " "); } if (text_location == TextLocation.AllWords) { bool found = false; foreach (string pattern_word in negative_words) { if (simplified_emlaaei_text.Contains(pattern_word)) { found = true; break; } } if (found) continue; foreach (string pattern_word in positive_words) { if (!simplified_emlaaei_text.Contains(pattern_word)) { found = true; break; } } if (found) continue; if ( (unsigned_words.Count == 0) || (simplified_emlaaei_text.ContainsWordsOf(unsigned_words)) ) { result.Add(new Phrase(verse, 0, "")); } } else if (text_location == TextLocation.AnyWord) { bool found = false; foreach (string pattern_word in negative_words) { if (simplified_emlaaei_text.Contains(pattern_word)) { found = true; break; } } if (found) continue; foreach (string pattern_word in positive_words) { if (!simplified_emlaaei_text.Contains(pattern_word)) { found = true; break; } } if (found) continue; if ( (negative_words.Count > 0) || (positive_words.Count > 0) || ( (unsigned_words.Count == 0) || (simplified_emlaaei_text.ContainsWordOf(unsigned_words)) ) ) { result.Add(new Phrase(verse, 0, "")); } } else // at start, middle, end, or anywhere { MatchCollection matches = Regex.Matches(simplified_emlaaei_text, pattern, regex_options); if (multiplicity != -1) // with multiplicity { if (matches.Count >= multiplicity) { // don't colorize emaleei matches to let user know this is unofficial spelling //result.AddRange(BuildPhrases(verse, matches)); result.Add(new Phrase(verse, 0, "")); } } else // without multiplicity { if (matches.Count > 0) { // don't colorize emaleei matches to let user know this is unofficial spelling //result.AddRange(BuildPhrases(verse, matches)); result.Add(new Phrase(verse, 0, "")); } } } } // end for } } } } catch { // log exception } } } } return result; }
private static List<Phrase> DoFindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string text, LanguageType language_type, string translation, TextLocation text_location, bool case_sensitive, TextWordness wordness, int multiplicity, bool at_word_start, bool with_diacritics, bool try_emlaaei_if_nothing_found) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); if (language_type == LanguageType.Arabic) { return DoFindPhrases(source, find_scope, current_selection, previous_result, text, text_location, wordness, multiplicity, at_word_start, with_diacritics, try_emlaaei_if_nothing_found); } else //if (language_type == FindByTextLanguageType.Translation) { return DoFindPhrases(translation, source, find_scope, current_selection, previous_result, text, text_location, case_sensitive, wordness, multiplicity, at_word_start); } }
private static List<Word> DoFindWords(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, NumberQuery query) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindWords(source, query); }
private static List<Word> DoFindWords(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string phrase, int letter_frequency_sum, FrequencySumType frequency_sum_type) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindWords(source, phrase, letter_frequency_sum, frequency_sum_type); }
private static List<Phrase> DoFindPhrases(List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string word_text, double similarity_percentage) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = new List<Verse>(); if (source != null) { if (source.Count > 0) { if (!String.IsNullOrEmpty(word_text)) { Book book = source[0].Book; if (!String.IsNullOrEmpty(word_text)) { try { foreach (Verse verse in source) { foreach (Word word in verse.Words) { if (word.Text.IsSimilarTo(word_text, similarity_percentage)) { if (!found_verses.Contains(verse)) { found_verses.Add(verse); } result.Add(new Phrase(verse, word.Position, word.Text)); } } } } catch { // log exception } } } } } return result; }
// find by similarity - phrases similar to given text public static List<Phrase> FindPhrases(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string text, double similarity_percentage) { List<Phrase> result = new List<Phrase>(); List<Verse> found_verses = null; while (text.Contains(" ")) { text = text.Replace(" ", " "); } while (text.Contains("+")) { text = text.Replace("+", ""); } while (text.Contains("-")) { text = text.Replace("-", ""); } string[] word_texts = text.Split(); if (word_texts.Length == 0) { return result; } else if (word_texts.Length == 1) { return DoFindPhrases(book, find_scope, current_selection, previous_result, text, similarity_percentage); } else if (word_texts.Length > 1) // enable nested searches { if (text.Length > 1) // enable nested searches { List<Phrase> phrases = null; List<Verse> verses = null; foreach (string word_text in word_texts) { phrases = DoFindPhrases(book, find_scope, current_selection, found_verses, word_text, similarity_percentage); verses = new List<Verse>(GetVerses(phrases)); // if first result if (found_verses == null) { // fill it up with a copy of the first similar word search result result = new List<Phrase>(phrases); found_verses = new List<Verse>(verses); // prepare for nested search by search find_scope = FindScope.Result; } else // subsequent search result { found_verses = new List<Verse>(verses); List<Phrase> union_phrases = new List<Phrase>(phrases); foreach (Phrase phrase in result) { if (phrase != null) { if (verses.Contains(phrase.Verse)) { union_phrases.Add(phrase); } } } result = union_phrases; } } } } return result; }
private static List<List<Verse>> DoFindVerseRanges(List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, SimilarityMethod find_similarity_method, double similarity_percentage) { List<List<Verse>> result = new List<List<Verse>>(); Dictionary<Verse, List<Verse>> verse_ranges = new Dictionary<Verse, List<Verse>>(); // need dictionary to check if key exist bool[] already_compared = new bool[Verse.MAX_NUMBER]; if (source != null) { if (source.Count > 0) { Book book = source[0].Book; switch (find_similarity_method) { case SimilarityMethod.SimilarText: { for (int i = 0; i < source.Count - 1; i++) { for (int j = i + 1; j < source.Count; j++) { if (!already_compared[j]) { if (source[i].Text.IsSimilarTo(source[j].Text, similarity_percentage)) { if (!verse_ranges.ContainsKey(source[i])) // first time matching verses found { List<Verse> similar_verses = new List<Verse>(); verse_ranges.Add(source[i], similar_verses); similar_verses.Add(source[i]); similar_verses.Add(source[j]); already_compared[i] = true; already_compared[j] = true; } else // matching verses already exists { List<Verse> similar_verses = verse_ranges[source[i]]; similar_verses.Add(source[j]); already_compared[j] = true; } } } } } } break; case SimilarityMethod.SimilarWords: { for (int i = 0; i < source.Count - 1; i++) { for (int j = i + 1; j < source.Count; j++) { if (!already_compared[j]) { if (source[i].Text.HasSimilarWordsTo(source[j].Text, (int)Math.Round((Math.Min(source[i].Words.Count, source[j].Words.Count) * similarity_percentage)), 1.0)) { if (!verse_ranges.ContainsKey(source[i])) // first time matching verses found { List<Verse> similar_verses = new List<Verse>(); verse_ranges.Add(source[i], similar_verses); similar_verses.Add(source[i]); similar_verses.Add(source[j]); already_compared[i] = true; already_compared[j] = true; } else // matching verses already exists { List<Verse> similar_verses = verse_ranges[source[i]]; similar_verses.Add(source[j]); already_compared[j] = true; } } } } } } break; case SimilarityMethod.SimilarFirstWord: { for (int i = 0; i < source.Count - 1; i++) { for (int j = i + 1; j < source.Count; j++) { if (!already_compared[j]) { if (source[j].Text.HasSimilarFirstWordTo(source[j].Text, similarity_percentage)) { if (!verse_ranges.ContainsKey(source[i])) // first time matching verses found { List<Verse> similar_verses = new List<Verse>(); verse_ranges.Add(source[i], similar_verses); similar_verses.Add(source[i]); similar_verses.Add(source[j]); already_compared[i] = true; already_compared[j] = true; } else // matching verses already exists { List<Verse> similar_verses = verse_ranges[source[i]]; similar_verses.Add(source[j]); already_compared[j] = true; } } } } } } break; case SimilarityMethod.SimilarLastWord: { for (int i = 0; i < source.Count - 1; i++) { for (int j = i + 1; j < source.Count; j++) { if (!already_compared[j]) { if (source[i].Text.HasSimilarLastWordTo(source[j].Text, similarity_percentage)) { if (!verse_ranges.ContainsKey(source[i])) // first time matching verses found { List<Verse> similar_verses = new List<Verse>(); verse_ranges.Add(source[i], similar_verses); similar_verses.Add(source[i]); similar_verses.Add(source[j]); already_compared[i] = true; already_compared[j] = true; } else // matching verses already exists { List<Verse> similar_verses = verse_ranges[source[i]]; similar_verses.Add(source[j]); already_compared[j] = true; } } } } } } break; default: break; } } } // copy dictionary to list of list if (verse_ranges.Count > 0) { foreach (List<Verse> verse_range in verse_ranges.Values) { result.Add(verse_range); } } return result; }
// find by prostration type public static List<Verse> FindVerses(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, ProstrationType prostration_type) { return DoFindVerses(book, find_scope, current_selection, previous_result, prostration_type); }
private static List<Verse> DoFindVerses(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, Verse verse, SimilarityMethod similarity_method, double similarity_percentage) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindVerses(source, find_scope, current_selection, previous_result, verse, similarity_method, similarity_percentage); }
// find by letter frequency sum public static List<Word> FindWords(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, string phrase, int letter_frequency_sum, FrequencySumType frequency_sum_type) { return DoFindWords(book, find_scope, current_selection, previous_result, phrase, letter_frequency_sum, frequency_sum_type); }
private static List<Verse> DoFindVerses(List<Verse> source, FindScope find_scope, Selection current_selection, List<Verse> previous_result, Verse verse, SimilarityMethod find_similarity_method, double similarity_percentage) { List<Verse> result = new List<Verse>(); if (source != null) { if (source.Count > 0) { Book book = source[0].Book; if (verse != null) { switch (find_similarity_method) { case SimilarityMethod.SimilarText: { for (int j = 0; j < source.Count; j++) { if (verse.Text.IsSimilarTo(source[j].Text, similarity_percentage)) { result.Add(source[j]); } } } break; case SimilarityMethod.SimilarWords: { for (int j = 0; j < source.Count; j++) { if (verse.Text.HasSimilarWordsTo(source[j].Text, (int)Math.Round((Math.Min(verse.Words.Count, source[j].Words.Count) * similarity_percentage)), 1.0)) { result.Add(source[j]); } } } break; case SimilarityMethod.SimilarFirstHalf: { for (int j = 0; j < source.Count; j++) { if (verse.Text.HasSimilarFirstHalfTo(source[j].Text, similarity_percentage)) { result.Add(source[j]); } } } break; case SimilarityMethod.SimilarLastHalf: { for (int j = 0; j < source.Count; j++) { if (verse.Text.HasSimilarLastHalfTo(source[j].Text, similarity_percentage)) { result.Add(source[j]); } } } break; case SimilarityMethod.SimilarFirstWord: { for (int j = 0; j < source.Count; j++) { if (verse.Text.HasSimilarFirstWordTo(source[j].Text, similarity_percentage)) { result.Add(source[j]); } } } break; case SimilarityMethod.SimilarLastWord: { for (int j = 0; j < source.Count; j++) { if (verse.Text.HasSimilarLastWordTo(source[j].Text, similarity_percentage)) { result.Add(source[j]); } } } break; default: break; } } } } return result; }
private static void AddToResult(List<Phrase> phrases, ref List<Verse> source, ref FindScope find_scope, ref List<Phrase> previous_result) { if (phrases != null) { List<Verse> verses = new List<Verse>(GetVerses(phrases)); // if first result if (source == null) { // fill it up with a copy of the first root search result previous_result = new List<Phrase>(phrases); source = new List<Verse>(verses); // prepare for nested search by search find_scope = FindScope.Result; } else // subsequent search result { source = new List<Verse>(verses); List<Phrase> union_phrases = new List<Phrase>(phrases); if (previous_result != null) { foreach (Phrase phrase in previous_result) { if (phrase != null) { if (verses.Contains(phrase.Verse)) { union_phrases.Add(phrase); } } } } previous_result = union_phrases; } } }
private static List<Verse> DoFindVerses(Book book, FindScope find_scope, Selection current_selection, List<Verse> previous_result, ProstrationType prostration_type) { List<Verse> source = GetSourceVerses(book, find_scope, current_selection, previous_result); return DoFindVerses(source, prostration_type); }