java 向量空间模型_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> 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++) {// 樣本中向量值都是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> 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的實現,樣本特征自動來實現。。
本文轉載自:CSDN博客
歡迎加入我愛機器學習QQ14群:336582044
微信掃一掃,關注我愛機器學習公眾號
總結
以上是生活随笔為你收集整理的java 向量空间模型_VSM向量空间模型对文本的分类以及简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 年计算_用Java计算leap
- 下一篇: java旋转图片并画出_java实现图片