如何将段落分成句子?
请看下面的内容。
String[]sentenceHolder = titleAndBodyContainer.split("\n|\\.(?!\\d)|(?<!\\d)\\.");
这就是我试图将一个段落分成句子的方式。 但有个问题。 我的段落包括Jan. 13, 2014
日期,像US
这样的字样和2.2
类的数字。 他们都被上面的代码分开了。 所以基本上,这个代码分裂了许多“点”,无论它是否完整。
我试过String[]sentenceHolder = titleAndBodyContainer.split(".\n");
和String[]sentenceHolder = titleAndBodyContainer.split("\\.");
同样。 都失败了。
如何“恰当地”将段落分成句子?
你可以试试这个
String str = "This is how I tried to split a paragraph into a sentence. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like US and numbers like 2.2. They all got split by the above code."; Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)", Pattern.MULTILINE | Pattern.COMMENTS); Matcher reMatcher = re.matcher(str); while (reMatcher.find()) { System.out.println(reMatcher.group()); }
输出:
This is how I tried to split a paragraph into a sentence. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like US and numbers like 2.2. They all got split by the above code.
String[] sentenceHolder = titleAndBodyContainer.split("(?i)(?<=[.?!])\\S+(?=[az])");
试试这个对我有用。
这会将段落分开.
?
!
:
String a[]=str.split("\\.|\\?|\\!");
您可以在\\
之后放置任何您想要使用的符号并使用|
分开每个条件。