mirror of
https://github.com/php/doc-ja.git
synced 2026-04-29 02:53:11 +02:00
223 lines
6.7 KiB
XML
223 lines
6.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 873f4a3d5027bd1b584f1d1e590e22cd4a08ae84 Maintainer: hirokawa Status: ready -->
|
|
<!-- CREDITS: shimooka -->
|
|
<refentry xml:id="function.levenshtein" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<refnamediv>
|
|
<refname>levenshtein</refname>
|
|
<refpurpose>二つの文字列のレーベンシュタイン距離を計算する</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>int</type><methodname>levenshtein</methodname>
|
|
<methodparam><type>string</type><parameter>string1</parameter></methodparam>
|
|
<methodparam><type>string</type><parameter>string2</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>insertion_cost</parameter><initializer>1</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>replacement_cost</parameter><initializer>1</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>deletion_cost</parameter><initializer>1</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
レーベンシュタイン距離は、<parameter>string1</parameter> を
|
|
<parameter>string2</parameter> に変換するために置換、挿入、削除
|
|
しなければならない最小の文字数として定義されます。アルゴリズムの計算量は、
|
|
<literal>O(m*n)</literal> です。
|
|
ここで、<literal>n</literal> および <literal>m</literal> はそれぞれ
|
|
<parameter>string1</parameter> および <parameter>string2</parameter> の長さです。
|
|
<literal>O(max(n,m)**3)</literal> となる <function>similar_text</function> よりは良いですが、
|
|
まだかなりの計算量です)。
|
|
</para>
|
|
<para>
|
|
<parameter>insertion_cost</parameter>, <parameter>replacement_cost</parameter>
|
|
かつ/または <parameter>deletion_cost</parameter> が <literal>1</literal> 以外の場合、
|
|
変換コストが最も小さいアルゴリズムを採用します。
|
|
たとえば、<code>$insertion_cost + $deletion_cost < $replacement_cost</code> の場合、
|
|
置換をせず、挿入と削除が行われます。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>string1</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
レーベンシュタイン距離を計算する文字列のひとつ。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>string2</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
レーベンシュタイン距離を計算する文字列のひとつ。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>insertion_cost</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
挿入のコストを定義します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>replacement_cost</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
置換のコストを定義します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>deletion_cost</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
削除のコストを定義します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
この関数は、引数で指定した二つの文字列のレーベンシュタイン距離を返します。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
これより前のバージョンでは、
|
|
引数を2個、または5個指定して呼び出さなければなりませんでした。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
これより前のバージョンでは、
|
|
引数文字列の一つが 255 文字の制限より長い場合に <literal>-1</literal> を返していました。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>levenshtein</function> の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// スペルミスした単語を入力します
|
|
$input = 'carrrot';
|
|
|
|
// チェックするための単語の配列
|
|
$words = array('apple','pineapple','banana','orange',
|
|
'radish','carrot','pea','bean','potato');
|
|
|
|
// まだ最短距離は見つかっていません
|
|
$shortest = -1;
|
|
|
|
// 最短距離を見つけるため単語をループします
|
|
foreach ($words as $word) {
|
|
|
|
// 入力した単語と現在の単語の距離を
|
|
// 計算します
|
|
$lev = levenshtein($input, $word);
|
|
|
|
// マッチするかどうかチェックします
|
|
if ($lev == 0) {
|
|
|
|
// 最短な単語はこれだ (マッチした)
|
|
$closest = $word;
|
|
$shortest = 0;
|
|
|
|
// ループを抜ける; マッチしたものを見つけました
|
|
break;
|
|
}
|
|
|
|
// もし距離が次に見つけた最短距離よりも短い場合、
|
|
// もしくは次の最短の単語がまだ見つかっていない場合
|
|
if ($lev <= $shortest || $shortest < 0) {
|
|
// 最短のマッチと最短距離をセットします
|
|
$closest = $word;
|
|
$shortest = $lev;
|
|
}
|
|
}
|
|
|
|
echo "入力した単語: $input\n";
|
|
if ($shortest == 0) {
|
|
echo "一致するものが見つかりました: $closest\n";
|
|
} else {
|
|
echo "もしかして: $closest\n";
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
入力した単語: carrrot
|
|
もしかして: carrot
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>soundex</function></member>
|
|
<member><function>similar_text</function></member>
|
|
<member><function>metaphone</function></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|