xsl-list
[Top] [All Lists]

[xsl] Re: Anyone implemented a fuzzy matcher in XSLT/XPath?

2013-02-07 04:50:29
"Costello, Roger L." <costello(_at_)mitre(_dot_)org> wrote:

Has anyone implemented a fuzzy matcher 
(approximate string matcher [1]) in XSLT/XPath?

On 1/30/2013 Roger L. Cauvin responded:

I implemented one, based on pairwise string 
alignment, as an XSLT 1.0 template.  The 
compare-strings template takes as parameters 
two strings and returns a score between 0 and 1 
representing the closeness of the match

This is so cool Roger. I tested your program and it is fantastic.

Here's my test. I first created this XML document containing a list of names:

<Names>
    <Name>Smith</Name>
    <Name>Costello</Name>
    <Name>costello</Name>
    <Name>COSTELLO</Name>
    <Name>CoStEllO</Name>
    <Name>Costelo</Name>
    <Name>Costell</Name>
    <Name>Costllo</Name>
</Names>

I want to do a fuzzy match on the name 'Costello'.

I created a stylesheet that imports your fuzzy matcher code and then loops 
through each name, calling your compare-strings named-template and printing out 
the results. Here are the results. My test-stylesheet follows. Note: if the 
fuzzy matcher returned a score greater than or equal to 0.9 then I decided it's 
a match.

-------------------------------------------
                Output
-------------------------------------------
Searching for matches on: Costello

Results:

Not a match: Smith
Fuzzy match score: 0

Found a match: Costello
Fuzzy match score: 1

Found a match: costello
Fuzzy match score: 1

Found a match: COSTELLO
Fuzzy match score: 1

Found a match:CoStEllO
Fuzzy match score:1

Found a match: Costelo
Fuzzy match score: 0.9230769230769231

Found a match: Costell
Fuzzy match score: 0.9230769230769231

Not a match: Costllo
Fuzzy match score: 0.7692307692307693

------------------------------------------
    test-fuzzy-matcher.xsl
------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        version="1.0">
    
    <xsl:import href="fuzzy-matcher.xsl"/>
    
    <xsl:output method="text"/>
    
    <xsl:variable name="name-being-searched-for" select="'Costello'" />
    
    <xsl:template match="Names">
        
        <xsl:text>Searching for matches on:</xsl:text>
        <xsl:value-of select="$name-being-searched-for"/>
        <xsl:text>Results:</xsl:text>
        
        <xsl:for-each select="Name">
            <xsl:variable name="check-this-name" select="." />
            <xsl:variable name="result-of-fuzzy-match-test">
                <xsl:call-template name="compare-strings">
                    <xsl:with-param name="string1" 
select="$name-being-searched-for" />
                    <xsl:with-param name="string2" select="$check-this-name" />
                </xsl:call-template>
            </xsl:variable>

            <xsl:choose>
                <xsl:when test="$result-of-fuzzy-match-test &gt;= 0.9">
                    <xsl:text>Found a match:</xsl:text>
                    <xsl:value-of select="$check-this-name"/>
                    <xsl:text>Fuzzy match score:</xsl:text>
                    <xsl:value-of select="$result-of-fuzzy-match-test"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>Not a match:</xsl:text>
                    <xsl:value-of select="$check-this-name"/>
                    <xsl:text>Fuzzy match score:</xsl:text>
                    <xsl:value-of select="$result-of-fuzzy-match-test"/>    
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
        
    </xsl:template>
    
</xsl:stylesheet>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] Re: Anyone implemented a fuzzy matcher in XSLT/XPath?, Costello, Roger L. <=