Introduce custom EntityIdValues to store localized labels

This commit is contained in:
Antonin Delpeuch 2018-01-10 11:32:38 +00:00
parent 1ea1377734
commit c844742395
8 changed files with 107 additions and 35 deletions

View File

@ -3,6 +3,9 @@ package org.openrefine.wikidata.editing;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import org.openrefine.wikidata.schema.entityvalues.NewEntityIdValue;
import java.util.HashSet; import java.util.HashSet;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -12,8 +15,6 @@ import com.google.refine.model.Recon;
import com.google.refine.model.ReconCandidate; import com.google.refine.model.ReconCandidate;
import com.google.refine.model.ReconStats; import com.google.refine.model.ReconStats;
import org.openrefine.wikidata.schema.NewEntityIdValue;
/** /**
* This keeps track of the new items that we * This keeps track of the new items that we
* have created for each cell that was marked * have created for each cell that was marked

View File

@ -18,8 +18,8 @@ import org.openrefine.wikidata.editing.NewItemLibrary;
import org.openrefine.wikidata.editing.CellCoordinates; import org.openrefine.wikidata.editing.CellCoordinates;
import org.openrefine.wikidata.editing.CellCoordinatesKeyDeserializer; import org.openrefine.wikidata.editing.CellCoordinatesKeyDeserializer;
import org.openrefine.wikidata.schema.ItemUpdate; import org.openrefine.wikidata.schema.ItemUpdate;
import org.openrefine.wikidata.schema.NewEntityIdValue;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import org.openrefine.wikidata.schema.entityvalues.NewEntityIdValue;
import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl; import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
import org.wikidata.wdtk.datamodel.interfaces.DataObjectFactory; import org.wikidata.wdtk.datamodel.interfaces.DataObjectFactory;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;

View File

@ -1,6 +1,7 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.entityvalues.NewEntityIdValue;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
@ -31,8 +32,12 @@ public class WbItemVariable extends WbItemExpr {
ReconCandidate match = cell.recon.match; ReconCandidate match = cell.recon.match;
return Datamodel.makeItemIdValue(match.id, ctxt.getBaseIRI()); return Datamodel.makeItemIdValue(match.id, ctxt.getBaseIRI());
} else if (recon.judgment == Recon.Judgment.New) { } else if (recon.judgment == Recon.Judgment.New) {
return new NewEntityIdValue(ctxt.getRowId(), int rowId = ctxt.getRowId();
ctxt.getCellIndexByName(getColumnName())); int columnId = ctxt.getCellIndexByName(getColumnName());
String siteIRI = ctxt.getBaseIRI();
String label = ctxt.getCellByName(getColumnName()).value.toString();
return new NewEntityIdValue(
rowId, columnId, siteIRI, label);
} }
} }
throw new SkipStatementException(); throw new SkipStatementException();

View File

@ -1,5 +1,6 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.entityvalues.TermedPropertyIdValue;
import org.wikidata.wdtk.datamodel.implementation.PropertyIdValueImpl; import org.wikidata.wdtk.datamodel.implementation.PropertyIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
@ -26,7 +27,7 @@ public class WbPropConstant extends WbPropExpr {
@Override @Override
public PropertyIdValue evaluate(ExpressionContext ctxt) { public PropertyIdValue evaluate(ExpressionContext ctxt) {
return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI()); return new TermedPropertyIdValue(pid, ctxt.getBaseIRI(), label);
} }
public String getPid() { public String getPid() {

View File

@ -1,4 +1,4 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
@ -12,7 +12,7 @@ import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
* *
* @author antonin * @author antonin
*/ */
public class NewEntityIdValue implements ItemIdValue { public class NewEntityIdValue extends TermedItemIdValue {
private final int rowId; private final int rowId;
private final int colId; private final int colId;
@ -25,8 +25,11 @@ public class NewEntityIdValue implements ItemIdValue {
* the index of the row for the cell * the index of the row for the cell
* @param colId * @param colId
* the index of the column for the cell * the index of the column for the cell
* @param label
* the value of the cell
*/ */
public NewEntityIdValue(int rowId, int colId) { public NewEntityIdValue(int rowId, int colId, String siteIRI, String label) {
super("Q0", siteIRI, label);
this.rowId = rowId; this.rowId = rowId;
this.colId = colId; this.colId = colId;
} }
@ -61,30 +64,4 @@ public class NewEntityIdValue implements ItemIdValue {
hash = 41*hash + colId; hash = 41*hash + colId;
return hash; return hash;
} }
@Override
public String getIri() {
return getSiteIri() + getId();
}
@Override
public <T> T accept(ValueVisitor<T> valueVisitor) {
return valueVisitor.visit(this);
}
@Override
public String getEntityType() {
return ET_ITEM;
}
@Override
public String getId() {
return "Q0";
}
@Override
public String getSiteIri() {
return EntityIdValue.SITE_LOCAL;
}
} }

View File

@ -0,0 +1,57 @@
package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/**
* An EntityIdValue that holds not just the id but also
* the label as fetched by either the reconciliation interface
* or the suggester.
*
* This label will be localized depending on the language chosen
* by the user for OpenRefine's interface. Storing it lets us
* reuse it later on without having to re-fetch it.
* @author antonin
*
*/
public abstract class TermedEntityIdValue implements EntityIdValue {
private String _id;
private String _siteIRI;
private String _label;
public TermedEntityIdValue(String id, String siteIRI, String label) {
_id = id;
_siteIRI = siteIRI;
_label = label;
}
@Override
public abstract String getEntityType();
@Override
public String getId() {
return _id;
}
@Override
public String getSiteIri() {
return _siteIRI;
}
public String getLabel() {
return _label;
}
@Override
public String getIri() {
return getSiteIri() + getId();
}
@Override
public <T> T accept(ValueVisitor<T> valueVisitor) {
return valueVisitor.visit(this);
}
}

View File

@ -0,0 +1,16 @@
package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
public class TermedItemIdValue extends TermedEntityIdValue implements ItemIdValue {
public TermedItemIdValue(String id, String siteIRI, String label) {
super(id, siteIRI, label);
}
@Override
public String getEntityType() {
return ET_ITEM;
}
}

View File

@ -0,0 +1,15 @@
package org.openrefine.wikidata.schema.entityvalues;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
public class TermedPropertyIdValue extends TermedEntityIdValue implements PropertyIdValue {
public TermedPropertyIdValue(String id, String siteIRI, String label) {
super(id, siteIRI, label);
}
@Override
public String getEntityType() {
return ET_PROPERTY;
}
}