在 java 中查找答案类型

find the type of answer in java

抱歉,如果这个问题重复,但我还没有找到答案。我想使用 java 查找问题类型。问题是我不知道如何从用户将要提交的问题中找到问题的类型。

例如:

Where is the Louvre Museum?

The type of this question is location. 

有人知道怎么做吗?

使用用户输入:

Scanner s = new Scanner(System.in)

求输入题:

System.out.println("Question:");
String[] q = s.nextLine().split(" "); //changing user input to word array
String w = q[0].toLowerCase();

检查疑问词:

switch(w){
    case "where":
        //print out "this is a location question"
        break;
    case "who":
        //print out "this is a who question"
        break;

    ... //keep on doing this for all words

    default:
        //print out "this is not a valid question"
        break;

}

如果有多个:

System.out.println("Question:");
String[] q = s.nextLine().split(" "); 
List<String> a = Arrays.asList(q);

检查是否包含疑问词:

if(a.contains("Who"))
    //print out "Who question"
if(a.contains("Where"))
    //print out "Where question"

您可以考虑使用字典列表,其中将包含成对的问题类型,以及与问题类型对应的特定子字符串,例如:

"yes/no", "is"
"location",  {"where", "how far", "direction"}
"time", {"when", "how long"}

你明白了。对于这种情况,使用映射(例如 HashMap)可以很容易地完成:

HashMap <String, String[]> hm = new HashMap <String, String[]>();

你可以替换

String[]

ArrayList <String>

还有。