forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pubmed/Medline Query Transformer (JabRef#8818)
* Add Pubmed/Medline Query Transformer Supports the default search fields and boolean operators Fixes https://discourse.jabref.org/t/native-pubmed-search/3354 * checkstyle * Update SuffixTransformerTest.java
- Loading branch information
1 parent
b4e08ac
commit 74f33d6
Showing
7 changed files
with
186 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/jabref/logic/importer/fetcher/transformers/MedlineQueryTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jabref.logic.importer.fetcher.transformers; | ||
|
||
/** | ||
* | ||
* Medline/Pubmed specific transformer which uses suffixes for searches | ||
* see <a href="https://pubmed.ncbi.nlm.nih.gov/help/#search-tags">Pubmed help</a> for details | ||
* | ||
*/ | ||
public class MedlineQueryTransformer extends AbstractQueryTransformer { | ||
|
||
@Override | ||
protected String getLogicalAndOperator() { | ||
return " AND "; | ||
} | ||
|
||
@Override | ||
protected String getLogicalOrOperator() { | ||
return " OR "; | ||
} | ||
|
||
@Override | ||
protected String getLogicalNotOperator() { | ||
return "NOT "; | ||
} | ||
|
||
@Override | ||
protected String handleAuthor(String author) { | ||
return author + "[au]"; | ||
} | ||
|
||
@Override | ||
protected String handleTitle(String title) { | ||
return title + "[ti]"; | ||
} | ||
|
||
@Override | ||
protected String handleJournal(String journalTitle) { | ||
return journalTitle + "[ta]"; | ||
} | ||
|
||
@Override | ||
protected String handleYear(String year) { | ||
return year + "[dp]"; | ||
} | ||
|
||
@Override | ||
protected String handleYearRange(String yearRange) { | ||
parseYearRange(yearRange); | ||
if (endYear == Integer.MAX_VALUE) { | ||
return yearRange; | ||
} | ||
return Integer.toString(startYear) + ":" + Integer.toString(endYear) + "[dp]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/org/jabref/logic/importer/fetcher/transformers/SuffixTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.jabref.logic.importer.fetcher.transformers; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; | ||
import org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* Test Interface for all transformers that use suffix notation for their logical binary operators | ||
*/ | ||
public abstract class SuffixTransformerTest<T extends AbstractQueryTransformer> { | ||
|
||
protected abstract T getTransformer(); | ||
|
||
protected abstract String getAuthorSuffix(); | ||
|
||
protected abstract String getUnFieldedSuffix(); | ||
|
||
protected abstract String getJournalSuffix(); | ||
|
||
protected abstract String getTitleSuffix(); | ||
|
||
@Test | ||
public void convertAuthorFieldSuffix() throws Exception { | ||
String queryString = "author:\"Igor Steinmacher\""; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of("\"Igor Steinmacher\"" + getAuthorSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public void convertUnFieldedTermSuffix() throws Exception { | ||
String queryString = "\"default value\""; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of(queryString + getUnFieldedSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public void convertExplicitUnFieldedTermSuffix() throws Exception { | ||
String queryString = "default:\"default value\""; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of("\"default value\"" + getUnFieldedSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public void convertJournalFieldSuffix() throws Exception { | ||
String queryString = "journal:Nature"; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of("Nature" + getJournalSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public abstract void convertYearField() throws Exception; | ||
|
||
@Test | ||
public abstract void convertYearRangeField() throws Exception; | ||
|
||
@Test | ||
public void convertMultipleValuesWithTheSameSuffix() throws Exception { | ||
String queryString = "author:\"Igor Steinmacher\" author:\"Christoph Treude\""; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of("\"Igor Steinmacher\"" + getAuthorSuffix() + getTransformer().getLogicalAndOperator() + "\"Christoph Treude\"" + getAuthorSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public void groupedOperationsSuffix() throws Exception { | ||
String queryString = "(author:\"Igor Steinmacher\" OR author:\"Christoph Treude\" AND author:\"Christoph Freunde\") AND title:test"; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of("(" + "\"Igor Steinmacher\"" + getAuthorSuffix() + getTransformer().getLogicalOrOperator() + "(" + "\"Christoph Treude\"" + getAuthorSuffix() + getTransformer().getLogicalAndOperator() + "\"Christoph Freunde\"" + getAuthorSuffix() + "))" + getTransformer().getLogicalAndOperator() + "test" + getTitleSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
|
||
@Test | ||
public void notOperatorSufix() throws Exception { | ||
String queryString = "!(author:\"Igor Steinmacher\" OR author:\"Christoph Treude\")"; | ||
QueryNode luceneQuery = new StandardSyntaxParser().parse(queryString, AbstractQueryTransformer.NO_EXPLICIT_FIELD); | ||
Optional<String> searchQuery = getTransformer().transformLuceneQuery(luceneQuery); | ||
Optional<String> expected = Optional.of(getTransformer().getLogicalNotOperator() + "(" + "\"Igor Steinmacher\"" + getAuthorSuffix() + getTransformer().getLogicalOrOperator() + "\"Christoph Treude\")" + getAuthorSuffix()); | ||
assertEquals(expected, searchQuery); | ||
} | ||
} |