Pārlūkot izejas kodu

在reader中显示相似句

visuddhinanda 4 gadi atpakaļ
vecāks
revīzija
d59f5494b3

+ 49 - 0
app/db/pali_sent.php

@@ -0,0 +1,49 @@
+<?php
+require_once "../path.php";
+require_once "../db/table.php";
+
+class PaliSentence extends Table
+{
+    function __construct($redis=false) {
+		parent::__construct(_FILE_DB_PALI_SENTENCE_, "pali_sent", "", "",$redis);
+    }
+
+	public function getId($book,$para,$start,$end)
+	{
+		if (isset($book) && isset($para)) 
+		{
+			if($this->redis!==false){
+				$result = $this->redis->hGet('pali://sent/' . $book . "_" . $para . "_" . $start . "_" . $end, "id");
+				if($result!==FALSE){
+					return $result;
+				}
+			}
+			$id=0;
+			$query = "SELECT id from pali_sent where book = ? and paragraph = ? and begin=? and end=?";
+			$stmt = $this->dbh->prepare($query);
+			$stmt->execute(array($book,$para,$start,$end));
+			$result = $stmt->fetch(PDO::FETCH_ASSOC);
+			if ($result) {
+				$id= $result["id"];
+			}
+			$output = $this->redis->hSet('pali://sent/' . $book . "_" . $para . "_" . $start . "_" . $end, "id",$id);
+			return $id;
+		} else {
+			return 0;
+		}
+	}
+	public function getInfo($id)
+	{
+		$query = "SELECT book,paragraph, begin,end from pali_sent where id = ? ";
+		$stmt = $this->dbh->prepare($query);
+		$stmt->execute(array($id));
+		$result = $stmt->fetch(PDO::FETCH_ASSOC);
+		if ($result) {
+			return $result;
+		}
+		return false;
+	}
+
+}
+
+?>

+ 49 - 0
app/db/pali_sim_sent.php

@@ -0,0 +1,49 @@
+<?php
+require_once "../path.php";
+require_once "../db/table.php";
+
+class PaliSimSentence extends Table
+{
+    function __construct($redis=false) {
+		parent::__construct(_FILE_DB_PALI_SENTENCE_SIM_, "sent_sim", "", "",$redis);
+    }
+
+	public function getSimById($id)
+	{
+
+			if($this->redis!==false){
+				$result = $this->redis->hGet('pali://sim/id' , $id );
+				if($result!==FALSE){
+					return  json_decode($result,true);
+				}
+			}
+			$query = "SELECT sent2 as id  FROM sent_sim WHERE  sent1 = ? ";
+			$stmt = $this->dbh->prepare($query);
+			if($stmt){
+				$stmt->execute(array($id));
+				$simList = $stmt->fetchAll(PDO::FETCH_ASSOC);
+				$output =  json_encode($simList, JSON_UNESCAPED_UNICODE);
+				
+				$this->redis->hSet('pali://sim/id',$id, $output);
+				return $simList;
+			}
+			else{
+				return false;
+			}
+
+	}
+	public function getInfo($id)
+	{
+		$query = "SELECT book,paragraph, begin,end from pali_sent where id = ? ";
+		$stmt = $this->dbh->prepare($query);
+		$stmt->execute(array($id));
+		$result = $stmt->fetch(PDO::FETCH_ASSOC);
+		if ($result) {
+			return $result;
+		}
+		return false;
+	}
+
+}
+
+?>

+ 0 - 1
app/db/pali_text.php

@@ -1,6 +1,5 @@
 <?php
 require_once "../path.php";
-require_once "../share/function.php";
 require_once "../db/table.php";
 
 class PaliText extends Table

+ 2 - 2
app/pali_sent/get_sim.php

@@ -9,7 +9,7 @@ require_once "../public/function.php";
 //获取相似句子列表
 
 if (isset($_POST["sent_id"])) {
-    $dns = "" . _FILE_DB_PALI_SENTENCE_SIM_;
+    $dns = _FILE_DB_PALI_SENTENCE_SIM_;
     $dbh_sim = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
     $dbh_sim->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
     $query = "SELECT sent2 FROM sent_sim WHERE  sent1 = ? limit 0 , 10";
@@ -23,7 +23,7 @@ if (isset($_POST["sent_id"])) {
 }
 $output = array();
 
-$dns = "" . _FILE_DB_PALI_SENTENCE_;
+$dns = _FILE_DB_PALI_SENTENCE_;
 $dbh = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 

+ 33 - 2
app/reader/get_para.php

@@ -1,18 +1,23 @@
 <?php
 require_once "../public/_pdo.php";
 require_once "../path.php";
+require_once "../redis/function.php";
+require_once "../db/pali_sent.php";
+require_once "../db/pali_sim_sent.php";
+
+$mRedis = redis_connect();
 
 define("_MAX_CHAPTER_LEN_", 20000);
 
 if (isset($_GET["book"])) {
     $_book = $_GET["book"];
 } else {
-    exit;
+    $_book = 0;
 }
 if (isset($_GET["para"])) {
     $_para = $_GET["para"];
 } else {
-    exit;
+    $_para = 0;
 }
 
 if (isset($_GET["begin"])) {
@@ -32,6 +37,29 @@ if ($_view == "sent") {
     echo json_encode($output, JSON_UNESCAPED_UNICODE);
     exit;
 }
+if ($_view == "sim") {
+	$mPaliSent = new PaliSentence($mRedis);
+	$mPaliSim = new PaliSimSentence($mRedis);
+	if(!isset($_GET["id"])){
+		$id = $mPaliSent->getId($_book,$_para,$_begin,$_end);
+	}
+	else{
+		$id = $_GET["id"];
+	}
+	{
+		$sent_list[] = $mPaliSent->getInfo($id);
+		$arrList = $mPaliSim->getSimById($id);
+		if($arrList){
+			foreach ($arrList as $key => $value) {
+				# code...
+				$sent_list[] = $mPaliSent->getInfo($value["id"]);
+			}
+			$output["sentences"] = $sent_list;
+			echo json_encode($output, JSON_UNESCAPED_UNICODE);
+		}
+	}
+	exit;
+}
 
 $paraBegin=0;
 $paraEnd=0;
@@ -60,6 +88,9 @@ if ($FetchParInfo) {
             $paraEnd = $_para;
             # code...
             break;
+		case "sim":
+
+			break;
         default:
             # code...
             $paraBegin = $_para;

+ 3 - 0
app/reader/index.php

@@ -23,6 +23,9 @@ require_once "../pcdl/html_head.php";
 	if(isset($_GET["view"])){
 		echo "_reader_view='".$_GET["view"]."';";
 	}
+	if(isset($_GET["id"])){
+		echo "_reader_sent_id='".$_GET["id"]."';";
+	}
 	if(isset($_GET["book"])){
 		echo "_reader_book='".$_GET["book"]."';";
 	}

+ 97 - 94
app/reader/reader.js

@@ -1,4 +1,5 @@
 var _reader_view = "sent";
+var _reader_sent_id = 0;
 var _reader_book = -1;
 var _reader_para = -1;
 var _reader_begin = -1;
@@ -14,119 +15,121 @@ var link_str = "";
 palicanon_load_term();
 
 function reader_load() {
-	$.get(
-		"../reader/get_para.php",
-		{
+	let param;
+	if (_reader_view == "sim") {
+		param = { view: "sim", id: _reader_sent_id };
+	} else {
+		param = {
 			view: _reader_view,
 			book: _reader_book,
 			para: _reader_para,
 			begin: _reader_begin,
 			end: _reader_end,
-		},
-		function (data) {
-			_sent_data = JSON.parse(data);
-			let tpl = "";
-			let currPara = 0;
-			$("#contents").html("");
-			if (_sent_data.sentences.length > 0) {
-				let firstPara = _sent_data.sentences[0].paragraph;
-				for (const iterator of _sent_data.sentences) {
-					if (currPara != iterator.paragraph) {
-						currPara = parseInt(iterator.paragraph);
-						if (currPara == parseInt(_reader_para) + 1 && parseInt(_reader_para) != firstPara) {
-							tpl += "</div>\n\n";
-						}
-						tpl += "\n\n";
-						tpl += "```para\n";
-						tpl += currPara + "\n";
-						tpl += "```\n\n";
-						if (currPara == _reader_para && parseInt(_reader_para) != firstPara) {
-							tpl += "<div id='para_focus' class='focus'>\n\n";
-						}
+		};
+	}
+	$.get("../reader/get_para.php", param, function (data) {
+		_sent_data = JSON.parse(data);
+		let tpl = "";
+		let currPara = 0;
+		$("#contents").html("");
+		if (_sent_data.sentences.length > 0) {
+			let firstPara = _sent_data.sentences[0].paragraph;
+			for (const iterator of _sent_data.sentences) {
+				if (currPara != iterator.paragraph && _reader_view != "sim") {
+					currPara = parseInt(iterator.paragraph);
+					if (currPara == parseInt(_reader_para) + 1 && parseInt(_reader_para) != firstPara) {
+						tpl += "</div>\n\n";
+					}
+					tpl += "\n\n";
+					tpl += "```para\n";
+					tpl += currPara + "\n";
+					tpl += "```\n\n";
+					if (currPara == _reader_para && parseInt(_reader_para) != firstPara) {
+						tpl += "<div id='para_focus' class='focus'>\n\n";
 					}
-					tpl +=
-						"{{" +
-						iterator.book +
-						"-" +
-						iterator.paragraph +
-						"-" +
-						iterator.begin +
-						"-" +
-						iterator.end +
-						"}}\n";
 				}
-				link_str = tpl;
-				$("#contents").html(note_init(tpl));
-				note_refresh_new(function () {
-					document.querySelector("#para_focus").scrollIntoView({
-						block: "end",
-						behavior: "smooth",
-					});
-					//document.querySelector("#para_focus").scrollTo(0, 200);
+				tpl +=
+					"{{" +
+					iterator.book +
+					"-" +
+					iterator.paragraph +
+					"-" +
+					iterator.begin +
+					"-" +
+					iterator.end +
+					"}}\n";
+			}
+			link_str = tpl;
+			$("#contents").html(note_init(tpl));
+			note_refresh_new(function () {
+				document.querySelector("#para_focus").scrollIntoView({
+					block: "end",
+					behavior: "smooth",
 				});
-				reader_draw_para_menu();
+				//document.querySelector("#para_focus").scrollTo(0, 200);
+			});
+			reader_draw_para_menu();
+
+			//右侧目录
+			let tocHtml = "";
+			let tocNextMenu = "";
+			if (_sent_data.toc.length > 0) {
+				let firstLevel = _sent_data.toc[0].level;
+				for (let index = 1; index < _sent_data.toc.length; index++) {
+					const element = _sent_data.toc[index];
+					tocHtml +=
+						"<div class='reader_right_toc level_" +
+						(element.level - firstLevel) +
+						"'><a href='#para_" +
+						element.paragraph +
+						"'>" +
+						element.toc +
+						"</a></div>";
+					tocNextMenu +=
+						"<a href='../reader/?view=chapter&book=" +
+						_reader_book +
+						"&para=" +
+						element.paragraph +
+						"'>" +
+						element.toc +
+						"</a>";
+				}
+				$("#toc_content").html(tocHtml);
+				if (tocNextMenu === "") {
+					$("#para_path_next_level").hide();
+				} else {
+					$("#toc_next_menu").html(tocNextMenu);
+					$("#para_path_next_level").show();
+				}
+			}
+		}
 
-				//右侧目录
-				let tocHtml = "";
-				let tocNextMenu = "";
-				if (_sent_data.toc.length > 0) {
-					let firstLevel = _sent_data.toc[0].level;
-					for (let index = 1; index < _sent_data.toc.length; index++) {
-						const element = _sent_data.toc[index];
-						tocHtml +=
-							"<div class='reader_right_toc level_" +
+		if (_sent_data.head == 1 || _sent_data.sentences.length == 0) {
+			//渲染目录
+			tpl = "<h2>Table of Content</h2>";
+			if (_sent_data.toc.length > 0) {
+				let firstLevel = _sent_data.toc[0].level;
+				for (let index = 1; index < _sent_data.toc.length; index++) {
+					const element = _sent_data.toc[index];
+					if (element.level < 8) {
+						tpl +=
+							"<div class='reader_main_toc level_" +
 							(element.level - firstLevel) +
-							"'><a href='#para_" +
-							element.paragraph +
-							"'>" +
-							element.toc +
-							"</a></div>";
-						tocNextMenu +=
-							"<a href='../reader/?view=chapter&book=" +
+							"'><a href='../reader/?view=chapter&book=" +
 							_reader_book +
 							"&para=" +
 							element.paragraph +
+							"&display=" +
+							_display +
 							"'>" +
 							element.toc +
-							"</a>";
-					}
-					$("#toc_content").html(tocHtml);
-					if (tocNextMenu === "") {
-						$("#para_path_next_level").hide();
-					} else {
-						$("#toc_next_menu").html(tocNextMenu);
-						$("#para_path_next_level").show();
-					}
-				}
-			}
-
-			if (_sent_data.head == 1 || _sent_data.sentences.length == 0) {
-				//渲染目录
-				tpl = "<h2>Table of Content</h2>";
-				if (_sent_data.toc.length > 0) {
-					let firstLevel = _sent_data.toc[0].level;
-					for (let index = 1; index < _sent_data.toc.length; index++) {
-						const element = _sent_data.toc[index];
-						if (element.level < 8) {
-							tpl +=
-								"<div class='reader_main_toc level_" +
-								(element.level - firstLevel) +
-								"'><a href='../reader/?view=chapter&book=" +
-								_reader_book +
-								"&para=" +
-								element.paragraph +
-								"&display=" +
-								_display +
-								"'>" +
-								element.toc +
-								"</a></div>";
-						}
+							"</a></div>";
 					}
 				}
-				$("#contents_toc").html(tpl);
 			}
+			$("#contents_toc").html(tpl);
 		}
-	);
+	});
 
 	reader_get_path();
 }

+ 25 - 17
app/term/pali_sim_sent.js

@@ -1,3 +1,4 @@
+var _sent_id = 0;
 function pali_sim_dlg_init(title = gLocal.gui.similar_sentences) {
 	$("body").append('<div id="pali_sim_dlg" title="' + title + '"><div id="pali_sim_dlg_content"></div></div>');
 
@@ -5,6 +6,13 @@ function pali_sim_dlg_init(title = gLocal.gui.similar_sentences) {
 		autoOpen: false,
 		width: 550,
 		buttons: [
+			{
+				text: "在新窗口打开",
+				click: function () {
+					window.open("../reader/?view=sim&id=" + _sent_id + "&display=sent&direction=col", "_blank");
+					$(this).dialog("close");
+				},
+			},
 			{
 				text: gLocal.gui.close,
 				click: function () {
@@ -15,23 +23,23 @@ function pali_sim_dlg_init(title = gLocal.gui.similar_sentences) {
 	});
 }
 function pali_sim_dlg_open(id, start, length) {
-	{
-		$.post(
-			"../pali_sent/get_sim.php",
-			{
-				sent_id: id,
-				start: start,
-				length: length,
-			},
-			function (data) {
-				let sents = JSON.parse(data);
-				let html = pali_sim_dlg_render(sents);
-				$("#pali_sim_dlg_content").html(html);
-				note_ref_init()
-				$("#pali_sim_dlg").dialog("open");
-			}
-		);
-	}
+	_sent_id = id;
+
+	$.post(
+		"../pali_sent/get_sim.php",
+		{
+			sent_id: id,
+			start: start,
+			length: length,
+		},
+		function (data) {
+			let sents = JSON.parse(data);
+			let html = pali_sim_dlg_render(sents);
+			$("#pali_sim_dlg_content").html(html);
+			note_ref_init();
+			$("#pali_sim_dlg").dialog("open");
+		}
+	);
 }
 
 function pali_sim_dlg_render(sent_list) {