Skip to content

Commit

Permalink
Merge 2ad395c into 667fce4
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Jul 25, 2018
2 parents 667fce4 + 2ad395c commit a1076e3
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 211 deletions.
Expand Up @@ -23,8 +23,6 @@
import org.wikidata.wdtk.datamodel.interfaces.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This is a utility class that allows to filter {@link ItemDocument} and {@link PropertyDocument}
Expand All @@ -44,9 +42,9 @@ public DatamodelFilter(DataObjectFactory dataObjectFactory, DocumentDataFilter f
public ItemDocument filter(ItemDocument item) {
return dataObjectFactory.getItemDocument(
item.getEntityId(),
filterMonoLingualTextValues(item.getLabels().values().stream()),
filterMonoLingualTextValues(item.getDescriptions().values().stream()),
filterMonoLingualTextValues(item.getAliases().values().stream().flatMap(List::stream)),
filterMonoLingualTextValues(item.getLabels().values()),
filterMonoLingualTextValues(item.getDescriptions().values()),
filterMonoLingualTextValues(flatten(item.getAliases().values())),
filterStatementGroups(item.getStatementGroups()),
filterSiteLinks(item.getSiteLinks()),
item.getRevisionId()
Expand All @@ -56,9 +54,9 @@ public ItemDocument filter(ItemDocument item) {
public PropertyDocument filter(PropertyDocument property) {
return dataObjectFactory.getPropertyDocument(
property.getEntityId(),
filterMonoLingualTextValues(property.getLabels().values().stream()),
filterMonoLingualTextValues(property.getDescriptions().values().stream()),
filterMonoLingualTextValues(property.getAliases().values().stream().flatMap(List::stream)),
filterMonoLingualTextValues(property.getLabels().values()),
filterMonoLingualTextValues(property.getDescriptions().values()),
filterMonoLingualTextValues(flatten(property.getAliases().values())),
filterStatementGroups(property.getStatementGroups()),
property.getDatatype(),
property.getRevisionId()
Expand All @@ -70,44 +68,72 @@ public LexemeDocument filter(LexemeDocument lexeme) {
lexeme.getEntityId(),
lexeme.getLexicalCategory(),
lexeme.getLanguage(),
filterMonoLingualTextValues(lexeme.getLemmas().values().stream()),
filterMonoLingualTextValues(lexeme.getLemmas().values()),
filterStatementGroups(lexeme.getStatementGroups()),
lexeme.getForms().stream().map(this::filter).collect(Collectors.toList()),
lexeme.getSenses().stream().map(this::filter).collect(Collectors.toList()),
filterForms(lexeme.getForms()),
filterSenses(lexeme.getSenses()),
lexeme.getRevisionId()
);
}

public FormDocument filter(FormDocument form) {
return dataObjectFactory.getFormDocument(
form.getEntityId(),
filterMonoLingualTextValues(form.getRepresentations().values().stream()),
filterMonoLingualTextValues(form.getRepresentations().values()),
form.getGrammaticalFeatures(),
filterStatementGroups(form.getStatementGroups()),
form.getRevisionId()
);
}

public SenseDocument filter(SenseDocument form) {
public SenseDocument filter(SenseDocument sense) {
return dataObjectFactory.getSenseDocument(
form.getEntityId(),
filterMonoLingualTextValues(form.getGlosses().values().stream()),
filterStatementGroups(form.getStatementGroups()),
form.getRevisionId()
sense.getEntityId(),
filterMonoLingualTextValues(sense.getGlosses().values()),
filterStatementGroups(sense.getStatementGroups()),
sense.getRevisionId()
);
}

private List<MonolingualTextValue> filterMonoLingualTextValues(Stream<MonolingualTextValue> values) {
private List<FormDocument> filterForms(List<FormDocument> forms) {
List<FormDocument> filtered = new ArrayList<>(forms.size());
for(FormDocument form : forms) {
filtered.add(filter(form));
}
return filtered;
}

private List<SenseDocument> filterSenses(List<SenseDocument> senses) {
List<SenseDocument> filtered = new ArrayList<>(senses.size());
for(SenseDocument sense : senses) {
filtered.add(filter(sense));
}
return filtered;
}

private <T> List<T> flatten(Collection<List<T>> values) {
List<T> flattened = new ArrayList<>();
for(Collection<T> part : values) {
flattened.addAll(part);
}
return flattened;
}

private List<MonolingualTextValue> filterMonoLingualTextValues(Collection<MonolingualTextValue> values) {
if (filter.getLanguageFilter() == null) {
return values.collect(Collectors.toList());
return new ArrayList<>(values);
}
if (filter.getLanguageFilter().isEmpty()) {
return Collections.emptyList();
}

return values
.filter(value -> filter.getLanguageFilter().contains(value.getLanguageCode()))
.collect(Collectors.toList());
List<MonolingualTextValue> output = new ArrayList<>();
for(MonolingualTextValue value : values) {
if (filter.getLanguageFilter().contains(value.getLanguageCode())) {
output.add(value);
}
}
return output;
}

private List<StatementGroup> filterStatementGroups(List<StatementGroup> statementGroups) {
Expand All @@ -118,9 +144,13 @@ private List<StatementGroup> filterStatementGroups(List<StatementGroup> statemen
return Collections.emptyList();
}

return statementGroups.stream()
.filter(statementGroup-> filter.getPropertyFilter().contains(statementGroup.getProperty()))
.collect(Collectors.toList());
List<StatementGroup> output = new ArrayList<>(statementGroups.size());
for(StatementGroup statementGroup : statementGroups) {
if(filter.getPropertyFilter().contains(statementGroup.getProperty())) {
output.add(statementGroup);
}
}
return output;
}

private Map<String, SiteLink> filterSiteLinks(Map<String, SiteLink> siteLinks) {
Expand Down
Expand Up @@ -20,12 +20,11 @@
* #L%
*/

import org.wikidata.wdtk.datamodel.interfaces.*;

import java.text.DecimalFormat;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.wikidata.wdtk.datamodel.interfaces.*;

/**
* Static class for computing a toString of arbitrary data objects using only
Expand Down Expand Up @@ -439,10 +438,14 @@ public static String toString(LexemeDocument o) {
sb.append(toString(o.getLemmas().get(key)));
}
sb.append(toStringForStatementDocument(o));
sb.append("\n* Forms: \n").append(
o.getForms().stream().map(Object::toString).collect(Collectors.joining("\n")));
sb.append("\n* Senses: \n").append(
o.getSenses().stream().map(Object::toString).collect(Collectors.joining("\n")));
sb.append("\n* Forms: \n");
for(FormDocument form : o.getForms()) {
sb.append(form.toString()).append('\n');
}
sb.append("\n* Senses: \n");
for(SenseDocument sense : o.getSenses()) {
sb.append(sense.toString()).append('\n');
}
return sb.toString();
}

Expand Down Expand Up @@ -471,8 +474,10 @@ public static String toString(FormDocument o) {
}
sb.append(toString(o.getRepresentations().get(key)));
}
sb.append("\n* Grammatical features: ").append(
o.getGrammaticalFeatures().stream().map(Object::toString).collect(Collectors.joining(", ")));
sb.append("\n* Grammatical features: ");
for(ItemIdValue feature : o.getGrammaticalFeatures()) {
sb.append(feature.toString()).append(' ');
}
sb.append(toStringForStatementDocument(o));
return sb.toString();
}
Expand Down
Expand Up @@ -13,9 +13,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
@@ -1,5 +1,25 @@
package org.wikidata.wdtk.datamodel.implementation;

/*
* #%L
* Wikidata Toolkit Data Model
* %%
* Copyright (C) 2014 - 2018 Wikidata Toolkit Developers
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.wikidata.wdtk.datamodel.interfaces.*;

import java.util.*;
import java.util.stream.Collectors;


/**
* Jackson implementation of {@link FormDocument}.
Expand Down Expand Up @@ -96,10 +96,7 @@ public class FormDocumentImpl extends StatementDocumentImpl implements FormDocum
this.representations = representations;
this.grammaticalFeatures = (grammaticalFeatures == null || grammaticalFeatures.isEmpty())
? Collections.emptyList()
: grammaticalFeatures.stream()
.sorted()
.map(id -> new ItemIdValueImpl(id, siteIri))
.collect(Collectors.toList());
: constructGrammaticalFeatures(grammaticalFeatures, siteIri);
}

/**
Expand Down Expand Up @@ -131,6 +128,14 @@ private static Map<String, MonolingualTextValue> constructTermMap(List<Monolingu
return map;
}

private List<ItemIdValue> constructGrammaticalFeatures(List<String> grammaticalFeatures, String siteIri) {
List<ItemIdValue> output = new ArrayList<>(grammaticalFeatures.size());
for(String grammaticalFeature : grammaticalFeatures) {
output.add(new ItemIdValueImpl(grammaticalFeature, siteIri));
}
return output;
}

@JsonIgnore
@Override
public FormIdValue getEntityId() {
Expand All @@ -148,9 +153,11 @@ List<String> getJsonGrammaticalFeatures() {
if (grammaticalFeatures.isEmpty()) {
return Collections.emptyList();
}
return grammaticalFeatures.stream()
.map(EntityIdValue::getId)
.collect(Collectors.toList());
List<String> output = new ArrayList<>(grammaticalFeatures.size());
for(ItemIdValue feature : grammaticalFeatures) {
output.add(feature.getId());
}
return output;
}

@JsonProperty("type")
Expand Down
Expand Up @@ -20,10 +20,8 @@
* #L%
*/

import java.math.BigDecimal;
import java.util.Optional;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.helpers.Equality;
import org.wikidata.wdtk.datamodel.helpers.Hash;
Expand All @@ -32,7 +30,7 @@
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;

/**
* Jackson implementation of {@link QuantityValue}.
Expand Down Expand Up @@ -122,12 +120,12 @@ public String getUnit() {

@JsonIgnore
@Override
public Optional<ItemIdValue> getUnitItemId() {
public ItemIdValue getUnitItemId() {
String unit = this.value.getUnit();
if(unit.equals("1")) {
return Optional.empty();
return null;
} else {
return Optional.of(ItemIdValueImpl.fromIri(unit));
return ItemIdValueImpl.fromIri(unit);
}
}

Expand Down
@@ -1,7 +1,5 @@
package org.wikidata.wdtk.datamodel.implementation;

import java.util.*;

/*
* #%L
* Wikidata Toolkit Data Model
Expand All @@ -22,8 +20,10 @@
* #L%
*/

import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.wikidata.wdtk.datamodel.helpers.Equality;
import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;
Expand All @@ -32,10 +32,7 @@
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.util.NestedIterator;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.*;

/**
* Jackson implementation of {@link Reference}.
Expand Down Expand Up @@ -71,11 +68,13 @@ public class ReferenceImpl implements Reference {
* the snaks group which form the reference
*/
public ReferenceImpl(List<SnakGroup> groups) {
this.propertyOrder = groups.stream()
.map(g -> g.getProperty().getId())
.collect(Collectors.toList());
this.snaks = groups.stream()
.collect(Collectors.toMap(g -> g.getProperty().getId(), SnakGroup::getSnaks));
propertyOrder = new ArrayList<>(groups.size());
snaks = new HashMap<>(groups.size());

for(SnakGroup group : groups) {
propertyOrder.add(group.getProperty().getId());
snaks.put(group.getProperty().getId(), group.getSnaks());
}
}

/**
Expand Down
Expand Up @@ -28,8 +28,11 @@
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
* Jackson implementation of {@link SenseDocument}.
Expand Down

0 comments on commit a1076e3

Please sign in to comment.