Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jobergum/bolding control #335

Merged
merged 3 commits into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,6 +14,7 @@
</chain>
<chain id="default" inherits="vespa">
<searcher id="ai.vespa.example.cord19.searcher.RelatedArticlesByNNSearcher" bundle="cord-19"/>
<searcher id="ai.vespa.example.cord19.searcher.BoldingSearcher" bundle="cord-19"/>
</chain>

</search>
Expand Down
@@ -0,0 +1,42 @@
package ai.vespa.example.cord19.searcher;
import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PhraseItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;

/**
* This searcher traverse the query tree and looks for known stopwords, the stopwords are annotated with filter which will avoid
* bolding them in the search result
*/

public class BoldingSearcher extends Searcher {



@Override
public Result search(Query query, Execution execution) {
setFilterForStopWords(query.getModel().getQueryTree().getRoot());
return execution.search(query);
}

private void setFilterForStopWords(Item item) {
if (item instanceof WordItem) {
String word = ((WordItem)item).getWord();
word = word.toLowerCase();
if (RelatedArticlesByWeakAndSearcher.stopwords.contains(word) ) {
item.setFilter(true);
}
} else if (item instanceof PhraseItem) {
return;
}
else if (item instanceof CompositeItem) {
CompositeItem cItem = (CompositeItem)item;
for (Item i : cItem.items())
setFilterForStopWords(i);
}
}
}
Expand Up @@ -97,7 +97,7 @@ private void addWeakAndItem(Article article, boolean includeAbstract, Query quer
query.getModel().getQueryTree().setRoot(andItem);
}

private static Set<String> stopwords = Set.of(
protected static Set<String> stopwords = Set.of(
"i", "me", "my", "myself", "we", "our", "ours", "ourselves",
"you", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers",
"herself", "it", "its", "itself", "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom",
Expand Down
@@ -0,0 +1,34 @@
package ai.vespa.example.cord19.searcher;

import com.yahoo.component.chain.Chain;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BoldingSearcherTest {

@Test
public void testThatStopWordIsAnnotedAsFilter(){
Query input = new Query("?query=basic+reproduction+numbers+For+covid-19+IN+%22south+korea%22&type=any");
Result result = execute(input, new BoldingSearcher());
assertEquals("OR basic reproduction numbers |For (AND covid 19) |IN \"south korea\"",
result.getQuery().getModel().getQueryTree().toString());

}
@Test
public void testThatPhraseUntouched(){
Query input = new Query("?query=temperature+%22impact+on%22+viral+transmission&type=any");
Result result = execute(input, new BoldingSearcher());
assertEquals("OR temperature \"impact on\" viral transmission",
result.getQuery().getModel().getQueryTree().toString());

}

private Result execute(Query query, Searcher... searcher) {
Execution execution = new Execution(new Chain<>(searcher), Execution.Context.createContextStub());
return execution.search(query);
}
}