String-PHP short text match Sorting Algorithm

Source: Internet
Author: User
PHP uses the longest common subsequence for short text search and matching. However, if the keyword matches the similarity of the two texts, how can we return the first keyword in the two texts? For example, if the keyword "NONE" matches "unparalleled" and "nothing", how can I set "... PHP uses the longest common subsequence for short text search and matching. However, if the keyword matches the similarity of the two texts, how can we return the first keyword in the two texts? For example, if the keyword "NONE" matches "No dual" and "nothing", how can I put "No dual" in the returned results before "nothing? What about the "no pair" matching "No Xiao Ming's double" and "No Xiao Ming's double?


  $ Len_2? $ Len_1: $ len_2; $ dp = array (); for ($ I = 0; $ I <= $ len; $ I ++) {$ dp [$ I] = array (); $ dp [$ I] [0] = 0; $ dp [0] [$ I] = 0 ;} for ($ I = 1; $ I <= $ len_1; $ I ++) {for ($ j = 1; $ j <= $ len_2; $ j ++) {if ($ str_1 [$ I-1] = $ str_2 [$ j-1]) {$ dp [$ I] [$ j] = $ dp [$ I-1] [$ j-1] + 1 ;} else {$ dp [$ I] [$ j] = $ dp [$ I-1] [$ j]> $ dp [$ I] [$ j-1]? $ Dp [$ I-1] [$ j]: $ dp [$ I] [$ j-1] ;}} return $ dp [$ len_1] [$ len_2];} function search ($ name) {Global $ names; $ sort_list = array (); if (mb_strlen ($ name, 'utf-8 ')! = Strlen ($ name) {// whether all English characters $ arr_1 = array_unique (split_name ($ name); foreach ($ names as $ value) {$ arr_2 = array_unique (split_name ($ value); $ similarity = count ($ arr_2)-count (array_diff ($ arr_2, $ arr_1 )); $ sort_list [$ value] = $ similarity;} else {foreach ($ names as $ value) {$ similarity = LCS ($ name, $ value ); $ sort_list [$ value] = $ similarity; }} arsort ($ sort_list); return $ sort_list;} header ('content-type: text/html; charset = UTF-8 '); print_r (search ('None '));

Reply content:

PHP uses the longest common subsequence for short text search and matching. However, if the keyword matches the similarity of the two texts, how can we return the first keyword in the two texts? For example, if the keyword "NONE" matches "No dual" and "nothing", how can I put "No dual" in the returned results before "nothing? What about the "no pair" matching "No Xiao Ming's double" and "No Xiao Ming's double?


  $ Len_2? $ Len_1: $ len_2; $ dp = array (); for ($ I = 0; $ I <= $ len; $ I ++) {$ dp [$ I] = array (); $ dp [$ I] [0] = 0; $ dp [0] [$ I] = 0 ;} for ($ I = 1; $ I <= $ len_1; $ I ++) {for ($ j = 1; $ j <= $ len_2; $ j ++) {if ($ str_1 [$ I-1] = $ str_2 [$ j-1]) {$ dp [$ I] [$ j] = $ dp [$ I-1] [$ j-1] + 1 ;} else {$ dp [$ I] [$ j] = $ dp [$ I-1] [$ j]> $ dp [$ I] [$ j-1]? $ Dp [$ I-1] [$ j]: $ dp [$ I] [$ j-1] ;}} return $ dp [$ len_1] [$ len_2];} function search ($ name) {Global $ names; $ sort_list = array (); if (mb_strlen ($ name, 'utf-8 ')! = Strlen ($ name) {// whether all English characters $ arr_1 = array_unique (split_name ($ name); foreach ($ names as $ value) {$ arr_2 = array_unique (split_name ($ value); $ similarity = count ($ arr_2)-count (array_diff ($ arr_2, $ arr_1 )); $ sort_list [$ value] = $ similarity;} else {foreach ($ names as $ value) {$ similarity = LCS ($ name, $ value ); $ sort_list [$ value] = $ similarity; }} arsort ($ sort_list); return $ sort_list;} header ('content-type: text/html; charset = UTF-8 '); print_r (search ('None '));

So you just want to make the search result rank more advanced than the previous one? Isn't the smaller the positions of all matched words directly added, isn't it in front? I don't know what I understand wrong no... code running online: http://3v4l.org/K0X7m


  $ V) {$ pos = array (); foreach ($ v ['single '] as $ k => $ s) {if (in_array ($ s, $ search )) $ pos [$ s] [] = $ k;} if (count ($ pos )! = Count ($ search) {unset ($ res [$ name]);} else {$ seq = 0; array_walk_recursive ($ pos, function ($ I) use (& $ seq) {$ seq + = $ I ;}); $ res [$ name] = $ seq ;}} ksort ($ res ); $ res = array_keys ($ res); var_dump ($ res );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.