Commit 2ef207fd authored by Zhili Tian's avatar Zhili Tian
Browse files

add powerful search

parent a48ce5e7
Pipeline #174061 failed with stages
in 1 minute and 17 seconds
......@@ -56,6 +56,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
......@@ -2,15 +2,36 @@ package ic.doc;
public class QueryProcessor {
private boolean containsAll(String answer, String[] queryArr) {
for (String str :queryArr) {
if (!answer.contains(str)) return false;
}
return true;
}
public String process(String query) {
String [] queryArr = query.toLowerCase().split("\\s+");
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
if (containsAll("william shakespeare's life introduction", queryArr)
&& containsAll(query, new String[]{"william", "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 (containsAll("william shakespears's poems", queryArr)
&& containsAll(query,new String[]{"william", "shakespeare"})) {
results.append("n 1593 and 1594, when the theatres were closed because of plague, \n" +
"Shakespeare published two narrative poems on sexual themes, Venus and \n" +
"Adonis and The Rape of Lucrece. He dedicated them to Henry Wriothesley, \n" +
"Earl of Southampton. In Venus and Adonis, an innocent Adonis rejects the \n" +
" sexual advances of Venus; ...");
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" +
......
......@@ -15,27 +15,32 @@ public class HTMLResultPage implements Page {
}
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
// Content
if (answer == null || answer.isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
if (query.isBlank()) {
// Do Nothing
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
// Content
if (answer == null || answer.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><a href=\"/\">Back to Search Page</a></p>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
}
......@@ -6,43 +6,45 @@ import javax.servlet.http.HttpServletResponse;
public class MarkdownResultPage {
private final String query;
private final String answer;
private final String query;
private final String answer;
public MarkdownResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
public MarkdownResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
public void writeTo(HttpServletResponse resp) throws IOException {
if (answer == null || answer.isEmpty()) {
if (query.isBlank()) {
// Do Nothing
} else if (answer == null || answer.isEmpty()) {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("</body>");
writer.println("</html>");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("</body>");
writer.println("</html>");
} else {
} else {
File temp = new File("temp.md");
File temp = new File("temp.md");
final PrintStream out = new PrintStream(new FileOutputStream(temp));
out.printf("# %s\n\n%s\n", query, answer);
out.close();
final PrintStream out = new PrintStream(new FileOutputStream(temp));
out.printf("# %s\n\n%s\n", query, answer);
out.close();
resp.setContentType("application/octet-stream");
resp.setContentLengthLong(temp.length());
resp.setHeader("Content-Disposition", "filename=result.md");
Files.copy(temp.toPath(), resp.getOutputStream());
resp.setContentType("application/octet-stream");
resp.setContentLengthLong(temp.length());
resp.setHeader("Content-Disposition", "filename=" + query + ".md");
Files.copy(temp.toPath(), resp.getOutputStream());
temp.deleteOnExit();
temp.deleteOnExit();
}
}
}
}
......@@ -4,7 +4,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
public class PDFResultPage implements Page{
public class PDFResultPage implements Page {
private final String query;
private final String answer;
......@@ -17,7 +17,9 @@ public class PDFResultPage implements Page{
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
if (answer == null || answer.isEmpty()) {
if (query.isBlank()) {
// Do Nothing
} else if (answer == null || answer.isEmpty()) {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
......@@ -48,7 +50,7 @@ public class PDFResultPage implements Page{
}
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment;filename=result.pdf");
resp.setHeader("Content-Disposition", "attachment;filename=" + query + ".pdf");
Files.copy(pdf.toPath(), resp.getOutputStream());
temp.deleteOnExit();
......
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