Skip to content

Commit

Permalink
Autocomplete prioritisation issue (#24)
Browse files Browse the repository at this point in the history
* More clear and strict categorization of matches
  • Loading branch information
tomasstrba authored Oct 15, 2021
1 parent d4d89f8 commit 9e3a3c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
75 changes: 34 additions & 41 deletions Sources/BrowserServicesKit/Suggestions/Score.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,52 @@ extension Score {

var score = 0
let lowercasedTitle = title?.lowercased() ?? ""

// Exact matches - full query
let queryCount = query.count
if queryCount > 1 && lowercasedTitle.starts(with: query) { // High score for exact match from the begining of the title
score += 200
} else if queryCount > 2 && lowercasedTitle.contains(" \(query)") { // Exact match from the begining of the word within string.
score += 100
}

let domain = url.host?.droppingWwwPrefix() ?? ""
let nakedUrl = url.nakedString ?? ""

// Tokenized matches
if queryTokens.count > 1 {
var matchesAllTokens = true
for token in queryTokens {
// Match only from the begining of the word to avoid unintuitive matches.
if !lowercasedTitle.starts(with: token) && !lowercasedTitle.contains(" \(token)") && !nakedUrl.starts(with: token) {
matchesAllTokens = false
break
}
}

if matchesAllTokens {
// Score tokenized matches
score += 10

// Boost score if first token matches:
if let firstToken = queryTokens.first { // nakedUrlString - high score boost
if nakedUrl.starts(with: firstToken) {
score += 300
} else if lowercasedTitle.starts(with: firstToken) { // begining of the title - moderate score boost
score += 50
// Full matches
if nakedUrl.starts(with: query) {
score += 300
// Prioritize root URLs most
if url.isRoot { score += 2000 }
} else if lowercasedTitle.starts(with: query) {
score += 200
if url.isRoot { score += 2000 }
} else if queryCount > 2 && domain.contains(query) {
score += 150
} else if queryCount > 2 && lowercasedTitle.contains(" \(query)") { // Exact match from the begining of the word within string.
score += 100
} else {
// Tokenized matches
if queryTokens.count > 1 {
var matchesAllTokens = true
for token in queryTokens {
// Match only from the begining of the word to avoid unintuitive matches.
if !lowercasedTitle.starts(with: token) && !lowercasedTitle.contains(" \(token)") && !nakedUrl.starts(with: token) {
matchesAllTokens = false
break
}
}
}
} else {
// High score for matching URL
if let firstToken = queryTokens.first {
if nakedUrl.starts(with: firstToken) {
score += 300

// Prioritize root URLs most
if url.isRoot { score += 2000 }
} else if firstToken.count > 2 && domain.contains(firstToken) {
score += 150
if url.isRoot { score += 2000 }
if matchesAllTokens {
// Score tokenized matches
score += 10

// Boost score if first token matches:
if let firstToken = queryTokens.first { // nakedUrlString - high score boost
if nakedUrl.starts(with: firstToken) {
score += 70
} else if lowercasedTitle.starts(with: firstToken) { // begining of the title - moderate score boost
score += 50
}
}
}
}
}

// If there are matches, add visitCount to prioritise more visited
if score > 0 {
// Second sort based on visitCount
score *= 1000
score += visitCount
}
Expand Down
11 changes: 3 additions & 8 deletions Tests/BrowserServicesKitTests/Suggestions/ScoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ final class ScoreTests: XCTestCase {
}

func testWhenURLMatchesWithQuery_ThenScoreIsIncreased() {
let query = "test case"
let query = "testcase.com/no"
let score1 = Score(title: "Test case website",
url: URL(string: "https://www.test.com/case")!,
url: URL(string: "https://www.testcase.com/notroot")!,
visitCount: 100,
query: query)

let score2 = Score(title: "Test case website 2",
url: URL(string: "https://www.other.com")!,
visitCount: 100,
query: query)

XCTAssert(score1 > score2)
XCTAssert(score1 > 0)
}

func testWhenTitleMatchesFromTheBeginning_ThenScoreIsIncreased() {
Expand Down

0 comments on commit 9e3a3c6

Please sign in to comment.