Find out the same and different Phrase Word Segmentation of two sentences (algorithm) statement 1 "CSDN Forum is a very good forum" statement 2 "CSDN is a good forum" the following result is: CSDN = & gt; 2 forum = & gt; 2 is a = & gt; 2 very = & gt; 1 good = & gt; 2 jar = & gt; 1, PHPcode $ a = 'csdn Forum is to find the same and different Phrase Word Segmentation for two sentences (algorithm ideas)
Statement 1 "CSDN Forum is a very good forum"
Statement 2 "CSDN is a good Jar"
Returns the following results after the same variance
CSDN => 2
Forum => 2
Yes => 2
Very => 1
Good => 2
Jar => 1
------ Solution --------------------
PHP code
$ A = 'csdn Forum is a very good BBS '; $ B = 'csdn is a good bucket'; print_r (Sunday ($ a, $ B )); function Sunday ($ a, $ B) {// Check the input string's character set mb_internal_encoding (mb_check_encoding ($ a, 'utf-8 ')? 'Utf-8': 'gbk'); // splits the input string into an array, mainly for the convenience of writing the following code $ t = array (); for ($ I = 0; $ I
= Count ($ a) break;} elseif ($ k) break;} // Check matching conditions if ($ n-$ I> 1) {if ($ buf) {$ r [] = $ buf; $ buf = '';} $ r [] = join ('', array_slice ($ a, $ I, $ n-$ I); $ I = $ n;} else $ buf. = $ a [$ I ++];} if ($ buf) $ r [] = $ buf; list ($ B, $ a) = array ($, $ B); // exchange two groups of data, and try again} while ($ loop --); return array_count_values ($ r );}