Java applet中的词频分析
我正在用applet编写一个程序来测量给定文本量中字长的频率,并在applet运行期间显示这些频率。我已经写了这个程序,它调试没有错误 然而,输出显示我输入的文本中每种长度有255个单词,我已经盯着它看了好几个小时,运气不好,我知道drawstring方法水平列出了这些输出,我将在以后修复这些输出 我假设问题存在于AnalyzeSetext方法中Java applet中的词频分析,java,string,math,applet,character,Java,String,Math,Applet,Character,我正在用applet编写一个程序来测量给定文本量中字长的频率,并在applet运行期间显示这些频率。我已经写了这个程序,它调试没有错误 然而,输出显示我输入的文本中每种长度有255个单词,我已经盯着它看了好几个小时,运气不好,我知道drawstring方法水平列出了这些输出,我将在以后修复这些输出 我假设问题存在于AnalyzeSetext方法中 import java.awt.*; import java.awt.event.*; import java.applet.Applet; impo
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) {}
import java.awt.*;
导入java.awt.event.*;
导入java.applet.applet;
导入java.awt.Graphics;
导入java.util.*;
公共类StringAnalysis扩展小程序实现MouseListener、ActionListener{
字体为arielBold;
标签pr_标签;
文本字段pr_txt;
int textFieldSize=15;
按钮pr_输入1、pr_输入2、pr_输入3;
字符串textToAnalyse=(“未输入任何内容”);
布尔输出=假;
字符串测试=”;
//创建数组以显示相应的字长
int[]lengthsOfWords=新int[textFieldSize];
//首先,我们必须将字符串和空格分开,并填充数组进行比较
String[]arrayOfWords=新字符串[textFieldSize];
public void init(){
pr_Label=新标签(“输入要分析的文本:”);
添加(pr_标签);
pr_txt=新文本字段(textFieldSize);
添加(pr_txt);
pr_input1=新按钮(“分析”);
pr_input2=新按钮(“重置”);
添加(pr_输入1);
添加(pr_输入2);
pr_input1.addActionListener(此);
pr_input2.addActionListener(此);
}
公共无效开始(){
设置大小(1000500);
arielBold=新字体(“Ariel”,Font.BOLD,20);
}
已执行的公共无效操作(操作事件e){
如果(例如getSource()==pr_input2){
showStatus(“presseting…”);
重置();
}
else if(e.getSource()==pr_input1){
showStatus(“分析……a”);
textToAnalyse=pr_txt.getText();
分析文本(文本分析);
}
}
公共空间涂料(图g){
字符串statsToOutput=(“有:”+“\n”);
int计数器=1;
for(整数长度:字长){
statsToOutput=statsToOutput+lengthsOfWords[0]+“长度字”+计数器+“\n”;
计数器++;
}
如果(输出=真){
g、 抽绳(statsToOutput,25200);
g、 抽绳(“待分析文本为:”+textToAnalyse,25100);
showStatus(“完成”);
}
else if(输出=false){
g、 setFont(arielBold);
g、 抽绳(textToAnalyse,50100);
showStatus(“完成”);
}
}
公共无效分析文本(字符串文本){
///////////////////////////////////////////////////////////////////////////////////////////////////////
内部位置1=0,位置2=0;
字符串newWord=“”;
字符串字母;
int pos1=0,pos2=0;
对于(pos1=0;pos1让我们试试代码部分:
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);
}
}
}
private-Map-freqWords=new-HashMap();
公共无效分析文本(字符串文本){
//可以使用其他类型的分隔符进行拆分
字符串正则表达式=。。。。
字符串[]输入=text.split(regex);
for(字符串s:输入){
if(freqWords.containtsKey){
int frequency=inputs.get(s);
频率++;
输入。输入(s,频率);
}否则{
输入。输入(s,1);
}
}
}
希望它能对您有所帮助。这里的要点是您应该使用数据结构Map来存储频率词。阅读这篇文章似乎很有意义,但是它没有运行,输入数组似乎有很多问题……谢谢!而且其中很多“无法调用containsKey(字符串)”在数组上键入字符串[]“