Skip to content

Commit

Permalink
QNTM-5870: When searching weights should be taken into account before…
Browse files Browse the repository at this point in the history
… string matching (#9312)

* Update changes from librarie.js to fix QNTM-3710

* Add initial logging to the search dictionary

* Update output message

* Add returned count to the logging after the Search method is called

* Fix merge of tagDictionary changes

* Only return the first 20 search results (will be placed under a debug mode after running CI)

* Put search result truncation behind a debug mode

* Add commentds to the constructors

* Remove unused code
  • Loading branch information
ColinDayOrg authored Dec 14, 2018
1 parent 7ab5632 commit 6925ebb
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ protected DynamoModel(IStartConfiguration config)

pathManager.Preferences = PreferenceSettings;

SearchModel = new NodeSearchModel();
SearchModel = new NodeSearchModel(Logger);
SearchModel.ItemProduced +=
node => ExecuteCommand(new CreateNodeCommand(node, 0, 0, true, true));

Expand Down
9 changes: 9 additions & 0 deletions src/DynamoCore/Search/NodeSearchModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Dynamo.Graph.Nodes;
using Dynamo.Search.SearchElements;
using DynamoUtilities;
using Dynamo.Logging;

namespace Dynamo.Search
{
Expand All @@ -14,6 +15,14 @@ namespace Dynamo.Search
/// </summary>
public class NodeSearchModel : SearchLibrary<NodeSearchElement, NodeModel>
{
/// <summary>
/// Construct a NodeSearchModel object
/// </summary>
/// <param name="logger"> (Optional) A logger to pass through to SearchLibrary for logging search data</param>
internal NodeSearchModel(ILogger logger = null) : base(logger)
{
}

internal override void Add(NodeSearchElement entry)
{
SearchElementGroup group = SearchElementGroup.None;
Expand Down
70 changes: 60 additions & 10 deletions src/DynamoCore/Search/SearchDictionary.cs
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
{
Expand All @@ -12,11 +12,27 @@ namespace Dynamo.Search
/// </summary>
public class SearchDictionary<V>
{
private ILogger logger;

/// <summary>
/// Construct a SearchDictionary object
/// </summary>
/// <param name="logger"> (Optional) A logger to use to log search data</param>
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>
/// Indicates whether to truncate the search results (currently to 20 items).
/// </summary>
internal bool TruncateSearchResults { get; set; } = false;

/// <summary>
/// All the current entries in search.
/// </summary>
Expand Down Expand Up @@ -307,14 +323,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)
Expand All @@ -330,13 +355,38 @@ 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);

var searchResults = orderedSearchDict.Select(x => x.Key);
if (TruncateSearchResults)
{
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;
Expand Down
9 changes: 9 additions & 0 deletions src/DynamoCore/Search/SearchLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dynamo.Utilities;
using Dynamo.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -13,6 +14,14 @@ namespace Dynamo.Search
public class SearchLibrary<TEntry, TItem> : SearchDictionary<TEntry>, ISource<TItem>
where TEntry : ISearchEntry, ISource<TItem>
{
/// <summary>
/// Construct a SearchLibrary object
/// </summary>
/// <param name="logger"> (Optional) A logger to pass through to SearchDictionary for logging search data</param>
internal SearchLibrary(ILogger logger = null) : base(logger)
{
}

/// <summary>
/// Adds an entry to search.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2184,4 +2184,8 @@ Do you want to install the latest Dynamo update?</value>
<data name="RerunButtonToolTip" xml:space="preserve">
<value>Rerun the graph.</value>
</data>
<data name="DynamoViewDebugMenuTruncateSearchResults" xml:space="preserve">
<value>Truncate Search Results</value>
<comment>Debug Menu | Truncate Search Results</comment>
</data>
</root>
4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2184,4 +2184,8 @@ Want to publish a different package?</value>
<value>Custom Nodes will be placed in the Add-Ons section of the library.</value>
<comment>Note regarding Custom Node library location</comment>
</data>
<data name="DynamoViewDebugMenuTruncateSearchResults" xml:space="preserve">
<value>Truncate Search Results</value>
<comment>Debug menu | Truncate Search Results</comment>
</data>
</root>
12 changes: 12 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ public bool EnableTSpline
}
}

/// <summary>
/// Indicates whether search results should be truncated (currently to 20 items).
/// </summary>
public bool TruncateSearchResults
{
get { return model.SearchModel.TruncateSearchResults; }
set
{
model.SearchModel.TruncateSearchResults = value;
}
}

public int LibraryWidth
{
get
Expand Down
5 changes: 5 additions & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@
IsCheckable="True"
Header="{x:Static p:Resources.DynamoViewDebugMenuShowDebugAST}"
IsChecked="{Binding ShowDebugASTs}"></MenuItem>
<MenuItem Focusable="False"
Name="ExperimentalSearch"
IsCheckable="True"
IsChecked="{Binding TruncateSearchResults}"
Header="{x:Static p:Resources.DynamoViewDebugMenuTruncateSearchResults}" />
<MenuItem Focusable="False"
Name="ForceReexec"
Header="{x:Static p:Resources.DynamoViewDebugMenuForceReExecute}"
Expand Down

0 comments on commit 6925ebb

Please sign in to comment.