博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VSM向量空间模型对文本的分类以及简单实现
阅读量:4079 次
发布时间:2019-05-25

本文共 3366 字,大约阅读时间需要 11 分钟。

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
> samples = vsm.loadSample(basePath + "sort.txt"); vsm.samilarity(content, samples); } /** * 计算比对文章和样本的余弦值 * * @param content * @param samples */ public void samilarity(String content, Vector
> samples) { for (int i = 0; i < samples.size(); i++) { Vector
single = samples.get(i); // 存放每个样本中的词语,在该对比文本中出现的次数 Vector
wordCount = new Vector
(); 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
> loadSample(String path) { Vector
> vector = new Vector
>(); 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
single = new Vector
(); 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的实现,样本特征自动来实现。。

转载地址:http://soini.baihongyu.com/

你可能感兴趣的文章
时间不多了,在准备找工作上面,我们就像考研后期一样,多刷真题吧,而不是依旧抱着书看了。
查看>>
你会发现C++现在去做笔试题根本动不了笔
查看>>
笔试就重在刷题
查看>>
刷了下七月在线的C++笔试题和看了下《程序员面试笔记》我发现他们很多考点,知识点都是相似的
查看>>
腾讯课堂的C++ STL听课笔记
查看>>
STL运用的三个档次
查看>>
b站有不少STL的教学视频
查看>>
刷leetcode究竟要不要使用库函数
查看>>
LeetCode 刷题攻略
查看>>
一线互联网公司技术面试的流程以及注意事项
查看>>
现代C++语言可以看作是三部分组成
查看>>
别人推荐的一些C++书籍
查看>>
两种乐趣
查看>>
《C++ primer》第五版习题答案
查看>>
无人机桨的型号的含义
查看>>
木桨震动小
查看>>
关于桨叶越大,拉力越大,效率越高
查看>>
原来容器比如vector的本质是一个类模板
查看>>
opencv使用了很多标准模板库(STL)
查看>>
嵌入式软件工程师真的串口的开发是必备的技能
查看>>