viv:term-key-match

replaces term nodes based on a key match from an XML file

Synopsis

node
viv:term-key-match
(query, db, tag, where, flags);
node-set    query;
string    db;
string    tag;
"start"|"end"|"all"|"first"|"last"    where;
""|"s"|"g"|"gs"    flags;

Description

Replace some term nodes in a structured query (likely to have been obtained with viv:current-query) or a node-set of terms, based on a key match in an XML file db.

This is a powerful function allowing you to hardcode some results (or even any kind of output) based on "fuzzy" matches with the query. Note that when testing for match, the function will look at certain combinations of successive words (see the where option) after a combination of stemming and/or knowledge bases have eventually been applied.

Arguments

  • query: a structured query node (likely to have been obtained through a call to viv:current-query) or node-set of term elements.
  • db: this is the name of an XML file containing key-matches. If the filename is not fully qualified it will be considered relative to {INSTALL_DIR}/data/key-match.

    The key match file (i.e, XML "database") should have a top element

    <key-match xmlns:xi="http://www.w3.org/2003/XInclude" stem="...." kb="....">...</key-match>

    The sub-elements can have any name but they need to have a name attribute.

    The term-key-match looks up the longest matching sequence of terms that matches a name attribute using the stemmer(s) and the knowledge base(s) for both the name attributes and the search strings).

  • tag: the name of the node by which the matched terms will be replaced. This also restricts which elements in the key match XML file can be matched.
  • where: decides which groups of term nodes will be tested for a match. The default value for this parameter is "all".
    • "start": starting with the first term in the list.
    • "end": ending with the last term in the list.
    • "all": match must include all terms in the list.
    • "first": first matching occurrence (this is quadratic in the number of terms!).
    • "last": last matching occurrence (this is quadratic in the number of terms!).
  • flags: any combination of
    • 'g': find all matches.
    • 's': match only single terms (much faster if that's what you want).

    The default value for this parameter is the empty string, '""'.

Returns

a copy of the query in which the eventual replacements have been operated. Note that the original terms which have been replaced will be inserted under the replacement node.

Examples

Consider the following key match XML file:

    <key-match xmlns:xi="http://www.w3.org/2003/XInclude" kb="states">
    <city name="pittsburgh"/>
    <city name="pittsburgh, pa"/>
    </key-match>

with the states kb containing:

    <rephrase xmlns:xi="http://www.w3.org/2003/XInclude" this="pennsylvania" as="PA"/>

When matching:

    <operator xmlns:xi="http://www.w3.org/2003/XInclude">
    <term str="pittsburgh"/><term str="pennsylvania"/>
    </operator>

viv:term-key-match will return

    <operator xmlns:xi="http://www.w3.org/2003/XInclude">
    <city name="pittsburgh, pa">
    <term str="pittsburgh"/><term str="pennsylvania"/>
    </city>
    </operator>

Input Example:

            <param name="query" value="map of the United States"/>
            <query form="simple"/>
            <process-xsl>
  <!-- <<<< -->
  <xsl:variable name="current-query" select="viv:current-query()"/>
  <current-query>
    <xsl:copy-of select="$current-query"/>
  </current-query>
  <xsl:variable name="current-query-identify-map-terms"
    select="viv:term-match($current-query, 'map|map of|map of the|directions', 'mapterms', 'wc-set', 'first', 'g')"
  />
  <current-query-identify-map-terms>
    <xsl:copy-of select="$current-query-identify-map-terms"/>
  </current-query-identify-map-terms>
  <xsl:variable name="current-query-identify-country"
    select="viv:term-key-match($current-query-identify-map-terms, 'countries.xml', 'country', 'first')"
  />
  <current-query-identify-country>
    <xsl:copy-of select="$current-query-identify-country"/>
  </current-query-identify-country>
  <identify-country>
  <xsl:copy-of select="viv:string-key-match('united states', 'countries.xml', 'country')"
  />
  </identify-country>

</process-xsl>

Output Example:

            <param name="query" value="map of the United States"/>
            <meta query=" map of the United States "/>
            <query>
               <operator logic="and">
                  <term field="query" str="map" position="0" processing="strict"/>
                  <term field="query" str="of" position="1" processing="strict"/>
                  <term field="query" str="the" position="2" processing="strict"/>
                  <term field="query" str="United" position="3" processing="strict"/>
                  <term field="query" str="States" position="4" processing="strict"/>
               </operator>
            </query>
            <current-query>
               <operator logic="and">
                  <term field="query" str="map" position="0" processing="strict"/>
                  <term field="query" str="of" position="1" processing="strict"/>
                  <term field="query" str="the" position="2" processing="strict"/>
                  <term field="query" str="United" position="3" processing="strict"/>
                  <term field="query" str="States" position="4" processing="strict"/>
               </operator>
            </current-query>
            <current-query-identify-map-terms>
               <operator logic="and">
                  <mapterms>
                     <term field="query" str="map" position="0" processing="strict"/>
                     <term field="query" str="of" position="1" processing="strict"/>
                     <term field="query" str="the" position="2" processing="strict"/>
                  </mapterms>
                  <term field="query" str="United" position="3" processing="strict"/>
                  <term field="query" str="States" position="4" processing="strict"/>
               </operator>
            </current-query-identify-map-terms>
            <current-query-identify-country>
               <operator logic="and">
                  <mapterms>
                     <term field="query" str="map" position="0" processing="strict"/>
                     <term field="query" str="of" position="1" processing="strict"/>
                     <term field="query" str="the" position="2" processing="strict"/>
                  </mapterms>
                  <country name="United States">
                     <code name="us"/>
                     <capital name=""/>
                     <term field="query" str="United" position="3" processing="strict"/>
                     <term field="query" str="States" position="4" processing="strict"/>
                  </country>
               </operator>
            </current-query-identify-country>
            <identify-country>
               <country name="United States">
                  <code name="us"/>
                  <capital name=""/>
               </country>
            </identify-country>