-
Notifications
You must be signed in to change notification settings - Fork 635
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
QNTM-5870: When searching weights should be taken into account before string matching #9312
Changes from 14 commits
56dbd73
eec64b9
4362151
75643c7
6a07e6c
38b8461
44a00a8
5c04a06
b24eb02
d0e3224
4c88d1b
857fd28
18ecaa0
fe91d62
2504bdd
7950f23
dd67883
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using System.Diagnostics; | ||
using Dynamo.Utilities; | ||
using Dynamo.Utilities; | ||
using Dynamo.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Diagnostics; | ||
|
||
namespace Dynamo.Search | ||
{ | ||
|
@@ -12,9 +12,16 @@ namespace Dynamo.Search | |
/// </summary> | ||
public class SearchDictionary<V> | ||
{ | ||
private ILogger logger; | ||
|
||
internal SearchDictionary(ILogger logger = null) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
protected readonly Dictionary<V, Dictionary<string, double>> entryDictionary = | ||
new Dictionary<V, Dictionary<string, double>>(); | ||
|
||
private List<IGrouping<string, Tuple<V, double>>> tagDictionary; | ||
|
||
/// <summary> | ||
|
@@ -307,14 +314,23 @@ internal void RebuildTagDictionary() | |
tagWeightAndEntry => | ||
Tuple.Create(tagWeightAndEntry.Entry, tagWeightAndEntry.Weight)).ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// Search for elements in the dictionary based on the query | ||
/// </summary> | ||
/// <param name="query"> The query </param> | ||
/// <param name="minResultsForTolerantSearch">Minimum number of results in the original search strategy to justify doing more tolerant search</param> | ||
internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0) | ||
{ | ||
#if DEBUG | ||
Stopwatch stopwatch = null; | ||
if (this.logger != null) | ||
{ | ||
stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
} | ||
#endif | ||
|
||
var searchDict = new Dictionary<V, double>(); | ||
|
||
if (tagDictionary == null) | ||
|
@@ -330,13 +346,53 @@ internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0 | |
ComputeWeightAndAddToDictionary(query, pair, searchDict); | ||
} | ||
|
||
return searchDict | ||
.OrderByDescending(x => x.Value) | ||
.Select(x => x.Key); | ||
var orderedSearchDict = searchDict.OrderByDescending(x => x.Value); | ||
#if DEBUG | ||
//if (this.logger != null) | ||
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. @ColinDayOrg did you mean to leave this commented out? 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. Yes. Normally I would delete code like this, but it seemed that it could possibly be useful in the future. It can be removed if that would be preferable. It can output a lot of logging data and may not need to be used a lot which is why I commented it out. 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. If these are debug mode only code, I prefer to leave them not commented and once we flip the debug mode, we can remove then. 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. Due to this code possibly outputting a large amount of logging data if it were uncommented, and it not needing to be used currently, I have removed this code. It can always be put back if needed. |
||
//{ | ||
// int index = 0; | ||
// foreach (var pair in orderedSearchDict) | ||
// { | ||
// var message = | ||
// string.Format( | ||
// "{0} -> Key = {1}, Value = {2}", | ||
// index, | ||
// pair.Key, | ||
// pair.Value); | ||
// this.logger.Log(message); | ||
// } | ||
//} | ||
#endif | ||
|
||
var searchResults = orderedSearchDict.Select(x => x.Key); | ||
|
||
// TODO: DYN-1326 - Place this behind a debug mode (after running CI in a preliminary test) | ||
searchResults = searchResults.Take(20); | ||
|
||
#if DEBUG | ||
if (this.logger != null) | ||
{ | ||
stopwatch.Stop(); | ||
|
||
var message = | ||
string.Format( | ||
"Searching for: \"{0}\", [Entries:{1}, Tags:{2}] : {3}ms -> {4}", | ||
query, | ||
entryDictionary.Count, | ||
tagDictionary.Count, | ||
stopwatch.ElapsedMilliseconds, | ||
searchResults.Count()); | ||
this.logger.Log(message); | ||
} | ||
#endif | ||
|
||
return searchResults; | ||
} | ||
|
||
private static void ComputeWeightAndAddToDictionary(string query, | ||
IGrouping<string, Tuple<V, double>> pair, Dictionary<V, double> searchDict) | ||
private static void ComputeWeightAndAddToDictionary( | ||
string query, | ||
IGrouping<string, Tuple<V, double>> pair, | ||
Dictionary<V, double> searchDict) | ||
{ | ||
// it has a match, how close is it to matching the entire string? | ||
double matchCloseness = ((double)query.Length) / pair.Key.Length; | ||
|
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.
even though these are internal - what do you think about adding some comments on why these constructors were added?