XQuery/過濾單詞
外觀
< XQuery
有時您有一個文字主體,並且您想過濾掉給定列表中的單詞,通常稱為停用詞列表。

xquery version "1.0";
(: Test to see if a word is in a list :)
declare namespace exist = "http://exist.sourceforge.net/NS/exist";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes omit-xml-declaration=yes";
(: A list of words :)
let $stopwords :=
<words>
<word>a</word>
<word>and</word>
<word>in</word>
<word>the</word>
<word>or</word>
<word>over</word>
</words>
let $input-text := 'a quick brown fox jumps over the lazy dog'
return
<html>
<head>
<title>Test of is a word on a list</title>
</head>
<body>
<h1> Test of is a word on a list</h1>
<h2>WordList</h2>
<table border="1">
<thead>
<tr>
<th>StopWord</th>
</tr>
</thead>
<tbody>{
for $word in $stopwords/word
return
<tr>
<td align="center">{$word}</td>
</tr>
}</tbody>
</table>
<h2>Sample Input Text</h2>
<p>Input Text: <div style="border:1px solid black">{$input-text}</div></p>
<table border="1">
<thead>
<tr>
<th>Word</th>
<th>On List</th>
</tr>
</thead>
<tbody>{
for $word in tokenize($input-text, '\s+')
return
<tr>
<td>{$word}</td>
<td>{
if ($stopwords/word = $word)
then(<span style="color:green;">true</span>)
else(<span style="color:red;">false</span>)
}</td>
</tr>
}</tbody>
</table>
</body>
</html>
輸入字串使用tokenize函式拆分為單詞,該函式接受兩個引數,要解析的字串和用正則表示式表示的分隔符。這裡的單詞由一個或多個空格分隔。結果是一個單詞序列。
該程式使用 XPath 泛化相等來比較序列$stopwords/word和序列(一個專案)$word。如果這兩個序列具有公共專案,即停用詞列表包含該詞,則為真。
您還可以使用量化表示式來使用some...satisfies執行停用詞查詢——參見XQuery/量化表示式表示式,例如
some $word in $stopwords
satisfies ($word = $thisword)
還有其他選擇;停用詞作為字串序列,或者是一個長字串,並使用 contains() 或資料庫中的元素。
但是,效能存在顯著差異。有一組測試顯示了多種選擇之間的差異。單元測試
這些測試表明,在 eXist db 平臺上,建議的實現都遠非最佳。與元素相比,針對字串序列的測試大約需要五分之一的時間。泛化相等性與使用限定表示式的優越性相同。
看起來首選的方法是
let $stopwords := ("a","and","in","the","or","over")
let $input-string := 'a quick brown fox jumps over the lazy dog'
let $input-words := tokenize($input-string, '\s+')
return
for $word in $input-words
return $stopwords = $word
如果停用詞作為元素儲存,最好先轉換為原子序列
let $stopwords :=
<words>
<word>a</word>
<word>and</word>
<word>in</word>
<word>the</word>
<word>or</word>
<word>over</word>
</words>
let $stopwordsx := $stopwords/word/string(.)
let $input-string := 'a quick brown fox jumps over the lazy dog'
let $input-words := tokenize($input-string, '\s+')
return
for $word in $input-words
return $stopwordsx = $word
請注意,在資料庫中引用停用詞列表略微提高了效能。