reference/strings/functions/similar-text.xml
e095023e408c8cb6378ae16bb6870343a3946919
...
...
@@ -1,6 +1,6 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.similar-text">
3
+
<refentry xml:id="function.similar-text" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
5
5
<refname>similar_text</refname>
6
6
<refpurpose>Calculate the similarity between two strings</refpurpose>
...
...
@@ -10,9 +10,9 @@
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>int</type><methodname>similar_text</methodname>
13
-
<methodparam><type>string</type><parameter>first</parameter></methodparam>
14
-
<methodparam><type>string</type><parameter>second</parameter></methodparam>
15
-
<methodparam choice="opt"><type>float</type><parameter role="reference">percent</parameter></methodparam>
13
+
<methodparam><type>string</type><parameter>string1</parameter></methodparam>
14
+
<methodparam><type>string</type><parameter>string2</parameter></methodparam>
15
+
<methodparam choice="opt"><type>float</type><parameter role="reference">percent</parameter><initializer>&null;</initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
18
This calculates the similarity between two strings as described in
...
...
@@ -28,7 +28,7 @@
28
28
<para>
29
29
<variablelist>
30
30
<varlistentry>
31
-
<term><parameter>first</parameter></term>
31
+
<term><parameter>string1</parameter></term>
32
32
<listitem>
33
33
<para>
34
34
The first string.
...
...
@@ -36,11 +36,18 @@
36
36
</listitem>
37
37
</varlistentry>
38
38
<varlistentry>
39
-
<term><parameter>second</parameter></term>
39
+
<term><parameter>string2</parameter></term>
40
40
<listitem>
41
41
<para>
42
42
The second string.
43
43
</para>
44
+
<note>
45
+
<para>
46
+
Swapping the <parameter>string1</parameter> and
47
+
<parameter>string2</parameter> may yield a different result; see the
48
+
example below.
49
+
</para>
50
+
</note>
44
51
</listitem>
45
52
</varlistentry>
46
53
<varlistentry>
...
...
@@ -49,7 +56,9 @@
49
56
<para>
50
57
By passing a reference as third argument,
51
58
<function>similar_text</function> will calculate the similarity in
52
-
percent for you.
59
+
percent, by dividing the result of <function>similar_text</function> by
60
+
the average of the lengths of the given strings times
61
+
<literal>100</literal>.
53
62
</para>
54
63
</listitem>
55
64
</varlistentry>
...
...
@@ -62,6 +71,38 @@
62
71
<para>
63
72
Returns the number of matching chars in both strings.
64
73
</para>
74
+
<para>
75
+
The number of matching characters is calculated by finding the longest first
76
+
common substring, and then doing this for the prefixes and the suffixes,
77
+
recursively. The lengths of all found common substrings are added.
78
+
</para>
79
+
</refsect1>
80
+

81
+
<refsect1 role="examples">
82
+
&reftitle.examples;
83
+
<example xml:id="similar_text.example.swapping">
84
+
<title><function>similar_text</function> argument swapping example</title>
85
+
<para>
86
+
This example shows that swapping the <parameter>string1</parameter> and
87
+
<parameter>string2</parameter> argument may yield different results.
88
+
</para>
89
+
<programlisting role="php">
90
+
<![CDATA[
91
+
<?php
92
+
$sim = similar_text('bafoobar', 'barfoo', $perc);
93
+
echo "similarity: $sim ($perc %)\n";
94
+
$sim = similar_text('barfoo', 'bafoobar', $perc);
95
+
echo "similarity: $sim ($perc %)\n";
96
+
]]>
97
+
</programlisting>
98
+
&example.outputs.similar;
99
+
<screen>
100
+
<![CDATA[
101
+
similarity: 5 (71.428571428571 %)
102
+
similarity: 3 (42.857142857143 %)
103
+
]]>
104
+
</screen>
105
+
</example>
65
106
</refsect1>
66
107

67
108
<refsect1 role="seealso">
...
...
@@ -75,7 +116,6 @@
75
116
</refsect1>
76
117

77
118
</refentry>
78
-

79
119
<!-- Keep this comment at the end of the file
80
120
Local variables:
81
121
mode: sgml
82
122