简体   繁体   English

iOS中的Tesseract(Swift)-如何在UITextField中分隔文本和数字?

[英]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 ). 我有一个基于Swift的应用程序,该应用程序当前实现了Tesseract OCR框架(类似于本教程中的表格: 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: 因此,在拍照并使用Tesseract时,我在UITextField对象中获得以下输出:

Subtotal 155.60
Tax 14.02
Total 169.82

So now I would like to separate the text from the numbers in the UITextField. 所以现在我想将文本与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? ). 我正在考虑在包含价格格式的所有值的矩阵([0.01 0.02等])上使用Swift内置的“包含”功能,但这只会返回本文所述的布尔值( 如何进行文本字段扫描)迅速将数组中的所有值单独添加? )。 Does anyone have any suggestions on how to do this? 有人对此有任何建议吗? Cheers! 干杯!

Tesseract Implementation Tesseract实施

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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