-
Notifications
You must be signed in to change notification settings - Fork 44
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
improved search results #2116
Merged
Merged
improved search results #2116
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e350195
improved search results
ABSitf 8419aa9
fix
ABSitf 5bde45b
update match threshold
ABSitf 8d61c0d
update search logic
ABSitf 9a275d0
remove commented code
ABSitf 1e9cc0f
small refactoring
ABSitf 0fd1072
added context comments
ABSitf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,51 +32,102 @@ bool RibbonSchemaHolder::addItem( std::shared_ptr<RibbonMenuItem> item ) | |
return true; | ||
} | ||
|
||
std::vector<RibbonSchemaHolder::SearchResult> RibbonSchemaHolder::search( const std::string& searchStr ) | ||
std::vector<RibbonSchemaHolder::SearchResult> RibbonSchemaHolder::search( const std::string& searchStr, | ||
std::vector<SearchResultWeight>* weights /*= nullptr*/ ) | ||
{ | ||
std::vector<SearchResult> res; | ||
std::vector<std::pair<SearchResult, SearchResultWeight>> rawResult; | ||
|
||
if ( searchStr.empty() ) | ||
return res; | ||
return {}; | ||
auto words = split( searchStr, " " ); | ||
std::erase_if( words, [] ( const auto& str ) { return str.empty(); } ); | ||
std::vector<std::pair<float, SearchResult>> resultListForSort; | ||
auto enweight = [] ( const std::vector<std::string>& searchWords, const std::string& testLine )->float | ||
auto calcWeight = [&words] ( const std::string& sourceStr )->Vector2f | ||
{ | ||
if ( testLine.empty() ) | ||
return 1.0f; | ||
float sumWeight = 0.0f; | ||
for ( const auto& word : searchWords ) | ||
if ( sourceStr.empty() ) | ||
return { 1.0f, 1.f }; | ||
|
||
auto sourceWords = split( sourceStr, " " ); | ||
std::erase_if( sourceWords, [] ( const auto& str ) { return str.empty(); } ); | ||
if ( sourceWords.empty() ) | ||
return { 1.0f, 1.f }; | ||
|
||
const int sourceWordsSize = int( sourceWords.size() ); | ||
//std::vector<int> errorArr( words.size() * sourceWordsSize, -1 ); | ||
std::vector<bool> busyWord( sourceWordsSize, false ); | ||
int sumError = 0; | ||
int searchCharCount = 0; | ||
int posWeight = sourceWordsSize; | ||
for ( int i = 0; i < words.size(); ++i ) | ||
{ | ||
int minDistance = std::abs( int( word.size() ) - int( testLine.size() ) ); | ||
int maxDistance = std::max( int( word.size() ), int( testLine.size() ) ); | ||
int distance = calcDamerauLevenshteinDistance( word, testLine, false ); | ||
sumWeight += float( distance - minDistance ) / ( maxDistance - minDistance ); | ||
searchCharCount += int( words[i].size() ); | ||
int minError = int( words[i].size() ); | ||
int minErrorIndex = -1; | ||
for ( int j = 0; j < sourceWordsSize; ++j ) | ||
{ | ||
if ( busyWord[j] ) | ||
continue; | ||
int error = calcDamerauLevenshteinDistance( words[i], sourceWords[j], false ); | ||
if ( i == words.size() - 1 ) | ||
error -= std::max( int( sourceWords[j].size() ) - int( words[i].size() ), 0 ); | ||
if ( error < minError ) | ||
{ | ||
minError = error; | ||
minErrorIndex = j; | ||
} | ||
} | ||
if ( minErrorIndex != -1 ) | ||
{ | ||
busyWord[minErrorIndex] = true; | ||
posWeight += minErrorIndex; | ||
} | ||
sumError += minError; | ||
} | ||
return std::clamp( sumWeight / float( searchWords.size() ), 0.0f, 1.0f ); | ||
return { std::clamp( float( sumError ) / searchCharCount, 0.0f, 1.0f ), float( posWeight ) / sourceWordsSize / words.size()}; | ||
}; | ||
|
||
const float maxWeight = 0.25f; | ||
bool exactMatch = false; | ||
// check item (calc difference from search item) and add item to raw results if difference less than threshold | ||
auto checkItem = [&] ( const MenuItemInfo& item, int t ) | ||
{ | ||
const auto& caption = item.caption.empty() ? item.item->name() : item.caption; | ||
const auto& tooltip = item.tooltip; | ||
auto captionRes = enweight( words, caption ); | ||
auto tooltipRes = enweight( words, tooltip ); | ||
if ( captionRes > 0.1f && tooltipRes > 0.1f ) | ||
std::pair<SearchResult, SearchResultWeight> itemRes; | ||
itemRes.first.tabIndex = t; | ||
itemRes.first.item = &item; | ||
const auto posCE = findSubstringCaseInsensitive( caption, searchStr ); | ||
if ( posCE != std::string::npos ) | ||
{ | ||
if ( !exactMatch ) | ||
{ | ||
rawResult.clear(); | ||
exactMatch = true; | ||
} | ||
itemRes.second.captionWeight = 0.f; | ||
itemRes.second.captionOrderWeight = float( posCE ) / caption.size(); | ||
rawResult.push_back( itemRes ); | ||
return; | ||
for ( const auto& word : words ) | ||
} | ||
else if ( exactMatch ) | ||
{ | ||
auto posC = findSubstringCaseInsensitive( caption, word ); | ||
auto posT = findSubstringCaseInsensitive( tooltip, word ); | ||
if ( posC == std::string::npos ) | ||
captionRes += 10.0f; | ||
else | ||
captionRes += 0.5f * ( float( posC ) / caption.size() ); | ||
if ( posT == std::string::npos ) | ||
tooltipRes += 10.0f; | ||
else | ||
tooltipRes += 0.5f * ( float( posT ) / tooltip.size() ); | ||
const auto posTE = findSubstringCaseInsensitive( tooltip, searchStr ); | ||
if ( posTE == std::string::npos ) | ||
return; | ||
itemRes.second.tooltipWeight = 0.f; | ||
itemRes.second.tooltipOrderWeight = float( posTE ) / tooltip.size(); | ||
rawResult.push_back( itemRes ); | ||
return; | ||
} | ||
resultListForSort.push_back( { captionRes + 0.5f * tooltipRes, SearchResult{t,&item} } ); | ||
|
||
Vector2f weightEP = calcWeight( caption ); | ||
itemRes.second.captionWeight = weightEP.x; | ||
itemRes.second.captionOrderWeight = weightEP.y; | ||
weightEP = calcWeight( tooltip ); | ||
itemRes.second.tooltipWeight = weightEP.x; | ||
itemRes.second.tooltipOrderWeight = weightEP.y; | ||
if ( itemRes.second.captionWeight > maxWeight && itemRes.second.tooltipWeight > maxWeight ) | ||
return; | ||
rawResult.push_back( itemRes ); | ||
}; | ||
const auto& schema = RibbonSchemaHolder::schema(); | ||
auto lookUpMenuItemList = [&] ( const MenuItemsList& list, int t ) | ||
|
@@ -121,25 +172,62 @@ std::vector<RibbonSchemaHolder::SearchResult> RibbonSchemaHolder::search( const | |
lookUpMenuItemList( schema.headerQuickAccessList, -1 ); | ||
lookUpMenuItemList( schema.sceneButtonsList, -1 ); | ||
|
||
std::sort( resultListForSort.begin(), resultListForSort.end(), [] ( const auto& a, const auto& b ) | ||
// clear duplicated results | ||
std::sort( rawResult.begin(), rawResult.end(), [] ( const auto& a, const auto& b ) | ||
{ | ||
return intptr_t( a.second.item ) < intptr_t( b.second.item ); | ||
return intptr_t( a.first.item ) < intptr_t( b.first.item ); | ||
} ); | ||
resultListForSort.erase( | ||
std::unique( resultListForSort.begin(), resultListForSort.end(), | ||
rawResult.erase( | ||
std::unique( rawResult.begin(), rawResult.end(), | ||
[] ( const auto& a, const auto& b ) | ||
{ | ||
return a.second.item == b.second.item; | ||
return a.first.item == b.first.item; | ||
} ), | ||
resultListForSort.end() ); | ||
rawResult.end() ); | ||
|
||
std::sort( resultListForSort.begin(), resultListForSort.end(), [] ( const auto& a, const auto& b ) | ||
std::sort( rawResult.begin(), rawResult.end(), [] ( const auto& a, const auto& b ) | ||
{ | ||
return a.first < b.first; | ||
if ( a.second.captionWeight < b.second.captionWeight ) | ||
return true; | ||
else if ( a.second.captionWeight > b.second.captionWeight ) | ||
return false; | ||
else if ( a.second.captionOrderWeight < b.second.captionOrderWeight ) | ||
return true; | ||
else if ( a.second.captionOrderWeight > b.second.captionOrderWeight ) | ||
return false; | ||
else if ( a.second.tooltipWeight < b.second.tooltipWeight ) | ||
return true; | ||
else if ( a.second.tooltipWeight > b.second.tooltipWeight ) | ||
return false; | ||
else if ( a.second.tooltipOrderWeight < b.second.tooltipOrderWeight ) | ||
return true; | ||
else | ||
return false; | ||
} ); | ||
res.reserve( resultListForSort.size() ); | ||
for ( const auto& sortedRes : resultListForSort ) | ||
res.push_back( sortedRes.second ); | ||
|
||
// filter results with error threshold as 3x minimum caption error | ||
if ( !rawResult.empty() && rawResult[0].second.captionWeight < maxWeight / 3.f ) | ||
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. please add comment for this block 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. fixed |
||
{ | ||
const float maxWeightNew = rawResult[0].second.captionWeight * 3.f; | ||
if ( rawResult.back().second.captionWeight > maxWeightNew ) | ||
{ | ||
auto tailIt = std::find_if( rawResult.begin(), rawResult.end(), [&] ( const auto& a ) | ||
{ | ||
return a.second.captionWeight > maxWeightNew && a.second.tooltipWeight > maxWeightNew; | ||
} ); | ||
rawResult.erase( tailIt, rawResult.end() ); | ||
} | ||
} | ||
|
||
std::vector<SearchResult> res( rawResult.size() ); | ||
if ( weights ) | ||
*weights = std::vector<SearchResultWeight>( rawResult.size() ); | ||
for ( int i = 0; i < rawResult.size(); ++i ) | ||
{ | ||
res[i] = rawResult[i].first; | ||
if ( weights ) | ||
( *weights )[i] = rawResult[i].second; | ||
} | ||
|
||
return res; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add comment for this function