-
-
Notifications
You must be signed in to change notification settings - Fork 16
LT-22524: Add substring search mode to StringSearcher #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Icu; | ||
|
|
@@ -30,7 +31,11 @@ public enum SearchType | |
| /// <summary> | ||
| /// Matches any words in a string. | ||
| /// </summary> | ||
| FullText | ||
| FullText, | ||
| /// <summary> | ||
| /// Matches any portion within a string. | ||
| /// </summary> | ||
| Substring | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -120,6 +125,7 @@ public IEnumerable<T> GetItems(byte[] lower, byte[] upper) | |
| #endregion SortKeyIndex class | ||
|
|
||
| private readonly Dictionary<Tuple<int, int>, SortKeyIndex> m_indices = new Dictionary<Tuple<int, int>, SortKeyIndex>(); | ||
| private readonly Dictionary<Tuple<int, int>, List<KeyValuePair<T, string>>> m_rawIndices = new Dictionary<Tuple<int, int>, List<KeyValuePair<T, string>>>(); | ||
| private readonly SearchType m_type; | ||
| private readonly Func<int, string, byte[]> m_sortKeySelector; | ||
| private readonly Func<int, string, IEnumerable<string>> m_tokenizer; | ||
|
|
@@ -195,6 +201,10 @@ public void Add(T item, int indexId, int wsId, string text) | |
| foreach (string token in RemoveWhitespaceAndPunctTokens(m_tokenizer(wsId, text))) | ||
| index.Add(m_sortKeySelector(wsId, token), item); | ||
| break; | ||
|
|
||
| case SearchType.Substring: | ||
| GetRawIndex(indexId, wsId).Add(new KeyValuePair<T, string>(item, text ?? string.Empty)); | ||
|
hahn-kev marked this conversation as resolved.
Outdated
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -268,6 +278,16 @@ public IEnumerable<T> Search(int indexId, int wsId, string text) | |
| results = results == null ? items : results.Intersect(items); | ||
| } | ||
| return results; | ||
|
|
||
| case SearchType.Substring: | ||
| { | ||
| List<KeyValuePair<T, string>> raw; | ||
| if (!m_rawIndices.TryGetValue(Tuple.Create(indexId, wsId), out raw)) | ||
| return Enumerable.Empty<T>(); | ||
| CompareInfo ci = CultureInfo.InvariantCulture.CompareInfo; | ||
| return raw.Where(kv => ci.IndexOf(kv.Value, text, | ||
| CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) >= 0).Select(kv => kv.Key); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we might need to consider if this will work correctly for us, or if we need to create a manged wrapper in icu-dotnet for string search so we can use what ICU provides for this use case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that this is the same behavior as FWLite's search, so hopefully it will be a good way to go? I did find another behavior to include, see the "Fold diacritics only when the search term has none" change here. |
||
| } | ||
| } | ||
|
|
||
| return Enumerable.Empty<T>(); | ||
|
|
@@ -284,6 +304,7 @@ private static IEnumerable<string> RemoveWhitespaceAndPunctTokens(IEnumerable<st | |
| public void Clear() | ||
| { | ||
| m_indices.Clear(); | ||
| m_rawIndices.Clear(); | ||
| } | ||
|
|
||
| private SortKeyIndex GetIndex(int indexId, int ws) | ||
|
|
@@ -299,6 +320,18 @@ private SortKeyIndex GetIndex(int indexId, int ws) | |
| return index; | ||
| } | ||
|
|
||
| private List<KeyValuePair<T, string>> GetRawIndex(int indexId, int ws) | ||
| { | ||
| var key = Tuple.Create(indexId, ws); | ||
| List<KeyValuePair<T, string>> list; | ||
| if (!m_rawIndices.TryGetValue(key, out list)) | ||
| { | ||
| list = new List<KeyValuePair<T, string>>(); | ||
| m_rawIndices[key] = list; | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| private static IEnumerable<Tuple<int, string>> GetWsStrings(ITsString tss) | ||
| { | ||
| var sb = new StringBuilder(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.