我尝试用HTML / PHP / XML构建一个简单的聊天机器人。大脑'是用XML构建的。将通过AJAX提交一个html表单,调用searchBrain.php。问题是,当提交表单时,searchBrain不会查询我的brain.xml文件。有人可以给我一些帮助吗?
brain.xml
<?xml version="1.0" encoding="UTF-8"?>
<brain>
<neuron>
<id>1</id>
<input>wie ben jij?</input>
<code></code>
<output>Ik ben Skye</output>
<keywords>wie,ben,jij,Wie,?</keywords>
<image></image>
</neuron>
<neuron>
<id>2</id>
<input>hoe gaat het?</input>
<code></code>
<output>Met mij gaat het goed. Hoe gaat het me u?</output>
<keywords>hoe,gaat,het,Hoe,?</keywords>
<image></image>
</neuron>
<neuron>
<id>3</id>
<input>wat ben jij?</input>
<code></code>
<output>Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
<keywords>wat,ben,jij,Wat,?</keywords>
<image></image>
</neuron>
<neuron>
<id>4</id>
<input>hoe zie jij er uit?</input>
<code></code>
<output></output>
<keywords>hoe,zie,jij,er,uit,Hoe,?</keywords>
<image><!-- insert binary.jpg from imgLib --></image>
</neuron>
<neuron>
<id>5</id>
<input>stel jezelf even voor</input>
<code></code>
<output>Hoi ik ben Skye, ik ben online gegaan op 17-02-2016.Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
<keywords>stel,jezelf,even,voor</keywords>
<image></image>
</neuron>
<neuron>
<id>6</id>
<input>Welke dag is het vandaag?</input>
<code><!-- PHP code om huidige dag weer te geven --></code>
<output>Vandaag is het </output>
<keywords>welke,dag,is,het,vandaag,?</keywords>
<image></image>
</neuron>
<neuron>
<id>7</id>
<input>wanneer ben je jarig?</input>
<code></code>
<output>Ik ben online gegaan op 17 Februari, dus ik ben jarig op 17 Februari :-)</output>
<keywords>wanneer,ben,je,jarig,?</keywords>
<image><!-- jarig.jpeg from imgLib --></image>
</neuron>
</brain>
searchBrain.php
<?php
//Create DOMDocument based on XML
$dom = new DOMDocument();
// We don't want to bother with white spaces
$dom->preserveWhiteSpace = false;
//Load brain.xml
$dom->Load('brain.xml');
//Get POST value
$sentence = $_POST['sentence'];
//Lowercase the XML so non case sensitive search is possible
$xml = strtolower($xml);
//Create XPath based on the DOM Document to search it
$xpath = new DOMXpath($dom);
//Define keywords for search
$searchKeywords = array($sentence);
//Iterate all of them to make them into valid XPath
$searchKeywords = array_map(
function($keyword){
//Replace any single quotes with an escaped single quote
$keyword = str_replace('\'','\\\'',$keyword);
return 'contains(.,\''.$keyword.'\')';
},
$searchKeywords
);
//Implode all the keywords using and, could
//be changed to be an "or" condition if needed
$searchKeywords = implode(' and ',$searchKeywords);
//The search keywords now look like contains(.,'good') and
//contains(.,'hello')
//Search for any neuron tag that contains the text
//submitted by the from
$nodes = $xpath->query('//keywords['.$searchKeywords.']');
//Iterate all nodes
foreach($nodes as $node){
//Find the output node an print its content
var_dump($xpath->query('output',$node)->item[3]->textContent);
}
?>
和index.php
<!DOCTYPE html>
<head>
<title>Skye</title>
<script src="jquery-2.1.4.js"></script>
<script>
$(document).ready(function() {
$("#Skye").submit(function(e){
var url = "searchBrain.php";
$.ajax({
type: "POST",
url: url,
data: $("#Skye").serialize(),
success: function(response){
$("#result").html(response);
//alert(response);
}
});
e.preventDefault();
});
});
</script>
<link rel="stylesheet" type="text/css" href="gui/css/skye.css" />
</head>
<body>
<h1>Skye</h1>
<div id="result"></div>
<form id="Skye" name="Skye" method="POST" >
<input type="text" id="sentence" name="sentence" autofocus="autofocus" />
<input type="submit" id="display" value="Stuur..." />
</form>
</body>
</html>
答案 0 :(得分:2)
我发现你的查询有问题。如果您正在寻找完全匹配并想要父节点,则需要:
$nodes = $xpath->query('//neuron/keywords[contains(text(), "wanneer") and contains(text(), "gaat")]/..');
所以你要用上面的内容替换implode(' and ', $searchKeywords)
。
您也可以像下面的评论员一样简写上述语法:
$nodes = $xpath->query('//neuron[keywords[contains(., "wanneer") or contains(., "gaat")]]');
它做了什么?
然后查看每个神经元,然后搜索<keywords>
的文本值是否具有您要查找的内容,/..
就像一个基本上返回父级的文件路径。很漂亮。现在,您可以获得<output>
代码。
//Iterate all nodes
foreach($nodes as $node){
//Find the output node an print its content
var_dump($xpath->query('output',$node)->item(0)->textContent);
}
这是我测试过的完整代码。您可以将其粘贴到测试文件中以查看结果并进行修补,以便将其用于脚本。
<?php
$xml = xml_str();
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath= new DOMXPath($dom);
$nodes = $xpath->query('//neuron/keywords[contains(text(), "wanneer") or contains(text(), "gaat")]/..');
foreach ($nodes as $node) {
var_dump($xpath->query('output', $node)->item(0)->textContent);
}
function xml_str()
{
return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<brain>
<neuron>
<id>1</id>
<input>wie ben jij?</input>
<code></code>
<output>Ik ben Skye</output>
<keywords>wie,ben,jij,Wie,?</keywords>
<image></image>
</neuron>
<neuron>
<id>2</id>
<input>hoe gaat het?</input>
<code></code>
<output>Met mij gaat het goed. Hoe gaat het me u?</output>
<keywords>hoe,gaat,het,Hoe,?</keywords>
<image></image>
</neuron>
<neuron>
<id>3</id>
<input>wat ben jij?</input>
<code></code>
<output>Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
<keywords>wat,ben,jij,Wat,?</keywords>
<image></image>
</neuron>
<neuron>
<id>4</id>
<input>hoe zie jij er uit?</input>
<code></code>
<output></output>
<keywords>hoe,zie,jij,er,uit,Hoe,?</keywords>
<image><!-- insert binary.jpg from imgLib --></image>
</neuron>
<neuron>
<id>5</id>
<input>stel jezelf even voor</input>
<code></code>
<output>Hoi ik ben Skye, ik ben online gegaan op 17-02-2016.Ik ben het laatste resultaat in kunstmatige intelligentie die de functies van het menselijk brein kan reproduceren met een grotere snelheid en nauwkeurigheid.</output>
<keywords>stel,jezelf,even,voor</keywords>
<image></image>
</neuron>
<neuron>
<id>6</id>
<input>Welke dag is het vandaag?</input>
<code><!-- PHP code om huidige dag weer te geven --></code>
<output>Vandaag is het </output>
<keywords>welke,dag,is,het,vandaag,?</keywords>
<image></image>
</neuron>
<neuron>
<id>7</id>
<input>wanneer ben je jarig?</input>
<code></code>
<output>Ik ben online gegaan op 17 Februari, dus ik ben jarig op 17 Februari :-)</output>
<keywords>wanneer,ben,je,jarig,?</keywords>
<image><!-- jarig.jpeg from imgLib --></image>
</neuron>
</brain>
XML;
}
?>