sim_sent.php 7.1 KB

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