Fuzzy Search with Regular Expressions
Sample Code
This section explains how to perform fuzzy search on detected text using regular expressions. Even if the characters in the regular expression do not exactly match the characters in the text, matching can be performed within a certain tolerance.
Implementing FuzzyRegex
Use the SDK's FuzzyRegex class to perform matching between detection results and a regular expression.
- iOS (Swift)
- Android (Java)
class FuzzyRegexAnalyzer {
let fuzzyRegex = FuzzyRegex(
pattern: #"[0-9]+-[0-9]+"#,
fuzzyType: .NNDistance,
threshold: 0.4)
func analyze(_ detections: [Text]) -> AnalyzerResult {
var targetDetections = [Text]()
var notTargetDetections = [Text]()
for detection in detections {
let text = detection.getText()
let matched = fuzzyRegex.match(text)
if !matched.isEmpty {
detection.setText(matched)
targetDetections.append(detection)
} else {
notTargetDetections.append(detection)
}
}
return AnalyzerResult(
targetDetections: targetDetections,
notTargetDetections: notTargetDetections)
}
}
On Android, use the FuzzyRegex class in the same way to perform fuzzy matching with regular expressions. See the API reference for details.
Parameters
| Parameter | Description |
|---|---|
pattern | Regular expression pattern |
fuzzyType | Fuzzy search type (e.g., .NNDistance) |
threshold | Matching threshold (smaller values are stricter) |
Displaying Detection Results
Text that matches the regular expression is displayed in green, while non-matching text is displayed in red.
- iOS (Swift)
- Android (Java)
let detections = result.getTextDetections()
let analyzerResult = analyzer.analyze(detections)
for targetDetection in analyzerResult.getTargetDetections() {
let bbox = targetDetection.getBoundingBox()
drawDetection(bbox: bbox, text: targetDetection.getText())
}
for notTargetDetection in analyzerResult.getNotTargetDetections() {
let bbox = notTargetDetection.getBoundingBox()
drawDetection(
bbox: bbox, text: notTargetDetection.getText(),
boxColor: UIColor.red.withAlphaComponent(0.5).cgColor)
}
On Android, color-coded display is performed based on FuzzyRegex results in the same way. See the sample app for details.