Skip to content
Open
Changes from all commits
Commits
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
41 changes: 32 additions & 9 deletions BrowserSearch/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,40 @@ public List<Result> Query(Query query)
private int CalculateScore(string query, string title, string url)
{
// Since PT Run's FuzzySearch is too slow, and the history usually has a lot of entries,
// lets calculate the scores manually using a faster (but less accurate) method
float titleScore = title.Contains(query, StringComparison.InvariantCultureIgnoreCase)
? (query.Length / (float)title.Length * 100f)
: 0;
float urlScore = url.Contains(query, StringComparison.InvariantCultureIgnoreCase)
? (query.Length / (float)url.Length * 100f)
: 0;
// lets calculate the scores manually using a faster (but less accurate) method.
// Multi-token: every whitespace-separated token must appear in title or url (AND).
string[] tokens = query.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 0)
{
return 0;
}

float score = new[] { titleScore, urlScore }.Max();
score += _defaultBrowser!.CalculateExtraScore(query, title, url);
float score = 0;
foreach (string token in tokens)
{
bool inTitle = title.Contains(token, StringComparison.InvariantCultureIgnoreCase);
bool inUrl = url.Contains(token, StringComparison.InvariantCultureIgnoreCase);
if (!inTitle && !inUrl)
{
return 0;
}

float titleContrib = inTitle ? token.Length / (float)title.Length * 100f : 0f;
float urlContrib = inUrl ? token.Length / (float)url.Length * 100f : 0f;
score += Math.Max(titleContrib, urlContrib);
}

// Bonus when the whole query appears contiguously, so phrase matches still outrank scattered tokens
if (title.Contains(query, StringComparison.InvariantCultureIgnoreCase))
{
score += query.Length / (float)title.Length * 100f;
}
else if (url.Contains(query, StringComparison.InvariantCultureIgnoreCase))
{
score += query.Length / (float)url.Length * 100f;
}

score += _defaultBrowser!.CalculateExtraScore(query, title, url);
return (int)score;
}
}
Expand Down