Commit c74764c3 authored by Zhili Tian's avatar Zhili Tian
Browse files

fix powerful search

parent 2ef207fd
Pipeline #174069 passed with stages
in 3 minutes and 54 seconds
......@@ -2,7 +2,7 @@ package ic.doc;
public class QueryProcessor {
private boolean containsAll(String answer, String[] queryArr) {
public boolean containsAll(String answer, String[] queryArr) {
for (String str :queryArr) {
if (!answer.contains(str)) return false;
}
......@@ -14,22 +14,24 @@ public class QueryProcessor {
String [] queryArr = query.toLowerCase().split("\\s+");
StringBuilder results = new StringBuilder();
if (containsAll("william shakespeare's life introduction", queryArr)
&& containsAll(query, new String[]{"william", "shakespeare"})) {
if (containsAll("shakespeare's life introduction", queryArr)
&& 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());
return results.toString();
}
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" +
if (containsAll("shakespeare's poems", queryArr)
&& query.toLowerCase().contains("shakespeare")) {
results.append("In 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());
return results.toString();
}
if (query.toLowerCase().contains("asimov")) {
......@@ -37,6 +39,7 @@ public class QueryProcessor {
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
return results.toString();
}
if (query.toLowerCase().contains("trump")) {
......@@ -44,7 +47,9 @@ public class QueryProcessor {
"was the 45th president of the United States from 2017 to 2021. Before\n" +
"entering politics, he was a businessman and television personality.\n");
results.append((System.lineSeparator()));
return results.toString();
}
return results.toString();
}
}
......@@ -17,7 +17,7 @@ public class QueryProcessorTest {
@Test
public void knowsAboutShakespeare() throws Exception {
assertThat(queryProcessor.process("Shakespeare"), containsString("playwright"));
assertThat(queryProcessor.process("Shakespeare Introduction"), containsString("playwright"));
}
@Test
......@@ -32,6 +32,11 @@ public class QueryProcessorTest {
@Test
public void isNotCaseSensitive() throws Exception {
assertThat(queryProcessor.process("shakespeare"), containsString("playwright"));
assertThat(queryProcessor.process("Shakespeare"), containsString("playwright"));
}
@Test
public void knowsAboutShakespearePoem() throws Exception {
assertThat(queryProcessor.process("Shakespeare Poem"), containsString("The Rape of Lucrece"));
}
}
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