-
-
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
base: master
Are you sure you want to change the base?
Changes from all commits
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)); | ||
|
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're handling the case of a null text argument here by using an empty string, but searching for empty strings is explicitly banned. Should we just not add a value to the index if the text is null or empty? We could probably safely do this on the first line of this method. Taking a quick look at the |
||
| 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. |
||
| } | ||
| } | ||
|
|
||
| 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(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish I had a suggestion, but this type is really ugly lol. I might replace KeyValuePair with a custom struct which does the same thing but is more understandable what's going on when you look at it.
Another idea (bigger tweak) is to create a custom struct (maybe named SearchBucket or IndexKey) which is a new struct that replaces all the uses of
Tuple<int, int>(which is quite meaning less IMO), you would just need to match the compare and equality rules of the Tuple. This more falls in line with leaving code better than when you found it.