Javafx - [Histogram] Text Frequency Statistics Tool Chinese/English Word Statistics

Keywords: javafx

Last week I poured over javafx, which was originally my usual performance system. When he mentioned JavaFX to the teacher, he suddenly rose up and developed an experiment that counted Chinese and English words and displayed them in a histogram... Give only two or three days to do so, and laughed to test our potential SOS, so he spent a day and a half with three people in the group.

The main interface is as follows

When no text is selected, a prompt is displayed to start analysis at the point where no text is checked

Analyzing Text Documents

The only function is to select a local.txt file, select the text language and the lowest frequency, and display histogram statistics.
Try a large.txt document, beyond the interface can not be displayed, x-axis and column bar are not aligned, have not changed this bug, at present the idea is to add a pull bar, but it does not feel convenient.

Horizontal histograms are preferred, as the javafx-encapsulated BarChart fixes the data types that can be placed on the x- and y-axes. It's great to see BarChart encapsulated in Youtube! (Many javafx UI interfaces on plus are also amazing!
Because time is urgent, and then it is a beginner, the code is horrible, the Java background is not solid, and the use of Javafx controls is not fully understood. Unleash the implementation code for some of the main functions
Filter out the regular expressions that are used in both English and Chinese!!! There is only a problem with the's'in English, because'that is also split, I don't know how to change it for now, here is the code.

public static void Analysis(String flag) throws IOException
    {
        int bufferSize = 20 * 1024 * 1024;
        File file = new File(AnalysisFile);
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);
        BufferedReader input = new BufferedReader(inputStreamReader, bufferSize);
        String line;
        while((line = input.readLine()) != null) {
            if(flag == "English") { // Combox na selects the parameter flag brought in in English and Chinese
                String[] str = line.split("\\W"); // Use regular expressions to split symbols with non-letters, numbers, and underscores
                for(String e : str) {
                    if(e.matches("[a-z]+")) { // Filter out English words
                        if(sumOfWords.containsKey(e)) {
                            int num = sumOfWords.get(e);
                            sumOfWords.put(e, num + 1);
                        }
                        else sumOfWords.put(e, 1);
                    }
                }
            }
            else if(flag == "Chinese") {
                String d;
                char[] charArray = line.toCharArray(); //Converts a string to an array of characters
                for(int x = 0;x < charArray.length;x++) { //Loop through character arrays
                    d=Character.toString(charArray[x]); //Returns a string object
                    if (d.matches("[\\u4e00-\\u9fa5]")) {//Determine whether it is a Chinese character
                        if(!sumOfChinese.containsKey(charArray[x])) {
                            sumOfChinese.put(charArray[x], 1);
                        } else {
                            sumOfChinese.put(charArray[x],sumOfChinese.get(charArray[x])+1);
                        }
                    }
                }
            }
        }
        input.close();

Open the code for the local file selector when you click Select File

    @FXML
    void choosefile(ActionEvent event) {
        Stage primaryStage = new Stage();
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(primaryStage);
        if(file != null) {
            FileInputField.setText(file.getAbsolutePath());
            AnalysisFile = String.valueOf(file);
            TipLabel.setText("");
        }
    }

Finally, before submitting, the console used jdk packager to package the.exe file. During the process of resolving, it thought that this was a tool that still had bugs and had no effect, and then abandoned packaging. After some time to write it again, after fixing the bug, some functions will be uploaded to github, named Useless Tool in the group, so it can only be referred to for beginners.
In December, the project of the student's usual performance management system will also be submitted, which is more troublesome, with three participants and ten activities. Designing databases has been on your mind for a long time (the database hasn't seen the chapter on design yet, so you have to design it yourself. This project will also be uploaded if you do it! Before that, you need to look at java and databases, standardize the design of classes and databases, and not write code: /You don't want to look at the second eye anymore

Posted by C_Calav on Tue, 09 Nov 2021 12:04:31 -0800