Java统计词频
public class Test2{
public staticvoidmain(String[] args)
{
String file ="King Athamus of northern Greece had two children," +
"Phrixus and Helle.After he left his first wife and mar " +
"ried Ino,a wicked woman,the two children received all" +
"At one timethe kingdom was ruined by a famine.";
String[] words = new String;
StringBuffer tempWord = new StringBuffer();
int[] times = new int;
String[] word = new String;
String vocabulary1 = new String();
String vocabulary2 = new String();
int index = 0;
int i = 0;
for (int j = 0; j<file.length();j++)
{
i=j;
char c;
while (((c = file.charAt(i)) != ' ')
&&((c = file.charAt(i)) != ','
&&((c = file.charAt(i)) != '.')))//charArt返回当前位置的字符 从0开始
{
tempWord.append(c);
i++;
}
j=i;
System.out.print(i);
System.out.print("");
String temp = tempWord.toString();
// System.out.println(temp);
words = temp;
System.out.println(words);
tempWord.delete(0,tempWord.length());
}
System.out.println("ending");
System.out.println(words);
for (int j = 0; j<50;j++)
{
for(i = 0;i<=index;i++)
{
vocabulary1 = words;
vocabulary2 = word;
if(vocabulary1.equalsIgnoreCase(vocabulary2))
{
times = times + 1;
break;
}
else if (i == index)
{
if(vocabulary1.equalsIgnoreCase(vocabulary2))
{
times = times + 1;
break;
}
else
{
index += 1;
word = words;
times += 1;
}
}
}
}
System.out.println(word);
for(i = 0;i<50;i++)
{
System.out.println(times);
}
}
}
刚学Java,有个统计词频的东西要写,呃用数组来统计的,求大佬指点,一直没闹懂哪出问题了
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test4 {
public static void main(String[] args) {
String file = "King Athamus of northern Greece had two children , " +
"Phrixus and Helle . After he left his first wife and mar " +
"ried Ino , a wicked woman , the two children received all" +
"At one timethe kingdom was ruined by a famine .";
String[] s1 = file.split(" ");
Map map = new LinkedHashMap();
int count = 0;
for (String s2 : s1) {
if (!s2.equals(".") && !s2.equals(",") && !s2.equals(" ")) {
//判断指定的Key是否存在
if (map.containsKey(s2)) {
//获取当前单词次数count
count = (Integer) map.get(s2);
//次数加1
map.put(s2, ++count);
} else {
count=1;
map.put(s2, count);
}
}
}
Iterator it = map.keySet().iterator();
String word;
while (it.hasNext()) {
word = (String) it.next();
System.out.println(word + "------->" + map.get(word));
}
}
} public class Text3
{
public static void main(String args[])
{
String file ="King Athamus of northern Greece had two children , " +
"Phrixus and Helle . After he left his first wife and mar " +
"ried Ino , a wicked woman , the two children received all" +
"At one timethe kingdom was ruined by a famine .";
String[] words = file.split(" ");
int[] times = new int;
String[] word = new String;
String vocabulary1 = new String();
String vocabulary2 = new String();
int index = 0;
for(int i = 0;i<42;i++)
{
word = " ";
}
for(int i = 0;i<42;i++)
{
if(words.equalsIgnoreCase("."))
{
words = " ";
}
else if(words.equalsIgnoreCase(","))
{
words = " ";
}
else
{
continue;
}
}
for (int j = 0; j<42;j++)
{
for(int i = 0;i<=index;i++)
{
vocabulary1 = words;
vocabulary2 = word;
if (i == index)
{
index += 1;
word = words;
times += 1;
break;
}
else if(vocabulary1.equalsIgnoreCase(vocabulary2))
{
times = times + 1;
break;
}
}
}
for(int i = 0;i<42;i++)
{
if(word.equalsIgnoreCase(" "))
{
continue;
}
else
{
System.out.println(word+"---->"+times);
}
}
}
}
页:
[1]