求一个JAVA小程序 要求统计输出的一段英文段落中单词出现的频率

2025-01-04 01:18:57
推荐回答(5个)
回答1:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Article {

// 保存文章的内容
String content;
String[] rawWords;
String[] words;
int[] wordFreqs;

public Article() {
content = " ";
}

public void splitWord() {
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE)
.replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE)
.replace('-', SPACE);
rawWords = content.split("\\s+");
}

public void countWordFreq() {
Set set = new TreeSet();
for (String word : rawWords) {
set.add(word);
}
Iterator ite = set.iterator();
List wordsList = new ArrayList();
List freqList = new ArrayList();
while (ite.hasNext()) {
String word = (String) ite.next();
int count = 0;
for (String str : rawWords) {
if (str.equals(word)) {
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for (int i = 0; i < freqList.size(); i++) {
wordFreqs[i] = freqList.get(i);
}
}

public void sort() {
class Word {
private String word;
private int freq;

public Word(String word, int freq) {
this.word = word;
this.freq = freq;
}
}
class WordComparator implements Comparator {

public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if (word1.freq < word2.freq) {
return 1;
} else if (word1.freq > word2.freq) {
return -1;
} else {

int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();

String min = len1 > len2 ? word2.word : word1.word;
String max = len1 > len2 ? word1.word : word2.word;

for (int i = 0; i < min.length(); i++) {
if (min.charAt(i) < max.charAt(i)) {
return 1;
}
}

return 1;

}
}

}

List wordList = new ArrayList();

for (int i = 0; i < words.length; i++) {
wordList.add(new Word(words[i], wordFreqs[i]));
}

Collections.sort(wordList, new WordComparator());

for (int i = 0; i < wordList.size(); i++) {
Word wor = (Word) wordList.get(i);

words[i] = wor.word;
wordFreqs[i] = wor.freq;
}

}

public void printResult() {
System.out.println("Total " + words.length
+ " different words in the content!");

for (int i = 0; i < words.length; i++) {
System.out.println(wordFreqs[i] + " " + words[i]);
}
}

public static void main(String[] args) {
Article a = new Article();
a.content = "sssssssssssaadwerqwersssdfsdftrytyum1242357689235cvbbnm,masdfasdfasdfasdf,fasdf.asdfasdfasdfasdfaasdfasdf";
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
运行一下,看是否成功,的确是一个分号出错了!现在已经改好了!

回答2:

import java.util.*;

public class List {
public int[] letterList(String bb){
String b = bb.toLowerCase();
int[] test = new int[26];
for(int n=0;n {
char a=b.charAt(n);
int index=a-'a';
if(index=0){
test[index]++;
}
}
return test;
}

public void printlist(int test[]){
for(int i=0;i System.out.print(test[i]);
}
}
public static void main(String[] args) {
int []chars=new int[100];
String str = args[0].trim();
List list=new List();
chars=list.letterList(str);
int[] flag = list.letterList(str);
System.out.println("String is:" );
list.printlist(flag);

}
}--------------------------------------------------------
上面的代码能实现统计,在命令行,先编译, 再输入 java List ababcdasgf 既可.面板什么的自己加吧。全让别人帮你做了,你很难学会java的。

回答3:

while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
这段代码中的int count = 0;的分号是中文格式的,改成英文格式的就OK

回答4:

已经改了,小问题,一个分号出错;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Article {

//保存文章的内容
String content;
String[] rawWords;
String[] words;
int[] wordFreqs;
public Article() {
content = " ";
}
public void splitWord(){
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);
rawWords = content.split("\\s+"); }
public void countWordFreq() {
Set set = new TreeSet();
for(String word: rawWords){
set.add(word);
}
Iterator ite = set.iterator();
List wordsList = new ArrayList();
List freqList = new ArrayList();
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for(int i = 0; i < freqList.size(); i++){
wordFreqs[i] = freqList.get(i);
}
}
public void sort() {
class Word{
private String word;
private int freq; public Word(String word, int freq){
this.word = word;
this.freq = freq;
}
}
class WordComparator implements Comparator{

public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if(word1.freq < word2.freq){
return 1;
}else if(word1.freq > word2.freq){
return -1;
}else{

int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();

String min = len1 > len2? word2.word: word1.word;
String max = len1 > len2? word1.word: word2.word;

for(int i = 0; i < min.length(); i++){
if(min.charAt(i) < max.charAt(i)){
return 1;
}
}

return 1;

}
}

}

List wordList = new ArrayList();

for(int i = 0; i < words.length; i++){
wordList.add(new Word(words[i], wordFreqs[i]));
}

Collections.sort(wordList, new WordComparator());

for(int i = 0; i < wordList.size(); i++){
Word wor = (Word) wordList.get(i);

words[i] = wor.word;
wordFreqs[i] = wor.freq;
}

}

public void printResult() {
System.out.println("Total " + words.length + " different words in the content!");

for(int i = 0; i < words.length; i++){
System.out.println(wordFreqs[i] + " " + words[i]);
}
}

public static void main(String[] args) {
Article a = new Article();
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
只要在当前文件下创建一个文本文件,里面可以写一些单词,就可以了

回答5:

一个分号出错