db_insert_sentence.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <!--句子库生成-->
  2. <?php
  3. require_once "install_head.php";
  4. ?>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. </head>
  9. <body>
  10. <h2>Insert to Sentence DB</h2>
  11. <p><a href="index.php">Home</a></p>
  12. <?php
  13. include "./_pdo.php";
  14. require_once '../path.php';
  15. $db_file =_FILE_DB_PALI_SENTENCE_;
  16. $thisfile = '.'.mb_substr(__FILE__,mb_strlen(__DIR__));
  17. if(isset($_GET["from"])==false){
  18. ?>
  19. <form action="<?php echo $thisfile; ?>" method="get">
  20. From: <input type="text" value="0" name="from"><br>
  21. To: <input type="text" value="216" name="to"><br>
  22. <input type="submit">
  23. </form>
  24. <?php
  25. PDO_Connect("sqlite:$db_file");
  26. $query="CREATE TABLE pali_sent (
  27. id INTEGER PRIMARY KEY AUTOINCREMENT,
  28. book INTEGER,
  29. paragraph INTEGER,
  30. [begin] INTEGER,
  31. [end] INTEGER,
  32. length INTEGER,
  33. count INTEGER,
  34. text TEXT,
  35. real TEXT,
  36. real_en TEXT
  37. )";
  38. $stmt = @PDO_Execute($query);
  39. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  40. $error = PDO_ErrorInfo();
  41. print_r($error[2]);
  42. }
  43. else{
  44. echo "create table pali_sent .";
  45. }
  46. /*
  47. $query="CREATE INDEX 'search' ON \"pali_sent\" (\"text\", \"real\", \"real_en\" ASC)";
  48. $stmt = @PDO_Execute($query);
  49. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  50. $error = PDO_ErrorInfo();
  51. print_r($error[2]);
  52. $log=$log."$from, $FileName, error, $error[2] \r\n";
  53. }
  54. */
  55. return;
  56. }
  57. $from=$_GET["from"];
  58. $to=$_GET["to"];
  59. $filelist=array();
  60. $fileNums=0;
  61. $log="";
  62. echo "<h2>$from-$to</h2>";
  63. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  64. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  65. $fileNums++;
  66. }
  67. }
  68. if($to>=$fileNums) $to=$fileNums-1;
  69. $FileName=$filelist[$from][1].".htm";
  70. $fileId=$filelist[$from][0];
  71. $fileId=$filelist[$from][0];
  72. $dirLog=_DIR_LOG_."/";
  73. $dirDb="db/";
  74. $inputFileName=$FileName;
  75. $outputFileNameHead=$filelist[$from][1];
  76. $bookId=$filelist[$from][2];
  77. $vriParNum=0;
  78. $wordOrder=1;
  79. $dirXmlBase=_DIR_PALI_CSV_."/";
  80. $dirXml=$outputFileNameHead."/";
  81. $currChapter="";
  82. $currParNum="";
  83. $arrAllWords[0]=array("id","wid","book","paragraph","word","real","type","gramma","mean","note","part","partmean","bmc","bmt","un","style","vri","sya","si","ka","pi","pa","kam");
  84. $g_wordCounter=0;
  85. $arrUnWords[0]=array("id","word","type","gramma","parent","mean","note","part","partmean","cf","state","delete","tag","len");
  86. $g_unWordCounter=0;
  87. $arrUnPart[0]="word";
  88. $g_unPartCounter=-1;
  89. /*去掉标点符号的统计*/
  90. $arrAllPaliWordsCount=array();
  91. $g_paliWordCounter=0;
  92. $g_wordCounterInSutta=0;
  93. $g_paliWordCountCounter=0;
  94. $xmlfile = $inputFileName;
  95. echo "doing:".$xmlfile."<br>";
  96. $log=$log."$from,$FileName,open\r\n";
  97. $arrInserString=array();
  98. function getWordEn($strIn){
  99. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ' , 'ṭ', 'ḍ', 'ṇ', 'ḷ', 'ṃ');
  100. $replace = array('a', 'i', 'u', 'n', 'n' , 't', 'd', 'n', 'l', 'm');
  101. return(str_replace($search,$replace,$strIn));
  102. }
  103. // 打开文件并读取数据
  104. $iWord=0;
  105. $pre=null;
  106. $curr=null;
  107. $next=null;
  108. $wordlist=array();
  109. $arrSent=array();
  110. $book=0;
  111. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead.".csv", "r"))!==FALSE){
  112. while(($data=fgetcsv($fp))!==FALSE){
  113. //id,wid,book,paragraph,word,real,type,gramma,mean,note,part,partmean,bmc,bmt,un,style,vri,sya,si,ka,pi,pa,kam
  114. //$data = mb_split(",",$data);
  115. $wordlist[]=$data;
  116. if($book==0){
  117. $book=substr($data[2],1);
  118. }
  119. }
  120. fclose($fp);
  121. $iWord=0;
  122. $iCurrPara=0;
  123. $Note_Mark=0;
  124. if($wordlist[1][6]!=".ctl."){
  125. $sent=$wordlist[1][4]." ";
  126. $sent_real=$wordlist[1][5];
  127. $wordcount=1;
  128. }
  129. else{
  130. $sent="";
  131. $sent_real="";
  132. $wordcount=0;
  133. }
  134. $begin=1;
  135. $end=1;
  136. $iSent=0;
  137. for($i=2;$i<count($wordlist);$i++){
  138. if($wordlist[$i][3]>$iCurrPara){
  139. //echo "new paragraph<br>";
  140. $iWord=0;
  141. if($i>2){
  142. //echo "上一段结束<br>";
  143. if(strlen(trim($sent))>0){
  144. $end = $wordlist[$i-1][16];
  145. $arrSent[]=array($book,$iCurrPara,$begin,$end,mb_strlen($sent_real,"UTF-8"),$wordcount,$sent,$sent_real,getWordEn($sent_real));
  146. //echo "end={$end}<br>";
  147. //echo "<div>[{$iCurrPara}-{$begin}-{$end}]({$wordcount})<br>{$sent}<br>{$sent_real}<br>".getWordEn($sent_real)."</div>";
  148. }
  149. $iCurrPara=$wordlist[$i][3];
  150. //下一段开始
  151. if($wordlist[$i][6]!=".ctl."){
  152. $sent=$wordlist[$i][4]." ";
  153. if($wordlist[$i][5]=='"'){
  154. $sent_real="";
  155. }
  156. else{
  157. $sent_real=$wordlist[$i][5];
  158. }
  159. $wordcount=1;
  160. }
  161. else{
  162. $sent="";
  163. $sent_real="";
  164. $wordcount=0;
  165. }
  166. $begin = $wordlist[$i][16];
  167. $iSent++;
  168. continue;
  169. }
  170. $iCurrPara=$wordlist[$i][3];
  171. }
  172. $isEndOfSen=false;
  173. if($i<count($wordlist)-1){
  174. $pre=$wordlist[$i-1];
  175. $curr=$wordlist[$i];
  176. $next=$wordlist[$i+1];
  177. if($curr[5]!=""){
  178. $wordcount++;
  179. }
  180. if($next[4]=="("){
  181. $Note_Mark=1;
  182. }
  183. else if($pre[4]==")" && $Note_Mark==1){
  184. $Note_Mark=0;
  185. }
  186. if($curr[15]!="note" || mb_substr($curr[1],0,5,"UTF-8")!="gatha"){
  187. if($curr[4]=="." && !is_numeric($pre[4]) && $next[3]==$iCurrPara && $Note_Mark==0){//以.結尾且非註釋
  188. if($next[4]!="("){
  189. $isEndOfSen=true;
  190. }
  191. }
  192. else if($curr[4]=="–" && $next[4]=="‘" && $Note_Mark==0){
  193. $isEndOfSen=true;
  194. }
  195. else if($Note_Mark==0){//以!或?或;結尾
  196. if($curr[4]=="!"){
  197. if($next[4]!="!"){
  198. if($next[4]!="("){
  199. $isEndOfSen=true;
  200. }
  201. }
  202. }
  203. else if($curr[4]==";" || $curr[4]=="?"){
  204. if($next[4]!="("){
  205. $isEndOfSen=true;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. if($curr[6]!=".ctl."){
  212. if($next[5]!=""){
  213. $sent .= $curr[4]." ";
  214. }
  215. else{
  216. $sent .= $curr[4];
  217. }
  218. if($wordlist[$i][5]!='"'){
  219. if($wordlist[$i][5]=="iti"){
  220. $sent_real .=$curr[4];
  221. }
  222. else{
  223. $sent_real .=$curr[5];
  224. }
  225. }
  226. }
  227. if($isEndOfSen==true && strlen(trim($sent))>0){
  228. $end = $wordlist[$i][16];
  229. $arrSent[]=array($book,$iCurrPara,$begin,$end,mb_strlen($sent_real,"UTF-8"),$wordcount,$sent,$sent_real,getWordEn($sent_real));
  230. //echo "end={$end}<br>";
  231. //echo "<div>[{$iCurrPara}-{$begin}-{$end}]({$wordcount})<br>{$sent}<br>{$sent_real}<br>".getWordEn($sent_real)."</div>";
  232. $sent="";
  233. $sent_real="";
  234. $iSent++;
  235. $begin = $wordlist[$i][16]+1;
  236. $wordcount=0;
  237. }
  238. $iWord++;
  239. }
  240. if(strlen(trim($sent))>0){
  241. $end = $wordlist[count($wordlist)-1][16];
  242. $arrSent[]=array($book,$iCurrPara,$begin,$end,mb_strlen($sent_real,"UTF-8"),$wordcount,$sent,$sent_real,getWordEn($sent_real));
  243. //echo "end={$end}<br>";
  244. //echo "<div>[{$iCurrPara}-{$begin}-{$end}]({$wordcount})<br>{$sent}<br>{$sent_real}<br>".getWordEn($sent_real)."</div>";
  245. }
  246. }
  247. else{
  248. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  249. }
  250. // 开始一个事务,关闭自动提交
  251. PDO_Connect("sqlite:$db_file");
  252. $PDO->beginTransaction();
  253. $query="INSERT INTO pali_sent ('id','book','paragraph','begin','end','length','count','text','real','real_en') VALUES (NULL,?,?,?,?,?,?,?,?,?)";
  254. $stmt = $PDO->prepare($query);
  255. foreach($arrSent as $oneParam){
  256. $stmt->execute($oneParam);
  257. }
  258. // 提交更改
  259. $PDO->commit();
  260. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  261. $error = PDO_ErrorInfo();
  262. echo "error - $error[2] <br>";
  263. $log=$log."$from, $FileName, error, $error[2] \r\n";
  264. }
  265. else{
  266. $count=count($arrSent);
  267. echo "updata $count recorders.";
  268. }
  269. $myLogFile = fopen(_DIR_LOG_."insert_sent.log", "a");
  270. fwrite($myLogFile, $log);
  271. fclose($myLogFile);
  272. ?>
  273. <?php
  274. if($from>=$to){
  275. echo "<h2>齐活!功德无量!all done!</h2>";
  276. }
  277. else{
  278. echo "<script>";
  279. echo "window.location.assign(\"db_insert_sentence.php?from=".($from+1)."&to=".$to."\")";
  280. echo "</script>";
  281. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  282. }
  283. ?>
  284. </body>
  285. </html>