主题:求助:字符串问题,大家来帮忙```
飞侠
[专家分:1380] 发布于 2007-11-08 09:45:00
你好,我有一个字符串,名字叫 s
请问:如何得出里面每个单词出现的个数呢(及频率)?
比如:有个s="this is a test to get the frequency of the words, how to do it"
然后
this 1
is 1
a 1
test 1
to 2
get 1
the 2
frequency 1
of 1
words 1
how 1
do 1
it 1
麻烦做的给点思路,非常感谢~~~
回复列表 (共9个回复)
沙发
sjhlovejava [专家分:1690] 发布于 2007-11-08 16:17:00
现在会抛出索引越界异常,不过个人思路大体就这样
import java.util.ArrayList;
import java.util.List;
public class WordNum {
/**
* @param args
*/
public static void main(String[] args) {
String s = "this is a test to get the frequency of the words, how to do it";
dealStr(s);
}
public static List dealStr(String str){
String word; //用于放置临时的单词
List list = new ArrayList(); //用于放置实体WordAndFrequency
int fromIndex = -1;
int beginIndex = 0;
int endIndex = 0; //放置处理字符串的位置
do{
beginIndex = fromIndex+1;
fromIndex = str.indexOf(" ", beginIndex); //以空格为分格符
endIndex = fromIndex;
word = str.substring(beginIndex, endIndex);
countWord(list, word);
System.out.println("beginIndex : "+beginIndex);
System.out.println("fromIndex : "+fromIndex);
System.out.println("endIndex : "+endIndex);
System.out.println("word : "+word);
System.out.println("str.length() : "+str.length());
}while(endIndex != str.length());
return list;
}
/**
* 判断word是否已经存在,若已经存在,则num+1
* 否则list增加个实体WordAndFrequency
*/
public static List countWord(List list , String word){
for(Object obj : list){
WordAndFrequency waf = (WordAndFrequency)obj;
if(word.equals(waf.word)){
waf.num = waf.num + 1;
}else{
WordAndFrequency waf0 = new WordAndFrequency();
waf0.num = 1;
waf0.word = word;
list.add(waf0);
}
}
return list;
}
}
/**
* 放置单词和对应个数的实体
*/
class WordAndFrequency{
int num; //用于放置对应单词的个数
String word; //用于放置单词
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
板凳
zcs179895723 [专家分:330] 发布于 2007-11-08 17:58:00
可以先把每个单词从字符串里提出来,再用字符串里面的一个equals方法判断是否相等,相等则在对应的上面加1几行了
3 楼
飞侠 [专家分:1380] 发布于 2007-11-09 09:56:00
1楼的方法怎么打印出的都是index啊,没有单词哦
我用了很多循环做出来了
谢谢大家
4 楼
sjhlovejava [专家分:1690] 发布于 2007-11-09 09:56:00
完整的,做个参考.
PS:有想过使用Map 类,用抽取的单词做 key, 单词个数 就是 对应key的 值,思路类似,
我想这样应该会比较简洁,有兴趣自己做做
import java.util.ArrayList;
import java.util.List;
public class WordNum {
/**
* @param args
*/
public static void main(String[] args) {
String s = "this is a test to get the frequency of the words, how to do it";
List list = dealStr(s);
for (Object obj : list) {
WordAndFrequency waf = (WordAndFrequency) obj;
System.out.println(waf.getWord() + " : " + waf.getNum());
}
}
/**
* 对给定的字符串进行处理
*/
public static List dealStr(String str) {
String word; //用于放置临时的单词
List list = new ArrayList(); //用于放置实体WordAndFrequency
int fromIndex = -1;
int beginIndex = 0;
int endIndex = 0; //放置处理字符串的位置
do {
beginIndex = fromIndex + 1;
fromIndex = str.indexOf(" ", beginIndex); //以空格为分格符
endIndex = fromIndex;
if (endIndex == -1) {
endIndex = str.length();
word = str.substring(beginIndex, endIndex);
} else {
word = str.substring(beginIndex, endIndex);
}
list = countWord(list, word);
} while (endIndex != str.length());
return list;
}
/**
* 判断word是否已经存在,若已经存在,则num+1
* 否则list增加个实体WordAndFrequency
*/
public static List countWord(List list, String word) {
Boolean flag = false;
int index = 0;
for (Object obj : list) {
WordAndFrequency waf = (WordAndFrequency) obj;
if (word.equals(waf.getWord())) {
flag = true;
break;
}
index++;
}
if (flag) {
WordAndFrequency waf = (WordAndFrequency) list.get(index);
waf.setNum(waf.getNum() + 1);
} else {
WordAndFrequency waf0 = new WordAndFrequency();
waf0.setNum(1);
waf0.setWord(word);
list.add(waf0);
}
return list;
}
}
/**
* 放置单词和对应个数的实体
*/
class WordAndFrequency {
private int num; //用于放置对应单词的个数
private String word; //用于放置单词
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
5 楼
sjhlovejava [专家分:1690] 发布于 2007-11-09 09:57:00
不好意思,昨天 只是给个思路,没 完善 代码.
6 楼
飞侠 [专家分:1380] 发布于 2007-11-09 10:02:00
谢谢你啊,map我不熟悉,后来我自己写了一个,代码有点乱:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
class main extends JFrame implements ActionListener
{
private JFrame frame=new JFrame("Searching engine");
private JLabel label=new JLabel("Please enter the key words:");
private JTextField jtf=new JTextField(20);
private JButton jbt=new JButton("Search");
private String keyWords="";
//construct the application interface
main()
{
super("IR - Seaching Engine");
setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
add(label);
add(jtf);
add(jbt);
jtf.addActionListener(this);
jbt.addActionListener(this);
setVisible(true);
//this.setSize(450,300);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//handle the serach function
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbt)
{
keyWords=jtf.getText();
searchingProcess(keyWords);
}
if(e.getSource()==jtf)
{
keyWords=jtf.getText();
searchingProcess(keyWords);
}
}
//searching process
public void searchingProcess(String keyWords)
{
String query=keyWords;
int totalNum=0;
if(!query.equals(""))
{
Vector queryVector=new Vector();
StringTokenizer queryToken=new StringTokenizer(query);
int count=0;
int[] deleteItem=null;
//get each of the word in the query string
while(queryToken.hasMoreTokens())
{
//System.out.println(queryToken.nextToken());
String addWord=queryToken.nextToken();
if(!isAllSymbol(addWord))//if it is not all symbols
{
addWord=filterWord(addWord);
if(isWord(addWord))//if it is not blank
{
queryVector.addElement(new String(addWord));
}
}
else
{
totalNum++;
}
}
deleteItem=new int[queryVector.size()];
//count the word
while(queryVector.size()>0)
{
for(int i=0;i<queryVector.size();i++)
{
//System.out.println(queryVector.elementAt(0));
if(((String)queryVector.elementAt(0)).equalsIgnoreCase((String)queryVector.elementAt(i)))
{
deleteItem[count]=i;//used for delete the words from queryVector
count++;
}
}
System.out.println(queryVector.elementAt(0)+" appears: "+count);
for(int i=count-1;i>=0;i--)
{
queryVector.removeElementAt(deleteItem[i]);
}
count=0;
}
}
else
{
JOptionPane.showMessageDialog(this,"Please enter the keywords",
"Hint",JOptionPane.INFORMATION_MESSAGE);
jtf.requestFocus();
}
if(totalNum!=0)
{
JOptionPane.showMessageDialog(this,"No matches",
"Hint",JOptionPane.INFORMATION_MESSAGE);
}
}
7 楼
飞侠 [专家分:1380] 发布于 2007-11-09 10:03:00
public String filterWord(String word)
{
String checkWord=word;
try
{
//check the first letter to see whether it is a sysbol
while(!((checkWord.charAt(0)>='a'&&checkWord.charAt(0)<='z')||(checkWord.charAt(0)>='A'&&checkWord.charAt(0)<='Z')))
{
char ch=checkWord.charAt(0);
if(ch<'a'||ch>'z'||ch<'A'||ch>'Z')
{
//remove the first letter
checkWord=checkWord.substring(1);
System.out.println(checkWord);
}
}
//end check the first letter
//check the last letter to see whether it is a sysbol
while(!((checkWord.charAt(checkWord.length()-1)>='a'&&checkWord.charAt(checkWord.length()-1)<='z')
||(checkWord.charAt(checkWord.length()-1)>='A'&&checkWord.charAt(checkWord.length()-1)<='Z')))
{
char ch=checkWord.charAt(checkWord.length()-1);
if(ch<'a'||ch>'z'||ch<'A'||ch>'Z')
{
//remove the last letter
checkWord=checkWord.substring(0,checkWord.length()-1);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
//end check the last letter
return checkWord;
}
public boolean isWord( String source )
{
return source.matches("^[a-z]+$")||source.matches("^[A-Z]+$");
}
//the a word is consists of symbols
public boolean isAllSymbol(String source)
{
boolean flag=true;
String getSource=source;
char[] comChar=new char[getSource.length()];
getSource.getChars(0,getSource.length(),comChar,0);//get the char array
for(int i=0;i<comChar.length;i++)
{
if(comChar[i]>='a'&&comChar[i]<='z'||comChar[i]>='A'&&comChar[i]<='Z')
{
flag=false;
}
}
return flag;
}
public static void main(String args[])
{
new main();
}
}
8 楼
飞侠 [专家分:1380] 发布于 2007-11-09 10:12:00
1楼的兄弟,请问你知道怎样得到一个指定路径下,所有文件的名字吗???
9 楼
sjhlovejava [专家分:1690] 发布于 2007-11-09 16:16:00
这边只写读取下级目录的,下下级就多做个判断,看是目录还是文件就可以了
多嘴下: 养成 类 头字母大写的习惯
import java.io.File;
public class AllFileName {
/**
* @param args
*/
public static void main(String[] args) {
//此处文件名不能有空格,如:F:\\my file
String str = "D:\\WTK2.5.2";
File file = new File(str);
File[] files = file.listFiles();
for(Object obj : files){
File f = (File)obj;
System.out.println(f.getName());
}
/*for(int i = 0; i<files.length; i++){
System.out.println(files[i].getName());
}*/
}
}
我来回复