简体   繁体   English

如何在iOS中使用Swift格式化字符串?

[英]How do I format a string with Swift in iOS?

I am working on an iOS Swift project that takes takes OCR data and then searches the text for key phrases. 我正在开发一个iOS Swift项目,该项目需要OCR数据,然后在文本中搜索关键短语。 The OCR output looks like this: OCR输出如下所示:

INGREDIENTS WATER, BROWN SUGAR, RED RIPE 成分水,棕糖,红丝带

TOMATO CONCENTRATE, APPLE CIDERVINEGAR 番茄浓缩汁,苹果醋

W01CESTERSHlWSMJCE(WATERW4EGAR CORN W01CESTERSHlWSMJCE(WATERW4EGAR CORN

SYRUP, SALT, MOLASSE, SPICE, NATURAL FLAVOR 糖浆,盐,糖蜜,香料,天然香料

GARLIC POWDER, CARAMEL COLOR, ANCHOVIES 大蒜粉,焦糖色,AN鱼

CFlSril,TAMARiN0), MOLASSES, LEMON JUICE, CFlSril,TAMARiN0),糖浆,柠檬汁,

ONION, HONEY, MODIFIED TAVIOCA STARCH, 洋葱,蜂蜜,改良的TAVIOCA淀粉,

When I search the string for "corn syrup", nothing is found. 当我在字符串中搜索“玉米糖浆”时,什么也没找到。 Searching for "corn" and "syrup" does produce positive results. 搜索“玉米”和“糖浆”确实会产生积极的结果。

I have also tried 我也尝试过

tesseract.recognizedText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

to no avail. 无济于事。

Any thoughts on how to format this text for searching that would allow "corn syrup" to be identified? 关于如何格式化此搜索文本的想法,可以识别“玉米糖浆”? The qualifier is that only the exact phrase is useful - after all there are corn, corn starch, maple syrup, etc. as potential ingredients. 限定词是只有确切的短语才有用-毕竟有玉米,玉米淀粉,枫糖浆等作为潜在成分。

Thanks. 谢谢。

OK here is the solution that worked 好的,这是有效的解决方案

'textView.text = tesseract.recognizedText.stringByReplacingOccurrencesOfString("\\n", withString: " ", options: NSStringCompareOptions.LiteralSearch, range: nil)' 'textView.text = tesseract.recognizedText.stringByReplacingOccurrencesOfString(“ \\ n”,withString:“”,选项:NSStringCompareOptions.LiteralSearch,范围:nil)'

I thought the initial code was accomplishing the same task. 我以为最初的代码可以完成相同的任务。

If you want to search for "corn syrup", you most likely need to replace all new lines with spaces (and then ideally check for double spaces and replace with single space). 如果要搜索“玉米糖浆”,则很可能需要所有新行替换为空格(然后理想地检查是否有两个空格并替换为单个空格)。

The quality of the character recognition is not very good and I think the text would deserve more maintenance before being used for searching. 字符识别的质量不是很好,我认为文本在用于搜索之前应该得到更多维护。 You might, for example split the phrases into array of individual strings, then trim spaces etc. from beginning and the end, perhaps you could use UITextChecker to help identify misspelled terms and fix them... 例如,您可能将短语分成单个字符串数组,然后从头到尾修剪空格等,也许您可​​以使用UITextChecker来帮助识别拼写错误的术语并加以修复...

That's because "corn syrup", which is the string you're looking for, is not the same as "corn\\nsyrup", which is what your wall of text is showing. 这是因为您要查找的字符串“玉米糖浆”与您的文字墙所显示的“ corn \\ nsyrup”不同。

You could instead try searching for "corn\\nsyrup" or "corn \\nsyrup" instead. 您可以尝试搜索“ corn \\ nsyrup”或“ corn \\ nsyrup”。

Notice in your picture how "corn\\nsyrup" produces the same results that your wall of text is showing? 注意图片中的“ corn \\ nsyrup”如何产生与您的文字墙相同的结果?

Also, your code to replace "\\n" by " " might not be working because it could be "corn\\n syrup", which will make it have 2 spaces in between. 另外,用“”替换“ \\ n”的代码可能无法正常工作,因为它可能是“玉米\\ n糖浆”,这将使其之间有2个空格。

图片比较

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

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