public float SearchHanzi(StringPointer hanzi) { float strActualLength = hanzi.ActualLength; float relevance = 0; foreach (string chineseSearchWord in ChineseSearchWords) { StringSearch.Result result; // check if the word (AB) is part of the search (ABC) result = StringSearch.Search(hanzi, chineseSearchWord, 0, chineseSearchWord.Length, HanziSearchFlags); if (result.Found) { // in this case, order the results based on character order in the word relevance += 4 - (float)result.Start / chineseSearchWord.Length; } // check if the word (AB) contains our search (A) result = StringSearch.Search(chineseSearchWord, 0, chineseSearchWord.Length, hanzi, HanziSearchFlags); if (result.Found) { relevance += 2 * (float)chineseSearchWord.Length / hanzi.ActualLength; } } return(relevance); }
public bool Equals(StringPointer other) { if (Length == other.Length) { return(StringSearch.Search(this, other, SearchFlags.IGNORE_CASE | SearchFlags.IGNORE_DIACRITICS).Found&& StringSearch.Search(other, this, SearchFlags.IGNORE_CASE | SearchFlags.IGNORE_DIACRITICS).Found); //return string.Compare(WordDictionary.StringMemory, Start, WordDictionary.StringMemory, other.Start, Length) == 0; } else { return(false); } }
public SearchQuery(string searchText) { SearchText = searchText; int actualLength; SearchText = StringSearch.PreprocessStr(SearchText, 0, SearchText.Length, SearchFlags.IGNORE_CASE | SearchFlags.IGNORE_DIACRITICS, out actualLength); List <string> chineseSearchWords = new List <string>(); List <StringSearch> pinyinSearchWords = new List <StringSearch>(); List <StringSearch> searchWords = new List <StringSearch>(); foreach (string searchWord in SearchText.Split(WordSeperators, StringSplitOptions.RemoveEmptyEntries)) { string processedSearchWord = StringSearch.PreprocessStr(searchWord, 0, searchWord.Length, SearchFlags.IGNORE_NON_LETTER, out actualLength); if (processedSearchWord.IsChinese()) { chineseSearchWords.Add(processedSearchWord); } else { if (processedSearchWord.IsPinyin()) { pinyinSearchWords.Add(new StringSearch(processedSearchWord, SearchFlags.NONE)); PinyinMask |= processedSearchWord.LetterMask(); } if (processedSearchWord.Length >= 2) { searchWords.Add(new StringSearch(processedSearchWord, SearchFlags.NONE)); } } } searchScope = SearchScope.NONE; if (chineseSearchWords.Count > 0) { searchScope |= SearchScope.HANZI; ChineseSearchWords = chineseSearchWords.ToArray(); } if (pinyinSearchWords.Count > 0) { searchScope |= SearchScope.PINYIN; PinyinSearchWords = pinyinSearchWords.ToArray(); } if (searchWords.Count > 0) { searchScope |= SearchScope.TRANSLATION; SearchWords = searchWords.ToArray(); int totalCharCount = 0; foreach (StringSearch search in SearchWords) { totalCharCount += search.SearchActualLength; } SearchScopeMin = totalCharCount / 2; SearchScopeMax = totalCharCount * 5; SearchInWordsMask = new int[(WordDictionary.Words.Count + 31) / 32]; foreach (StringSearch search in searchWords) { ulong letterMask = search.SearchStr.LetterMask(); int indexedWordMatchStart = 0; foreach (IndexedWord indexedWord in WordDictionary.IndexedWords) { if ((indexedWord.LetterMask & letterMask) == letterMask && search.SearchIn(indexedWord.IndexedWordStr, SearchFlags.NONE).Found) { for (int i = 0; i < indexedWord.MatchesCount; ++i) { int wordMatch = WordDictionary.IndexedWordMatches[indexedWordMatchStart + i]; SearchInWordsMask[wordMatch / 32] |= 1 << (wordMatch & 31); } } indexedWordMatchStart += indexedWord.MatchesCount; } } } }
private static float GetTranslationRelevance(StringPointer str, float strActualLength, StringSearch search) { StringSearch.Result result = search.SearchIn(str); return(result.Found ? GetRelevance(str, strActualLength, search.SearchActualLength, result.Start, result.End) : 0); }