├── LICENSE ├── README.md └── TextSimilarity.class.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 宋小北 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 通过余弦定理+分词计算文本相似度PHP版 2 | 3 | --- 4 | 5 | ### 参考: 6 | * http://www.ruanyifeng.com/blog/2013/03/cosine_similarity.html 7 | * http://my.oschina.net/BreathL/blog/42477 8 | 9 | ### Use: 10 | 11 | ```php 12 | run(); 16 | 17 | ``` 18 | -------------------------------------------------------------------------------- /TextSimilarity.class.php: -------------------------------------------------------------------------------- 1 | run(); 14 | */ 15 | 16 | class TextSimilarity 17 | { 18 | /** 19 | * [排除的词语] 20 | * 21 | * @var array 22 | */ 23 | private $_excludeArr = array('的','了','和','呢','啊','哦','恩','嗯','吧'); 24 | 25 | /** 26 | * [词语分布数组] 27 | * 28 | * @var array 29 | */ 30 | private $_words = array(); 31 | 32 | /** 33 | * [分词后的数组一] 34 | * 35 | * @var array 36 | */ 37 | private $_segList1 = array(); 38 | 39 | /** 40 | * [分词后的数组二] 41 | * 42 | * @var array 43 | */ 44 | private $_segList2 = array(); 45 | 46 | /** 47 | * [分词两段文字] 48 | * 49 | * @param [type] $text1 [description] 50 | * @param [type] $text2 [description] 51 | */ 52 | public function __construct($text1, $text2) 53 | { 54 | $this->_segList1 = $this->segment($text1); 55 | $this->_segList2 = $this->segment($text2); 56 | } 57 | 58 | /** 59 | * [外部调用] 60 | * 61 | * @return [type] [description] 62 | */ 63 | public function run() 64 | { 65 | $this->analyse(); 66 | $rate = $this->handle(); 67 | return $rate ? $rate : 'errors'; 68 | } 69 | 70 | /** 71 | * [分析两段文字] 72 | */ 73 | private function analyse() 74 | { 75 | //t1 76 | foreach ($this->_segList1 as $v) { 77 | if (!in_array($v, $this->_excludeArr)) { 78 | if (!array_key_exists($v, $this->_words)) { 79 | $this->_words[$v] = array(1 , 0); 80 | } else { 81 | $this->_words[$v][0] += 1; 82 | } 83 | } 84 | } 85 | 86 | //t2 87 | foreach ($this->_segList2 as $v) { 88 | if (!in_array($v, $this->_excludeArr)) { 89 | if (!array_key_exists($v, $this->_words)) { 90 | $this->_words[$v] = array(0 , 1); 91 | } else { 92 | $this->_words[$v][1] += 1; 93 | } 94 | } 95 | } 96 | } 97 | 98 | /** 99 | * [处理相似度] 100 | * 101 | * @return [type] [description] 102 | */ 103 | private function handle() 104 | { 105 | $sum = $sumT1 = $sumT2 = 0; 106 | foreach ($this->_words as $word) { 107 | $sum += $word[0] * $word[1]; 108 | $sumT1 += pow($word[0], 2); 109 | $sumT2 += pow($word[1], 2); 110 | } 111 | 112 | $rate = $sum / (sqrt($sumT1 * $sumT2)); 113 | return $rate; 114 | } 115 | 116 | /** 117 | * [分词 【http://www.xunsearch.com/scws/docs.php#pscws23】] 118 | * 119 | * @param [type] $text [description] 120 | * 121 | * @return [type] [description] 122 | * 123 | * @description 分词只是一个简单的例子,你可以使用任意的分词服务 124 | */ 125 | private function segment($text) 126 | { 127 | $outText = array(); 128 | //实例化 129 | $so = scws_new(); 130 | //字符集 131 | $so->set_charset('utf8'); 132 | //处理 133 | $so->send_text($text); 134 | 135 | //便利出需要的数组 136 | while ($res = $so->get_result()) { 137 | foreach ($res as $v) { 138 | $outText[] = isset($v['word']) ? strtoupper($v['word']) : ""; 139 | } 140 | } 141 | //关闭 142 | $so->close(); 143 | 144 | return $outText; 145 | } 146 | } 147 | --------------------------------------------------------------------------------