-4

Sorry if this question is duplicate, but I have not found the answer. I want to find the type of question using java. The problem is that I do not know how to find the type of question from the question which the user will be submit.

For example:

Where is the Louvre Museum?

The type of this question is location. 

Anybody has an idea how to do it?

  • Make a `HashMap`. First parameter is the keyword ("where") - second parameter is the Answer-String. Use: `if (hashmap.contains(sentence.split(" ")))`. and so on... really easy. – X-Fate Jan 17 '15 at 20:04
  • yes thanks for your answer, but if the question had two or more type of answer what can I do for this issue or that is not impossible? – user4462040 Jan 17 '15 at 20:09
  • @user4462040 The idea is that you look at the sort of questions you have for each type of answer, and find the common keywords for each type, e.g.: "who" => person, "what" => object, "where" => location, "when" => time, and so on. If the question/answer mapping you have to deal with is not this simple then you might want to look into some Natural Language Processing methods. – Alan Jan 17 '15 at 20:14
  • which are the NLP methods? – user4462040 Jan 17 '15 at 20:31

2 Answers2

1

using user input:

Scanner s = new Scanner(System.in)

ask for the input question:

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

check for question words:

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;

}

if there are multiple:

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

Check if it contains the question words:

if(a.contains("Who"))
    //print out "Who question"
if(a.contains("Where"))
    //print out "Where question"
EDToaster
  • 3,160
  • 3
  • 16
  • 25
0

You could consider using a dictionary list, which will contain pairs of question type, and specific substrings corresponding to the question type, for example:

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

you get the idea. It could be done very easily using mapping, for example a HashMap, for this case:

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

You can replace

String[]

with

ArrayList <String>

as well.

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55