简体   繁体   English

Java从文本文件中读取元音

[英]java read vowels from text file

I'm creating a program that reads vowels from a text file. 我正在创建一个从文本文件中读取元音的程序。 The text is a paragraph long and I want the program to count the vowels per sentence. 文本长了一段,我希望程序计算每个句子中的元音。

So this is an example 7 vowels 这是7个元音的例子

Another 3 vowels 另外3个元音

So far I've written the code to be able to read vowels. 到目前为止,我已经编写了能够读取元音的代码。 Though, it reads it as an additional whole. 虽然,它作为一个附加的整体来读取。 In the loop it will count 7 first then the second line would output it as 10. I want it to output 7 as first line and 3 as second line. 在循环中,它将首先计数7,然后第二行将其输出为10。我希望它输出7作为第一行,输出3作为第二行。

I'm looking at the String API from java and I don't see anything that can help solve this. 我正在查看来自Java的String API,但没有发现任何可以帮助解决此问题的方法。 The way I'm currently counting the vowels is having a for loop to loop through with Charat(). 我目前计算元音的方式是使用for循环与Charat()循环。 Am I missing something or is there no way to stop it from reading and adding to the counter? 我是否缺少某些东西,还是没有办法阻止它阅读并添加到柜台?

Here's an example 这是一个例子

    while(scan.hasNext){
      String str = scan.nextLine();
      for(int i = 0; i<str.length(); i++){
        ch = str.charAt(i);
        ...
        if(...)
          vowel++;
        }//end for
      S.O.P();
        vowel = 0;//This is the answer... Forgotten that java is sequential...
      }

    }// end main()
  }//end class

  /*output:
  This sentence have 7 vowels.
  This sentence have 3 vowels.
  */

I created a simple class to achieve what I believe your goal is. 我创建了一个简单的类来实现我认为的目标。 The vowelTotal resets so that you don't have the issue you mentioned of the sentences' vowels adding to each other. vowelTotal会重置,因此您不会遇到提到的句子中的元音相互叠加的问题。 I'm assuming that by looking at my code, you can see the solution to your own? 我假设通过查看我的代码,您可以看到自己的解决方案吗? Also, this code assumes you include "y" as a vowel and it also assumes the sentences end with proper punctuation. 另外,此代码假定您将“ y”作为元音,并且还假定句子以正确的标点符号结尾。

public class CountVowels{
    String paragraph;
    public CountVowels(String paragraph){
        this.paragraph = paragraph;
        countVowels(paragraph);
    }

    int vowelTotal = 0;
    int sentenceNumber = 0;
    public void countVowels(String paragraph){
        for(int c = 0; c < paragraph.length(); c++){
            if( paragraph.charAt(c) == 'a' || paragraph.charAt(c) == 'e' || paragraph.charAt(c) == 'i' || paragraph.charAt(c) == 'o' || paragraph.charAt(c) == 'u' || paragraph.charAt(c) == 'y'){
                vowelTotal++; //Counts a vowel
            } else if( paragraph.charAt(c) == '.' || paragraph.charAt(c) == '!' || paragraph.charAt(c) == '?' ){
                sentenceNumber++; //Used to tell which sentence has which number of vowels
                System.out.println("Sentence " + sentenceNumber + " has " + vowelTotal + " vowels.");
                vowelTotal = 0; //Resets so that the total doesn't keep incrementing
            }
        }
    }
}

Maybe not the most elegant way but real quick to count vowels in each sentence I came up with this, tested and works (at least with my test string): 也许不是最优雅的方式,但是真正快速地在每个句子中计算元音,这是我想出的,经过测试并且可以使用的(至少使用我的测试字符串):

String testString = ("This is a test string. This is another sentence. " +
            "This is yet a third sentence! This is also a sentence?").toLowerCase();
    int stringLength = testString.length();
    int totalVowels = 0;
    int i;

        for (i = 0; i < stringLength - 1; i++) {
            switch (testString.charAt(i)) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    totalVowels++;
                    break;
                case '?':
                case '!':
                case '.':
                    System.out.println("Total number of vowels in sentence: " + totalVowels);
                    totalVowels = 0;
            }

        }

    System.out.println("Total number of vowels in last sentence: " + totalVowels);

Here is a complete example to count the number of vowels in each sentence of a file. 这是一个完整的示例,用于计算文件中每个句子中的元音数量。 It uses some advanced techniques: (1) a regular expression to split paragraphs into sentences; 它使用一些高级技术:(1)正则表达式将段落拆分为句子; and (2) a HashSet data structure to quickly check if a character is a vowel. (2)HashSet数据结构,用于快速检查字符是否为元音。 The program assumes that each line in the file is a paragraph. 该程序假定文件中的每一行都是一个段落。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CountVowels {

    // HashSet of vowels to quickly check if a character is a vowel.
    // See usage below.
    private Set<Character> vowels =
        new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'y'));

    // Read a file line-by-line. Assume that each line is a paragraph.
    public void countInFile(String fileName) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;

        // Assume one file line is a paragraph.
        while ((line = br.readLine()) != null) {
            if (line.length() == 0) {
                continue; // Skip over blank lines.
            }
            countInParagraph(line);
        }

        br.close();
    }

    // Primary function to count vowels in a paragraph. 
    // Splits paragraph string into sentences, and for each sentence,
    // counts the number of vowels.
    private void countInParagraph(String paragraph) {

        String[] sentences = splitParagraphIntoSentences(paragraph);

        for (String sentence : sentences) {
            sentence = sentence.trim(); // Remove whitespace at ends.
            int vowelCount = countVowelsInSentence(sentence);
            System.out.printf("%s : %d vowels\n", sentence, vowelCount);
        }
    }

    // Splits a paragraph string into an array of sentences. Uses a regex.
    private String[] splitParagraphIntoSentences(String paragraph) {
        return paragraph.split("\n|((?<!\\d)\\.(?!\\d))");
    }

    // Counts the number of vowels in a sentence string.
    private int countVowelsInSentence(String sentence) {

        sentence = sentence.toLowerCase();

        int result = 0;    
        int sentenceLength = sentence.length();

        for (int i = 0; i < sentenceLength; i++) {
            if (vowels.contains(sentence.charAt(i))) {
                result++;
            }
        }

        return result;
    }

    // Entry point into the program.
    public static void main(String argv[]) throws IOException {

        CountVowels cw = new CountVowels();

        cw.countInFile(argv[0]);
    }
}

For this file example.txt: 对于此文件example.txt:

So this is an example. Another.

This is Another line.

Here is the result: 结果如下:

% java CountVowels example.txt
So this is an example : 7 vowels
Another : 3 vowels
This is Another line : 7 vowels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM