Skip to content
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

Merged
merged 17 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 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,10 @@ namespace Dynamo.Search
/// </summary>
public class NodeSearchModel : SearchLibrary<NodeSearchElement, NodeModel>
{
internal NodeSearchModel(ILogger logger = null) : base(logger)
{
Copy link
Member

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?

}

internal override void Add(NodeSearchElement entry)
{
SearchElementGroup group = SearchElementGroup.None;
Expand Down
76 changes: 66 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,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>
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ColinDayOrg did you mean to leave this commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Expand Down
5 changes: 5 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,10 @@ namespace Dynamo.Search
public class SearchLibrary<TEntry, TItem> : SearchDictionary<TEntry>, ISource<TItem>
where TEntry : ISearchEntry, ISource<TItem>
{
internal SearchLibrary(ILogger logger) : base(logger)
{
}

/// <summary>
/// Adds an entry to search.
/// </summary>
Expand Down