Class ProfessionalMagician

java.lang.Object
  extended by ProfessionalMagician

public class ProfessionalMagician
extends java.lang.Object

The professional magician reads the "aether" to answer the user's questions.

In fact, the program uses some tricky Google searches to try to determine an anwser to the user's question without ever actually understanding the question.

First, the magician determines the type of question the user wants to ask: "what is", "who is", "what was", or "who was". The important part of the question is its key word: "is" for "what is" and "who is" and "was" for "what was" and "who was".

Next, the magician obtains the remainder of the user's question. For example, for "What is the tallest mountain in the world?", the magician works with the phrase "the tallest mountain in the world".

Third, the magician searches on Google for the phrase with the key word tacked onto the end. The magician uses phrase search so that the search will find those exact words. For our example, the search is for "the tallest mountain in the world is". Note that if you search on Google for "the tallest mountain in the world is" WITH the quotes, it's a phrase search, and Google will only return pages that include all of those words in the specified order together. If you search for the same phrase WITHOUT the quotes, it's a word search, and Google will return any page that contains all the words, regardless of their order. (In fact, without the quotes, Google will ignore common words that it calls "stop" words such as "is" and "the".)

Spend a moment right now figuring out how to create a string in Java that looks like "the tallest mountain in the world is" WITH the quotes. Hint: just "the tallest mountain in the world is" creates the string WITHOUT quotes. How do you put quotation marks into a string?

Fourth, the magician sifts through the results that come back from Google, investigating "candidate" responses. If our example brought back the results "the tallest mountain in the world is actually mauna kea in hawaii..." and "...question anyway the tallest mountain in the world is mount everest in the himalayas...", the candidates would be groups of words following the phrase sought: "the tallest mountain in the world". In this case: "actually", "actually mauna", "actually mauna kea", "mount", "mount everest", "mount everest in", and so forth. In other words, find the phrase sought and then pull out longer and longer word sequences from the string immediately AFTER the phrase. (Note: in your implementation, this step will likely overlap step five and six below.)

Fifth, the magician tries to "verify" each candidate by searching for it together with the key word and the original phrase, again as a "phrase search" so that all words in exactly that order together are found. For our example, the searches would be: "actually is the tallest mountain in the world", "actually mauna is the tallest mountain in the world", "actually mauna kea is the tallest mountain in the world", "mount is the tallest mountain in the world", "mount everest is the tallest mountain in the world", "mount everest in is the tallest mountain in the world", and so forth, all WITH the quotes.

Sixth, the magician picks the candidate that had the largest number of hits in its verification search and reports it as the answer. In our example, that would be "mount everest" with more than a hundred hits (although "actually mauna kea" will probably receive a small number of hits as well).


Method Summary
private static int getQuestionType()
          Reads a question type from the user.
static void main(java.lang.String[] args)
          Repeatedly ask the user for a question to answer and answer the question.
private static void printBanner()
          Prints the opening banner.
private static void printInstructions()
          Explains to the user how to select a question type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getQuestionType

private static int getQuestionType()
Reads a question type from the user.

Keeps prompting the user for a response until the user supplies a valid response.

There should be no input the user can provide that will cause an error in this method, even if the user provides letters rather than numbers. Instead, the method should explain any problems with the user's input and re-prompt for proper input until it gets an appropriate number.

Note: the question types are hard-coded. 0 = quit. 1 = What is. 2 = Who is. 3 = What was. 4 = Who was. If we had arrays, we could make a more flexible solution.

Hint: can you somehow use the Scanner methods hasNextInt, nextInt, and next to ensure that you get valid input?

Returns:
a number corresponding to the question type to use, or 0 if the user wants to quit.

main

public static void main(java.lang.String[] args)
Repeatedly ask the user for a question to answer and answer the question.


printBanner

private static void printBanner()
Prints the opening banner.


printInstructions

private static void printInstructions()
Explains to the user how to select a question type.