Skip to content

Commit

Permalink
fixed Trie search for single-character queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Honza Dvorsky committed Sep 6, 2015
1 parent 9a32cd1 commit 17a5fde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "hit"
s.version = "0.2"
s.version = "0.3"
s.summary = "Lightweight full-text search written in Swift"

s.description = <<-DESC
Expand Down
7 changes: 4 additions & 3 deletions hit/Trie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public struct Trie {

let prefixHeadRange = TokenRange(start: prefix.startIndex, end: prefix.startIndex.advancedBy(1))
let prefixHead = prefix.substringWithRange(prefixHeadRange)

if length == 1 {
let emptyTrie = trie.token.characters.count == 0

if length == 1 && !emptyTrie {

//potentially might be found if trie matches
let match = (trie.token == prefixHead)
return match ? trie : nil
}

let emptyTrie = trie.token.characters.count == 0
let tokenMatches = trie.token == prefixHead
if emptyTrie || tokenMatches {

Expand Down
21 changes: 18 additions & 3 deletions hitTests/TrieTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import XCTest
@testable import hit

class TrieTests: HitTestCase {


func commonWords() -> [String] {
let strings = ["swiftkey", "swype", "hello", "london", "reality", "fantasy", "stuff"]
return strings
}

func testTrieCreation_exactMatches() {

let strings = ["swiftkey", "swype", "hello", "london", "reality", "fantasy"]
let strings = self.commonWords()
let trie = Trie(strings: strings)

let foundStrings = trie.stringsMatchingPrefix("sw")
Expand Down Expand Up @@ -54,6 +58,17 @@ class TrieTests: HitTestCase {
_ = trie.stringsMatchingPrefix("sw")
}
}

func testCorrectness_singleCharSearch() {
let strings = self.commonWords()
let trie = Trie(strings: strings)

let foundStrings = Set(trie.stringsMatchingPrefix("s"))
XCTAssertEqual(foundStrings.count, 3)
XCTAssert(foundStrings.contains("swiftkey"))
XCTAssert(foundStrings.contains("swype"))
XCTAssert(foundStrings.contains("stuff"))
}


}

0 comments on commit 17a5fde

Please sign in to comment.