db_insert_sentence.php 7.6 KB

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