文本相似度的設(shè)計與實現(xiàn)_第1頁
文本相似度的設(shè)計與實現(xiàn)_第2頁
文本相似度的設(shè)計與實現(xiàn)_第3頁
文本相似度的設(shè)計與實現(xiàn)_第4頁
文本相似度的設(shè)計與實現(xiàn)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、文本相似度的設(shè)計與實現(xiàn)摘要:本文主要設(shè)計并實現(xiàn)了一個文本相似度系統(tǒng),該系統(tǒng)主要功能計算文檔之間的相似度,通過使用向量空間模型(VSM, Vector Space Model)及余弦相似度計算公式計算文檔之間的相似度,數(shù)據(jù)預處理過程中加入word2vec模型進行語義擴充,從而能夠匹配到更多相關(guān)文檔。1. 向量空間模型向量空間模型(VSM, Vector Space Model)由Salton等人于20世紀70年代年提出1,2。向量空間模型的主要思想是將文本內(nèi)容的處理簡化為向量空間中的向量運算,這樣將空間上的相似度轉(zhuǎn)化為語義上的相似度。當文檔被表示為文檔空間的向量時,便可通過計算向量之間的相似性來

2、度量文檔間的相似性。文本處理中最常用的相似性度量方式是余弦距離。向量空間模型的基本思想:給定一篇文檔D=D(T1,T2,Ti,Tn),若Ti在文檔中既可以重復出現(xiàn)又存在先后次序,因此分析起來會較為困難。針對上述情況,暫不考慮Ti的順序,并要求Ti互異,此時可將T1,T2,Ti,Tn看作n維坐標,每一維對應相應值Wi,因此D(W1,W2,Wi,Wn)便可以看作一個n維向量。例如:有一篇文檔D=大家好,才是真的好,首先進行分詞后轉(zhuǎn)換為D=大家/好/才是/真的/好,之后提取出公因詞D=大家,好,才是,真的,最后通過向量空間模型將文檔轉(zhuǎn)換為對應的向量D=1,2,1,1。向量空間模型只是將文檔轉(zhuǎn)換為方便

3、計算的格式,若進行相似度計算,還需使用相似度計算公式進行計算。本文使用余弦相似度計算公式。2. 余弦相似度余弦相似度計算公式廣泛應用于文本數(shù)據(jù)之間的相似度計算過程中。其數(shù)學表達如下:計算過程如下:例如,有2個文檔D1=大家好,D2=才是真的好,首先將D1、D2分詞后,D1=大家/好,D2=才是/真的/好,其次提取出公因詞D=大家,好,才是,真的,然后通過向量空間模型轉(zhuǎn)換成向量表達,D1=1,1,0,0,D2=0,1,1,1,最后進行相似度計算3. 文本相似度系統(tǒng)本文主要使用向量空間模型及余弦相似度距離公式進行文本相似度計算任務,系統(tǒng)的基本架構(gòu)如下圖1所示:圖1 系統(tǒng)架構(gòu)其基本思想為:將文檔輸入

4、系統(tǒng),對文檔進行數(shù)據(jù)預處理操作,數(shù)據(jù)預處理完成后使用向量空間模型將詞組轉(zhuǎn)化為向量,之后使用余弦相似度計算公式求解文檔之間的相似度,最終將計算后的結(jié)果展示出來。數(shù)據(jù)預處理階段,包括分詞、取停用詞、word2vec語義擴展,其流程如下圖2所示:圖2 數(shù)據(jù)預處理在word2vec語義擴展階段,Word2vec是Google于2013年發(fā)布的一款基于深度學習的開源工具包,主要用于將單詞以向量形式表示3。Word2vec首先使用語料訓練模型,待模型訓練結(jié)束后,將新的單詞輸入模型進行預測,模型可按相關(guān)度排序?qū)⒆钕嘟念A測單詞展現(xiàn)給用戶,通常而言,會將top30展示給用戶。針對文檔語義擴充,系統(tǒng)會先使用搜狗

5、新聞語料訓練CBOW模型,待模型訓練結(jié)束后,將本檔中的單詞輸入CBOW模型進行預測,最終將預測結(jié)果擴充回文檔中,用于向量空間模型。同樣以之前的2篇文檔為例,D1=大家/好,D2=才是/真的/好,通過word2vec模型后,D1=大家/好/很好/不錯,D2=才是/真的/好/很好/不錯,提取出公因詞D=大家,好,很好,不錯,才是,真的,然后通過向量空間模型轉(zhuǎn)換成向量表達,D1=1,1,1,1,0,0,D2=0,1,1,1,1,1,最后進行相似度計算通過比較兩次的Score值可得出,通過word2vec能夠提高文本相似度的計算分值。另外系統(tǒng)會計算文檔中每一句話所對應的最大匹配及其相似度值,針對文檔與

6、文檔的相似度計算,本文提出一種平均相似度計算公式,即:其中n(dicList1)是所求文檔中包含的句子個數(shù),公式的主要思路即將每句話的最大匹配相似度疊加后求取平均值。4. 系統(tǒng)設(shè)計相應代碼如下:/* * 程序運行入口 * throws IOException */public static void main(String args) throws IOException String dir,inputPath1,inputPath2,outputPath,word2vecModel,str1,str2;long start,end,dur;start = System.currentTim

7、eMillis();dir = "data/test/"inputPath1 = dir + "doc3.txt"inputPath2 = dir + "doc3.txt"dir = "data/result/"outputPath = dir + "out3_3.txt"word2vecModel = "model/vectors.bin"FileHandler fh = new FileHandler();List<String> docList1 = fh.

8、putFileToList(inputPath1);List<String> docList2 = fh.putFileToList(inputPath2);Tool.initWriter1(outputPath);Word2VEC w = new Word2VEC() ; w.loadGoogleModel(word2vecModel) ; Model model = new Model(); model.run(docList1, docList2 ,w); end = System.currentTimeMillis(); dur = end - start; System.

9、out.println("dur time = " + (1.0 * dur / 1000) + " s"); System.out.println("dur time = " + (1.0 * dur / (1000 * 60) + " min");數(shù)據(jù)預處理階段如下:public class FileHandler public static FileReader fr=null;public static BufferedReader br=null;public static String line=nul

10、l;/* * input:path * 將文件內(nèi)容添加入list中,一句一條 */public List<String> putFileToList(String path) List<String> docList = new ArrayList<String>();try fr = new FileReader(path);br = new BufferedReader(fr);while (line = br.readLine() != null) String strArr;if(line.contains(".")strArr

11、= line.split(".");System.out.println(strArr.length);else if (line.contains("。") strArr = line.split("。");else if (line.contains("") strArr = line.split("");else if (line.contains("") strArr = line.split("");else strArr = new Strin

12、g1;strArr0 = line;int i,n;n = strArr.length;for(i = 0;i < n;i+)docList.add(strArri); catch (Exception e) e.printStackTrace();return docList;/* * input:path * output:List<String> * 讀取詞典信息,以list返回詞典 */public List<String> readDic(String path) List<String> list = new ArrayList<St

13、ring>();try fr=new FileReader(path);br=new BufferedReader(fr); while (line=br.readLine() != null ) StringTokenizer st = new StringTokenizer(line); while(st.hasMoreElements() list.add(st.nextToken(); br.close(); br.close(); catch (Exception e) e.printStackTrace();return list;public class StringHan

14、dler public static byte bt;public static InputStream is;public static Reader read;public static Lexeme t;public static IKSegmenter iks;/* * input:str * 將字符串分詞轉(zhuǎn)換成數(shù)組 */public List<String> stringToArray(String str) List<String> list = new ArrayList<String>();bt = str.getBytes();is = n

15、ew ByteArrayInputStream(bt);read = new InputStreamReader(is);iks = new IKSegmenter(read, true);try while (t = iks.next() != null) list.add(t.getLexemeText(); catch (IOException e) e.printStackTrace();return list;/* * input:arr * 使用word2vec將字符數(shù)組內(nèi)容擴充 */public List<String> extendWord(Word2VEC w,

16、List<String> list) List<String> tempList = new ArrayList<String>();Set<WordEntry> temp;Iterator iter;WordEntry entry;int i,n;n = list.size();for(i = 0;i < n;i+)temp = w.distance(list.get(i);iter = temp.iterator();if(!tempList.contains(list.get(i)tempList.add(list.get(i);wh

17、ile(iter.hasNext()entry = (WordEntry) iter.next();if(!tempList.contains()tempList.add();return tempList;/* * input:list,stopWordsPath * 刪除停用詞,通過讀取stopWordsPath中的停用詞表,將list中的停用詞刪除,并返回去除停用詞后的list */public List<String> deleteStopWords(List<String> list, String path2) Fil

18、eHandler fh = new FileHandler();List<String> stopDic = fh.readDic(path2);List<String> temp = new ArrayList<String>();int i,n;n = list.size();try for(i = 0;i < n;i+)if(!stopDic.contains(list.get(i)temp.add(list.get(i); catch (Exception e) e.printStackTrace();return temp;模型計算階段如下:

19、public class Model /* input:docList1,docList2 主方法入口及控制器 */public void run(List<String> docList1, List<String> docList2, Word2VEC w) int i, j, n1, n2;double similarArr;int locArr;double max, temp;int loc;n1 = docList1.size();n2 = docList2.size();similarArr = new doublen1;locArr = new intn

20、1;for (i = 0; i < n1; i+) max = 0.0;temp = 0.0;loc = 0;for (j = 0; j < n2; j+) try temp = getSimilar(docList1.get(i), docList2.get(j), w); catch (IOException e) temp = 0.0;e.printStackTrace();if (temp > max) max = temp;loc = j;similarArri = max;locArri = loc;Tool.output(docList1, docList2,

21、locArr, similarArr);/*input:str1,str2 計算2個字符串之間的相似度 */public double getSimilar(String str1, String str2, Word2VEC w) throws IOException double ret = 0.0;/ 創(chuàng)建向量空間模型,使用map實現(xiàn),主鍵為詞項,值為長度為2的數(shù)組,存放著對應詞項在字符串中的出現(xiàn)次數(shù)Map<String, int> vectorSpace = new HashMap<String, int>();int itemCountArray = null

22、;/ 為了避免頻繁產(chǎn)生局部變量,所以將itemCountArray聲明在此Iterator iter;double vector1Modulo = 0.00;/ 向量1的模double vector2Modulo = 0.00;/ 向量2的模double vectorProduct = 0.00; / 向量積List<String> list1,list1_temp,list2,list2_temp,temp1,temp2;StringHandler sh = new StringHandler();list1_temp = sh.stringToArray(str1);list2

23、_temp = sh.stringToArray(str2);/*/使用word2vec擴充語義temp1 = sh.stringToArray(str1);temp2 = sh.stringToArray(str2);list1 = sh.extendWord(w, temp1);list2 = sh.extendWord(w, temp2);*/list1 = sh.deleteStopWords(list1_temp, Conf.stopWordsPath);list2 = sh.deleteStopWords(list2_temp, Conf.stopWordsPath);int i,

24、n;n = list1.size();for (i = 0; i < n; +i) if (vectorSpace.containsKey(list1.get(i)+(vectorSpace.get(list1.get(i)0);else itemCountArray = new int2;itemCountArray0 = 1;itemCountArray1 = 0;vectorSpace.put(list1.get(i), itemCountArray);/ 對str2處理n = list2.size();for (i = 0; i < n; +i) if (vectorSpace.containsKey(list2.get(i)+(vectorSpace.get(list2.get(i)1);else itemCountArray = new int2;itemCountArray0 = 0;itemCountArray1 = 1;vectorSpace.put(list

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論