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++) {
				// 样本中向量值都是1
				sampleLength += 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的实现,样本特征自动来实现。。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值