【问题标题】:Counting number of words in a file计算文件中的单词数
【发布时间】:2011-05-04 21:26:55
【问题描述】:

我在计算文件中的字数时遇到问题。我正在采取的方法是,当我看到一个空格或一个新行时,我就知道要计算一个单词。

问题是,如果段落之间有多行,那么我最终也会将它们计为单词。如果您查看 readFile() 方法,您可以看到我在做什么。

你能帮我解决这个问题吗?

示例输入文件(包括一个空行):

word word word
word word

word word word

【问题讨论】:

标签: java algorithm loops io


【解决方案1】:

只需保留一个布尔标志,让您知道前一个字符是否为空格(伪代码如下):

boolean prevWhitespace = false;
int wordCount = 0;
while (char ch = getNextChar(input)) {
  if (isWhitespace(ch)) {
    if (!prevWhitespace) {
      prevWhitespace = true;
      wordCount++;
    }
  } else {
    prevWhitespace = false;
  }
}

【讨论】:

    【解决方案2】:

    我会稍微改变你的方法。首先,我将使用BufferedReader 使用readLine() 逐行读取文件文件。然后使用String.split("\\s") 在空白处分割每一行,并使用结果数组的大小来查看该行上有多少单词。要获取字符数,您可以查看每行的大小或每个拆分词的大小(取决于您是否要将空格计为字符)。

    【讨论】:

      【解决方案3】:

      破解解决方案

      您可以将文本文件读入 String var。然后使用单个空格作为分隔符 StringVar.Split(" ") 将字符串拆分为一个数组。

      数组计数将等于文件中“单词”的数量。 当然,这不会给您计算行号。

      【讨论】:

        【解决方案4】:

        您可以使用带有 FileInputStream 的 Scanner,而不是带有 FileReader 的 BufferedReader。例如:-

        File file = new File("sample.txt");
        try(Scanner sc = new Scanner(new FileInputStream(file))){
            int count=0;
            while(sc.hasNext()){
                sc.next();
                count++;
            }
        System.out.println("Number of words: " + count);
        }
        

        【讨论】:

        • 上面的代码给出了错误的字数,因为它计算了所有的样式、调整等......只需用这个String word=sc.next(); if(word.indexOf("\\")==-1) count++;修改while循环内的代码这会更精确一点数......
        • @SangeetMenon 你是什么意思?我不明白它计算所有样式和调整是什么意思。能举个例子吗?
        【解决方案5】:

        3个步骤:消耗所有的空白,检查是否是一行,消耗所有的nonwhitespace.3

        while(true){
            c = inFile.read();                
            // consume whitespaces
            while(isspace(c)){ inFile.read() }
            if (c == '\n'){ numberLines++; continue; }
            while (!isspace(c)){
                 numberChars++;
                 c = inFile.read();
            }
            numberWords++;
        }
        

        【讨论】:

          【解决方案6】:

          这只是一个想法。有一种非常简单的方法可以做到这一点。如果您只需要单词数量而不是实际单词,那么只需使用 Apache WordUtils

          import org.apache.commons.lang.WordUtils;
          
          public class CountWord {
          
          public static void main(String[] args) {    
          String str = "Just keep a boolean flag around that lets you know if the previous character was whitespace or not pseudocode follows";
          
              String initials = WordUtils.initials(str);
          
              System.out.println(initials);
              //so number of words in your file will be
              System.out.println(initials.length());    
            }
          }
          

          【讨论】:

            【解决方案7】:

            我认为正确的方法是使用正则表达式:

            String fileContent = <text from file>;    
            String[] words = Pattern.compile("\\s+").split(fileContent);
            System.out.println("File has " + words.length + " words");
            

            希望对您有所帮助。 “\s+”的意思在Pattern javadoc

            【讨论】:

              【解决方案8】:
              import java.io.BufferedReader;
              import java.io.FileReader;
              
              public class CountWords {
              
                  public static void main (String args[]) throws Exception {
              
                     System.out.println ("Counting Words");       
                     FileReader fr = new FileReader ("c:\\Customer1.txt");        
                     BufferedReader br = new BufferedReader (fr);     
                     String line = br.readLin ();
                     int count = 0;
                     while (line != null) {
                        String []parts = line.split(" ");
                        for( String w : parts)
                        {
                          count++;        
                        }
                        line = br.readLine();
                     }         
                     System.out.println(count);
                  }
              }
              

              【讨论】:

              • 记得关闭阅读器!
              【解决方案9】:

              文件字数统计

              如果单词之间有一些符号,那么你可以拆分并计算单词的数量。

              Scanner sc = new Scanner(new FileInputStream(new File("Input.txt")));
                      int count = 0;
                      while (sc.hasNext()) {
              
                          String[] s = sc.next().split("d*[.@:=#-]"); 
              
                          for (int i = 0; i < s.length; i++) {
                              if (!s[i].isEmpty()){
                                  System.out.println(s[i]);
                                  count++;
                              }   
                          }           
                      }
                      System.out.println("Word-Count : "+count);
              

              【讨论】:

                【解决方案10】:

                在这里看看我的解决方案,它应该可以工作。这个想法是从单词中删除所有不需要的符号,然后将这些单词分开并将它们存储在其他变量中,我使用的是 ArrayList。通过调整“excludedSymbols”变量,您可以添加更多您希望从单词中排除的符号。

                public static void countWords () {
                    String textFileLocation ="c:\\yourFileLocation";
                    String readWords ="";
                    ArrayList<String> extractOnlyWordsFromTextFile = new ArrayList<>();
                    // excludedSymbols can be extended to whatever you want to exclude from the file 
                    String[] excludedSymbols = {" ", "," , "." , "/" , ":" , ";" , "<" , ">", "\n"};
                    String readByteCharByChar = "";
                    boolean testIfWord = false;
                
                
                    try {
                        InputStream inputStream = new FileInputStream(textFileLocation);
                        byte byte1 = (byte) inputStream.read();
                        while (byte1 != -1) {
                
                            readByteCharByChar +=String.valueOf((char)byte1);
                            for(int i=0;i<excludedSymbols.length;i++) {
                            if(readByteCharByChar.equals(excludedSymbols[i])) {
                                if(!readWords.equals("")) {
                                extractOnlyWordsFromTextFile.add(readWords);
                                }
                                readWords ="";
                                testIfWord = true;
                                break;
                            }
                            }
                            if(!testIfWord) {
                                readWords+=(char)byte1;
                            }
                            readByteCharByChar = "";
                            testIfWord = false;
                            byte1 = (byte)inputStream.read();
                            if(byte1 == -1 && !readWords.equals("")) {
                                extractOnlyWordsFromTextFile.add(readWords);
                            }
                        }
                        inputStream.close();
                        System.out.println(extractOnlyWordsFromTextFile);
                        System.out.println("The number of words in the choosen text file are: " + extractOnlyWordsFromTextFile.size());
                    } catch (IOException ioException) {
                
                        ioException.printStackTrace();
                    }
                }
                

                【讨论】:

                  【解决方案11】:

                  这可以通过使用 Java 8 的方式完成:

                  Files.lines(Paths.get(file))
                      .flatMap(str->Stream.of(str.split("[ ,.!?\r\n]")))
                      .filter(s->s.length()>0).count();
                  

                  【讨论】:

                    【解决方案12】:
                    BufferedReader bf= new BufferedReader(new FileReader("G://Sample.txt"));
                            String line=bf.readLine();
                            while(line!=null)
                            {
                                String[] words=line.split(" ");
                                System.out.println("this line contains " +words.length+ " words");
                                line=bf.readLine();
                            }
                    

                    【讨论】:

                      【解决方案13】:

                      以下代码支持 Java 8

                      //将文件读入字符串

                      String fileContent=new String(Files.readAlBytes(Paths.get("MyFile.txt")),StandardCharacters.UFT_8);
                      

                      //通过分隔符将它们保存到字符串列表中

                      List<String> words = Arrays.asList(contents.split("\\PL+"));
                      
                      int count=0;
                      for(String x: words){
                       if(x.length()>1) count++;
                      }
                      
                      sop(x);
                      

                      【讨论】:

                        【解决方案14】:

                        如此简单,我们可以通过以下方法从文件中获取字符串:getText();

                        public class Main {
                        
                            static int countOfWords(String str) {
                                if (str.equals("") || str == null) {
                                    return 0;
                                }else{
                                    int numberWords = 0;
                                    for (char c : str.toCharArray()) {
                                        if (c == ' ') {
                                            numberWords++;
                                        }
                                    }
                        
                                    return ++numberWordss;
                                }
                            }
                        }
                        

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2015-07-13
                          • 2015-06-02
                          • 1970-01-01
                          相关资源
                          最近更新 更多