db_insert_sentence.php 7.5 KB

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