Skip to content

Commit

Permalink
Merge pull request #427 from Wikidata/mediainfo
Browse files Browse the repository at this point in the history
 Adds MediaInfoValueId and MediaInfoDocument
  • Loading branch information
Tpt committed Aug 14, 2019
2 parents a59e668 + 5614d4b commit 2055fda
Show file tree
Hide file tree
Showing 34 changed files with 1,376 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wikidata.wdtk.datamodel.helpers.JsonSerializer;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.LexemeDocument;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;

/**
Expand Down Expand Up @@ -100,6 +101,11 @@ public void processLexemeDocument(LexemeDocument lexemeDocument) {
this.serializer.processLexemeDocument(lexemeDocument);
}

@Override
public void processMediaInfoDocument(MediaInfoDocument mediaInfoDocument) {
this.serializer.processMediaInfoDocument(mediaInfoDocument);
}

@Override
public void close() {
this.serializer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.LexemeDocument;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.rdf.PropertyRegister;
import org.wikidata.wdtk.rdf.RdfSerializer;
Expand Down Expand Up @@ -196,6 +197,11 @@ public void processLexemeDocument(LexemeDocument lexemeDocument) {
this.serializer.processLexemeDocument(lexemeDocument);
}

@Override
public void processMediaInfoDocument(MediaInfoDocument mediaInfoDocument) {
this.serializer.processMediaInfoDocument(mediaInfoDocument);
}

@Override
public void close() {
this.serializer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public class Datamodel {
/**
* The site IRI of Wikidata.
*/
static public String SITE_WIKIDATA = "http://www.wikidata.org/entity/";
static final public String SITE_WIKIDATA = "http://www.wikidata.org/entity/";

/**
* The site IRI of Wikimedia Commons.
*/
static final public String SITE_WIKIMEDIA_COMMONS = "http://commons.wikimedia.org/entity/";

private final static DataObjectFactory factory = new DataObjectFactoryImpl();

Expand Down Expand Up @@ -183,6 +188,34 @@ public static SenseIdValue makeWikidataSenseIdValue(String id) {
return factory.getSenseIdValue(id, SITE_WIKIDATA);
}

/**
* Creates an {@link MediaInfoIdValue}.
*
* @param id
* a string of the form Mn... where n... is the string
* representation of a positive integer number
* @param siteIri
* IRI to identify the site, usually the first part of the entity
* IRI of the site this belongs to, e.g.,
* "http://www.wikidata.org/entity/"
* @return an {@link MediaInfoIdValue} corresponding to the input
*/
public static MediaInfoIdValue makeMediaInfoIdValue(String id, String siteIri) {
return factory.getMediaInfoIdValue(id, siteIri);
}

/**
* Creates an {@link MediaInfoIdValue} for Wikimedia Commons.
*
* @param id
* a string of the form Mn... where n... is the string
* representation of a positive integer number
* @return an {@link MediaInfoIdValue} corresponding to the input
*/
public static MediaInfoIdValue makeWikimediaCommonsMediaInfoIdValue(String id) {
return factory.getMediaInfoIdValue(id, SITE_WIKIMEDIA_COMMONS);
}

/**
* Creates a {@link DatatypeIdValue}. The datatype IRI is usually one of the
* constants defined in {@link DatatypeIdValue}, but this is not enforced,
Expand Down Expand Up @@ -810,4 +843,22 @@ public static SenseDocument makeSenseDocument(SenseIdValue senseIdValue,
return factory.getSenseDocument(senseIdValue, glosses, statementGroups, 0);
}

/**
* Creates an {@link MediaInfoDocument}.
*
* @param mediaInfoIdValue
* the id of the media that data is about
* @param labels
* the list of labels of this media, with at most one label for
* each language code
* @param statementGroups
* the list of statement groups of this media info; all of them must
* have the given mediaInfoIdValue as their subject
* @return an {@link MediaInfoDocument} corresponding to the input
*/
public static MediaInfoDocument makeMediaInfoDocument(MediaInfoIdValue mediaInfoIdValue,
List<MonolingualTextValue> labels,
List<StatementGroup> statementGroups) {
return factory.getMediaInfoDocument(mediaInfoIdValue, labels, statementGroups, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.*;

/**
* This is a utility class that allows to filter {@link ItemDocument} and {@link PropertyDocument}
* This is a utility class that allows to filter {@link EntityDocument}
* using the data declared in a {@link DocumentDataFilter}.
*/
public class DatamodelFilter {
Expand Down Expand Up @@ -63,6 +63,15 @@ public PropertyDocument filter(PropertyDocument property) {
);
}

public MediaInfoDocument filter(MediaInfoDocument mediaInfo) {
return dataObjectFactory.getMediaInfoDocument(
mediaInfo.getEntityId(),
filterMonoLingualTextValues(mediaInfo.getLabels().values()),
filterStatementGroups(mediaInfo.getStatementGroups()),
mediaInfo.getRevisionId()
);
}

public LexemeDocument filter(LexemeDocument lexeme) {
return dataObjectFactory.getLexemeDocument(
lexeme.getEntityId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,31 @@ public static boolean equalsSenseDocument(SenseDocument o1, Object o2) {
&& (o1.getRevisionId() == other.getRevisionId());
}

/**
* Returns true if the parameters are two {@link MediaInfoDocument} objects with
* exactly the same data. It does not matter if they are different
* implementations of the interface as long as their content is the same.
*
* @param o1
* the first object to compare
* @param o2
* the second object to compare
* @return true if both objects are equal
*/
public static boolean equalsMediaInfoDocument(MediaInfoDocument o1, Object o2) {
if (o2 == o1) {
return true;
}
if (!(o2 instanceof MediaInfoDocument)) {
return false;
}
MediaInfoDocument other = (MediaInfoDocument) o2;
return o1.getEntityId().equals(other.getEntityId())
&& o1.getLabels().equals(other.getLabels())
&& o1.getStatementGroups().equals(other.getStatementGroups())
&& (o1.getRevisionId() == other.getRevisionId());
}

private static boolean equalsTermedDocument(TermedDocument o1, TermedDocument other) {
return o1.getEntityId().equals(other.getEntityId())
&& o1.getAliases().equals(other.getAliases())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,21 @@ public static int hashCode(SenseDocument o) {
return result;
}

/**
* Returns a hash code for the given object.
*
* @see java.lang.Object#hashCode()
* @param o
* the object to create a hash for
* @return the hash code of the object
*/
public static int hashCode(MediaInfoDocument o) {
int result;
result = o.getLabels().hashCode();
result = prime * result + o.getStatementGroups().hashCode();
return result;
}

/**
* Returns a hash code for the given object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.compress.utils.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocumentDumpProcessor;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -54,9 +50,9 @@ public class JsonSerializer implements EntityDocumentDumpProcessor {

private static final Logger logger = LoggerFactory.getLogger(JsonSerializer.class);

private static final byte[] JSON_START_LIST = "[\n".getBytes(Charsets.UTF_8);
private static final byte[] JSON_SEP = ",\n".getBytes(Charsets.UTF_8);
private static final byte[] JSON_END_LIST = "\n]".getBytes(Charsets.UTF_8);
private static final byte[] JSON_START_LIST = "[\n".getBytes(StandardCharsets.UTF_8);
private static final byte[] JSON_SEP = ",\n".getBytes(StandardCharsets.UTF_8);
private static final byte[] JSON_END_LIST = "\n]".getBytes(StandardCharsets.UTF_8);

/**
* The stream that the resulting JSON is written to.
Expand Down Expand Up @@ -109,6 +105,16 @@ public void processPropertyDocument(PropertyDocument propertyDocument) {
serializeEntityDocument(propertyDocument);
}

@Override
public void processLexemeDocument(LexemeDocument lexemeDocument) {
serializeEntityDocument(lexemeDocument);
}

@Override
public void processMediaInfoDocument(MediaInfoDocument mediaInfoDocument) {
serializeEntityDocument(mediaInfoDocument);
}

@Override
public void close() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public static String toString(FormIdValue o) {
public static String toString(SenseIdValue o) {
return o.getIri() + " (sense)";
}
/**
* Returns a human-readable string representation of the given object.
*
* @see java.lang.Object#toString()
* @param o
* the object to represent as string
* @return a string representation of the object
*/
public static String toString(MediaInfoIdValue o) {
return o.getIri() + " (media-info)";
}

/**
* Returns a human-readable string representation of the given object.
Expand Down Expand Up @@ -537,6 +548,25 @@ public static String toString(SenseDocument o) {
return sb.toString();
}

/**
* Returns a human-readable string representation of the given object.
*
* @see java.lang.Object#toString()
* @param o
* the object to represent as string
* @return a string representation of the object
*/
public static String toString(MediaInfoDocument o) {
StringBuilder sb = new StringBuilder();
boolean first;
sb.append("==MediaInfoDocument ").append(o.getEntityId().getIri());
sb.append(" (r").append(o.getRevisionId()).append(") ");
sb.append("==");
sb.append(toStringForLabeledDocument(o));
sb.append(toStringForStatementDocument(o));
return sb.toString();
}

protected static String toStringForStatementDocument(StatementDocument o) {
StringBuilder sb = new StringBuilder();

Expand All @@ -562,18 +592,7 @@ protected static String toStringForTermedDocument(TermedDocument o) {
StringBuilder sb = new StringBuilder();
boolean first;

sb.append("\n* Labels: ");
first = true;
SortedSet<String> labelKeys = new TreeSet<>(o.getLabels()
.keySet());
for (String key : labelKeys) {
if (first) {
first = false;
} else {
sb.append("; ");
}
sb.append(toString(o.getLabels().get(key)));
}
sb.append(toStringForLabeledDocument(o));

sb.append("\n* Descriptions: ");
first = true;
Expand Down Expand Up @@ -606,6 +625,33 @@ protected static String toStringForTermedDocument(TermedDocument o) {
return sb.toString();
}

/**
* Returns a human-readable string representation of the given
* {@link LabeledDocument}.
*
* @see java.lang.Object#toString()
* @param o
* the object to represent as string
* @return a string representation of the object
*/
private static String toStringForLabeledDocument(LabeledDocument o) {
StringBuilder sb = new StringBuilder();
boolean first;
sb.append("\n* Labels: ");
first = true;
SortedSet<String> labelKeys = new TreeSet<>(o.getLabels()
.keySet());
for (String key : labelKeys) {
if (first) {
first = false;
} else {
sb.append("; ");
}
sb.append(toString(o.getLabels().get(key)));
}
return sb.toString();
}

/**
* Returns a human-readable string representation of a reference to a globe
* on which coordinates may be present. Known globes (such as Earth) are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public SenseIdValue getSenseIdValue(String id, String siteIri) {
return new SenseIdValueImpl(id, siteIri);
}

@Override
public MediaInfoIdValue getMediaInfoIdValue(String id, String siteIri) {
return new MediaInfoIdValueImpl(id, siteIri);
}

@Override
public DatatypeIdValue getDatatypeIdValue(String id) {
return new DatatypeIdImpl(id);
Expand Down Expand Up @@ -253,4 +258,15 @@ public SenseDocument getSenseDocument(SenseIdValue senseIdValue,
long revisionId) {
return new SenseDocumentImpl(senseIdValue, glosses, statementGroups, revisionId);
}


@Override
public MediaInfoDocument getMediaInfoDocument(MediaInfoIdValue mediaInfoIdValue,
List<MonolingualTextValue> labels,
List<StatementGroup> statementGroups,
long revisionId) {

return new MediaInfoDocumentImpl(
mediaInfoIdValue, labels, statementGroups, revisionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;

/**
* Abstract Jackson implementation of {@link EntityDocument}. Like all Jackson
Expand All @@ -43,6 +44,7 @@
@Type(value = LexemeDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_LEXEME),
@Type(value = FormDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_FORM),
@Type(value = SenseDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_SENSE),
@Type(value = MediaInfoDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_MEDIA_INFO),
@Type(value = PropertyDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_PROPERTY) })
public abstract class EntityDocumentImpl implements EntityDocument {

Expand All @@ -66,6 +68,10 @@ public abstract class EntityDocumentImpl implements EntityDocument {
* String used to refer to forms in JSON.
*/
static final String JSON_TYPE_SENSE = "sense";
/**
* String used to refer to forms in JSON.
*/
static final String JSON_TYPE_MEDIA_INFO = "mediainfo";

/**
* The id of the entity that the document refers to. This is not mapped to
Expand Down
Loading

0 comments on commit 2055fda

Please sign in to comment.