跳轉到內容

XQuery/潛在語義索引

來自華夏公益教科書,開放的書籍,為開放的世界

您有一組文件,對於任何文件,您都希望找出哪些文件與任何給定文件最相似。

我們將使用一種名為“潛在語義索引”的文字挖掘技術。 我們首先將建立所有概念詞(術語)與所有文件的矩陣。 每個單元格將包含每個文件中術語的出現頻率。 然後,我們將這個詞-文件矩陣傳送到執行標準 奇異值分解 或 SVD 的服務。 SVD 是一種非常計算密集型的演算法,如果您的詞語和文件數量很多,則可能需要數小時或數天的時間來計算。 SVD 服務然後返回一組“概念向量”,這些向量可用於對相關文件進行分組。

示例資料

[編輯 | 編輯原始碼]

為了保持示例簡單,我們將只使用文件標題,而不是完整的文件。

以下是一些文件標題

XQuery Tutorial and Cookbook 
XForms Tutorial and Cookbook 
Auto-generation of XForms with XQuery 
Building RESTful Web Applications with XRX 
XRX Tutorial and Cookbook 
XRX Architectural Overview 
The Return on Investment of XRX 

我們的第一步將是構建一個詞-文件矩陣。 此矩陣包含文件中的所有詞語(在列中),以及每個文件的一列。

我們將分幾個步驟進行操作。

  1. 獲取所有文件中的所有詞語,並將其放入一個序列中
  2. 建立一個包含所有非“停用詞”的唯一詞語列表
  3. 對於每個詞語
    1. 對於每個文件,統計該詞語在文件中出現的頻率


示例詞-文件矩陣

[編輯 | 編輯原始碼]
詞語 1 2 3 4 5 6 7
應用程式 0.03125
建築 0.03125
自動生成 0.03125
構建 0.03125
食譜 0.03125 0.03125 0.03125
投資 0.03125
概述 0.03125
RESTful 0.03125
返回 0.03125
教程 0.03125 0.03125 0.03125
Web 0.03125
XForms 0.03125 0.03125
XQuery 0.03125 0.03125
XRX 0.03125 0.03125 0.03125 0.03125

示例程式原始碼

[編輯 | 編輯原始碼]
xquery version "1.0";

declare option exist:serialize "method=xhtml media-type=text/html indent=yes";

(: this is where we get our data :)
let $app-collection := '/db/apps/latent-semantic-analysis'
let $data-collection := concat($app-collection , '/data')

(: get all the titles where $titles is a sequence of titles :)
let  $titles := collection($data-collection)/html/head/title/text()
let $doc-count := count($titles)

(: A list of words :)
let $stopwords :=
<words>
   <word>a</word>
   <word>and</word>
   <word>in</word>
   <word>the</word>
   <word>of</word>
   <word>or</word>
   <word>on</word>
   <word>over</word>
   <word>with</word>
</words>

(: a sequence of words in all the document titles :)
(: the \s is the generic whitespace regular expression :)
let $all-words :=
   for $title in $titles
      return
         tokenize($title, '\s')

(: just get a distinct list of the sorted words that are not stop words :)
let $concept-words :=
   for $word in distinct-values($all-words)
   order by $word
      return
         if ($stopwords/word = lower-case($word))
            then ()
            else $word

let $total-word-count := count($all-words)
return
<html>
    <head>
        <title>All Document Words</title>
    </head>
    <body>
        <p>Doc count =<b>{$doc-count}</b> Word count = <b>{$total-word-count}</b></p>
        
        <h2>Documents</h2>
        <ol>
        {for $title in $titles
           return
               <li>{$title}</li>
         }
         </ol>
         
         <h2>Word-Document Matrix</h2>
         <table border="1">
            <thead>
               <tr>
               <th>Word</th>
               {for $doc at $count in $titles
                       return
                          <th>{$count}</th>
                    }
               </tr>
            </thead>
             {for $word in $concept-words
             return
                 <tr>
                    <td>{$word}</td>
                    {for $title in $titles
                       return
                          <td>{if (contains($title, $word)) 
                                 then (1 div $total-word-count)
                                 else (' ')}</td>
                    }
                 </tr>
             }
          </table>
    </body>
</html>

建立 Sigma 值

[編輯 | 編輯原始碼]

Sigma 矩陣是一個矩陣,它乘以詞語向量和文件向量

[Word Document Matrix] = [Word Vectors] X [Sigma Values] X [Document Vectors]
華夏公益教科書