我正在撰写与smarterChild
类似的内容(当天后来自动化AOL Instant Messenger聊天程序),并对我确定句子结构和响应的方法感到好奇。
目前,裸骨设计是使用5种主要方法,根据用户输入共同确定适当的响应:
1) Read(): initially accepts user input, then calls first of many methods: sentenceType()
2) getSentenceType(): User sentence type: Question, statement, directive, suggestion, etc…
3) getWho(): If question is about the computer, or about someone else…
4) getCategory(): What category the question is about (weather, sports…)
5) getResponse(): finally, form a response
这些方法将查询数据库以确定用户输入是否包含关键字...
getSentenceType()
的示例:
public String getSentenceType(String sentence) {
String type = null;
for (String s : db.getQuestions()) {
if (sentence.contains(s)) {
type = "question";
break;
}
else {
type = "statement";
}
}
return getWho(type, sentence);
}
返回句子结构的最终方法示例:
//final call...
public String getResponse(String cat, String who, String type) {
String response = new String();
String auxVerb, subject, mainVerb, noun;
auxVerb = "do";
subject = who;
mainVerb = "like";
noun = cat;
//question structure: auxiliary verb + subject + main verb + noun/pronoun
// DO YOU LIKE MARY?
if (type.equals("question")) {
response = subject + " " + auxVerb + " " + mainVerb + " " + noun + ". ";
}
else {
response = auxVerb + " " + subject + " " + mainVerb + " " + noun + "?";
}
return response;
}
示例数据库方法:
public String[] getQuestions() {
String[] questions = new String[] {"why", "?"};
return questions;
}
public String[] getWeather() {
String[] weather = new String[] {"cold", "hot", "rainy", "weather"};
return weather;
}
然后逐步将所有方法结果连接成一个连贯的响应...然后将结果发送回Read(),将结果打印出来给用户......
这是一种效率低下的方法吗?我知道,如果我继续沿着这条路走下去......建立一个强大的系统,它将需要大量的if else
检查和一个庞大的数据库来确定用户输入的每种可能的响应类型。
有没有建议的方法?
谢谢