繁体   English   中英

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

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

我有一个基于Swift的应用程序,该应用程序当前实现了Tesseract OCR框架(类似于本教程中的表格: http : //www.raywenderlich.com/93276/implementing-tesseract-ocr-ios )。 因此,在拍照并使用Tesseract时,我在UITextField对象中获得以下输出:

Subtotal 155.60
Tax 14.02
Total 169.82

所以现在我想将文本与UITextField中的数字分开。 我正在考虑在包含价格格式的所有值的矩阵([0.01 0.02等])上使用Swift内置的“包含”功能,但这只会返回本文所述的布尔值( 如何进行文本字段扫描)迅速将数组中的所有值单独添加? )。 有人对此有任何建议吗? 干杯!

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

听起来您可能想研究使用正则表达式

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