基於語義網的自動問答技術研究項目源代碼

原文地址點擊打開連接

Section 1.簡介

下面是項目的演示視頻DEMO系統演示視頻(雙擊全屏觀看)
演示視頻
下面是對整個項目的總結文檔:基於語義網的自動問答系統實現
運行這個文件須要安裝mysql+php+apache,此外還須要將rdf數據發佈成爲關聯數據形式(利用Pubby+fuseki,參加我之前的文章)
整個程序的目錄以下
2013-07-13_233352php

本程序只是爲了作出demo系統,因此不講究格式語法框架效率之類的細節問題,實現的方法也比較笨。由於這個項目尚未結束,後期我會繼續更新,可能會有java作出更加合理且實用的東西,敬請期待。代碼很雜亂,有耐心的能夠交流。
下面我將全部的代碼都貼出來,並提供下載連接。
代碼下載連接DEMO系統程序源代碼css

簡單粗糙的一個開發文檔:
123456html

Section 2.源代碼

index.php
本代碼的主要功能是實現主頁面的顯示。
java

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
<html>
<head>
<title>創新項目</title> 
<meta http-equiv= "Content-Type" content= "text/html" ; charset= "utf-8" />
<link rel= "stylesheet" href= "css/css.css" />
<style type= "text/css"
     .submit{ 
          border: none;
         background:url( "picture/button.png" );
     }
     body{
         background-repeat:no-repeat;
     }
     li{display:inline;}
     input{vertical-align:middle;}
</style>
</head>
<body background= "picture/1.jpg" >
<span style= "float:right;margin-top:0px;" >
     <a href= "both/about.html" target= "_blank" >About</a>&nbsp;
     <a href= "http://davenzhang.com" target= "_blank" >Author</a>&nbsp;
     <a href= "http://davenzhang.com" target= "_blank" >Contact</a>&nbsp;
     <a href= "" target= "_blank" >Help</a>
</span>
<br><br><br>
 
<center style= "font-family: Cursive;font-size:60px;color:blue;" >Semantic QA</center>
<br><br>
<form action= "natural/QueryProcess.php" method= "post" >
<center><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type= "text" align= "absmiddle" class = "text" name= "nlpquery" style= "width:560px;height:40px;" >
<input type= "submit" align= "absmiddle" value= "" style= "width:80px;height:35px;"  class = "submit" />
 
</center>
</form>
<br>
 
<br><br><br>
 
<a href= "sparql/sparql.php" style= " padding-left:600px" >sparql query</a>
</body>
</html>

下面是文件夾natural下面的文件。
ChooseQuery.phpmysql

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
session_start();
require_once ( "FunctionComposeQuery.php" );
require_once ( "Mysql.php" );
require_once ( "GetInstance.php" );
require_once ( "GetPredicateTarget.php" );
require_once ( "GenerateQuery.php" );
 
function  GenQuery( $str_array )
{
$Function_Compose_Query = new FunctionComposeQuery();
$Mysql = new mysql_class();
$getinstance = new getinstance();
$getpredicatetarget = new getpredicatetarget();
 
 
$pre =get_pre();
$query_pre =get_query_pre();
$type =get_type();
$language =get_language();
$ctype =get_ctype();
 
$index = count ( $pre ); //從數據庫當中獲取到的數據的長度;
$len = count ( $str_array ); //分詞後結果數組的長度;
 
//print_r($language);
//print_r($str_array);
 
 
//把分詞數組中實例(書名、人名等)出來
$book_arr = $getinstance ->getbookinstance( $str_array );
//print_r($book_arr);
$len_book_arr = count ( $book_arr )-1; //獲取書籍實例的長度;
 
$person_arr = $getinstance ->getpersoninstance( $str_array );
//print_r($person_arr);
$len_person_arr = count ( $person_arr )-1;
//判斷有沒有在在數據庫language中的詞語,若是有的話,取出其相對應的pre;
//若是沒有的話呢?判斷相對應的實例是否是存在,若果存在的話,就返回該實例的uri
$type_book = "book" ; $type_person = "person" ;
 
$book_query_target = $getpredicatetarget ->get_target( $pre , $query_pre , $type , $language , $str_array , $type_book );
$book_query_pre = $getpredicatetarget ->get_predicate( $pre , $query_pre , $type , $language , $str_array , $type_book );
$book_query_ctype = $getpredicatetarget ->get_ctype( $pre , $query_pre , $type , $language , $ctype , $str_array , $type_book );
 
$person_query_target = $getpredicatetarget ->get_target( $pre , $query_pre , $type , $language , $str_array , $type_person );
$person_query_pre = $getpredicatetarget ->get_predicate( $pre , $query_pre , $type , $language , $str_array , $type_person );
$person_query_ctype = $getpredicatetarget ->get_ctype( $pre , $query_pre , $type , $language , $ctype , $str_array , $type_person );
 
//print_r($person_query_target);
//print_r($book_query_target);
//print_r($book_query_ctype);
//print_r($book_query_pre);
 
//echo "查詢person:";
//print_r($person_query_target);
 
 
$len_book_ctype = count ( $book_query_ctype )-1; //之因此減1是由於,ctype中有一個是用來標識該數組爲不爲空的;
 
//下面要根據ctype獲取到ctype的查詢目標詞;【很重要】
$index_subject =0;
$query_subject [ 'label' ]= "null" ;
for ( $j =0; $j < $len_book_arr ; $j ++){
for ( $i =0; $i < $len_book_ctype ; $i ++){
     if ( $book_query_ctype [ $i ]== "person" ){
         $query_subject [ $index_subject ]= $book_query_target [ $i ];
         $index_subject ++;
         }
     }
}
 
//echo "主語:";
//print_r($query_subject);
 
//處理查詢語句
//查詢語句分類,
//處理實例查詢詞並不屬於該實例的狀況;這個時候須要判斷利用其它查詢詞查詢出來的數據類型是否是對應了該查詢詞;
 
$_SESSION [ 'book_query_target' ]= $book_query_target ;
$_SESSION [ 'len_book_arr' ]= $len_book_arr ;
$_SESSION [ 'person_query_target' ]= $person_query_target ;
$_SESSION [ 'len_person_arr' ]= $len_person_arr ;
 
//第一種狀況:若是書籍和人名實例都爲不爲空【這一步有待完善,要查出全部的數據】
$book_main_predicate = "dcterms:title" ;
$person_main_predicate = "foaf:name" ;
 
if ( $book_arr [ 'attr' ]!= "null" && $person_arr [ 'attr' ]!= "null" ){
$query1 =case1( $book_query_target , $book_query_pre , $book_arr , $len_book_arr , $book_main_predicate );
$query2 =case1( $person_query_target , $person_query_pre , $person_arr , $len_person_arr , $person_main_predicate );
$query_arr [0]= $query1 ; $query_arr [ 'target0' ]= $Function_Compose_Query ->send_target_to_query_process( $book_query_target , $len_book_arr );
$query_arr [1]= $query2 ; $query_arr [ 'target1' ]= $Function_Compose_Query ->send_target_to_query_process( $person_query_target , $len_person_arr );
return $query_arr ;
}
else if ( $book_arr [ 'attr' ]!= "null" && $person_arr [ 'attr' ]== "null" && $person_query_target [ 'label' ]== "null" ){
$query1 =case1( $book_query_target , $book_query_pre , $book_arr , $len_book_arr , $book_main_predicate );
$query_arr [0]= $query1 ; $query_arr [ 'target0' ]= $Function_Compose_Query ->send_target_to_query_process( $book_query_target , $len_book_arr );
return $query_arr ;
}
else if ( $book_arr [ 'attr' ]== "null" && $person_arr [ 'attr' ]!= "null" && $book_query_target [ 'label' ]== "null" ){
$query2 =case1( $person_query_target , $person_query_pre , $person_arr , $len_person_arr , $person_main_predicate );
$query_arr [0]= $query2 ; $query_arr [ 'target0' ]= $Function_Compose_Query ->send_target_to_query_process( $person_query_target , $len_person_arr );
return $query_arr ;
}
else if ( $book_arr [ 'attr' ]!= "null" && $person_arr [ 'attr' ]== "null" && $person_query_target [ 'label' ]!= "null" ) //第2種狀況:若是書籍不爲空和人名實例爲空
{
$query =case2( $person_query_target , $person_query_pre , $query_subject , $book_query_target , $book_query_pre , $book_arr , $len_book_arr , $book_main_predicate );
$str1 = $Function_Compose_Query ->send_target_to_query_process( $book_query_target , $len_book_arr );
$str2 = $Function_Compose_Query ->send_target_to_query_process( $person_query_target , $len_book_arr );
$str1 .= $str2 ;
$query_arr [0]= $query ;
$query_arr [ 'target0' ]= $str1 ;
//echo $query[0];
//echo $str1;
return $query_arr ;
}
 
//下面考慮只出現實例,不出現其餘查詢詞的狀況
 
 
else //第3種狀況:若是書籍和人名實例都爲空
{
     echo "&nbsp;&nbsp;&nbsp;<font color=red>對不起,您請求的資源不存在或者本系統不支持您的查詢!<br/><br/>&nbsp;&nbsp;&nbsp;請您從新輸入查詢語句!</font>" ;
 
}
 
 
//$query_arr[0]=$query_2;
//$query_arr[1]=$query_1;
 
//print_r($query_arr);
 
 
//return $query;
 
}
 
?>

FunctionComposeQuery.phpweb

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
<?php
  class FunctionComposeQuery{
     //雖然咱們這裏寫的變量名稱所有是有關書籍查詢,可是其實函數是通用的;
     //該函數用於產生查詢時的謂語,如decterm:title;
function query_pre( $book_query_pre , $book_query_target , $i )
{
     if ( $book_query_target [ 'label' ]== "yes" ){
      $len = count ( $book_query_pre )-1;
      $str = '' ;
      for ( $j =0; $j < $len -1; $j ++){
          $str .= "  " . $book_query_pre [ $j ]. "  ?" . $book_query_target [ $j ]. "" . $i . ";" ;
      }
      //最後一行的最後一個標點符號爲「.」,而不是前面的分號;因此須要另外寫一行;
      $str .= "   " . $book_query_pre [ $len -1]. "  ?" . $book_query_target [ $len -1]. "" . $i ;
      return $str ;
     }
 
}
 
 
 
function query_target( $book_query_target , $i ){
      $len = count ( $book_query_target )-1;
      $str = '' ;
      for ( $k =0; $k < $i ; $k ++){
         $str .= " ?s" . $k ;
      for ( $j =0; $j < $len ; $j ++){
          $str .= "  ?" . $book_query_target [ $j ]. "" . $k ;
         }
      }
     return $str ;
}
 
 
function query_target_ctype( $book_query_target , $len_query_subject ){
      $len = count ( $book_query_target )-1;
      $str = '' ;
      for ( $i =0; $i < $len_query_subject ; $i ++){
      for ( $j =0; $j < $len ; $j ++){
          $str .= "  ?" . $book_query_target [ $j ]. "" .( $i ). "  " ; //這裏之因此+5是由於,可能存在相同名字的target,表明了不一樣的意思,這裏設法將它們分開;
         }
     }
     return $str ;
}
 
function send_target_to_query_process( $book_query_target , $i ){
      $len = count ( $book_query_target )-1;
      $str = '' ;
      for ( $k =0; $k < $i ; $k ++){
         $str .= "s" . $k . " " ;
      for ( $j =0; $j < $len ; $j ++){
          $str .= $book_query_target [ $j ]. "" . $k . " " ;
         }
      }
     //$res=explode("|",$str);
     // print_R($res);
      return $str ;
}
function explodestr( $str ){
      $res = explode ( " " , $str );
     // print_R($res);
      return $res ;
}
 
function book_query( $book_arr , $book_query_pre , $book_query_target , $main_predicate ){
     $len = count ( $book_arr );
     $str = '' ;
     for ( $i =0; $i < $len -1; $i ++){
     $str .= "?s" . $i . " " . $main_predicate . " \"" . $book_arr [ $i ]. "\"@zh;" . $this ->query_pre( $book_query_pre , $book_query_target , $i ). "." ;
     }
     return $str ;
}
 
function person_query( $book_arr , $book_query_pre , $book_query_target ){
     $len = count ( $book_arr );
     $str = '' ;
     for ( $i =0; $i < $len -1; $i ++){
     $str .= "?s" . $i . " foaf:name \"" . $book_arr [ $i ]. "\"@zh;" . $this ->query_pre( $book_query_pre , $book_query_target , $i ). "." ;
     }
     return $str ;
}
 
 
}
?>

GenerateQuery.phpsql

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
<?php
require_once ( "FunctionComposeQuery.php" );
 
     
       function case1( $book_query_target , $book_query_pre , $book_arr , $len_book_arr , $main_predicate ){
       $Function_Compose_Query = new FunctionComposeQuery();
         $query ="
         prefix dcterms:<http: //purl.org/dc/terms/>
         prefix metaonto:<http: //example/nju.edu.cn/Ontology/metaonto.owl#>
         prefix foaf:<http: //xmlns.com/foaf/1.0/>
         Select ".$Function_Compose_Query->query_target($book_query_target,$len_book_arr)."
         from <http: //book/book>
         Where
         { ".$Function_Compose_Query->book_query($book_arr,$book_query_pre,$book_query_target,$main_predicate)." }";
          return $query ;
         }
 
     
     function case2( $person_query_target , $person_query_pre , $query_subject , $book_query_target , $book_query_pre , $book_arr , $len_book_arr , $main_predicate ){
         $Function_Compose_Query = new FunctionComposeQuery();
         /**構造查詢模板的最後面一塊
          Select ?author ?name ?title
          from <http://book/book>
         Where
         { ?s dcterms:title "數據倉庫和數據挖掘"@zh;
         dcterms:creator ?author.
          ?author foaf:name ?name;
           foaf:title ?title.
            }
          中的
           ?author foaf:name ?name;
           foaf:title ?title.
           */
         $str = "" ;
         $len_person_query_target = count ( $person_query_target )-1;
         $len_query_subject = count ( $query_subject )-1;
         $k =0;
         for ( $j =0; $j < $len_query_subject ; $j ++){
         for ( $i =0; $i < $len_person_query_target ; $i ++){
             $str .= "?" . $query_subject [ $j ]. "" . $j . "  " . $person_query_pre [ $i ]. "  ?" . $person_query_target [ $i ]. "" .( $k ). "." ;
 
           
         $k ++;
         }
        
         
         //echo $str;
         $query ="
         prefix dcterms:<http: //purl.org/dc/terms/>
         prefix metaonto:<http: //example/nju.edu.cn/Ontology/metaonto.owl#>
         prefix foaf:<http: //xmlns.com/foaf/1.0/>
         Select ".$Function_Compose_Query->query_target($book_query_target,$len_book_arr)." ".$Function_Compose_Query->query_target_ctype($person_query_target,$len_query_subject)."
         from <http: //book/book>
         Where
         { ".$Function_Compose_Query->book_query($book_arr,$book_query_pre,$book_query_target,$main_predicate)." ".$str." }";
          return $query ;
      }
 
function case3( $arr , $pre ){
     $len = count ( $arr );
     $str1 = "" ;
     $str2 = "" ;
     for ( $i =0; $i < $len ; $i ++)
     {
         $str1 .= " ?s" . $i ;
     }
     for ( $i =0; $i < $len ; $i ++)
     {
         $str2 .= " ?s" . $i . " " . $pre . " \"" . $arr [ $i ]. "\"@zh ." ;
     }
     $query ="
         prefix dcterms:<http: //purl.org/dc/terms/>
         prefix metaonto:<http: //example/nju.edu.cn/Ontology/metaonto.owl#>
         prefix foaf:<http: //xmlns.com/foaf/1.0/>
         Select ".$str1."
         from <http: //book/book>
         Where
         { ".$str2." }";
          
 
     return $query ;
      }
function case3_get( $arr , $pre ){
     $len = count ( $arr );
     $str1 = "" ;
     for ( $i =0; $i < $len ; $i ++)
     {
         $str1 .= " s" . $i ;
     }   
     return $str1 ;
      }   
?>

GetInstance.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
<?php
class getinstance{
     function getpersoninstance( $str_array )
     {
         //因爲這個idf值是一個精確的值,只是在瀏覽器打印的狀況下被截斷爲小數位只有12位,因此須要先截斷,再比較!
         //number_format($str_array[$i]['idf'], 12, '.', '')==14.189999580383)如今不用這種方法,改用其餘方法!
         //取出全部做者實例;對應 foaf:name
         $person_arr [ 'attr' ]= "null" ;
         $len = count ( $str_array ); $p =0; $b =0;
         for ( $i =0; $i < $len ; $i ++)
         {
             if ( $str_array [ $i ][ 'attr' ]== "pn" || $str_array [ $i ][ 'attr' ]== "nr" )
                 {
                     $person_arr [ 'attr' ]= "pn" ;
                     $person_arr [ $p ]= $str_array [ $i ][ 'word' ];
                     $p ++;
                 }
         }
         return $person_arr ;
     }
     function getbookinstance( $str_array )
     {    
         //因爲這個idf值是一個精確的值,只是在瀏覽器打印的狀況下被截斷爲小數位只有12位,因此須要先截斷,再比較!
         //number_format($str_array[$i]['idf'], 12, '.', '')==14.189999580383)如今不用這種方法,改用其餘方法!
         //取出全部做者實例;對應 foaf:name
         $book_arr [ 'attr' ]= "null" ;
         $len = count ( $str_array ); $p =0; $b =0;
         for ( $i =0; $i < $len ; $i ++)
         {
         //取出全部書名實例;對應 dcterms:title
             if ( $str_array [ $i ][ 'attr' ]== "bn" )
                 {
                     $book_arr [ 'attr' ]= "bn" ;
                     $book_arr [ $b ]= $str_array [ $i ][ 'word' ];
                     $b ++;
                 }
             
         }
         return $book_arr ;
     }
 
     function getplaceinstance( $str_array )
     {
         $len = count ( $str_array ); $p =0; $b =0;
         for ( $i =0; $i < $len ; $i ++)
         {
         //取出全部書名實例;對應 dcterms:title
             if ( $str_array [ $i ][ 'attr' ]== "bn" )
                 {
                     $book_arr [ 'attr' ]= "bn" ;
                     $book_arr [ $b ]= $str_array [ $i ][ 'word' ];
                     $b ++;
                 }
         }
     }        
}
?>

GetPredicateTarget.phpapache

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
<?php
class getpredicatetarget{
     //獲取查詢Book的謂語;
     function get_predicate( $pre , $query_pre , $type , $language , $str_array , $type_book ){
         $index = count ( $pre );
         $len = count ( $str_array );
         $book_query_pre [ 'label' ]= "null" ;
         $book_index =0;
         for ( $j =0; $j < $index ; $j ++){
         for ( $i =0; $i < $len ; $i ++){
         if (substr_count( $language [ $j ], $str_array [ $i ][ 'word' ]))
             {
             //取出和book相關的信息;&&$pre[$j]!="metaonto:Book"
             if ( $type [ $j ]== $type_book && $pre [ $j ]!= "dcterms:title" ){
                 $book_query_pre [ $book_index ]= $pre [ $j ];
                 $book_index ++;
             }}
         }}
         return $book_query_pre ;
     }
     //獲取查詢Book的查詢詞。
     function get_target( $pre , $query_pre , $type , $language , $str_array , $type_book ){
         $index = count ( $pre );
         $len = count ( $str_array );
         $book_query_target [ 'label' ]= "null" ;
         $book_index =0;
         for ( $j =0; $j < $index ; $j ++){
         for ( $i =0; $i < $len ; $i ++){
         if (substr_count( $language [ $j ], $str_array [ $i ][ 'word' ]))
             {
             //取出和book相關的信息;&&$pre[$j]!="metaonto:Book"&&$pre[$j]!="dcterms:title"
             if ( $type [ $j ]== $type_book ){
                 $book_query_target [ 'label' ]= "yes" ;
                 $book_query_target [ $book_index ]= $query_pre [ $j ];
                 $book_index ++;
             }}
         }}
         return $book_query_target ;
     }
     function get_ctype( $pre , $query_pre , $type , $language , $ctype , $str_array , $type_book ){
         $index = count ( $pre );
         $len = count ( $str_array );
         $book_query_target [ 'label' ]= "null" ;
         $book_index =0;
         for ( $j =0; $j < $index ; $j ++){
         for ( $i =0; $i < $len ; $i ++){
         if (substr_count( $language [ $j ], $str_array [ $i ][ 'word' ]))
             {
             //取出和book相關的信息;&&$pre[$j]!="metaonto:Book"&&$pre[$j]!="dcterms:title"
             if ( $type [ $j ]== $type_book ){
                 $book_query_target [ $book_index ]= $ctype [ $j ];
                 $book_index ++;
             }}
         }}
         return $book_query_target ;
     }
     //獲取查詢person的謂語;
     function getpersonpredicate( $pre , $query_pre , $type , $language , $str_array ){
         $index = count ( $pre );
         $len = count ( $str_array );
         $book_query_pre [ 'label' ]= "null" ;
         $book_index =0;
         for ( $j =0; $j < $index ; $j ++){
         for ( $i =0; $i < $len ; $i ++){
         if (substr_count( $language [ $j ], $str_array [ $i ][ 'word' ]))
             {
             //取出和book相關的信息;&&$pre[$j]!="dcterms:title"
             if ( $type [ $j ]== "person" && $pre [ $j ]!= "foaf:Person" ){
                 $book_query_pre [ $book_index ]= $pre [ $j ];
                 $book_index ++;
             }}
         }}
         return $book_query_pre ;
     }
     //獲取查詢person的查詢詞。&&$pre[$j]!="dcterms:title"
     function getpersontarget( $pre , $query_pre , $type , $language , $str_array ){
         $index = count ( $pre );
         $len = count ( $str_array );
         $book_query_target [ 'label' ]= "null" ;
         $book_index =0;
         for ( $j =0; $j < $index ; $j ++){
         for ( $i =0; $i < $len ; $i ++){
         if (substr_count( $language [ $j ], $str_array [ $i ][ 'word' ]))
             {
             //取出和book相關的信息;
             if ( $type [ $j ]== "person" && $pre [ $j ]!= "foaf:Person" ){
                 $book_query_target [ $book_index ]= $query_pre [ $j ];
                 $book_index ++;
             }}
         }}
         return $book_query_target ;
     }
 
}
?>

LanguageParser.php數組

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
function languageparser( $text ){
     $sh = scws_open();
     scws_set_charset( $sh , 'utf8' );
     //添加本身的詞典,該詞典用於書名、人名等的查詢;這樣就能夠把書名和人名等完整的分類出來;
     scws_add_dict( $sh , 'D:/Program Files/scws/etc/mydict.txt' ,SCWS_XDICT_TXT);
     //系統默認的詞典
     scws_add_dict( $sh , 'D:/Program Files/scws/etc/dict.utf8.xdb' );
     scws_set_rule( $sh , 'D:/Program Files/scws/etc/rules_cht.utf8.ini' );                   
     //送入函數進行句子處理
     scws_send_text( $sh , $text );
     //獲取結果
     $top =scws_get_result( $sh );
 
     //返回結果
     return $top ;
}
?>


Mysql.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
<?php
class mysql_class{
     //先連接數據庫
//mysql_connect(servername,username,password);
     public static  $pre ;
     public $query_pre ;
     public $type ;
     public  $localhost ;
     function mysql_link_select(){
         $con =mysql_connect( "localhost" , "root" , "" );
         //控制編碼,否則會顯示亂碼;
         mysql_query( "SET NAMES 'UTF8'" );
         if (! $con )
         {
             die ( 'Could not connect: ' . mysql_error());
         }
        //查詢數據庫innov_proj
         mysql_select_db( "innov_proj" , $con );
         $result = mysql_query( "SELECT * FROM predicate" );
         mysql_close( $con );
         return $result ;   
     }
}
 
     function get_pre(){
         $mysql_class = new mysql_class();
         $result = $mysql_class ->mysql_link_select();
         $index =0;
         while ( $row = mysql_fetch_array( $result ))
         {
             $pre [ $index ]= $row [ 'pre' ];
             ++ $index ;
         }
         return $pre ;
     }
     function get_query_pre(){
         $mysql_class = new mysql_class();
         $result = $mysql_class ->mysql_link_select();
         $index =0;
         while ( $row = mysql_fetch_array( $result ))
         {
             $query_pre [ $index ]= $row [ 'query_pre' ];
             ++ $index ;
         }
         return $query_pre ;
     }
     function get_type(){
         $mysql_class = new mysql_class();
         $result = $mysql_class ->mysql_link_select();
         $index =0;
         while ( $row = mysql_fetch_array( $result ))
         {
             $type [ $index ]= $row [ 'type' ];
             ++ $index ;
         }
         return $type ;
     }
     function get_language(){
         $mysql_class = new mysql_class();
         $result = $mysql_class ->mysql_link_select();
         $index =0;
         while ( $row = mysql_fetch_array( $result ))
         {
             $language [ $index ]= $row [ 'language' ];
             ++ $index ;
         }
         return $language ;
     }  
     function get_ctype(){
         $mysql_class = new mysql_class();
         $result = $mysql_class ->mysql_link_select();
         $index =0;
         while ( $row = mysql_fetch_array( $result ))
         {
             $ctype [ $index ]= $row [ 'ctype' ];
             ++ $index ;
         }
         return $ctype ;
     }
 
?>


QueryProcess.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
<html>
<head>
<meta http-equiv= "content-type" content= "text/html" ;charset= "utf-8" />
<link rel= "stylesheet" type= "text/css" href= "../css/css.css" />
<style type= "text/css"
     table{ 
     border-collapse:collapse; 
     /*table-layout:fixed;*/ 
    
     table,table td{ 
         border-color:green; 
     }
</style>
</head>
<body background= "../picture/1.jpg" >
<font style= "font-family: verdana" size= "50px" color= "green"  >Query Result!</font><br><br>
<form action= "QueryProcess.php" method= "post" >
<input type= "text" name= "nlpquery" style= "width:500px;height:30px" ><input type= "submit" value= "Go" style= "font-size:18px;color:green" />
<a href= "../index.php" >Home</a>
<br>
<br><br>
</from>
 
<?php
/* ARC2 static class inclusion */
include_once ( "../arc2/ARC2.PHP" );
require_once ( "LanguageParser.php" );
require_once ( "FunctionComposeQuery.php" );
require_once ( "ChooseQuery.php" );
require_once ( "../both/URIProcess.php" );
header( "Content-Type:text/html;  charset=utf-8" );
 
$Function_Compose_Query = new FunctionComposeQuery();
/* configuration */
$config = array (
   /* remote endpoint */
   'remote_store_endpoint' => 'http://localhost:3030/ds/sparql' ,
);
 
/* instantiation */
 
$store = ARC2::getRemoteStore( $config );
 
 
//獲取到檢索框輸入的檢索句子
$str1 = $_POST [ 'nlpquery' ];
if ( $str1 ){
 
//將句子進行分詞;獲得分詞後的數組
$ParserArray =languageparser( $str1 );
//print_r($ParserArray);
 
 
 
//生成sparql查詢語句
$query =GenQuery( $ParserArray );
//$q1=$query_arr[0];
//$q2=$query_arr[1];
$num_query = count ( $query );
     
   for ( $m =0; $m <( $num_query /2); $m ++){
     $str = $query [ 'target' . $m ];
    // echo $str;
     $res = $Function_Compose_Query ->explodestr( $str );
     // print_r($res);
     $len_res = count ( $res );
 
     echo "<strong>您輸入的問句爲:</strong>" . $str1 . "<br>" ;  
?>
<br>
自動生成的<strong>sparql</strong>語句爲:<br>
<table style= "word-break:break-all" width= "40%" height= "10%" ">
   <tr>
    <td>
   <?php
   echo htmlentities( $query [ $m ], ENT_COMPAT);
   ?>
    </td>
   </tr>
</table>
<br>
<table>
<?php
 
if ( $rows = $store ->query( $query [ $m ], 'rows' )) { 
   // print_r($rows);
   $len_rows = count ( $rows );
   //echo $len_rows;
 
   foreach ( $rows as $row ) {
?>
<tr>
<?php
   
     for ( $i =0; $i < $len_res -1; $i ++){
       for ( $j =0; $j < $len_rows ; $j ++){
 
       }
     $var = $res [ $i ];
     if ( $row [ $var ])  URIprocess( $row [ $var ]);
   }
   //URIprocess($row["name5"]);
?>
 
</tr>
 
<?php
   }}
   //header("Location:../both/WrongRequestProcess.php");
?>
</table>
<br><br><br><br>
 
  <?php }
}
else  echo "&nbsp;&nbsp;&nbsp;<font color=red><strong>您沒有填寫查詢框哦!請用天然語言輸入您想查詢的信息!</strong></font>" ;
  ?>
 
 
</body>
</html>

ResultView.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
<?php
function result1( $query , $m , $bqt , $lba ){
     $str1 = $Function_Compose_Query ->send_target_to_query_process( $bqt , $lba );
     $res = $Function_Compose_Query ->explodestr( $str1 );
      //print_r($res);
     $len_res = count ( $res );
     echo "<strong>您輸入的問句爲:</strong>" . $str . "<br><br>" ;
     echo $query [ $m ];
?>
<table >
<?php
if ( $rows = $store ->query( $query [ $m ], 'rows' )) { 
    //print_r($rows);
   $len_rows = count ( $rows );
   //echo $len_rows;
 
   foreach ( $rows as $row ) {
?>
<tr>
<?php
   
     for ( $i =0; $i < $len_res -1; $i ++){
       for ( $j =0; $j < $len_rows ; $j ++){
 
       }
     $var = $res [ $i ];
     URIprocess( $row [ $var ]);
   }
   //URIprocess($row["name5"]);
?>
</tr>
<?php }} //else header("Location:../both/WrongRequestProcess.php");?>
</table>
<br><br><br><br>
}
 
 
function result2(){
     
}
?>

sparql文件夾下面的代碼:
<strong<linktofuseki.php< strong="">

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
<html>
<head>
<meta http-equiv= "content-type" content= "text/html" ;charset= "utf-8" />
<h1 style= "text-align:center;" >Query Result!</h1>
<style type= "text/css"
     table{ 
     border-collapse:collapse; 
    
     table,table td{ 
         border-color:green; 
     }
</style>
</head>
<?php
/* ARC2 static class inclusion */
include_once ( "../arc2/ARC2.PHP" );
require_once ( "../both/URIProcess.php" );
header( "Content-Type:text/html;  charset=utf-8" );
 
 
/* configuration */
$config = array (
   /* remote endpoint */
   'remote_store_endpoint' => 'http://localhost:3030/ds/sparql' ,
);
 
/* instantiation */
 
$store = ARC2::getRemoteStore( $config );
 
$q = $_POST [ 'id' ];
 
 
   ?>
 
<table border= "1" width= "100%"
 
<?php
//注意,這些文件的編碼必須是Utf8的編碼,不然問題出錯
    if ( $rows = $store ->query( $q , 'rows' )) {
   foreach ( $rows as $row ){
?>
<tr>
<?php $var1 = $_POST [ 'var1' ]; if ( $var1 != '' ) URIprocess( $row [ $var1 ]);?>
<?php $var2 = $_POST [ 'var2' ]; if ( $var2 != '' ) URIprocess( $row [ $var2 ]);?>
<?php $var3 = $_POST [ 'var3' ]; if ( $var3 != '' ) URIprocess( $row [ $var3 ]);?>
</tr>
<?php }} else header( "Location:WrongRequestProcess.php" );?>
</table>
 
 
 
 
 
 
<!--
print_r( $rows );
  $keys = array_keys ( $row );
    $len = count ( $keys );
    print_r( $keys );
    // echo $len;
   // print_r( $keys);
   $j =0;
    for ( $i =1; $i < $len ; $i = $i +2)
     {
     $arr [ $j ]= $keys [ $i ]; $j ++; }
     print_r( $arr );
     
/*
$start=strcspn($q,"?");
$end=strcspn($q,"");
$len=$end-$start+1;
$str=substr($q,$start,$len);
$times=substr_count($str,"?");
echo $times;
 
$token = strtok($str, " ");
 
while ($token !== false)
   {
   echo "$token<br />";
   $token = strtok(" ");
   }
*/
-->

sparql

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
<html>
<head>
<title>創新項目</title> 
<meta http-equiv= "Content-Type" content= "text/html" ; charset= "utf-8" />
<link rel= "stylesheet" href= "../css/css.css" />
</head>
<span style= "float:right;margin-top:0px;" >
     <a href= "../both/about.html" >About</a>&nbsp;
     <a href= "http://davenzhang.com" >Author</a>&nbsp;
     <a href= "http://davenzhang.com" >Contact</a>&nbsp;
     <a href= "" >Help</a>
</span>
&nbsp;&nbsp;<center style= "font-family: Cursive;font-size:60px;color:blue;" >Semantic QA</center>
 
<body background= "../picture/1.jpg" >
 
<form action= "LinkTofuseki.php" method= "post" >
<center>
請在下面的輸入框填寫sparql查詢語句:
  <br/>
請輸入你要查詢的變量名稱:<br/>
</center>
<center>
變量1:<input type= "text" name= "var1" value= "s" />
變量2:<input type= "text" name= "var2" value= "p" />
變量3:<input type= "text" name= "var3" value= "o" />
<br/>
</center>
<center>
<textarea name= "id" rows= "20"  cols= "100" id= "sparql" style= "width:630px;height:300px;color:#333"
onfocus= "if(this.value=='SELECT * where {?s ?p ?o .}') {this.value='';}this.style.color='#ff0000';"
onblur= "if(this.value=='') {this.value='SELECT * where {?s ?p ?o .}';this.style.color='#333';}  " >
SELECT * where {?s ?p ?o .}
</textarea>
</center>
<center>
<input type= "submit" value= "提交查詢" />
<input type= "reset" value= "從新填寫" />
</center>
 
</form>
<center><a href= "../index.php" >天然語言</a></center>
</head>
</body>
</html>

下面是Both文件夾下面的代碼:
URIProcess.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
     function URIprocess( $urivar ){
         if ( strchr ( $urivar , "http://" )){
             $var = $urivar ;
             $rep_str1 = "127.0.0.1:8080/webapp/page/" ;
             $rep_str2 = "%23" ;
             $result = str_replace ( "example/nju.edu.cn/" , $rep_str1 , $var );
             $res = str_replace ( "#" , $rep_str2 , $result );
             echo "<td style=\"word-break:break-all\" width=\"5%\"><a href=" . $res . " target=\"_blank\">" ;
             echo $urivar ;
             echo "</a></td>" ;
         }
         else {
             echo "<td style=\"word-break:break-all\" width=\"5%\">" ;
             //print_r($urivar);
             echo $urivar ;
             echo "<br/></td>" ;
         }
     
    }
 
  ?>

WrongRequestProcess.php

1
2
3
4
<?php
header( "Content-Type:text/html;  charset=utf-8" );
echo "對不起,您請求的資源不存在或者本系統不支持您的查詢!<br/>請您從新輸入查詢語句!" ;
?>
相關文章
相關標籤/搜索