【问题标题】:How to calculate the frequency of a word from a txt file - Java如何从 txt 文件中计算单词的频率 - Java
【发布时间】:2017-04-03 09:09:51
【问题描述】:

我需要一些有关此代码的帮助。我希望我的程序计算与描述的模式匹配的每个单词的频率。

public class Project {
    public static void main(String[] args) throws FileNotFoundException{
    Scanner INPUT_TEXT = new Scanner(new File("moviereview.txt")).useDelimiter(" ");

    String pattern = "[a-zA-Z'-]+";
    Pattern r = Pattern.compile(pattern);

    int occurences=0;

    while(INPUT_TEXT.hasNext()){
        //read next word
        String Stringcandidate=INPUT_TEXT.next();   

        //see if pattern matches (boolean find)
        if(r.matcher(Stringcandidate).find()) {
            occurences++; //increment occurences if pattern is found
            String moviereview = m.group(0); //retrieve found string
            String moviereview2 = moviereview.toLowerCase(); // ???

            System.out.println(moviereview2 + " appears " + occurences);
            if(occurences>1){
                 System.out.println(" times\n");
            }
            else{
                System.out.println(" time\n");
            }
        }
        INPUT_TEXT.close();//Close your Scanner.     
    }

}

【问题讨论】:

  • 你能说得更具体点吗?现在发生了什么?我们不是来为您运行代码的。此外,我们没有您的文本文件
  • 我帮不了你。当您甚至懒得正确格式化(缩进)代码以显示代码结构时,我拒绝查看代码。
  • 欢迎来到 StackOverflow。如果您遵循帮助中心的指南,您最有可能获得有用的答案。例如,像这个问题:“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。”
  • 您是否曾经将变量 occurences 设置为 0 以外的值?如果没有,您的 IDE 应该警告您。更多提示:How to debug small programs.
  • 请举例说明“moviereview.txt”的内容。代码现在出现的方式对所有单词进行全局计数。我认为您希望与模式匹配的不同单词单独计算。因此,您需要记住在地图或基数树或您喜欢的任何数据结构/算法中找到的单词。

标签: java frequency word-count


【解决方案1】:

正如我之前的评论中所述,可以使用Map 实现,如HashMap,来存储匹配的单词及其出现/频率。

我建议将程序的功能封装成更小的方法/类,这样每个方法/类只做一个小任务。这样代码可以更好的阅读。

我假设您的文件包含字符串“auto bush trumped her tomato in the petunia auto”

代码如下:

package how_to_calculate_the_frequency;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Project {

    HashMap<String, Integer> map = new HashMap<String, Integer>();

    public static void main(String[] args){

        Project project = new Project();

        Scanner INPUT_TEXT = project.readFile();

        project.analyse(INPUT_TEXT);

        project.showResults();

    }

    /**
     * logic to count the occurences of words matched by REGEX in a scanner that
     * loaded some text
     * 
     * @param scanner
     *            the scanner holding the text
     */
    public void analyse(Scanner scanner) {

        String pattern = "[a-zA-Z'-]+";
        Pattern r = Pattern.compile(pattern);

        while (scanner.hasNext()) {
            // read next word
            String Stringcandidate = scanner.next();

            // see if pattern matches (boolean find)
            Matcher matcher = r.matcher(Stringcandidate);
            if (matcher.find()) {
                String matchedWord = matcher.group();
                //System.out.println(matchedWord); //check what is matched
                this.addWord(matchedWord);

            }

        }
        scanner.close();// Close your Scanner.
    }

    /**
     * adds a word to the <word,count> Map if the word is new, a new entry is
     * created, otherwise the count of this word is incremented
     */
    public void addWord(String matchedWord) {

        if (map.containsKey(matchedWord)) {
            // increment occurrence
            int occurrence = map.get(matchedWord);
            occurrence++;
            map.put(matchedWord, occurrence);
        } else {
            // add word and set occurrence to 1
            map.put(matchedWord, 1);
        }

    }

    /**
     * reads a file from disk and returns a scanner to analyse it
     * 
     * @return the file from disk as scanner
     */
    public Scanner readFile() {

        Scanner scanner = null;

        /* use that for reading a file from disk
         * try { scanner = new Scanner(new
         * File("moviereview.txt")).useDelimiter(" "); } catch (Exception e) {
         * e.printStackTrace(); }
         */

        scanner = new Scanner("auto bush trumped her tomato in the petunia auto");

        return scanner;
    }

    /**
     * prints the matched words and their occurrences
     * in a readable way
     */
    public void showResults() {

        for (HashMap.Entry<String, Integer> matchedWord : map.entrySet()) {
            int occurrence = matchedWord.getValue();
            System.out.print("\"" + matchedWord.getKey() + "\" appears " + occurrence);
            if (occurrence > 1) {
                System.out.print(" times\n");
            } else {
                System.out.print(" time\n");
            }
        }

        // or as the new Java 8 lambda expression
        // map.forEach((word,occurrence)->System.out.println("\"" + word + "\"
        // appears " + occurrence + " times"));
    }
}

// DONE seperate reading a file, analysing the file and
// word-frequency-counting-logic in different
// methods
// Done implement <word,count> Map and logic to add new and known(to the map)
// words

这会产生:

“the”出现 1 次

“auto”出现 2 次​​p>

“她”出现了 1 次

“in”出现 1 次

“灌木”出现 1 次

“胜过”出现 1 次

“番茄”出现 1 次

“矮牵牛”出现1次

问候

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多