StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : Distances and Metrics : StringTools/PrefixDistance

StringTools

 PrefixDistance
 compute the prefix distance between two strings
 SuffixDistance
 compute the suffix distance between two strings

 Calling Sequence PrefixDistance( s, t ) SuffixDistance( s, t )

Parameters

 s - Maple string t - Maple string

Description

 • The PrefixDistance(s,t) and SuffixDistance(s,t) commands return integer measures of the distance between the two strings s and t.
 • Let s and t be arbitrary strings. The prefix distance PrefixDistance( s, t ) is defined to be $\mathrm{length}\left(s\right)+\mathrm{length}\left(t\right)-2\mathrm{CommonPrefix}\left(s,t\right)$, and the suffix distance $\mathrm{SuffixDistance}\left(s,t\right)$ is defined, similarly, to be $\mathrm{length}\left(s\right)+\mathrm{length}\left(t\right)-2\mathrm{CommonSuffix}\left(s,t\right)$. Both are metrics.
 • Other string metrics available in this package include StringTools[HammingDistance] and StringTools[Levenshtein].

Examples

 > $\mathbf{use}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{StringTools}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{PrefixDistance}\left("Mathematics","Mathematische"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end use}$
 ${6}$ (1)
 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{PrefixDistance}\left("abc","abd"\right)$
 ${2}$ (2)
 > $\mathrm{PrefixDistance}\left("abc","abcd"\right)$
 ${1}$ (3)
 > $\mathrm{PrefixDistance}\left("Elisabeth","Elyse"\right)$
 ${10}$ (4)
 > $\mathrm{PrefixDistance}\left("Connor","Constance"\right)$
 ${9}$ (5)
 > $\mathrm{SuffixDistance}\left("foe","Defoe"\right)$
 ${2}$ (6)
 > $\mathrm{SuffixDistance}\left("North America","Central America"\right)$
 ${12}$ (7)
 > $\mathrm{SuffixDistance}\left("North America","South America"\right)$
 ${6}$ (8)
 > $\mathrm{SuffixDistance}\left("abba","abba"\right)$
 ${0}$ (9)

Being metrics, both PrefixDistance and SuffixDistance satisfy the triangle inequality.

 > $s≔\mathrm{Random}\left(10,'\mathrm{binary}'\right)$
 ${s}{≔}{"0000101110"}$ (10)
 > $t≔\mathrm{Random}\left(10,'\mathrm{binary}'\right)$
 ${t}{≔}{"1101110100"}$ (11)
 > $u≔\mathrm{Random}\left(10,'\mathrm{binary}'\right)$
 ${u}{≔}{"0100010001"}$ (12)
 > $\mathrm{SuffixDistance}\left(s,t\right)$
 ${18}$ (13)
 > $\mathrm{SuffixDistance}\left(s,u\right)+\mathrm{SuffixDistance}\left(u,t\right)$
 ${40}$ (14)