我也正在研究关键字类型的计数机制,就像发布在这里的 Word Frequency Counter in XSLT :
我的问题是我的关键字可能有多个单词,例如:
<xsl:variable name="stopwords"
select="('audio codec', 'dual audio', 'audio switch' )"/>
我正在处理上面问题中的代码,并得到了这样的结果:
<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="('audio codec', 'dual audio', 'audio switch' )"/>
<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>
当然,使用“\W+”进行标记会将其分解为单词,但是这将与我的停用词不匹配,因为它们是并且可以是多个单词。
当关键字可能有多个单词时,有人可以建议一种优雅的方法来进行字数统计吗?
感谢您的帮助!
拉斯
最佳答案
给定此输入 XML:
<?xml version="1.0" ?>
<a>
<b>match: audio switch</b>
<c>no match:</c>
<d>no match: audiocodec</d>
<e attr="no match: audio codec"/>
No match: Audio switch/dual AUDIO
Match x2: audio switch/dual audio/audio switch
No match: <f>xxx audio</f><g>codec yyy</g>
</a>
此 XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://example.com/f"
exclude-result-prefixes="f">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="keyphrases"
select="('audio codec', 'dual audio', 'audio switch' )"/>
<xsl:template match="/">
<xsl:variable name="docText" select="string-join(//text(), '|')"/>
<keyphrases>
<xsl:for-each select="$keyphrases">
<keyphrase phrase="{.}" count="{f:substr-count($docText, .)}"/>
</xsl:for-each>
</keyphrases>
</xsl:template>
<xsl:function name="f:substr-count">
<xsl:param name="s"/>
<xsl:param name="substr"/>
<xsl:value-of select="if ($s and $substr and contains($s, $substr))
then f:substr-count(substring-after($s, $substr), $substr)+1
else 0"/>
</xsl:function>
</xsl:stylesheet>
将生成此输出 XML,用于计算“停止”词的出现次数(我将其重命名为关键短语
):
<?xml version="1.0" encoding="UTF-8"?>
<keyphrases>
<keyphrase phrase="audio codec" count="0"/>
<keyphrase phrase="dual audio" count="1"/>
<keyphrase phrase="audio switch" count="3"/>
</keyphrases>
关于xml - xslt 中的关键字计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26469954/