tfidf 做自然语言理解的经常用。文档的作为权重计算,
许多初学者搞不清楚,权重计算特征选择。
针对文本分类而言 作为很简单的说明,特征选择都跟 类别有关比如 卡方 信息增益
而权重计算则与类别无关 比如 tfidf,tf
具体公式请参看 http://zh.wikipedia.org/wiki/TF-IDF
下面代码 写的比较粗陋。高手请避开。,,,,,
训练集可以上搜狗数据集上下载
写完以后感觉还是matlab 比较爽啊。。几行 解决java100多行的代码
public class TfIdfcomputor {
static String path = "E:\\训练集\\train\\";
static String vsmpath = "E:\\训练集\\vsm\\vsm.txt";
//static String path = "E:\\训练集\\SogouC.reduced\\Reduced";
// static String path ="E:\\训练集\\SogouC.mini\\Sample";
static Map<String, Map<String, Double>> DFdic = new HashMap();
static HashMap<String, List<Map<String, Double>>> dic = new HashMap();
static int DCOUNT;
static HashMap<String, Double> idfDic = new HashMap();
static Map<String, Map<String,Double>> TFIDFdic = new HashMap();
// static Map<String,List<String>> Simpledic= new HashMap();
public static void main(String[] args) throws IOException {
TfIdfcomputor tf = new TfIdfcomputor();
File[] Filelist = tf.readData(path);
DCOUNT = Filelist.length;
tf.readir(Filelist);
System.out.println("DFdic");
tf.computeIDF(dic);
tf.ComputeTFIDF();
/* for( String s :TFIDFdic.keySet())
{
Map map = TFIDFdic.get(s);
for(Object key :map.keySet() )
{
System.out.println("file "+s +"word "+ key+" tfidf "+map.get(key));
}
}*/
System.out.println("计算完毕开始输出");
tf.toLibData();
}
public void readir(File[] dir) throws IOException {
File[] fileList = dir;
for(File f :fileList){
System.out.println(f.getPath());
}
for (int i = 0; i < fileList.length; i++) {
File f = fileList[i];
//System.out.println(f.getPath());
String[] textword = cutWord(FiletoText(f.getPath()));
Map tf = computeTf(textword);
DFdic.put(f.getPath(), tf);
addDic(f.getPath(), tf);
}
System.out.println("TF PROCESS IS OVER");
System.out.println(dic.size());
for (Object o : dic.keySet()) {
System.out.println(o);
List list = dic.get(o);
for (Object l : list) {
Map pair = (Map) l;
for (Object p : pair.keySet()) {
/* System.out.println("key" + (String) o + "..."
+ "filepath...." + p + "tf..." + pair.get(p));*/
}
}
}
}
public String FiletoText(String path) throws IOException {
File f = new File(path);
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(f), "GB2312"));
String result = "";
String temp;
while ((temp = br.readLine()) != null) {
result = result + temp;
}
br.close();
return result;
}
public String[] cutWord(String text) throws IOException {
MMAnalyzer analyzer = new MMAnalyzer();
String temp = analyzer.segment(text, "|");
//System.out.println(temp);
String[] result = temp.split("\\|");
/*
* for(String s :result ) { System.out.println(s); }
*/
return result;
}
public HashMap<String, Double> computeTf(String[] textword) {
double size = textword.length;
HashMap<String, Integer> map = new HashMap();
for (int i = 0; i < textword.length; i++) {
//System.out.println(textword[i]);
if (map.containsKey(textword[i])) {
Integer count = map.get(textword[i]);
map.put(textword[i], count + 1);
} else {
map.put(textword[i], 1);
}
}
HashMap<String, Double> result = new HashMap();
for (Object o : map.keySet()) {
Integer temp = map.get(o);
//System.out.println((String) o + "count..." + temp);
result.put((String) o, temp / size);
//System.out.println((String) o + "tf..." + result.get(o));
}
return result;
}
public void addDic(String path, Map tf) {
//System.out.println(",,,,,,,,,,,,,");
for (Object o : tf.keySet()) {
if (dic.containsKey((String) o)) {
ArrayList list = (ArrayList) dic.get(o);
HashMap map = new HashMap();
map.put(path, (Double) tf.get((String) o));
list.add(map);
dic.put((String) o, list);
} else {
HashMap map = new HashMap();
map.put(path, (Double) tf.get((String) o));
ArrayList list = new ArrayList<Map<String, Double>>();
list.add(map);
dic.put((String) o, list);
}
}
}
public static File[] readData(String path) {
int size = 0;
File[] floderdir = new File(path).listFiles();
ArrayList list = new ArrayList();
for (File f : floderdir) {
// size = size+(int)f.length();
File[] childdir = f.listFiles();
for (File file : childdir) {
list.add(file);
}
}
size = list.size();
File[] fdir = new File[size];
for (int i = 0; i < size; i++) {
fdir[i] = (File) list.get(i);
}
return fdir;
}
public void computeIDF(HashMap<String, List<Map<String, Double>>> map) {
for (String key : map.keySet()) {
List list = map.get(key);
double hasCount = (double) list.size();
double idf = DCOUNT / hasCount;
idfDic.put(key, idf);
}
}
public void ComputeTFIDF() {
for (String filepath : DFdic.keySet()) {
Map filedic = DFdic.get(filepath);
HashMap<String, Double> tfidfPair = tfidfPair = new HashMap();
for (Object key : filedic.keySet()) {
double tf = (Double) filedic.get(key);
double idf = idfDic.get((String) key);
double tfidf = tf* Math.log(idf);
// System.out.println( key+"tf" + tf + "idf" + idf + "tfidf" + tfidf);
tfidfPair.put((String) key, tfidf);
}
// System.out.println(tfidfPair.size());
TFIDFdic.put(filepath, tfidfPair);
}
}
public void toLibData() throws IOException// 转化成libsvm格式;
{
int count = 0;
// int size =dic.entrySet().size();
List wordList = new ArrayList();
for (String word : dic.keySet()) {
wordList.add(word);
System.out.println("worddic add" + word);
}
// System.out.println("total word is"+wordList.size());
BufferedWriter bw = new BufferedWriter(
new FileWriter(new File(vsmpath)));
/*
* String [] wordList = new String[size]; int num=0; for(String word:
* dic.keySet()) { wordList[num]=word; num++;
* System.out.println("worddic add"+word); }
*/
String vsm = "";
for (String filename : TFIDFdic.keySet()) {
String lable = new File(filename).getParentFile().getName();
Map map = TFIDFdic.get(filename);// 获取某片文章对应的tfidf
vsm = vsm + lable + " ";
for (int i = 0; i < wordList.size(); i++) {
// System.out.println( "map.."+ map.size());
// String temp =wordList[i];
// System.out.println("temp"+ temp);
String temp = (String) wordList.get(i);
if (map.containsKey(temp)) {
vsm = vsm + i + ":" + map.get(temp) + " ";
// System.out.println(filename + "...." + temp + "...."+
// map.get(temp) + "...");
}
}
count++;
vsm = vsm + "\n";
bw.write(vsm);
vsm = "";
System.out.println("format" + " " + count + " " + filename);
}
System.out.println("begin output");
// BufferedWriter bw = new BufferedWriter(new FileWriter(new
// File(vsmpath)));
// bw.write(vsm);
System.out.println(".............................");
// System.out.println(vsm);
bw.close();
}
}
分享到:
相关推荐
TF-IDF的java实现,eclipse工程,直接可用
主要介绍了Java实现TFIDF算法代码分享,对算法进行了简单介绍,概念,原理,以及实现代码的分享,具有一定参考价值,需要的朋友可以了解下。
自己实现的tfidf 自娱自乐,高手请闪开。谢谢
TFIDF算法 java实现,带文档注解。
需要的就下吧,其实源代码和jar包之前都有提供,这次花费了点时间整理了下。
tfidf java实现 适用于单篇文档提取关键词 实测效果还行
使用Java实现TFIDF方法的文本聚类
利用java实现TFIDF,提取关键词,是术语提取、推荐系统等应用的基础之一。
用lucene实现TFIDF计算,并进行排序
java语言写的一个TFIDF代码,可以用于特征选择,是自然语言处理的一个经典算法。
java实现的KNN算法,对学习KNN算法有帮助
使用JAVA编写的,分词、TFIDF相似度计算,K临近法聚类
java写的聚类程序,包括kmeans算法的实现. 部分java程序可以重用.
主要介绍了TF-IDF理解及其Java实现代码实例,简单介绍了tfidf算法及其相应公式,然后分享了Java实现代码,具有一定参考价值,需要的朋友可以了解下。
3、基于MapReduce的TFIDF算法实现(java语言) 4、基于MapReduce的大矩阵乘法(java语言) 5、MapReduce基础Demo(java语言) 6、Hbase基础Demo(java语言) 7、HDFS基础Demo(java语言) - -------- 不懂运行,下载...
自然语言处理(汉语)算法实现,Java语言实现,经过优化,效率很高。主要包括: 1)汉语分词,采用ICTCLAS系统和Lucene+庖丁解牛系统 2)情感倾向性分析,包括基于统计学习的SVM算法,基于情感词典的词语权重算法,给...
对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同...
文本分来,文中进行了分词,去停用词,用TFIDF来实现
用TFIDF和特征增益两种方式实现了特征向量空间的建立,将文本文件表示成特征向量的形式,为接下来的聚类做了准备。程序用JAVA写成。
THUCTC(THU Chinese Text Classification)是由清华大学自然语言处理实验室推出的中文文本分类工具包,能够自动高效地实现用户自定义的文本分类语料的训练、评测、分类功能。文本分类通常包括特征选取、特征降维、...