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的實現,樣本特徵自動來實現。。