读书人

VSM向量空间模型对文本的归类以及简单

发布时间: 2012-08-14 10:39:57 作者: rapoo

VSM向量空间模型对文本的分类以及简单实现

1:对文本的分类,不管用什么高级的方法,首先还是需要建立数学模型的,这个地方就用SVM来建立,他的原理是根据文本的特征,比如一个文本有10个特征(一般来说每个特征是一个代表这个文本的关键词),那么这个文本向量大小就是10了。具体的每个值就是这个特征的权重(关于权重的计算很多种,我这个地方只用了词频来代表)。然后读入测试本文,根据该测试文本中的特征,看和样本中的特征的向量做运算,这个地方用的是求向量的夹角,用余弦值来表达,夹角大的就偏的远,否则比较近(这个地方没考虑到角度大于90°的情况)。

2:这个例子是为了我接下来做SVM用的,对于搞此类的算是个入门。我觉得这个效果要和输入的样本特征关系很大,我对同类的比如股票下不同类别来做判断,基本也可以判断出来权重。

3:java源代码如下:

package com.baseframework.sort;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Vector;public class VsmMain {public static void main(String[] args) {VsmMain vsm = new VsmMain();String basePath = vsm.getClass().getClassLoader().getResource("").toString().substring(6);String content = vsm.getContent(basePath + "article.txt");Vector<Vector<String>> samples = vsm.loadSample(basePath + "sort.txt");vsm.samilarity(content, samples);}/** * 计算比对文章和样本的余弦值 *  * @param content * @param samples */public void samilarity(String content, Vector<Vector<String>> samples) {for (int i = 0; i < samples.size(); i++) {Vector<String> single = samples.get(i);// 存放每个样本中的词语,在该对比文本中出现的次数Vector<Integer> wordCount = new Vector<Integer>();for (int j = 0; j < single.size(); j++) {String word = single.get(j);int count = getCharInStringCount(content, word);wordCount.add(j, count);//System.out.print(word + ":" + tfidf + ",");}//System.out.println("\n");// 计算余弦值int sampleLength = 0;int textLength = 0;int totalLength = 0;for (int j = 0; j < single.size(); j++) {// 样本中向量值都是1sampleLength += 1;textLength += wordCount.get(j) * wordCount.get(j);totalLength += 1 * wordCount.get(j);}// 开方计算double value = 0.00;if(sampleLength > 0 && textLength > 0){value = (double)totalLength/(Math.sqrt(sampleLength) * Math.sqrt(textLength));}System.out.println(single.get(0) + "," + sampleLength + ","+ textLength + "," + totalLength + "," + value);}}/** * 计算word在content中出现的次数 *  * @param content * @param word * @return */public int getCharInStringCount(String content, String word) {String str = content.replaceAll(word, "");return (content.length() - str.length()) / word.length();}/** * 加载样本 *  * @param path * @return */public Vector<Vector<String>> loadSample(String path) {Vector<Vector<String>> vector = new Vector<Vector<String>>();try {try {FileReader reader = new FileReader(new File(path));BufferedReader bufferReader = new BufferedReader(reader);String hasRead = "";while ((hasRead = bufferReader.readLine()) != null) {String info[] = hasRead.split(",");Vector<String> single = new Vector<String>();for (int i = 0; i < info.length; i++) {single.add(info[i]);}vector.add(single);}} catch (FileNotFoundException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}return vector;}/** * 读取对应path的文件内容 *  * @param path * @return */public String getContent(String path) {StringBuffer buffer = new StringBuffer();try {try {FileReader reader = new FileReader(new File(path));BufferedReader bufferReader = new BufferedReader(reader);String hasRead = "";while ((hasRead = bufferReader.readLine()) != null) {buffer.append(hasRead);}} catch (FileNotFoundException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}return buffer.toString();}}


里面sort就是我自己手工维持的类别特征,每个特征用,隔开。article是我自己输入的待测试文本。。

入门用吧。。看起来效果还是可以的。接下来我再做svm的实现,样本特征自动来实现。。

读书人网 >VSTS

热点推荐