java - Android从字符串文本中提取句子

标签 java android split

我正在 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/

相关文章:

android - 自动完成 Google Places 出现错误 :This API project is not authorized to use this API. 请确保已在 API 控制台中激活此 API

android - Firebase 身份验证 - 未处理的异常 : [firebase_auth/admin-restricted-operation] This operation is restricted to administrators only

java - MPAndroidChart - 如何最好地将 X 轴值设置为字符串/日期?

java - 使用基本身份验证配置 Spring Boot 的嵌入式 tomcat 容器

java - Hashmap 中带有空格的 JSON - 来自 api

java - 如何以MapReduce格式在一行中打印一些 token ?

regex - 使用 regexp_split_to_array 将文本列拆分为 2

java - 如何使用给定的数字和空格来分割字符串?

java - BufferedReader 读取线

java - Windows 上的 "lo"接口(interface)等效项是什么