猿问

计算java字符串中的句子数

我想计算一个字符串中的句子数,到目前为止我正在使用这个:

int count = str.split("[!?.:]+").length;

但我的字符串包含“。” 例如在名字和单词之间

“他的名字是 Walton DC,他去年刚刚完成了他的 B.Tech。”

现在使用上面的行作为示例 count 将返回 4 个句子,但只有一个。

那么如何应对这些情况呢?


慕妹3146593
浏览 155回答 3
3回答

森栏

您可以使用BreakIterator,并检测不同类型的文本边界在你的情况下句子:private static void markBoundaries(String target, BreakIterator iterator) {&nbsp; &nbsp; StringBuffer markers = new StringBuffer();&nbsp; &nbsp; markers.setLength(target.length() + 1);&nbsp; &nbsp; for (int k = 0; k < markers.length(); k++) {&nbsp; &nbsp; &nbsp; &nbsp; markers.setCharAt(k, ' ');&nbsp; &nbsp; }&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; iterator.setText(target);&nbsp; &nbsp; int boundary = iterator.first();&nbsp; &nbsp; while (boundary != BreakIterator.DONE) {&nbsp; &nbsp; &nbsp; &nbsp; markers.setCharAt(boundary, '^');&nbsp; &nbsp; &nbsp; &nbsp; ++count;&nbsp; &nbsp; &nbsp; &nbsp; boundary = iterator.next();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(target);&nbsp; &nbsp; System.out.println(markers);&nbsp; &nbsp; System.out.println("Number of Boundaries: " + count);&nbsp; &nbsp; System.out.println("Number of Sentences: " + (count-1));}public static void main(String[] args) {&nbsp; &nbsp; Locale currentLocale = new Locale("en", "US");&nbsp; &nbsp; BreakIterator sentenceIterator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = BreakIterator.getSentenceInstance(currentLocale);&nbsp; &nbsp; String someText = "He name is Walton D.C. and he just completed his B.Tech last year.";&nbsp; &nbsp; markBoundaries(someText, sentenceIterator);&nbsp; &nbsp; someText = "This order was placed for QT3000! MK?";&nbsp; &nbsp; markBoundaries(someText, sentenceIterator);}输出将是:He name is Walton D.C. and he just completed his B.Tech last year.^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^Number of Boundaries: 2Number of Sentences: 1This order was placed for QT3000! MK?^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^&nbsp; ^Number of Boundaries: 3Number of Sentences: 2

繁星coding

解决方案可能是在点的情况下,您可以检查后面是否有空格和大写字母。“[点][空格][大写字母]”这将是对判决的肯定更新相同的代码:public static void main( String args[] ) {&nbsp; &nbsp; &nbsp; // String to be scanned to find the pattern.&nbsp; &nbsp; &nbsp; String line = "This order was placed for QT3000! MK? \n Thats amazing. \n But I am not sure.";&nbsp; String pattern = "([.!?])([\\s\\n])([A-Z]*)";&nbsp; // Create a Pattern object&nbsp; Pattern r = Pattern.compile(pattern);&nbsp; // Now create matcher object.&nbsp; Matcher m = r.matcher(line);&nbsp; int count=0;&nbsp; while (m.find( )) {&nbsp; &nbsp; &nbsp; count++;&nbsp; }&nbsp; count++; //for the last line, which will not get included here.&nbsp; System.out.println("COUNT=="+count);}

元芳怎么了

简单的方法公共类计数线{public static void main(String[] args) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; String s="Find the number Sentence";&nbsp; &nbsp; int count=0;&nbsp; &nbsp; for (int i = 0; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(s.charAt(i)==' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; count=count+1;&nbsp; &nbsp; System.out.println(count);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答