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

SourceDocument refactor for separate raw field #1054

Merged
merged 4 commits into from
Mar 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/AclAnthology.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void readNext() throws NoSuchElementException {
/**
* A document in a JSON collection.
*/
public class Document implements SourceDocument {
public class Document extends SourceDocument {
private String id;
private String contents;
private JsonNode paper;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/BibtexCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void readNext() throws NoSuchElementException {
/**
* A document in a Bibtex collection.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private String id;
private String contents;
private String type;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/CarCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void readNext() {
* The paraID serves as the id.
* See <a href="http://trec-car.cs.unh.edu/datareleases/">this reference</a> for details.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private final String paraID;
private final String paragraph;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public static Document readNextWarcRecord(DataInputStream in, String version)
* A document from the <a href="https://www.lemurproject.org/clueweb09.php/">ClueWeb09 collection</a>.
* This class derives from tools provided by CMU for reading the ClueWeb09 collection.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
public static final String WARC_VERSION = "WARC/0.18";
protected final static String NEWLINE = "\n";

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/CoreCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void readNext() throws NoSuchElementException {
/**
* A document in a Core collection.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private String id;
private String contents;
private JsonNode jsonNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;

public abstract class CovidCollectionDocument implements SourceDocument {
public abstract class CovidCollectionDocument extends SourceDocument {
private static final Logger LOG = LogManager.getLogger(CovidCollectionDocument.class);
protected String id;
protected String content;
Expand Down Expand Up @@ -76,6 +76,7 @@ public boolean indexable() {
return true;
}

@Override
public String raw() {
return raw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/HtmlCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void getNextEntry() throws IOException {
/**
* A generic document in {@code HtmlCollection}.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private String id;
private String contents;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/JsonCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void readNext() throws NoSuchElementException {
/**
* A document in a JSON collection.
*/
public static class Document implements MultifieldSourceDocument {
public static class Document extends MultifieldSourceDocument {
private String id;
private String contents;
private Map<String, String> fields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import java.util.Map;

public interface MultifieldSourceDocument extends SourceDocument {
public abstract class MultifieldSourceDocument extends SourceDocument {
/**
* Returns a map of fields associated with this document.
*
* @return a map of fields associated with this document
*/
Map<String, String> fields();
public abstract Map<String, String> fields();
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void getNextEntry() throws IOException {
* A document from the <a href="https://catalog.ldc.upenn.edu/products/LDC2008T19">New York Times
* Annotated Corpus</a>.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private final RawDocument raw;
private String id;
private String contents;
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/io/anserini/collection/SourceDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,35 @@
* Lucene {@link org.apache.lucene.document.Document}, which is the Lucene representation that
* can be directly inserted into an index.
*/
public interface SourceDocument {
public abstract class SourceDocument {
/**
* Returns the unique identifier of the document.
*
* @return the unique identifier of the document
*/
String id();
public abstract String id();

/**
* Returns the content of the document to be searched.
*
* @return the content of the document to be searched
*/
public abstract String content();

/**
* Returns the raw content of the document.
*
* @return the raw content of the document
*/
String content();
public String raw() {
return content();
};

/**
* Returns whether this document is meant to be indexed. Certain collections (e.g., ClueWeb)
* contained metadata records that aren't meant to be indexed.
*
* @return <code>true</code> if this document is meant to be indexed
*/
boolean indexable();
public abstract boolean indexable();
}
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/TrecCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void parseRecord(StringBuilder builder) {
/**
* A document in a classic TREC <i>ad hoc</i> document collection.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {

protected static final String DOCNO = "<DOCNO>";
protected static final String TERMINATING_DOCNO = "</DOCNO>";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/anserini/collection/TweetCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private boolean isFieldAvailable(Object field) {
/**
* A tweet (i.e., status).
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
// Required fields
protected String screenName;
protected int followersCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void parseRecord(String record) {
/**
* A document from the <a href="https://trec.nist.gov/data/wapost/">TREC Washington Post Corpus</a>.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private static final Logger LOG = LogManager.getLogger(Document.class);

// Required fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void readNext() {
/**
* A Wikipedia article. The article title serves as the id.
*/
public static class Document implements SourceDocument {
public static class Document extends SourceDocument {
private final String title;
private final String contents;

Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/anserini/collection/AclAnthologyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertEquals(expectedValue, aclDoc.id());
} else if (expectedKey.equals("contents")) {
assertEquals(expectedValue, aclDoc.content());
assertEquals(expectedValue, doc.raw());
} else if (expectedKey.equals("authors")) {
assertEquals(expectedValue, String.join(" ", aclDoc.authors()));
} else if (expectedKey.equals("sigs")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertEquals(expectedValue, ((BibtexCollection.Document) doc).type());
} else if (expectedKey.equals("contents")) {
assertEquals(expectedValue, doc.content());
assertEquals(expectedValue, doc.raw());
} else {
Value parsedValue = parsedFields.get(new Key(expectedKey));
assertNotNull(parsedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertEquals(expected.get("id"), doc.id());
}
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertEquals(expected.get("id"), doc.id());
}
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertEquals(expectedValue, coreDoc.id());
} else if (expectedKey.equals("contents")) {
assertEquals(expected.get("title") + " " + expected.get("abstract"), coreDoc.content());
assertEquals(expected.get("title") + " " + expected.get("abstract"), coreDoc.raw());
} else {
assert(coreDoc.jsonNode().has(expectedKey));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
assertEquals(expected.get("field1"), ((JsonCollection.Document) doc).fields().get("field1"));
assertEquals(expected.get("field2"), ((JsonCollection.Document) doc).fields().get("field2"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), nyt.id());
assertEquals(expected.get("content"), nyt.content());
assertEquals(expected.get("content"), nyt.raw());
assertEquals(expected.get("headline"), nyt.getRawDocument().getHeadline());
assertEquals(expected.get("abstract"), nyt.getRawDocument().getArticleAbstract());
assertEquals(expected.get("body"), nyt.getRawDocument().getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
assertEquals(expected.get("screen_name"), ((TweetCollection.Document) doc).getScreenName());
assertEquals((long) Long.valueOf(expected.get("timestamp_ms")),
((TweetCollection.Document) doc).getTimestampMs().getAsLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ void checkDocument(SourceDocument doc, Map<String, String> expected) {
assertTrue(doc.indexable());
assertEquals(expected.get("id"), doc.id());
assertEquals(expected.get("content"), doc.content());
assertEquals(expected.get("content"), doc.raw());
}
}