基于语义网的自动问答技术研究项目源代码

原文地址点击打开链接

Section 1.简介

下面是项目的演示视频DEMO系统演示视频(双击全屏观看)
演示视频
下面是对整个项目的总结文档:基于语义网的自动问答系统实现
运行这个文件需要安装mysql+php+apache,此外还需要将rdf数据发布成为关联数据形式(利用Pubby+fuseki,参加我以前的文章)
整个程序的目录如下

本程序只是为了做出demo系统,所以不讲究格式语法框架效率之类的细节问题,实现的方法也比较笨。因为这个项目还没有结束,后期我会继续更新,可能会有java做出更加合理且实用的东西,敬请期待。代码很杂乱,有耐心的可以交流。
下面我将所有的代码都贴出来,并提供下载链接。
代码下载链接DEMO系统程序源代码

简单粗糙的一个开发文档:

Section 2.源代码

index.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
<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.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
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.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
<?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.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
<?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.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
<?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/>请您重新输入查询语句!" ;
?>

 

你可能感兴趣的:(源代码)