Skip to content

Commit

Permalink
Additional speed optimizations by relying on indices() of string to r…
Browse files Browse the repository at this point in the history
…emove characters sequentially.
  • Loading branch information
gdetari committed Oct 1, 2024
1 parent afb81de commit 9b03364
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
6 changes: 0 additions & 6 deletions Sources/SymSpellSwift/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,3 @@ extension String {
return self[start ..< end]
}
}

extension Substring {
func removingCharacter(at index: Int) -> Substring {
prefix(index) + dropFirst(index + 1)
}
}
13 changes: 8 additions & 5 deletions Sources/SymSpellSwift/SymSpell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ class SymSpell {

if lengthDiff < maxEditDistance, candidateLen <= prefixLength {
if verbosity != .all, lengthDiff >= maxEditDistance2 { continue }
for i in 0 ..< candidateLen {
let delete = candidate.removingCharacter(at: i)
for index in candidate.indices {
var delete = candidate
delete.remove(at: index)

if consideredDeletes.insert(delete).inserted {
candidates.append(delete)
Expand Down Expand Up @@ -531,7 +532,8 @@ class SymSpell {
guard deleteLen != 0 else { return true }

let suggestionLen = min(prefixLength, suggestionLen)
return !delete.prefix(deleteLen).contains { !suggestion.prefix(suggestionLen).contains($0) }
let suggestionPrefix = suggestion.prefix(suggestionLen)
return !delete.prefix(deleteLen).contains { !suggestionPrefix.contains($0) }
}

private func parseWords(_ text: String) -> [String] {
Expand All @@ -558,8 +560,9 @@ class SymSpell {
guard word.count > 1 else { return }

let editDistance = editDistance + 1
for i in 0 ..< word.count {
let delete = word.removingCharacter(at: i)
for index in word.indices {
var delete = word
delete.remove(at: index)
if deleteWords.insert(String(delete)).inserted, editDistance < maxDictionaryEditDistance {
edits(delete, editDistance, &deleteWords)
}
Expand Down

0 comments on commit 9b03364

Please sign in to comment.