分析applet中的单词频率

时间:2014-03-06 00:40:24

标签: java string math applet character

我正在applet中编写一个程序来测量给定数量文本中字长的频率,并在applet中显示这些频率。我已经编写了程序并且它没有错误地进行调试。

然而输出说我在输入的文本中有每个长度255个单词,我一直盯着这几个小时没有运气,我知道抽绳方法水平列出这些输出,我将修复一个晚点。

我假设问题存在于analyseText方法中。

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.util.*;


public class StringAnalysis extends Applet implements MouseListener, ActionListener  {
Font arielBold;
Label pr_Label;
TextField pr_txt;
int textFieldSize = 15;
Button pr_input1, pr_input2, pr_input3;
String textToAnalyse = ("Nothing has been entered.");
boolean output = false;


    String testing ="";


    //create array to show respective lengths of words
    int [] lengthsOfWords = new int[textFieldSize];
    //first we must separate strings from spaces and populate array for comparison
    String [] arrayOfWords = new String[textFieldSize];


    public void init() {    

        pr_Label = new Label("Enter the text you wish to analise: ");
        add(pr_Label);
        pr_txt = new TextField(textFieldSize);
        add(pr_txt);
        pr_input1 = new Button("Analyse");
        pr_input2 = new Button("Reset");
        add(pr_input1);
        add(pr_input2);
        pr_input1.addActionListener(this);
        pr_input2.addActionListener(this);

    }

    public void start (){
        setSize(1000, 500);
        arielBold = new Font ("Ariel", Font.BOLD, 20);

    }


    public void actionPerformed(ActionEvent e) {


        if (e.getSource() == pr_input2) {
            showStatus("Resseting....");
            reset();
            }
        else if (e.getSource() == pr_input1){
            showStatus("Analysing...a");
            textToAnalyse =  pr_txt.getText();
            analyseText(textToAnalyse);
        }
    }

    public void paint(Graphics g) {


            String statsToOutput = ("There are:" + "\n" );
            int counter = 1;
            for (int lengths: lengthsOfWords) {
                statsToOutput = statsToOutput +lengthsOfWords[0] + " words of length " + counter + "\n ";
            counter ++;
            }
        if (output = true){
        g.drawString(statsToOutput, 25, 200);
        g.drawString("The text to be analysed is: " + textToAnalyse, 25, 100);
        showStatus("Finished.");
        }
        else if (output = false){
            g.setFont(arielBold);
            g.drawString(textToAnalyse,50, 100);
            showStatus("Finished.");
            }
    }

    public void analyseText(String text){
        ///////////////////////////////////////////////////////////////////////////////////////////////////////





        int position1 = 0, position2 = 0;
        String newWord = "";
        String currentLetter;
        int pos1 = 0, pos2 = 0;
        for ( pos1 = 0; pos1 <= arrayOfWords.length-1; pos1++) {

            //Initializes a string object for each address in array
            for (position1 = 0; position1 <= text.length()-1; position1++){

                //steps through each character in text
                currentLetter = Character.toString(text.charAt(position1));

                if (currentLetter.matches("[A-Z][a-z][0-9]")) {
                    //checks if character is alphanumeric using regular expressions (regex)

                    newWord = newWord + currentLetter;
                    //if passes regex then ads character to word
                }
            }
            if (newWord.length() > 0) {
            pos1 = arrayOfWords.length;
            }
            arrayOfWords[pos1] = newWord;


        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////

        emptyArrayInt(lengthsOfWords);

        //now compare each word with rest of array\
        for ( pos2 = 0; pos2 <= arrayOfWords.length-1; pos2++) {


                position2 = 0;
                for (position2 = 0; position2 <= arrayOfWords.length-1; position2++){


                if (arrayOfWords[pos2].length() == arrayOfWords[position2].length());

                lengthsOfWords[arrayOfWords[pos2].length()] = lengthsOfWords[arrayOfWords[pos2].length()] + 1;

            }

        }



        showStatus("finished analysing.");

        output = true;

        repaint();
    }

    public void emptyArrayInt(int[] array) {
        int position = 0;
        for (position = 0; position <= array.length-1; position ++)
        array[position] = 0;
    }
    public void emptyArrayStr(String[] array) {
        int position = 0;
        for (position = 0; position <= array.length-1; position ++)
        array[position] = "";
    }

    public void reset() {

    pr_txt.setText("");
    textToAnalyse = ("Nothing has been entered.");
    emptyArrayInt(lengthsOfWords);
    emptyArrayStr(arrayOfWords);
    //repaint();
    showStatus("Reset Successful.");
    repaint();

    }


    public void mouseClicked(MouseEvent arg0) {}
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}

我很感激您的帮助,(我很绝望)。

1 个答案:

答案 0 :(得分:0)

让我们尝试一下代码:

private Map<String, int> freqWords = new HashMap<String, int>();

public void analyzeText(String text) {
   // You can split with another type of delimiter
   String regex = ....
   String[] inputs = text.split(regex); 

   for (String s : inputs) {
       if(freqWords.containtsKey(s)) {
           int frequency = inputs.get(s);
           frequency++;
           inputs.put(s, frequency);
       } else {
           inputs.put(s, 1);
       }
   }
}

希望它可以帮到你。这里的要点是你应该使用数据结构 Map 来存储频率词。