PHP similar_text()、levenshtein()、lcs()支持中文汉字版,_PHP教程

WBOY
풀어 주다: 2016-07-13 10:16:02
원래의
1836명이 탐색했습니다.

PHP similar_text()、levenshtein()、lcs()支持中文汉字版,

PHP 原生的similar_text()函数、levenshtein()函数对中文汉字支持不好,我自己写了一个


similar_text()中文汉字版

<span> 1</span>     <?<span>php  
</span><span> 2</span>     <span>//</span><span>拆分字符串  </span>
<span> 3</span>     <span>function</span> split_str(<span>$str</span><span>) {  
</span><span> 4</span>       <span>preg_match_all</span>("/./u", <span>$str</span>, <span>$arr</span><span>);  
</span><span> 5</span>       <span>return</span> <span>$arr</span>[0<span>];  
</span><span> 6</span> <span>    }  
</span><span> 7</span>       
<span> 8</span>     <span>//</span><span>相似度检测  </span>
<span> 9</span>     <span>function</span> similar_text_cn(<span>$str1</span>, <span>$str2</span><span>) {  
</span><span>10</span>       <span>$arr_1</span> = <span>array_unique</span>(split_str(<span>$str1</span><span>));  
</span><span>11</span>       <span>$arr_2</span> = <span>array_unique</span>(split_str(<span>$str2</span><span>));  
</span><span>12</span>       <span>$similarity</span> = <span>count</span>(<span>$arr_2</span>) - <span>count</span>(<span>array_diff</span>(<span>$arr_2</span>, <span>$arr_1</span><span>));  
</span><span>13</span>         
<span>14</span>       <span>return</span> <span>$similarity</span><span>;  
</span><span>15</span>     }  
로그인 후 복사

levenshtein()中文汉字版

<span> 1</span>     <?<span>php  
</span><span> 2</span>     <span>//</span><span>拆分字符串  </span>
<span> 3</span>     <span>function</span> mbStringToArray(<span>$string</span>, <span>$encoding</span> = 'UTF-8'<span>) {  
</span><span> 4</span>         <span>$arrayResult</span> = <span>array</span><span>();  
</span><span> 5</span>       
<span> 6</span>         <span>while</span> (<span>$iLen</span> = mb_strlen(<span>$string</span>, <span>$encoding</span><span>)) {  
</span><span> 7</span>             <span>array_push</span>(<span>$arrayResult</span>, mb_substr(<span>$string</span>, 0, 1, <span>$encoding</span><span>));  
</span><span> 8</span>             <span>$string</span> = mb_substr(<span>$string</span>, 1, <span>$iLen</span>, <span>$encoding</span><span>);  
</span><span> 9</span> <span>        }  
</span><span>10</span>       
<span>11</span>         <span>return</span> <span>$arrayResult</span><span>;  
</span><span>12</span> <span>    }  
</span><span>13</span>       
<span>14</span>     <span>//</span><span>编辑距离  </span>
<span>15</span>     <span>function</span> levenshtein_cn(<span>$str1</span>, <span>$str2</span>, <span>$costReplace</span> = 1, <span>$encoding</span> = 'UTF-8'<span>) {  
</span><span>16</span>         <span>$count_same_letter</span> = 0<span>;  
</span><span>17</span>         <span>$d</span> = <span>array</span><span>();  
</span><span>18</span>       
<span>19</span>         <span>$mb_len1</span> = mb_strlen(<span>$str1</span>, <span>$encoding</span><span>);  
</span><span>20</span>         <span>$mb_len2</span> = mb_strlen(<span>$str2</span>, <span>$encoding</span><span>);  
</span><span>21</span>       
<span>22</span>         <span>$mb_str1</span> = mbStringToArray(<span>$str1</span>, <span>$encoding</span><span>);  
</span><span>23</span>         <span>$mb_str2</span> = mbStringToArray(<span>$str2</span>, <span>$encoding</span><span>);  
</span><span>24</span>       
<span>25</span>         <span>for</span> (<span>$i1</span> = 0; <span>$i1</span> <= <span>$mb_len1</span>; <span>$i1</span>++<span>) {  
</span><span>26</span>             <span>$d</span>[<span>$i1</span>] = <span>array</span><span>();  
</span><span>27</span>             <span>$d</span>[<span>$i1</span>][0] = <span>$i1</span><span>;  
</span><span>28</span> <span>        }  
</span><span>29</span>       
<span>30</span>         <span>for</span> (<span>$i2</span> = 0; <span>$i2</span> <= <span>$mb_len2</span>; <span>$i2</span>++<span>) {  
</span><span>31</span>             <span>$d</span>[0][<span>$i2</span>] = <span>$i2</span><span>;  
</span><span>32</span> <span>        }  
</span><span>33</span>       
<span>34</span>         <span>for</span> (<span>$i1</span> = 1; <span>$i1</span> <= <span>$mb_len1</span>; <span>$i1</span>++<span>) {  
</span><span>35</span>             <span>for</span> (<span>$i2</span> = 1; <span>$i2</span> <= <span>$mb_len2</span>; <span>$i2</span>++<span>) {  
</span><span>36</span>                 <span>//</span><span> $cost = ($str1[$i1 - 1] == $str2[$i2 - 1]) ? 0 : 1;  </span>
<span>37</span>                 <span>if</span> (<span>$mb_str1</span>[<span>$i1</span> - 1] === <span>$mb_str2</span>[<span>$i2</span> - 1<span>]) {  
</span><span>38</span>                     <span>$cost</span> = 0<span>;  
</span><span>39</span>                     <span>$count_same_letter</span>++<span>;  
</span><span>40</span>                 } <span>else</span><span> {  
</span><span>41</span>                     <span>$cost</span> = <span>$costReplace</span>; <span>//</span><span>替换  </span>
<span>42</span> <span>                }  
</span><span>43</span>       
<span>44</span>                 <span>$d</span>[<span>$i1</span>][<span>$i2</span>] = <span>min</span>(<span>$d</span>[<span>$i1</span> - 1][<span>$i2</span>] + 1, <span>//</span><span>插入  </span>
<span>45</span>                 <span>$d</span>[<span>$i1</span>][<span>$i2</span> - 1] + 1, <span>//</span><span>删除  </span>
<span>46</span>                 <span>$d</span>[<span>$i1</span> - 1][<span>$i2</span> - 1] + <span>$cost</span><span>);  
</span><span>47</span> <span>            }  
</span><span>48</span> <span>        }  
</span><span>49</span>       
<span>50</span>         <span>return</span> <span>$d</span>[<span>$mb_len1</span>][<span>$mb_len2</span><span>];  
</span><span>51</span>         <span>//</span><span>return array('distance' => $d[$mb_len1][$mb_len2], 'count_same_letter' => $count_same_letter);  </span>
<span>52</span>     }  
로그인 후 복사

最长公共子序列LCS()

<span> 1</span>         <?<span>php  
</span><span> 2</span>         <span>//</span><span>最长公共子序列英文版  </span>
<span> 3</span>         <span>function</span> LCS_en(<span>$str_1</span>, <span>$str_2</span><span>) {  
</span><span> 4</span>           <span>$len_1</span> = <span>strlen</span>(<span>$str_1</span><span>);  
</span><span> 5</span>           <span>$len_2</span> = <span>strlen</span>(<span>$str_2</span><span>);  
</span><span> 6</span>           <span>$len</span> = <span>$len_1</span> > <span>$len_2</span> ? <span>$len_1</span> : <span>$len_2</span><span>;  
</span><span> 7</span>           
<span> 8</span>           <span>$dp</span> = <span>array</span><span>();  
</span><span> 9</span>           <span>for</span> (<span>$i</span> = 0; <span>$i</span> <= <span>$len</span>; <span>$i</span>++<span>) {  
</span><span>10</span>             <span>$dp</span>[<span>$i</span>] = <span>array</span><span>();  
</span><span>11</span>             <span>$dp</span>[<span>$i</span>][0] = 0<span>;  
</span><span>12</span>             <span>$dp</span>[0][<span>$i</span>] = 0<span>;  
</span><span>13</span> <span>          }  
</span><span>14</span>           
<span>15</span>           <span>for</span> (<span>$i</span> = 1; <span>$i</span> <= <span>$len_1</span>; <span>$i</span>++<span>) {  
</span><span>16</span>             <span>for</span> (<span>$j</span> = 1; <span>$j</span> <= <span>$len_2</span>; <span>$j</span>++<span>) {  
</span><span>17</span>               <span>if</span> (<span>$str_1</span>[<span>$i</span> - 1] == <span>$str_2</span>[<span>$j</span> - 1<span>]) {  
</span><span>18</span>                 <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>$dp</span>[<span>$i</span> - 1][<span>$j</span> - 1] + 1<span>;  
</span><span>19</span>               } <span>else</span><span> {  
</span><span>20</span>                 <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>$dp</span>[<span>$i</span> - 1][<span>$j</span>] > <span>$dp</span>[<span>$i</span>][<span>$j</span> - 1] ? <span>$dp</span>[<span>$i</span> - 1][<span>$j</span>] : <span>$dp</span>[<span>$i</span>][<span>$j</span> - 1<span>];  
</span><span>21</span> <span>              }  
</span><span>22</span> <span>            }  
</span><span>23</span> <span>          }  
</span><span>24</span>           
<span>25</span>           <span>return</span> <span>$dp</span>[<span>$len_1</span>][<span>$len_2</span><span>];  
</span><span>26</span> <span>        }  
</span><span>27</span>           
<span>28</span>         <span>//</span><span>拆分字符串  </span>
<span>29</span>         <span>function</span> mbStringToArray(<span>$string</span>, <span>$encoding</span> = 'UTF-8'<span>) {  
</span><span>30</span>           <span>$arrayResult</span> = <span>array</span><span>();  
</span><span>31</span>           
<span>32</span>           <span>while</span> (<span>$iLen</span> = mb_strlen(<span>$string</span>, <span>$encoding</span><span>)) {  
</span><span>33</span>             <span>array_push</span>(<span>$arrayResult</span>, mb_substr(<span>$string</span>, 0, 1, <span>$encoding</span><span>));  
</span><span>34</span>             <span>$string</span> = mb_substr(<span>$string</span>, 1, <span>$iLen</span>, <span>$encoding</span><span>);  
</span><span>35</span> <span>          }  
</span><span>36</span>           
<span>37</span>           <span>return</span> <span>$arrayResult</span><span>;  
</span><span>38</span> <span>        }  
</span><span>39</span>           
<span>40</span>         <span>//</span><span>最长公共子序列中文版  </span>
<span>41</span>         <span>function</span> LCS_cn(<span>$str1</span>, <span>$str2</span>, <span>$encoding</span> = 'UTF-8'<span>) {  
</span><span>42</span>           <span>$mb_len1</span> = mb_strlen(<span>$str1</span>, <span>$encoding</span><span>);  
</span><span>43</span>           <span>$mb_len2</span> = mb_strlen(<span>$str2</span>, <span>$encoding</span><span>);  
</span><span>44</span>           
<span>45</span>           <span>$mb_str1</span> = mbStringToArray(<span>$str1</span>, <span>$encoding</span><span>);  
</span><span>46</span>           <span>$mb_str2</span> = mbStringToArray(<span>$str2</span>, <span>$encoding</span><span>);  
</span><span>47</span>           
<span>48</span>           <span>$len</span> = <span>$mb_len1</span> > <span>$mb_len2</span> ? <span>$mb_len1</span> : <span>$mb_len2</span><span>;  
</span><span>49</span>           
<span>50</span>           <span>$dp</span> = <span>array</span><span>();  
</span><span>51</span>           <span>for</span> (<span>$i</span> = 0; <span>$i</span> <= <span>$len</span>; <span>$i</span>++<span>) {  
</span><span>52</span>             <span>$dp</span>[<span>$i</span>] = <span>array</span><span>();  
</span><span>53</span>             <span>$dp</span>[<span>$i</span>][0] = 0<span>;  
</span><span>54</span>             <span>$dp</span>[0][<span>$i</span>] = 0<span>;  
</span><span>55</span> <span>          }  
</span><span>56</span>           
<span>57</span>           <span>for</span> (<span>$i</span> = 1; <span>$i</span> <= <span>$mb_len1</span>; <span>$i</span>++<span>) {  
</span><span>58</span>             <span>for</span> (<span>$j</span> = 1; <span>$j</span> <= <span>$mb_len2</span>; <span>$j</span>++<span>) {  
</span><span>59</span>               <span>if</span> (<span>$mb_str1</span>[<span>$i</span> - 1] == <span>$mb_str2</span>[<span>$j</span> - 1<span>]) {  
</span><span>60</span>                 <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>$dp</span>[<span>$i</span> - 1][<span>$j</span> - 1] + 1<span>;  
</span><span>61</span>               } <span>else</span><span> {  
</span><span>62</span>                 <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>$dp</span>[<span>$i</span> - 1][<span>$j</span>] > <span>$dp</span>[<span>$i</span>][<span>$j</span> - 1] ? <span>$dp</span>[<span>$i</span> - 1][<span>$j</span>] : <span>$dp</span>[<span>$i</span>][<span>$j</span> - 1<span>];  
</span><span>63</span> <span>              }  
</span><span>64</span> <span>            }  
</span><span>65</span> <span>          }  
</span><span>66</span>           
<span>67</span>           <span>return</span> <span>$dp</span>[<span>$mb_len1</span>][<span>$mb_len2</span><span>];  
</span><span>68</span>         } 
로그인 후 복사

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/900268.htmlTechArticlePHP similar_text()、levenshtein()、lcs()支持中文汉字版, PHP 原生的similar_text()函数、levenshtein()函数对中文汉字支持不好,我自己写了一个 simila...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!