package com.cmcm.goods_classification; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class WordProcess { private static final String DATASOURCEPATH = "D://mallet_data//0DataSource//Watches_Child//Sports_Watches"; private static final String STOPWORDSPATH = "C://mallet-2.0.7//stoplists//en.txt"; public static final String RESULTPATH = "D://automotives//result.txt"; private static Map<String, Integer> dataHash = new HashMap<String, Integer>(); private static Set<String> stopWordsSet = new HashSet<String>(); public static void main(String[] args) throws Exception { loadStopWords(); FileProcess.readFolder(DATASOURCEPATH); List<Map.Entry<String, Integer>> dataList = hashSort(); FileProcess.writeFile(dataList); } public static void pruneText(String textPath) { String text = FileProcess.readFile(textPath).toLowerCase();// 将所有字母化为小写 text = text.replaceAll("^[a-zA-Z0-9']|\\s+|\t|\r", " "); // 将非字母字符、多个空格回车换行均化为一个空格 String words[] = text.split("\\s+");// 取出单词,并将单词存入数组中 getFrequency(words); } public static void getFrequency(String[] words) { for (int i = 0; i < words.length; i++) { String key = words[i]; // key对应单词 if ((dataHash.get(key) != null) && (!stopWordsSet.contains(key))) { int value = ((Integer) dataHash.get(key)).intValue(); // value对应单词出现的频率,单词已在map中存在则value+1 value++; dataHash.put(key, new Integer(value)); } else { dataHash.put(key, new Integer(1)); // 单词未在map中存在则value初始化为1 } } } public static List<Map.Entry<String, Integer>> hashSort() { List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(dataHash.entrySet()); Collections.sort(list_Data, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (o2.getValue() != null && o1.getValue() != null && o2.getValue().compareTo(o1.getValue()) > 0) { return 1; } else { return -1; } } }); return list_Data; } public static void loadStopWords() { String stopWordsText = FileProcess.readFile(STOPWORDSPATH); // System.out.println(stopWordsText); String words[] = stopWordsText.split("\\s+|\\t|\\r|\\n");// 取出单词,并将单词存入数组中 System.out.println(words.length); for(String word : words){ stopWordsSet.add(word); } } }
package com.cmcm.goods_classification; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import java.util.List; import java.util.Map; public class FileProcess { /** * read all file in folder * @param path */ public static void readFolder(String path) { int fileNum = 0, folderNum = 0; File file = new File(path); if (file.exists()) { LinkedList<File> list = new LinkedList<File>(); File[] files = file.listFiles(); for (File file2 : files) { if (file2.isDirectory()) {//System.out.println("DIR : " + file2.getAbsolutePath()); list.add(file2); folderNum++; } else { System.out.println("FILE: " + file2.getAbsolutePath()); WordProcess.pruneText(file2.getAbsolutePath()); fileNum++; } } File temp_file; while (!list.isEmpty()) { temp_file = list.removeFirst(); files = temp_file.listFiles(); for (File file2 : files) { if (file2.isDirectory()) {//System.out.println("DIR : " + file2.getAbsolutePath()); list.add(file2); folderNum++; } else { System.out.println("FILE: " + file2.getAbsolutePath()); fileNum++; WordProcess.pruneText(file2.getAbsolutePath()); } } } } else { System.out.println("File is not exist!"); } System.out.println(" num dir is: " + folderNum + "\n num file is: "+ fileNum); } /** * read content from filePath and return content * @param filePath */ public static String readFile(String filePath) { File file = new File(filePath); StringBuffer result = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; while ((tempString = reader.readLine()) != null) { result.append(" "); result.append(tempString); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return result.toString(); } /** * write content into filePath * @param dataList */ public static void writeFile(List<Map.Entry<String, Integer>> dataList) { System.out.println("start write word and frequency"); int size = dataList.size(); File file = null; FileWriter fileWrite = null; PrintWriter pw = null; int count = 1; try { // if file exist ,append ; if not, create file = new File(WordProcess.RESULTPATH); fileWrite = new FileWriter(file, true); pw = new PrintWriter(fileWrite); for (int i = 0; i < size; i++) { String word = dataList.get(i).getKey(); int frequency = dataList.get(i).getValue(); // System.out.println(word + " : " + frequency); pw.print(word); pw.print(" "); pw.print(count++); pw.print(" "); pw.print(frequency); pw.println(); } pw.flush(); fileWrite.flush(); } catch(IOException e) { e.printStackTrace(); }finally{ try { pw.close(); fileWrite.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end write word and frequency"); } } }
相关推荐
用Java实现的词频统计,代码。为了统计词汇出现频率,最简单直接的做法是另外建一个Map:key是单词,value是次数。将文章从头读到尾,读到一个单词就到Map里查一下,如果查到了则次数加一,没查到则往Map里一扔。...
本应用持续更新中
使用词表,遍历替换文章单词,再进行词频统计 使用两重循环,分别遍历时态替换表 lemmas.txt 和介词表 disablewords.txt 替换文章重复时态和介词,之后使用 treeMap 统计对应词的数量
统计文本文档中汉字、字母、数字等的频次,并输入到新的文本文档中去。
中文文本分词 词频统计,分词,去掉停词。 仅支持UTF-8编码
主要是读取文本,然后进行分词、词干提取、去停用词、计算词频,有界面,很实用
用Java打开文件 集合操作 用户界面实现的英文词频统计
JAVA-词频统计工具.zip 词频统计JAVA教程地址:https://blog.csdn.net/LiHaoHang6/article/details/136155956
此案例使用的是IDEA开发工具,项目属于maven项目 该词频统计案例中,数据源是自动产生的(java程序自定义生成的),针对自定义生成的数据完成词频统计,完成后打包上传到storm程序中执行
一、利用Java程序实现词频统计 二、利用Scala程序实现词频统计 三、利用Python程序实现词频统计 四、利用Akka和Scala实现词频统计 五、利用MapReduce实现词频统计 六、利用Hive实现词频统计 七、利用Storm实现词频...
自然语言理解 关于词频统计的代码 利用treemap来完成
简单的词频统计,利用java 实现,使用hash表
IKAnalyzer 实现分词 词频统计,用Eclipse实现,调用IKanalyzer的类,可以用于IKanalyzer的二次开发
开发工具idea,统计方式为java8的stream方式,不是传统的字节流统计方式,内含答辩PPT以及统计单词的文本,配置好后可直接使用
同时能对手动输入的进行词频统计。通过此课题,熟练掌握文件、数组、指针的各种操作,以及一些递归算法思想的应用。 (1)统计显示英文文档中所有出现的字母及出现次数; (2)计算字母出现次数百分比及发布图(或表);...
java 实现多线程读取文件,实现词频统计,得出出现频率的前几名。 如果没有积分,需要代码的可以私聊我,发给你链接
大数据mapreduce利用java实现词频统计功能的jar包
介绍 通过使用三种不同语言编写来编写...项目分为三个模块,分别用Java,Python,Scala编写逻辑相同的分词词频统计程序,比较其编写难度及运行效率。 三个模块分别为: wordCountJava wordCountPython wordCountScala