使用JAVA进行词频统计

来源:互联网 发布:单片机的加密模式 编辑:程序博客网 时间:2024/04/30 15:27

在使用TF-IDF计算文章关键词的时候,需要知道某个词的词频是多少,使用 idf = Math.log(10000 + 10000.0 / (docFreq + 1))这个公式求解出idf的值。一般来说,词频统计越准确,每个词的idf的值就越准确,而词频的准确性与语料的多少有关,语料自然越多越好,但是通常情况下10G的数据量应该足够了。

假设现在我们有了10G的新闻语料和词列表,现在就可以来统计词频了,首先使用ANSJ对语料进行分词,分词时就把我们自定义的词列表作为用户字典,在得到分好词的数据后,再统计词频即可,JAVA代码如下:


import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class CalFreq {static Map<String, Integer> map = new HashMap<String, Integer>();public static void main(String[] args) throws IOException {InputWord();FileReader fr = new FileReader("resultbig.txt");BufferedReader br = new BufferedReader(fr);String line = "";String outline[] = new String[2000];int count;int progress = 0;while (line != null) {line = br.readLine();if (line == null) {break;}//System.out.println(line);progress++;System.out.println("now is processing the " + progress + "'s news");outline = line.split(" ", -1);for (int i = 0; i < outline.length; i++) {if (map.containsKey(outline[i]) != false) {count = map.get(outline[i]) + 1;map.put(outline[i], count);}}}fr.close();br.close();WriteWord();}// put the word into the mappublic static void InputWord() throws IOException {System.out.println("begin get the word");FileReader fr = new FileReader("baike.txt");BufferedReader br = new BufferedReader(fr);String line = new String();while (line != null) {line = br.readLine();if (line == null) {break;}System.out.println(line);map.put(line, 0);}br.close();fr.close();System.out.println("get the word is ok");}// write word and frequency into the filepublic static void WriteWord() throws IOException {System.out.println("begin write the word");File file = new File("wordfreq.txt");file.createNewFile();FileWriter fw = new FileWriter("wordfreq.txt");BufferedWriter bw = new BufferedWriter(fw);for (Map.Entry<String, Integer> entry : map.entrySet()) {String key = entry.getKey().toString();int value = entry.getValue();bw.write(key + " " + value);bw.newLine();bw.flush();}fw.close();bw.close();System.out.println("write the word is ok");}}

这里baike.txt就是词表,resultbig.txt为使用ANSJ分好词的文本,wordfreq.txt即词表中的每个词出现的频率。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑页面显示不全怎么办 脸上发痒长痘怎么办 扣扣魔性表情泡泡消失怎么办 脚起小泡泡很痒怎么办 孕妇脚痒起水泡怎么办 孕妇手脚起湿疹怎么办 嘴巴破皮了怎么办 小便刺痛阴唇红肿怎么办 集成墙面挂照片怎么办 苹果电脑网页游戏打不开怎么办 苹果6plus发热怎么办 玩手游手机太卡怎么办 苹果七发烫厉害怎么办 苹果手机延迟高怎么办 王者荣耀总是卡怎么办 王者荣耀卡屏怎么办 王者荣耀网络延迟怎么办 荣耀8手机卡了怎么办 荣耀v10有点卡怎么办 荣耀10有点卡怎么办 8g内存吃鸡会崩怎么办 玩看门狗很卡怎么办 拼多多人数不够怎么办 玩cf想吐怎么办 玩手机头晕恶心怎么办 玩手机头疼恶心怎么办 看手机想吐怎么办 英雄联盟取名后怎么办 王者荣耀改名重复怎么办 刺激战场改名重复怎么办 省钱快报忘记密码怎么办 手机直播网速卡怎么办 触手tv直播黑屏怎么办 酷狗id密码忘记怎么办 打游戏网络不稳定怎么办 电脑打字法没了怎么办 家庭版密钥专业版系统怎么办 win7应用程序不能启动怎么办 win7用户密码忘记了怎么办 win7用户密码忘了怎么办 windows开不了机怎么办