![](/img/trans.png)
[英]How can I split paragraphs into proper sentences in java using split()?
[英]How to split paragraphs into sentences?
請看下面的內容。
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 U.S 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 U.S and numbers like 2.2.
They all got split by the above code.
String[] sentenceHolder = titleAndBodyContainer.split("(?i)(?<=[.?!])\\S+(?=[a-z])");
試試這個對我有用。
這會將段落分開.
?
!
:
String a[]=str.split("\\.|\\?|\\!");
您可以在\\\\
之后放置任何您想要使用的符號並使用|
分開每個條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.