upgrade_pali_sent.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. get user sentence from db
  4. */
  5. require_once "../path.php";
  6. require_once "../public/_pdo.php";
  7. require_once "../public/function.php";
  8. if (isset($argv[1])) {
  9. if ($argv[1] == "del") {
  10. $redis = new redis();
  11. $r_conn = $redis->connect('127.0.0.1', 6379);
  12. $keys = $redis->keys('pali_sent_*');
  13. $count=0;
  14. foreach ($keys as $key => $value) {
  15. # code...
  16. $deleted = $redis->del($value);
  17. $count += $deleted;
  18. }
  19. echo "delete ok ".$count;
  20. }
  21. } else {
  22. $dns = "" . _FILE_DB_PALI_SENTENCE_;
  23. $dbh = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  24. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  25. $dns = "" . _FILE_DB_PALI_SENTENCE_SIM_;
  26. $db_pali_sent_sim = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  27. $db_pali_sent_sim->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  28. $query = "SELECT id, book,paragraph, begin,end ,text FROM pali_sent WHERE 1 ";
  29. $stmt = $dbh->prepare($query);
  30. $stmt->execute();
  31. $redis = new redis();
  32. $r_conn = $redis->connect('127.0.0.1', 6379);
  33. $stringSize = 0;
  34. $count = 0;
  35. if ($r_conn) {
  36. while ($sent = $stmt->fetch(PDO::FETCH_ASSOC)) {
  37. $count++;
  38. $stringSize += strlen($sent["text"]);
  39. if ($stringSize > 50000000) {
  40. sleep(1);
  41. $stringSize = 0;
  42. }
  43. if($count%10000==0){
  44. echo $count . "-".$sent["book"] . "_" . $sent["paragraph"] . "\n";
  45. }
  46. $result = $redis->hSet('pali://sent/' . $sent["book"] . "_" . $sent["paragraph"] . "_" . $sent["begin"] . "_" . $sent["end"], "pali", $sent["text"]);
  47. if($result===FALSE){
  48. echo "hSet error \n";
  49. }
  50. $result = $redis->hSet('pali://sent/' . $sent["book"] . "_" . $sent["paragraph"] . "_" . $sent["begin"] . "_" . $sent["end"], "id", $sent["id"]);
  51. $query = "SELECT count FROM 'sent_sim_index' WHERE sent_id = ? ";
  52. $sth = $db_pali_sent_sim->prepare($query);
  53. $sth->execute(array($sent["id"]));
  54. $row = $sth->fetch(PDO::FETCH_ASSOC);
  55. if ($row) {
  56. $pali_sim = $row["count"];
  57. } else {
  58. $pali_sim = 0;
  59. }
  60. $result = $redis->hSet('pali://sent/' . $sent["book"] . "_" . $sent["paragraph"] . "_" . $sent["begin"] . "_" . $sent["end"], "sim_count", $pali_sim);
  61. }
  62. echo "完成 ". count($redis->keys("pali://sent/*"));
  63. } else {
  64. echo "连接redis失败";
  65. }
  66. }