package com.yxcx.gettrain;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.SortedMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Iterator;
public class ComputeWordsVector {
// 统计每个词的总的出现次数,返回出现次数大于 3 词的词语的词汇构成最终
的属性词典
public SortedMap<String, Double> countWords(String strDir,
Map<String, Double> wordMap) throws IOException
{
File sampleFile = new File(strDir);// strDir 预处理好的文
件的路径
File[] sample = sampleFile.listFiles();
String word;
for (int i = 0; i < sample.length; i++) {
if (!sample[i].isDirectory()) {
FileReader samReader = new
FileReader(sample[i]);
BufferedReader samBR = new
BufferedReader(samReader);
while ((word = samBR.readLine()) !=
null) {
if (!word.isEmpty() &&
wordMap.containsKey(word)) {
double count =
wordMap.get(word) + 1;
wordMap.put(word,
count);
} else {
wordMap.put(word, 1.0);
}
}
} else
countWords(sample[i].getCanonicalPath(),
wordMap);
}
// 只返回出现次数大于 3 的词语
SortedMap<String, Double> newWordMap = new
TreeMap<String, Double>();
Set<Map.Entry<String, Double>> allWords =
wordMap.entrySet();
for (Iterator<Map.Entry<String, Double>> it =
allWords.iterator(); it
.hasNext();) {
Map.Entry<String, Double> me = it.next();
if (me.getValue() >= 3) {
newWordMap.put(me.getKey(),
me.getValue());
}
}
return newWordMap;
}
// 打印属性词典
void printWordMap(Map<String, Double> wordMap) throws
IOException {
// TODO Auto-generated method stub
int countLine = 0;
File outPutFile = new File(
"/data/classifydata/docVector/allDicWordCountMap.txt");
FileWriter outPutFileWriter = new
FileWriter(outPutFile);
Set<Map.Entry<String, Double>> allWords =
wordMap.entrySet();
for (Iterator<Map.Entry<String, Double>> it =
allWords.iterator(); it
.hasNext();) {
Map.Entry<String, Double> me = it.next();
outPutFileWriter.write(me.getKey() + " " +
me.getValue() + "\n");
countLine++;
}
System.out.println("WordMap size" + countLine);
}
}
2222222
最新推荐文章于 2024-09-09 14:40:58 发布