2014-03-06 23 views
0

我正在寫一個applet程序來測量給定數量的文本中的詞長度的頻率,並在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) {} 

我希望對此有任何幫助,(我絕望)。

回答

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); 
     } 
    } 
} 

希望它可以幫助你。這裏的要點是你應該使用數據結構地圖來存儲頻率字。

+0

閱讀這似乎是有道理的,但它不運行,輸入數組似乎有很多問題......但謝謝你! – thanatorr

+0

和很多這種「不能調用containsKey(String)數組類型String []」 – thanatorr

+0

對不起,我剛剛編輯我的代碼:) –