Commit 1ab169d7 authored by bc1417's avatar bc1417
Browse files

changed query processor to map names to answers; can handle query for first and last name n9ow

parent 0acf0585
Pipeline #65412 passed with stages
in 3 minutes and 42 seconds
package ic.doc;
import java.lang.reflect.Array;
import java.util.*;
public class QueryProcessor {
/*Add a map mapping query to answer; change if to switch?;*/
private final Map<List<String>, String> queryMap = new HashMap<>();
public QueryProcessor() {
/*this.queryMap*/
this.queryMap.put(Arrays.asList("william", "shakespeare"),
"William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n");
this.queryMap.put(Arrays.asList("isaac", "asimov"),
"Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
this.queryMap.put(Arrays.asList("alan", "mathison", "turing"),
"Alan Mathison Turing (23 June 1912 - 7 June 1954) was an English\n" +
"mathematician, computer scientist, logician, cryptanalyst, philosopher\n" +
"and theoretical biologist. Turing was highly influential in the\n" +
"development of theoretical computer science, providing a formalisation\n" +
"of the concepts of algorithm and computation with the Turing machine,\n" +
"which can be considered a model of a general-purpose computer. Turing\n" +
"is widely considered to be the father of theoretical computer science\n" +
"and artificial intelligence. Despite these accomplishments, he was\n" +
"never fully recognized in his home country during his lifetime due to\n" +
"his homosexuality, which was then a crime in the UK.");
this.queryMap.put(Arrays.asList("augusta", "ada", "king", "lovelace"),
"Augusta Ada King, Countess of Lovelace (10 December 1815 - 27 November\n" +
"November 1852) was an English mathematician and writer, chiefly\n" +
"known for her work on Charles Babbage's proposed mechanical\n" +
"general-purpose computer, the Analytical Engine. She was the first to\n" +
"recognise that the machine had applications beyond pure calculation,\n" +
"and published the first algorithm intended to be carried out by such\n" +
"a machine. As a result, she is sometimes regarded as the first to\n" +
"recognise the full potential of a \"computing machine\" and the first\n" +
"computer programmer.");
this.queryMap.put(Arrays.asList("william", "pascal"),
"Blaise Pascal (19 June 1623 - 19 August 1662) was a French\n" +
"mathematician, physicist, inventor, writer and Catholic theologian.\n" +
"He was a child prodigy who was educated by his father, a tax collector\n" +
"in Rouen. Pascal's earliest work was in the natural and applied\n" +
"sciences where he made important contributions to the study of fluids,\n" +
"and clarified the concepts of pressure and vacuum by generalising the\n" +
"work of Evangelista Torricelli. Pascal also wrote in defence of the\n" +
"scientific method.");
}
public String process(String query) {
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
results.append("William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("asimov")) {
results.append("Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("turing")) {
results.append("Alan Mathison Turing (23 June 1912 - 7 June 1954) was an English\n" +
"mathematician, computer scientist, logician, cryptanalyst, philosopher\n" +
"and theoretical biologist. Turing was highly influential in the\n" +
"development of theoretical computer science, providing a formalisation\n" +
"of the concepts of algorithm and computation with the Turing machine,\n" +
"which can be considered a model of a general-purpose computer. Turing\n" +
"is widely considered to be the father of theoretical computer science\n" +
"and artificial intelligence. Despite these accomplishments, he was\n" +
"never fully recognized in his home country during his lifetime due to\n" +
"his homosexuality, which was then a crime in the UK.");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("lovelace")) {
results.append("Augusta Ada King, Countess of Lovelace (10 December 1815 - 27 November\n" +
"November 1852) was an English mathematician and writer, chiefly\n" +
"known for her work on Charles Babbage's proposed mechanical\n" +
"general-purpose computer, the Analytical Engine. She was the first to\n" +
"recognise that the machine had applications beyond pure calculation,\n" +
"and published the first algorithm intended to be carried out by such\n" +
"a machine. As a result, she is sometimes regarded as the first to\n" +
"recognise the full potential of a \"computing machine\" and the first\n" +
"computer programmer.");
results.append(System.lineSeparator());
}
return search(query);
}
if (query.toLowerCase().contains("pascal")) {
results.append("Blaise Pascal (19 June 1623 - 19 August 1662) was a French\n" +
"mathematician, physicist, inventor, writer and Catholic theologian.\n" +
"He was a child prodigy who was educated by his father, a tax collector\n" +
"in Rouen. Pascal's earliest work was in the natural and applied\n" +
"sciences where he made important contributions to the study of fluids,\n" +
"and clarified the concepts of pressure and vacuum by generalising the\n" +
"work of Evangelista Torricelli. Pascal also wrote in defence of the\n" +
"scientific method.");
results.append(System.lineSeparator());
private String search(String query) {
/*For same surnames, add to a list instead of returning, if list length
is more than 1, then generate choice page, else return answer*/
for(List name:queryMap.keySet()) {
boolean found = true;
StringTokenizer queryTokens = new StringTokenizer(query);
for(int i = 0; queryTokens.hasMoreTokens(); i++){
found &= name.contains(queryTokens.nextToken().toLowerCase());
}
if (found) {
return queryMap.get(name).concat("\n");
}
}
return results.toString();
return "";
}
}
......@@ -8,10 +8,12 @@ public class HTMLResultPage implements Page {
private final String query;
private final String answer;
//private final String picURL;
public HTMLResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
//this.picURL = "https://en.wikipedia.org/wiki/William_Shakespeare#/media/File:Shakespeare.jpg";
}
public void writeTo(HttpServletResponse resp) throws IOException {
......@@ -32,7 +34,7 @@ public class HTMLResultPage implements Page {
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
}
//writer.println("<p><a href=\"/\">Download Query Result</a></p>" + // ME: what to put for href
//writer.println("<p><img href=\"" + picURL + "\"> </p>" );
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
// Footer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment