我猜是以字符串结尾的句子!
除了像Dr. .先生这样的事情,因为语法的原因,你确实不可能真正地知道java中的句子。
但我想我的意思是一个句号、感叹号或问号,然后是大写字母。
一个人会怎么做。
这是我所拥有的,但它行不通.
BufferedReader Compton = new BufferedReader(new FileReader(fileName));
int sentenceCount=0;
String violet;
String limit="?!.";
while(Compton.ready())
{
violet=Compton.readLine();
for(int i=0; i<violet.length()-1;i++)
{
if(limit.indexOf(violet.charAt(i)) != -1 && i>0 && limit.indexOf(violet.charAt(i-1)) != -1)
{
sentenceCount++;
}
}
}
System.out.println("the amount of sentence is " + sentenceCount);编辑工作更好的新方法
String violet;
while(Compton.ready())
{
violet=Compton.readLine();
sentenceCount=violet.split("[!?.:]+").length;
System.out.println("the number of words in line is " +
sentenceCount);
}发布于 2015-02-02 19:53:49
BufferedReader reader = new BufferedReader(new FileReader(fileName));
int sentenceCount = 0;
String line;
String delimiters = "?!.";
while ((line = reader.readLine()) != null) { // Continue reading until end of file is reached
for (int i = 0; i < line.length(); i++) {
if (delimiters.indexOf(line.charAt(i)) != -1) { // If the delimiters string contains the character
sentenceCount++;
}
}
}
reader.close();
System.out.println("The number of sentences is " + sentenceCount);发布于 2015-02-02 20:03:36
一艘班轮:
int n = new String (Files.readAllBytes(Paths.get(path))).split ("[\\.\\?!]").length使用Java 7构造读取整个文件到字节数组,从该数组创建一个字符串,然后拆分成句子数组,然后获取数组的长度。
发布于 2015-02-02 21:51:28
一种可能的方法是将文件扫描为单词,然后计数不在异常列表中的、以给定标点符号结尾的单词。
下面是使用Java 8流的一个可能的实现:
List<String> exceptions = Arrays.toList("Dr.", "Mr.");
Iterable<String> iterableScanner = () -> new Scanner(filename);
int sentenceCount = StreamSupport.stream(iterableScanner, false)
.filter(word -> word.matches(".*[\\.\\?!]))
.filter(word -> !exceptions.contains(word))
.count();https://stackoverflow.com/questions/28284972
复制相似问题