FuzzyBeamSearch API Guide
This page explains how to use the FuzzyBeamSearch API from EdgeOCRExtensions to perform fuzzy string matching against dictionary data.
API Reference
See the Swift API Reference / Kotlin API Reference for detailed API specifications.
FuzzyBeamSearch API Overview
- Create a
FuzzyBeamSearchinstance with a search mode (substring/prefix/suffix). - Load dictionary data with
load(). - Call
match()with an input string and parameters to run fuzzy matching. - Retrieve matched text, cost, and position information from the returned list of
Candidateobjects.
1. Initialize and load the dictionary
- iOS (Swift)
- Android (Kotlin)
import EdgeOCRExtensions
func loadFuzzyBeamSearch() -> FuzzyBeamSearch {
// Create an instance with the desired search mode.
let fbs = FuzzyBeamSearch(.substring)
// Load dictionary data.
guard fbs.load(dictionaryPath) else {
fatalError("Failed to load dictionary")
}
// Verify the state after loading.
guard fbs.isLoaded() else {
fatalError("FuzzyBeamSearch is not ready")
}
print("Dictionary size: \(fbs.getDictionarySize())")
return fbs
}
import com.nefrock.edgevision.fuzzy.beamsearch.FuzzyBeamSearch
fun loadFuzzyBeamSearch(dictionaryPath: String): FuzzyBeamSearch {
// Create an instance with the desired search mode.
val fbs = FuzzyBeamSearch(
mode = FuzzyBeamSearch.ScanMode.Substring,
)
// Load dictionary data.
check(fbs.load(dictionaryPath)) { "Failed to load dictionary" }
// Verify the state after loading.
check(fbs.isLoaded()) { "FuzzyBeamSearch is not ready" }
println("Dictionary size: ${fbs.getDictionarySize()}")
return fbs
}
SearchMode/ScanMode— Specifies the search mode.substring— Matches against substrings of the input. Tolerates noise at the beginning and end.prefix— Matches from the beginning of the input.suffix— Matches from the end of the input.
dictionaryPath— Path to a directory containing dictionary files (.txt).
2. Run matching
- iOS (Swift)
- Android (Kotlin)
import EdgeOCRExtensions
// Configure parameters.
let params = FuzzyBeamSearch.Params()
params.subCost = 0.7 // Substitution cost
params.delCost = 0.35 // Deletion cost
params.insCost = 0.35 // Insertion cost
params.beamWidth = 400 // Beam width
params.topK = 10 // Maximum number of candidates to return
// Run fuzzy matching.
let candidates = fbs.match(inputText, params: params)
import com.nefrock.edgevision.fuzzy.beamsearch.FuzzyBeamSearch
// Configure parameters.
val params = FuzzyBeamSearch.Params().apply {
subCost = 0.7f // Substitution cost
delCost = 0.35f // Deletion cost
insCost = 0.35f // Insertion cost
beamWidth = 400 // Beam width
topK = 10 // Maximum number of candidates to return
}
// Run fuzzy matching.
val candidates = fbs.match(inputText, params)
Pass an input string and Params to match() to get a list of Candidate results. All parameters have default values, so you can use them as-is if no tuning is needed.
Main Parameters
| Parameter | Default | Description |
|---|---|---|
subCost | 0.7 | Cost scale for character substitution |
delCost | 0.35 | Cost for character deletion |
insCost | 0.35 | Cost for character insertion |
beamWidth | 400 | Beam search width. Larger values improve accuracy but reduce speed |
topK | 10 | Maximum number of candidates to return |
earlyStopOnTerm | true | Finalize a candidate when a terminal node is reached |
tailDeleteCost | 0.0 | Cost per extra trailing character beyond the terminal |
maxExtraSteps | 128 | Additional search steps beyond the input length |
Substring Mode Parameters
| Parameter | Default | Description |
|---|---|---|
startSkipMax | 32 | Maximum number of characters to skip at the start |
startDelCost | 0.0 | Cost per skipped character at the start (0.0 = free) |
keepOnePrefixChar | true | Keep at least one character at the beginning |
tailSkipMax | 32 | Maximum penalty cap for tail noise |
3. Read the results
- iOS (Swift)
- Android (Kotlin)
for candidate in candidates {
print("text: \(candidate.text)")
print("cost: \(candidate.cost)")
print("range: \(candidate.startIndex)..\(candidate.endIndex)")
}
// Use the candidate with the lowest cost (highest similarity).
if let best = candidates.first {
print("Best match: \(best.text) (cost: \(best.cost))")
}
for (candidate in candidates) {
println("text: ${candidate.text}")
println("cost: ${candidate.cost}")
println("range: ${candidate.startIndex}..${candidate.endIndex}")
}
// Use the candidate with the lowest cost (highest similarity).
candidates.firstOrNull()?.let { best ->
println("Best match: ${best.text} (cost: ${best.cost})")
}
The list of Candidate objects returned by match() is sorted by cost in ascending order (highest similarity first). Each Candidate has the following properties:
text— The matched dictionary entry textcost— The matching cost (closer to 0.0 means higher similarity)startIndex— Start position of the match in the input stringendIndex— End position of the match in the input string
If candidates is empty, no matching entries were found in the dictionary. Try increasing beamWidth or adjusting the cost parameters.