Difference between revisions of "String Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 12: Line 12:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|string:levenshtein|$string1 as xs:string, $string2 as xs:string|xs:double}}<br/>
 
|{{Func|string:levenshtein|$string1 as xs:string, $string2 as xs:string|xs:double}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Computes the [https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance Damerau-Levenshtein Distance] for two strings and returns a double value ({{Code|0.0}} - {{Code|1.0}}). The returned value is computed as follows:<br/>
 
|Computes the [https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance Damerau-Levenshtein Distance] for two strings and returns a double value ({{Code|0.0}} - {{Code|1.0}}). The returned value is computed as follows:<br/>
 
* <code>1.0</code> – distance / max(length of strings)
 
* <code>1.0</code> – distance / max(length of strings)
 
* <code>1.0</code> is returned if the strings are equal; <code>0.0</code> is returned if the strings are too different.
 
* <code>1.0</code> is returned if the strings are equal; <code>0.0</code> is returned if the strings are too different.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 35: Line 35:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|string:soundex|$string as xs:string|xs:string}}<br/>
 
|{{Func|string:soundex|$string as xs:string|xs:string}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Computes the [https://en.wikipedia.org/wiki/Soundex Soundex] value for the specified string. The algorithm can be used to find and index English words with similar pronouncation.
 
|Computes the [https://en.wikipedia.org/wiki/Soundex Soundex] value for the specified string. The algorithm can be used to find and index English words with similar pronouncation.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
Line 51: Line 51:
  
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
 
|{{Func|string:cologne-phonetic|$string as xs:string|xs:string}}<br/>
 
|{{Func|string:cologne-phonetic|$string as xs:string|xs:string}}<br/>
|-
+
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
 
|Computes the [https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik Kölner Phonetik] value for the specified string. Similar to Soundex, the algorithm is used to find similarly pronounced words, but for the German language. As the first returned digit can be {{Code|0}}, the value is returned as string.
 
|Computes the [https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik Kölner Phonetik] value for the specified string. Similar to Soundex, the algorithm is used to find similarly pronounced words, but for the German language. As the first returned digit can be {{Code|0}}, the value is returned as string.
|-
+
|- valign="top"
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|

Revision as of 14:19, 20 July 2022

Updated with Version 10: Renamed from Strings Module to String Module. The namespace URI has been updated as well.

This XQuery Module contains functions for string computations.

Conventions

All functions and errors in this module and errors are assigned to the http://basex.org/modules/string namespace, which is statically bound to the string prefix.

Functions

string:levenshtein

Signatures string:levenshtein($string1 as xs:string, $string2 as xs:string) as xs:double
Summary Computes the Damerau-Levenshtein Distance for two strings and returns a double value (0.0 - 1.0). The returned value is computed as follows:
  • 1.0 – distance / max(length of strings)
  • 1.0 is returned if the strings are equal; 0.0 is returned if the strings are too different.
Examples
  • string:levenshtein("flower", "flower") returns 1
  • string:levenshtein("flower", "lewes") returns 0.5
  • In the following query, the input is first normalized (words are stemmed, converted to lower case, and diacritics are removed). It returns 1:

<syntaxhighlight lang="xquery"> let $norm := ft:normalize(?, map { 'stemming': true() }) return string:levenshtein($norm("HOUSES"), $norm("house")) </syntaxhighlight>

string:soundex

Signatures string:soundex($string as xs:string) as xs:string
Summary Computes the Soundex value for the specified string. The algorithm can be used to find and index English words with similar pronouncation.
Examples
  • string:soundex("Michael") returns M240
  • string:soundex("OBrien") = string:soundex("O'Brien") returns true

string:cologne-phonetic

Signatures string:cologne-phonetic($string as xs:string) as xs:string
Summary Computes the Kölner Phonetik value for the specified string. Similar to Soundex, the algorithm is used to find similarly pronounced words, but for the German language. As the first returned digit can be 0, the value is returned as string.
Examples
  • string:cologne-phonetic("Michael") returns 645
  • every $s in ("Mayr", "Maier", "Meier") satisfies string:cologne-phonetic($s) = "67" returns true

Changelog

Version 10.0
  • Updated: Renamed from Strings Module to String Module. The namespace URI has been updated as well.

The Module was introduced with Version 8.3.