Search in sources :

Example 11 with Reference

use of org.wikidata.wdtk.datamodel.interfaces.Reference in project OpenRefine by OpenRefine.

the class SnakOnlyStatementMerger method merge.

@Override
public Statement merge(Statement existing, Statement added) {
    List<SnakGroup> existingQualifiers = existing.getQualifiers();
    List<SnakGroup> addedQualifiers = added.getQualifiers();
    // flatten snak groups
    List<Snak> existingSnaks = flatten(existingQualifiers);
    List<Snak> addedSnaks = flatten(addedQualifiers);
    List<Snak> mergedSnaks = new ArrayList<>(existingSnaks);
    for (Snak addedSnak : addedSnaks) {
        boolean matchingSnakFound = mergedSnaks.stream().anyMatch(existingSnak -> match(existingSnak, addedSnak));
        if (!matchingSnakFound) {
            mergedSnaks.add(addedSnak);
        }
    }
    List<SnakGroup> groupedQualifiers = WbStatementExpr.groupSnaks(mergedSnaks);
    Claim newClaim = Datamodel.makeClaim(existing.getSubject(), existing.getMainSnak(), groupedQualifiers);
    List<Reference> references = mergeReferences(existing.getReferences(), added.getReferences());
    return Datamodel.makeStatement(newClaim, references, existing.getRank(), existing.getStatementId());
}
Also used : SomeValueSnak(org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) NoValueSnak(org.wikidata.wdtk.datamodel.interfaces.NoValueSnak) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) ArrayList(java.util.ArrayList) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim)

Example 12 with Reference

use of org.wikidata.wdtk.datamodel.interfaces.Reference in project OpenRefine by OpenRefine.

the class QuickStatementsExporter method translateStatement.

protected void translateStatement(String qid, Statement statement, String pid, boolean add, Writer writer) throws IOException {
    Claim claim = statement.getClaim();
    Snak mainSnak = claim.getMainSnak();
    String mainSnakQS = mainSnak.accept(mainSnakPrinter);
    if (!add) {
        // According to: https://www.wikidata.org/wiki/Help:QuickStatements#Removing_statements,
        // Removing statements won't be followed by qualifiers or references.
        writer.write("- ");
        writer.write(qid + mainSnakQS);
        writer.write("\n");
    } else {
        // add statements
        if (statement.getReferences().isEmpty()) {
            writer.write(qid + mainSnakQS);
            for (SnakGroup q : claim.getQualifiers()) {
                translateSnakGroup(q, false, writer);
            }
            writer.write("\n");
        } else {
            // So, to handle multiple references, we can duplicate the statement just with different references.
            for (Reference r : statement.getReferences()) {
                writer.write(qid + mainSnakQS);
                for (SnakGroup q : claim.getQualifiers()) {
                    translateSnakGroup(q, false, writer);
                }
                for (SnakGroup g : r.getSnakGroups()) {
                    translateSnakGroup(g, true, writer);
                }
                writer.write("\n");
            }
        }
    }
}
Also used : Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim)

Aggregations

Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)12 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)7 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)6 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)6 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)5 ArrayList (java.util.ArrayList)4 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)4 HashSet (java.util.HashSet)2 QAWarning (org.openrefine.wikidata.qa.QAWarning)2 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)2 Test (org.testng.annotations.Test)2 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)2 SomeValueSnak (org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak)2 Value (org.wikidata.wdtk.datamodel.interfaces.Value)2 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1