sim_sent.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. //计算句子相似度
  3. require_once "../path.php";
  4. require_once "../public/_pdo.php";
  5. global $PDO;
  6. PDO_Connect("sqlite:"._FILE_DB_PALI_SENTENCE_);
  7. // 输入一个句子,输出整个句子的单词 array
  8. function words_of_sentence(string $sent) {
  9. $words = preg_split("/[ \.\[\]\{\}\-,';‘’–0123456789]+/", $sent); // 去除标点、数字
  10. $words = array_filter($words); // 去除空词
  11. $words = array_filter($words, function($item) {
  12. if ($item != 'ca' && $item != 'vā' && $item != 'na') return $item; }); // 去除 ca, vā 和 na
  13. return $words;
  14. }
  15. // 采用 jaccard 相似度,考虑到圣典中的相似句单词、句式都是非常接近的
  16. function jaccard_similarity($words_of_sent1, $words_of_sent2) {
  17. $intersect = count(array_intersect($words_of_sent1, $words_of_sent2));
  18. $union = count($words_of_sent1)+count($words_of_sent2)-$intersect;
  19. if ($union) {
  20. return $intersect / $union;
  21. } else {
  22. return 0;
  23. }
  24. }
  25. // 带顺序的 jaccard 算法,当前效果一般,TODO: 切片相同时加入得分
  26. function ordered_jaccard_similarity($words_of_sent1, $words_of_sent2) {
  27. $score = 0;
  28. $k = min(count($words_of_sent1), count($words_of_sent2));
  29. for ($i=1; $i<$k; $i++) {
  30. $score += jaccard_similarity(
  31. array_slice($words_of_sent1, 0, $i),
  32. array_slice($words_of_sent2, 0, $i));
  33. }
  34. return $score / $k;
  35. }
  36. // 定义一个链表节点,方便 sim_sent_id 按照相似度插入
  37. class sim_sent_node {
  38. public $id;
  39. public $jaccard_score;
  40. public $next;
  41. public function __construct($id = null, $jaccard_score = null, $next = null) {
  42. $this->id = $id;
  43. $this->jaccard_score = $jaccard_score;
  44. $this->next = $next;
  45. }
  46. }
  47. // 定义链表
  48. class sim_sent_list {
  49. public $head; // 头节点,默认一个虚头节点
  50. public $size;
  51. public function __construct() {
  52. $this->head = new sim_sent_node();
  53. $this->size = 0;
  54. }
  55. // 按照 jaccard_score 相似度插入
  56. public function jaccard_add($id, $jaccard_score) {
  57. $prev = $this->head;
  58. while ($prev->next != null && $prev->next->jaccard_score > $jaccard_score) {
  59. $prev = $prev->next;
  60. }
  61. $prev->next = new sim_sent_node($id, $jaccard_score, $prev->next);
  62. $this->size++;
  63. }
  64. public function get_text_list() {
  65. $prev = $this->head;
  66. if ($this->size == 0) {
  67. return;
  68. }
  69. $ids = "";
  70. while ($prev->next != null) {
  71. $ids = $ids.",".$prev->next->id;
  72. $prev = $prev->next;
  73. }
  74. $ids = substr($ids, 1); // 去掉第一个逗号
  75. return $ids;
  76. }
  77. public function print_list() {
  78. $prev = $this->head;
  79. if ($this->size == 0) {
  80. return;
  81. }
  82. while ($prev->next != null) {
  83. print($prev->next->id."\t".$prev->next->jaccard_score."\n");
  84. $prev = $prev->next;
  85. }
  86. }
  87. }
  88. // 将相似句列表存入数据库
  89. function insert_similar_sent_list_into_sqlite($current_id, $text_list) {
  90. /* 使用这部分代码先为数据库添加一个 sim_sents 字段
  91. $add_column = "ALTER TABLE pali_sent ADD COLUMN sim_sents TEXT";
  92. $Action_add = PDO_Execute($add_column);
  93. $query = "PRAGMA TABLE_INFO (pali_sent)";
  94. $Fetch = PDO_FetchALL($query);
  95. print_r($Fetch);
  96. */
  97. global $PDO;
  98. $Update = "UPDATE pali_sent SET sim_sents = ".$PDO->quote($text_list)." WHERE id = ".$current_id;
  99. $Result = PDO_Execute($Update);
  100. return;
  101. }
  102. // 预计算,存入数据库
  103. function similar_sent_matrix($begin,$end) {
  104. // 按照 count = 18, 8, ..., 255 依次获得查询结果 (i-3,i+3)
  105. // count = 17,16,...,7 (i-2,i+2)
  106. for ($current_count=$begin; $current_count <$end ; $current_count++) {
  107. print("单词数:".$current_count."\n");
  108. $current_query = "select id,text from pali_sent where count=".$current_count;
  109. $Current = PDO_FetchAll($current_query);
  110. if (count($Current)) {
  111. foreach($Current as $current_row) {
  112. $current_id = $current_row['id'];
  113. $current_sent = $current_row['text'];
  114. $current_words = words_of_sentence($current_sent);
  115. // 按照 count > $current_count-3 and count <$current_count+3 查询希望比较的语句
  116. if($current_count>17){
  117. $section = 3;
  118. }
  119. else if($current_count>6){
  120. $section = 2;
  121. }
  122. else{
  123. $section = 1;
  124. }
  125. $compare_query = "select id,text from pali_sent where count>".($current_count-$section)." and count<".($current_count+$section);
  126. $Compare = PDO_FetchALL($compare_query);
  127. $current_sim_sent_list = new sim_sent_list(); // 新建相似句链表
  128. foreach($Compare as $compare_row) {
  129. if ($current_row != $compare_row) {
  130. $compare_id = $compare_row['id'];
  131. $compare_sent = $compare_row['text'];
  132. $compare_words = words_of_sentence($compare_sent);
  133. $jaccard_score = jaccard_similarity($current_words, $compare_words);
  134. if ($jaccard_score > 0.3) {
  135. $current_sim_sent_list->jaccard_add($compare_id, $jaccard_score);
  136. }
  137. }
  138. } // end of foreach $compare_row
  139. if ($current_sim_sent_list->size != 0) {
  140. print("update $current_count - ".$current_id."\n");
  141. $text_list = $current_sim_sent_list->get_text_list();
  142. insert_similar_sent_list_into_sqlite($current_id, $text_list);
  143. }
  144. } // end of foreach $current_row
  145. }
  146. }
  147. return;
  148. }
  149. // 实时计算相似句
  150. function sents_similar_to_id($id) {
  151. $query = "SELECT count,text FROM pali_sent WHERE id=".$id;
  152. $Current = PDO_FetchALL($query);
  153. if (count($Current)) {
  154. foreach($Current as $current_row) {
  155. $current_count = $current_row['count'];
  156. $current_sent = $current_row['text'];
  157. $current_words = words_of_sentence($current_sent);
  158. print("current text: \n".$current_sent."\n");
  159. if ($current_count <= 5) {
  160. print("[-] too short.\n");
  161. return;
  162. }
  163. // 只和单词数大于 5 的比较
  164. $compare_query = "SELECT id,text FROM pali_sent WHERE count>5";
  165. $Compare = PDO_FetchALL($compare_query);
  166. $current_sim_sent_list = new sim_sent_list(); // 新建相似句链表
  167. foreach($Compare as $compare_row) {
  168. if ($current_row != $compare_row) {
  169. $compare_id = $compare_row['id'];
  170. $compare_sent = $compare_row['text'];
  171. $compare_words = words_of_sentence($compare_sent);
  172. $jaccard_score = jaccard_similarity($current_words, $compare_words);
  173. if ($jaccard_score > 0.3) {
  174. print("Jaccard similarity: ".$jaccard_score."\tSentence id:".$compare_id."\n");
  175. print("Text: \n". $compare_sent."\n");
  176. $current_sim_sent_list->jaccard_add($compare_id, $jaccard_score);
  177. }
  178. }
  179. } // end of foreach $compare_row
  180. if ($current_sim_sent_list->size != 0) {
  181. // $current_sim_sent_list->print_list();
  182. } else {
  183. print("[-]not found.\n");
  184. }
  185. } // end of foreach($Current)
  186. } // end of if (count($Current))
  187. }
  188. //$id = $argv[1];
  189. //sents_similar_to_id($id);
  190. if ($argc != 3){
  191. echo "无效的参数 ";
  192. exit;
  193. }
  194. $from = (int)$argv[1];
  195. $to =(int)$argv[2];
  196. if($from<4){
  197. $from = 4;
  198. }
  199. if($to>255){
  200. $to = 255;
  201. }
  202. similar_sent_matrix($from,$to);
  203. echo "\n all done";
  204. ?>