From c84474239502adf038d30fe0a53cdc4d6dcc8974 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 10 Jan 2018 11:32:38 +0000 Subject: [PATCH] Introduce custom EntityIdValues to store localized labels --- .../wikidata/editing/NewItemLibrary.java | 5 +- .../PerformWikibaseEditsOperation.java | 2 +- .../wikidata/schema/WbItemVariable.java | 9 ++- .../wikidata/schema/WbPropConstant.java | 3 +- .../{ => entityvalues}/NewEntityIdValue.java | 35 ++---------- .../entityvalues/TermedEntityIdValue.java | 57 +++++++++++++++++++ .../entityvalues/TermedItemIdValue.java | 16 ++++++ .../entityvalues/TermedPropertyIdValue.java | 15 +++++ 8 files changed, 107 insertions(+), 35 deletions(-) rename extensions/wikidata/src/org/openrefine/wikidata/schema/{ => entityvalues}/NewEntityIdValue.java (71%) create mode 100644 extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedEntityIdValue.java create mode 100644 extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedItemIdValue.java create mode 100644 extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedPropertyIdValue.java diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java index be2eb0c55..0b57617c9 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java @@ -3,6 +3,9 @@ package org.openrefine.wikidata.editing; import java.util.Map; import java.util.HashMap; import java.util.Set; + +import org.openrefine.wikidata.schema.entityvalues.NewEntityIdValue; + import java.util.HashSet; 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.ReconStats; -import org.openrefine.wikidata.schema.NewEntityIdValue; - /** * This keeps track of the new items that we * have created for each cell that was marked diff --git a/extensions/wikidata/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperation.java b/extensions/wikidata/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperation.java index be594e3d2..5ba5d6a39 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperation.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperation.java @@ -18,8 +18,8 @@ import org.openrefine.wikidata.editing.NewItemLibrary; import org.openrefine.wikidata.editing.CellCoordinates; import org.openrefine.wikidata.editing.CellCoordinatesKeyDeserializer; import org.openrefine.wikidata.schema.ItemUpdate; -import org.openrefine.wikidata.schema.NewEntityIdValue; 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.interfaces.DataObjectFactory; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java index 500054896..ffebf152b 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java @@ -1,6 +1,7 @@ package org.openrefine.wikidata.schema; +import org.openrefine.wikidata.schema.entityvalues.NewEntityIdValue; import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; @@ -31,8 +32,12 @@ public class WbItemVariable extends WbItemExpr { ReconCandidate match = cell.recon.match; return Datamodel.makeItemIdValue(match.id, ctxt.getBaseIRI()); } else if (recon.judgment == Recon.Judgment.New) { - return new NewEntityIdValue(ctxt.getRowId(), - ctxt.getCellIndexByName(getColumnName())); + int rowId = ctxt.getRowId(); + 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(); diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java index 6aa60d273..4da1cfa06 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java @@ -1,5 +1,6 @@ package org.openrefine.wikidata.schema; +import org.openrefine.wikidata.schema.entityvalues.TermedPropertyIdValue; import org.wikidata.wdtk.datamodel.implementation.PropertyIdValueImpl; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; @@ -26,7 +27,7 @@ public class WbPropConstant extends WbPropExpr { @Override public PropertyIdValue evaluate(ExpressionContext ctxt) { - return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI()); + return new TermedPropertyIdValue(pid, ctxt.getBaseIRI(), label); } public String getPid() { diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/NewEntityIdValue.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/NewEntityIdValue.java similarity index 71% rename from extensions/wikidata/src/org/openrefine/wikidata/schema/NewEntityIdValue.java rename to extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/NewEntityIdValue.java index 13adcdc84..181a8b402 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/NewEntityIdValue.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/NewEntityIdValue.java @@ -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.ItemIdValue; @@ -12,7 +12,7 @@ import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; * * @author antonin */ -public class NewEntityIdValue implements ItemIdValue { +public class NewEntityIdValue extends TermedItemIdValue { private final int rowId; private final int colId; @@ -25,8 +25,11 @@ public class NewEntityIdValue implements ItemIdValue { * the index of the row for the cell * @param colId * 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.colId = colId; } @@ -61,30 +64,4 @@ public class NewEntityIdValue implements ItemIdValue { hash = 41*hash + colId; return hash; } - - @Override - public String getIri() { - return getSiteIri() + getId(); - } - - @Override - public T accept(ValueVisitor 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; - } - } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedEntityIdValue.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedEntityIdValue.java new file mode 100644 index 000000000..e914d4272 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedEntityIdValue.java @@ -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 accept(ValueVisitor valueVisitor) { + return valueVisitor.visit(this); + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedItemIdValue.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedItemIdValue.java new file mode 100644 index 000000000..f11926fd3 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedItemIdValue.java @@ -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; + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedPropertyIdValue.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedPropertyIdValue.java new file mode 100644 index 000000000..0fe6d4468 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/entityvalues/TermedPropertyIdValue.java @@ -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; + } +}