Пример #1
0
Match *Kernel::match(const string &context, const string &input, const string &that, const string &topic) {
	Match *m = NULL;
	string path = context + " <pattern> " + input + " <that> " + that + " <topic> " + topic;
	getStream("Match")->Read(string("Input: " + path + "\n").c_str());
	m = match(root, root, CONTEXT, path, "", "");
	if (m != NULL) {
		m->setPath(m->getContextPattern() + " : " + m->getInputPattern() + " : " + m->getThatPattern() + " : " + m->getTopicPattern());
		getStream("Match")->Write(string("Match: " + m->getPath() + "\n").c_str());
	} else {
		getStream("Match")->Write("No match found\n");
	}
	return m;
}
Пример #2
0
string Kernel::respond(const string &input, const string &id, Responder *r, int, bool srai, const string &prefix) {
	if (!srai) {
		recursionDepth = 0;
		timingResponse = timerMillis();
	}
	//	I want this to be configurable...
	if (++recursionDepth > maxRecursiveDepth) {
		predicates->addHistory("that", id, "");
		cerr << "AIML contains an infinite loop" << endl;
		cerr << "Input involved in loop: " << input << endl;
		return "";
	}
	string currentResponse = "", buffer = "";
	Match *m = NULL;
	if (!srai) {
		predicates->addHistory("input", id, input);
	}
	string sentence, inString = input;
	while (!(sentence = getSentence(inString)).empty()) {
		sentence = trim(sentence);
		string originalInput = sentence;
		if (sentence.length() < 1) {
			continue;
		}
		sentence = prefix + " " + Substituter::substitute(sentence);
		string context = predicates->getValue("context", id);
		context = Substituter::substitute(context);
		string that = predicates->getValue("that", id);
		if (!srai) {
			StringTokenizer stThat(that, ".?!");
			while (stThat.hasMoreTokens()) {
				string t = stThat.nextToken();
				t = trim(t);
				if (!t.empty()) {
					that = t;
					predicates->addHistory("that", id, that);
				}
			}
		}
		that = Substituter::substitute(that);
		string topic = predicates->getValue("topic", id);
		topic = Substituter::substitute(topic);
		if (that.empty()) {
			that = "*";
		}
		if (topic.empty()) {
			topic = "*";
		}
		if (context.empty()) {
			context = "*";
		}
	
		//--	DEBUGGING LINE
		string ktr = sentence + "\n";
		getStream("Kernel")->Read(ktr.c_str());
	
		m = match(context, sentence, that, topic);
		if (m == NULL) {
				cerr << "There is no match for input: " << sentence << endl;
		} else {
			cerr << endl;
			cerr << "INPUT: " << originalInput << endl;
			cerr << "MATCH PATH: " << m->getPath() << endl;
			cerr << "FILENAME: " << m->getNode()->getActualTemplate()->getFilename() << endl;
			
			string tmpl = "<template>" + m->getTemplate() + "</template>";
			strstream ss;
			ss << tmpl << endl;
			
			SaxParser *p = new SaxParser(new Parser());
			p->parse(ss);
			
			currentResponse = Kernel::process(m, ((Parser *)p->getListener())->getRoot(), r, id);
			
			predicates->setValue("beforethat", id, that);
			predicates->setValue("that", id, currentResponse);
			
			delete p;
		}
		if (m != NULL) {
			delete m;
			if (srai) {
				--recursionDepth;
				return currentResponse;
			} else {
				buffer += currentResponse + " ";
			}
		}
	}
	string result = Substituter::substitute(buffer, "output");
	--recursionDepth;
	if (!srai) {
		timingResponse = timerMillis() - timingResponse;
		cerr << "TIME: " << timingResponse << "ms" << endl;
	}
	//--	DEBUGGING LINE
	string ktw = result + "\n";
	getStream("Kernel")->Write(ktw.c_str());
	if (trimming) {
		return trim(result, " \t\r\n");
	}
	return result;
}