2222222

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值