简体   繁体   中英

Tesseract in iOS (Swift) - How to Separate Text and Numbers in UITextField?

I have a Swift-based application that currently implements the Tesseract OCR framework (similar to the form in this tutorial: http://www.raywenderlich.com/93276/implementing-tesseract-ocr-ios ). So upon taking a picture and employing Tesseract, I obtain the following output in a UITextField object:

Subtotal 155.60
Tax 14.02
Total 169.82

So now I would like to separate the text from the numbers in the UITextField. I was considering using the "contain" function built into Swift on a matrix containing all values in price format ([0.01 0.02, etc.]) but this will only return a boolean as outlined in this post ( How to have a textfield scan for all values in an array individually in swift? ). Does anyone have any suggestions on how to do this? Cheers!

Tesseract Implementation

func performImageRecognition(image: UIImage)        
    // 0

    // 1
    let tesseract = G8Tesseract()

    // 2
    tesseract.language = "eng"

    // 3
    tesseract.engineMode = .TesseractCubeCombined

    // 4
    tesseract.pageSegmentationMode = .Auto

    // 5
    tesseract.maximumRecognitionTime = 60.0

    // 6
    tesseract.image = image.g8_blackAndWhite()
    tesseract.recognize()

    // 7
    textView.text = tesseract.recognizedText
    textView.editable = true

Sounds like you might want to look into using Regular Expressions

func seperate (text: String) -> (text: String?, value: String?) {

    // You might want to do an extra check here to ensure the whole string is valid
    // i.e., nothing in between the two parts of the string

    let textMatch = text.rangeOfString("^([A-Z]|[a-z])+", options: .RegularExpressionSearch)
    let priceMatch = text.rangeOfString("[0-9]*.[0-9]{2}$", options: .RegularExpressionSearch)
    // You might want to adjust regex to handle price edge cases, such as 15 (rather than 15.00) etc

    if let textMatch = textMatch, priceMatch = priceMatch {
        let textValue = text.substringWithRange(textMatch)
        let priceValue = text.substringWithRange(priceMatch)
        return(textValue, priceValue)
    } else {
        return (nil, nil)
    }

}

seperate("Subtotal 155.60") // -> Subtotal, 155.60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM