鱼C论坛

 找回密码
 立即注册
查看: 5970|回复: 2

[已解决]Java统计词频

[复制链接]
发表于 2021-3-20 12:45:01 | 显示全部楼层 |阅读模式
5鱼币
  1. public class Test2
  2. {

  3.     public static  void  main(String[] args)
  4.     {
  5.         String file ="King Athamus of northern Greece had two children," +
  6.                 "Phrixus and Helle.After he left his first wife and mar " +
  7.                 "ried Ino,a wicked woman,the two children received all" +
  8.                 "At one timethe kingdom was ruined by a famine.";


  9.         String[] words = new String[10000];
  10.         StringBuffer tempWord = new StringBuffer();
  11.         int[] times = new int[100];
  12.         String[] word = new String[100];
  13.         String vocabulary1 = new String();
  14.         String vocabulary2 = new String();
  15.         int index = 0;

  16.         int i = 0;
  17.         for (int j = 0; j<file.length();j++)
  18.         {
  19.             i=j;

  20.             char c;
  21.             while (((c = file.charAt(i)) != ' ')
  22.                     &&((c = file.charAt(i)) != ','
  23.                     &&((c = file.charAt(i)) != '.')))//charArt返回当前位置的字符 从0开始
  24.             {
  25.                 tempWord.append(c);
  26.                 i++;

  27.             }

  28.             j=i;
  29.             System.out.print(i);
  30.             System.out.print("  ");
  31.             String temp = tempWord.toString();
  32.             // System.out.println(temp);

  33.             words[j] = temp;
  34.             System.out.println(words[j]);

  35.             tempWord.delete(0,tempWord.length());
  36.         }

  37.         System.out.println("ending");
  38.         System.out.println(words);

  39.         for (int j = 0; j<50;j++)
  40.         {
  41.             for(i = 0;i<=index;i++)
  42.             {
  43.                 vocabulary1 = words[j];
  44.                 vocabulary2 = word[i];

  45.                 if(vocabulary1.equalsIgnoreCase(vocabulary2))
  46.                 {
  47.                     times[i] = times[i] + 1;
  48.                     break;
  49.                 }

  50.                 else if (i == index)
  51.                 {
  52.                     if(vocabulary1.equalsIgnoreCase(vocabulary2))
  53.                     {
  54.                         times[i] = times[i] + 1;
  55.                         break;
  56.                     }

  57.                     else
  58.                     {
  59.                         index += 1;
  60.                         word[index] = words[j];
  61.                         times[index] += 1;
  62.                     }
  63.                 }
  64.             }
  65.         }

  66.         System.out.println(word);
  67.         for(i = 0;i<50;i++)
  68.         {
  69.             System.out.println(times[i]);
  70.         }

  71.     }






  72. }
复制代码


刚学Java,有个统计词频的东西要写,呃用数组来统计的,求大佬指点,一直没闹懂哪出问题了
最佳答案
2021-3-20 12:45:02

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

    }
}

最佳答案

查看完整内容

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" + ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-20 12:45:02 | 显示全部楼层    本楼为最佳答案   

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

    }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-20 16:57:55 | 显示全部楼层
  1. public class Text3
  2. {


  3.     public static void main(String args[])
  4.     {
  5.         String file ="King Athamus of northern Greece had two children , " +
  6.                 "Phrixus and Helle . After he left his first wife and mar " +
  7.                 "ried Ino , a wicked woman , the two children received all" +
  8.                 "At one timethe kingdom was ruined by a famine .";

  9.         String[] words = file.split(" ");
  10.         int[] times = new int[42];
  11.         String[] word = new String[42];
  12.         String vocabulary1 = new String();
  13.         String vocabulary2 = new String();
  14.         int index = 0;
  15.         for(int i = 0;i<42;i++)
  16.         {
  17.             word[i] = " ";
  18.         }

  19.         for(int i = 0;i<42;i++)
  20.         {
  21.             if(words[i].equalsIgnoreCase("."))
  22.             {
  23.                 words[i] = " ";
  24.             }

  25.             else if(words[i].equalsIgnoreCase(","))
  26.             {
  27.                 words[i] = " ";
  28.             }

  29.             else
  30.             {
  31.                 continue;
  32.             }

  33.         }

  34.         for (int j = 0; j<42;j++)
  35.         {
  36.             for(int i = 0;i<=index;i++)
  37.             {
  38.                 vocabulary1 = words[j];
  39.                 vocabulary2 = word[i];

  40.                 if (i == index)
  41.                 {
  42.                     index += 1;
  43.                     word[index] = words[j];
  44.                     times[index] += 1;
  45.                     break;
  46.                 }

  47.                 else if(vocabulary1.equalsIgnoreCase(vocabulary2))
  48.                 {
  49.                     times[i] = times[i] + 1;
  50.                     break;
  51.                 }
  52.             }
  53.         }


  54.         for(int i = 0;i<42;i++)
  55.         {
  56.             if(word[i].equalsIgnoreCase(" "))
  57.             {
  58.                 continue;
  59.             }
  60.             else
  61.             {
  62.                 System.out.println(word[i]+"---->"+times[i]);
  63.             }

  64.         }

  65.     }

  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 11:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表