我正在 Android 上开发食谱应用程序。我的食谱源(Rest API)基本上是一项允许用户手动向网站提交食谱的服务,这就是他们以 JSON 形式存档并返回给客户端的内容。
基本上,JSON 的“方向”部分只返回一大块文本作为说明。除了换行符外,它没有格式化。
为了给你一个想法,这里有一个返回的一些指令的例子
Place cast iron skillet in oven and heat oven to 500 degrees. Bring steak(s) to room temperature. Season both sides with salt, pepper, garlic powder, and cayenne pepper. When oven reaches temperature, remove pan and place on range over high heat. Immediately place steak in the middle of hot, dry pan. Cook 30 seconds without moving. Turn with tongs and cook another 30 seconds, then put the pan straight into the oven for 2 minutes. Flip steak and cook for another 2 minutes. (This time is for medium rare steaks. If you prefer medium, add a minute to both of the oven turns.) Remove steak from pan, cover loosely with foil, and rest for 2 minutes. Serve whole or slice thin and fan onto plate.
我需要将这一长串文本分成句子,以便在我设计的自定义 View 中显示每个句子。
有人知道我可以用来完成此任务的任何库吗?我知道英语语言检测可能很困难,并且在 research 之后我了解到这对于正则表达式来说并不是真正可行的,所以我现在正在考虑我的选择。如果有人知道任何事情,请随时分享!像往常一样谢谢你们。
最佳答案
private void ParagraphToSentences(){
String paragraph = "Place cast iron skillet in oven and heat oven to 500 degrees. Bring steak(s) to room temperature. Season both sides with salt, pepper, garlic powder, and cayenne pepper. When oven reaches temperature, remove pan and place on range over high heat. Immediately place steak in the middle of hot, dry pan. Cook 30 seconds without moving. Turn with tongs and cook another 30 seconds, then put the pan straight into the oven for 2 minutes. Flip steak and cook for another 2 minutes. (This time is for medium rare steaks. If you prefer medium, add a minute to both of the oven turns.) Remove steak from pan, cover loosely with foil, and rest for 2 minutes. Serve whole or slice thin and fan onto plate.";
Pattern sentencePatterns = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)", Pattern.MULTILINE | Pattern.COMMENTS);
Matcher match = sentencePatterns.matcher(paragraph);
while (match.find()) {
System.out.println(match.group());
}
}
关于java - Android从字符串文本中提取句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264134/