XSLT中的字频率计数器

保柏

我正在尝试在XSLT中创建一个单词频率计数器。我希望它使用停用词。我从迈克尔·凯(Michael Kay)的书开始但是我很难让停用词起作用。

此代码可在任何源XML文件上使用。

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet
   version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">   
    <xsl:variable name="stopwords" select="'a about an are as at be by for from how I in is it of on or that the this to was what when where who will with'"/>
     <wordcount>
        <xsl:for-each-group group-by="." select="
            for $w in //text()/tokenize(., '\W+')[not(.=$stopwords)] return $w">
            <word word="{current-grouping-key()}" frequency="{count(current-group())}"/>
        </xsl:for-each-group>
     </wordcount>
</xsl:template>

</xsl:stylesheet>

我认为这not(.=$stopwords)是我的问题所在。但是我不确定该怎么办。

我还将提示如何从外部文件加载停用词。

CM Sperberg-McQueen

您的$ stopwords变量现在是单个字符串;您希望它是一个字符串序列。您可以通过以下任意一种方式来执行此操作:

  • 将其声明更改为

    <xsl:variable name="stopwords" 
      select="('a', 'about', 'an', 'are', 'as', 'at', 
               'be', 'by', 'for', 'from', 'how', 
               'I', 'in', 'is', 'it', 
               'of', 'on', 'or', 
               'that', 'the', 'this', 'to', 
               'was', 'what', 'when', 'where', 
               'who', 'will', 'with')"/>
    
  • 将其声明更改为

    <xsl:variable name="stopwords" 
      select="tokenize('a about an are as at 
                        be by for from how I in is it 
                        of on or that the this to was 
                        what when where who will with',
                        '\s+')"/>
    
  • 从名为(例如)stoplist.xml的外部XML文档中读取该文件,格式为

    <stop-list>
      <p>This is a sample stop list [further description ...]</p>
      <w>a</w>
      <w>about</w>
      ...
    </stop-list>
    

    然后加载它,例如

    <xsl:variable name="stopwords"
      select="document('stopwords.xml')//w/string()"/>
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章