-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathback_end.php
125 lines (111 loc) · 3.38 KB
/
back_end.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
ini_set("display_errors", "Off");
//error_reporting(E_ALL | E_STRICT);
error_reporting(1);
ini_set('memory_limit', '1024M');
set_time_limit(0);
//ignore_user_abort(); // 后台运行
require_once dirname(__FILE__)."/vendor/multi-array/MultiArray.php";
require_once dirname(__FILE__)."/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once dirname(__FILE__)."/class/Jieba.php";
require_once dirname(__FILE__)."/class/Finalseg.php";
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\JiebaAnalyse;
//echo "hello";
function find($str){
//$link = mysqli_connect("localhost", "root", "666666", "python");
global $link;
mysqli_set_charset($link, "utf8");
/* check connection */
if (mysqli_connect_errno()) {
//printf("Connect failed: %s\n", mysqli_connect_error());
echo json_encode("");
exit();
}
$sql = "select * from `inverted index` where keywords='".$str."'";
//echo $sql." \n";
$result=$link->query($sql);
if(!$result){
echo json_encode("");
//echo "sql语句错误<br/>";
//echo "error:".$mysqli->error."|".$mysqli->error;
}
$num_results = $result -> num_rows; //结果行数
//echo "<p>Number of row found: ". $num_results ."</p>";//输出行数
global $result_array;
for($i = 0;$i < $num_results;$i++)//循环输出每组元素
{
$row = $result -> fetch_assoc();//提取元素,一次一行,fetch_assoc()提取出的元素,有属性以及值
$index_ = stripcslashes(($row['index_']));
$tf_idf = stripcslashes($row['tf_idf']);
//echo $index_." ".$tf_idf." ";
if(array_key_exists($index_,$result_array)){
$result_array[$index_]+=$tf_idf;
}
else{
$result_array[$index_] = 0+$tf_idf;
}
}
//mysqli_close($link);
}
function search($index){
global $return_array;
//$link = mysqli_connect("localhost", "root", "666666", "python");
global $link;
mysqli_set_charset($link, "utf8");
/* check connection */
if (mysqli_connect_errno()) {
echo json_encode("");
exit();
}
$sql = "select * from `search_result` where index_='".$index."'";
$result=$link->query($sql);
$num_results = $result -> num_rows; //结果行数
$row = $result -> fetch_assoc();//提取元素,一次一行,fetch_assoc()提取出的元素,有属性以及值
$url = stripcslashes(($row['url']));
$title = stripcslashes($row['title']);
$description = stripcslashes($row['description']);
$date = stripcslashes($row['date']);
$return_array[]=array("url"=>$url,"title"=>$title,"description"=>$description,"date"=>$date);
//mysqli_close($link);
}
session_start();
// $_POST['question']='大约';
if($_POST['question']!=""){
//$_SESSION["Jie"]=serialize(Jieba::return_self());
//unserialize($_SESSION["Jie"])::init();
Jieba::init();
Finalseg::init();
//open database
$link = mysqli_connect("localhost", "root", "666666", "python");
$result_array=[];
$return_array=[];
$seg_list=Jieba::cutForSearch($_POST['question']);
//$seg_list = unserialize($_SESSION["Jie"])::cutForSearch($_POST['question']);
//var_dump($seg_list);
if(count($seg_list)==0){
echo json_encode("");
exit();
}
foreach ($seg_list as $value){
//print($value." ");
find($value);
}
if(count($result_array)==0){
echo json_encode("");
exit();
}
arsort($result_array);
foreach ($result_array as $key=>$value){
search($key);
}
if(count($return_array)==0){
echo json_encode("");
exit();
}
echo json_encode($return_array);
//close database
mysqli_close($link);
}
?>