我正在写一些类似于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
检查和庞大的数据库来确定用户输入的每一种可能的响应类型。
有什么建议的方法吗?
谢谢