Commit 9cf4d395 authored by bc1417's avatar bc1417
Browse files

everything uses query now; changed tests to reflect this; added picture and wiki link to each item

parent a047764a
Pipeline #65521 passed with stages
in 3 minutes and 22 seconds
......@@ -11,6 +11,9 @@ public class Query {
public Query(List<String> query, String answer, String imgURL, String wikiURL) {
this.query = query;
this.answer = answer;
if(query != null) {
this.answer = this.answer.concat("\n");
}
this.imgURL = imgURL;
this.wikiURL = wikiURL;
}
......
......@@ -22,8 +22,8 @@ public class QueryProcessor {
"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",
"",
""));
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Isaac.Asimov01.jpg/800px-Isaac.Asimov01.jpg",
"https://en.wikipedia.org/wiki/Isaac_Asimov"));
this.queriesMap.add(new Query(Arrays.asList("alan", "mathison", "turing"),
"Alan Mathison Turing (23 June 1912 - 7 June 1954) was an English\n" +
......@@ -36,8 +36,8 @@ public class QueryProcessor {
"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.",
"",
""));
"https://upload.wikimedia.org/wikipedia/commons/a/a1/Alan_Turing_Aged_16.jpg",
"https://en.wikipedia.org/wiki/Alan_Turing"));
this.queriesMap.add(new Query(Arrays.asList("augusta", "ada", "king", "lovelace"),
"Augusta Ada King, Countess of Lovelace (10 December 1815 - 27 November\n" +
......@@ -49,10 +49,10 @@ public class QueryProcessor {
"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.",
"",
""));
"https://upload.wikimedia.org/wikipedia/commons/0/0b/Ada_Byron_daguerreotype_by_Antoine_Claudet_1843_or_1850.jpg",
"https://en.wikipedia.org/wiki/Ada_Lovelace"));
this.queriesMap.add(new Query(Arrays.asList("william", "pascal"),
this.queriesMap.add(new Query(Arrays.asList("william", "pascal", "blaise"),
"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" +
......@@ -61,58 +61,16 @@ public class QueryProcessor {
"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.",
"",
""));
/*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.");*/
"https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Blaise_Pascal_Versailles.JPG/800px-Blaise_Pascal_Versailles.JPG",
"https://en.wikipedia.org/wiki/Blaise_Pascal"));
}
public String process(String query) {
public Query process(String query) {
return search(query);
}
private String search(String query) {
private Query 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*/
......@@ -124,10 +82,10 @@ public class QueryProcessor {
found &= name.contains(queryTokens.nextToken().toLowerCase());
}
if (found) {
return q.getAnswer().concat("\n");
return q;
}
}
return "";
return new Query(null, "","","");
}
}
package ic.doc.web;
import ic.doc.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -7,9 +9,9 @@ import java.io.PrintWriter;
public class HTMLResultPage implements Page {
private final String query;
private final String answer;
private final Query answer;
public HTMLResultPage(String query, String answer) {
public HTMLResultPage(String query, Query answer) {
this.query = query;
this.answer = answer;
}
......@@ -24,14 +26,17 @@ public class HTMLResultPage implements Page {
writer.println("<body>");
// Content
if (answer == null || answer.isEmpty()) {
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
writer.println("<p>" + answer.getAnswer().replace("\n", "<br>") + "</p>");
}
writer.println("<p> <img src=\"" + answer.getImgURL() + "\"> </p>");
writer.println("<p><a href=\""+answer.getWikiURL()+"\" target=\"_blank" +
"\">Learn More!</a></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
// Footer
......
package ic.doc.web;
import ic.doc.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class MarkdownResultPage implements Page {
private final String query;
private final String answer;
private final Query answer;
public MarkdownResultPage(String query, String answer) {
public MarkdownResultPage(String query, Query answer) {
this.query = query;
this.answer = answer;
}
......@@ -17,7 +19,7 @@ public class MarkdownResultPage implements Page {
resp.setContentType("text/markdown"); // ME : THIS is the MIME Type? before said text/markdown?
// Content
if (answer == null || answer.isEmpty()) {
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
resp.setHeader("Content-Disposition", "attachment;filename=\"sorry.md\"");
PrintWriter writer = resp.getWriter();
writer.println("#Sorry");
......@@ -28,7 +30,7 @@ public class MarkdownResultPage implements Page {
File tmp = File.createTempFile(query, ".tmp");
FileWriter fw = new FileWriter(tmp);
fw.write("#" + query + "\n");
fw.write(answer);
fw.write(answer.getAnswer());
fw.close();
FileInputStream fileInputStream = new FileInputStream(tmp);
......
package ic.doc.web;
import ic.doc.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Path;
......@@ -10,9 +12,9 @@ import java.util.List;
public class PDFResultPage implements Page {
private final String query;
private final String answer;
private final Query answer;
public PDFResultPage(String query, String answer) {
public PDFResultPage(String query, Query answer) {
this.query = query;
this.answer = answer;
}
......@@ -20,7 +22,7 @@ public class PDFResultPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("application/pdf");
if (answer == null || answer.isEmpty()) {
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
resp.setHeader("Content-Disposition", "inline;filename=\"sorry.pdf\"");
PrintWriter writer = resp.getWriter();
writer.println("#Sorry");
......@@ -32,7 +34,7 @@ public class PDFResultPage implements Page {
md.deleteOnExit();
FileWriter fw = new FileWriter(md);
fw.write("#" + query + "\n");
fw.write(answer);
fw.write(answer.getAnswer());
fw.close();
File pdf = File.createTempFile(query, ".pdf");
......
......@@ -12,37 +12,42 @@ public class QueryProcessorTest {
@Test
public void returnsEmptyStringIfCannotProcessQuery() throws Exception {
assertThat(queryProcessor.process("test"), is(""));
assertThat(queryProcessor.process("test").getAnswer(), is(""));
}
@Test
public void knowsAboutShakespeare() throws Exception {
assertThat(queryProcessor.process("Shakespeare"), containsString("playwright"));
assertThat(queryProcessor.process("Shakespeare").getAnswer(), containsString(
"playwright"));
}
@Test
public void knowsAboutAsimov() throws Exception {
assertThat(queryProcessor.process("Asimov"), containsString("science fiction"));
assertThat(queryProcessor.process("Asimov").getAnswer(), containsString("science " +
"fiction"));
}
@Test
public void knowsAboutTuring() throws Exception {
assertThat(queryProcessor.process("Turing"), containsString("logician"));
assertThat(queryProcessor.process("Turing").getAnswer(), containsString("logician"));
}
@Test
public void knowsAboutLovelace() throws Exception {
assertThat(queryProcessor.process("Lovelace"), containsString("Countess of Lovelace"));
assertThat(queryProcessor.process("Lovelace").getAnswer(), containsString(
"Countess of Lovelace"));
}
@Test
public void knowsAboutPascal() throws Exception {
assertThat(queryProcessor.process("Pascal"), containsString("clarified the concepts " +
assertThat(queryProcessor.process("Pascal").getAnswer(), containsString(
"clarified the concepts " +
"of pressure and vacuum"));
}
@Test
public void isNotCaseSensitive() throws Exception {
assertThat(queryProcessor.process("shakespeare"), containsString("playwright"));
assertThat(queryProcessor.process("shakespeare").getAnswer(), containsString(
"playwright"));
}
}
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