Skip to main content

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​

  1. Create a FuzzyBeamSearch instance with a search mode (substring / prefix / suffix).
  2. Load dictionary data with load().
  3. Call match() with an input string and parameters to run fuzzy matching.
  4. Retrieve matched text, cost, and position information from the returned list of Candidate objects.

1. Initialize and load the dictionary​

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
}
  • 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​

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)

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​

ParameterDefaultDescription
subCost0.7Cost scale for character substitution
delCost0.35Cost for character deletion
insCost0.35Cost for character insertion
beamWidth400Beam search width. Larger values improve accuracy but reduce speed
topK10Maximum number of candidates to return
earlyStopOnTermtrueFinalize a candidate when a terminal node is reached
tailDeleteCost0.0Cost per extra trailing character beyond the terminal
maxExtraSteps128Additional search steps beyond the input length

Substring Mode Parameters​

ParameterDefaultDescription
startSkipMax32Maximum number of characters to skip at the start
startDelCost0.0Cost per skipped character at the start (0.0 = free)
keepOnePrefixChartrueKeep at least one character at the beginning
tailSkipMax32Maximum penalty cap for tail noise

3. Read the results​

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))")
}

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 text
  • cost — The matching cost (closer to 0.0 means higher similarity)
  • startIndex — Start position of the match in the input string
  • endIndex — 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.