Skip to content

Commit

Permalink
Merge ebbc2fa into e05fc92
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Oct 22, 2019
2 parents e05fc92 + ebbc2fa commit c599342
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 13 deletions.
Expand Up @@ -805,6 +805,9 @@ public static LexemeDocument makeLexemeDocument(LexemeIdValue lexemeIdValue,
/**
* Creates an {@link FormDocument}.
*
* If you plan to add this form to a specific lexeme,
* it might be easier to use {@link LexemeDocument#createForm}.
*
* @param formIdValue
* the id of the form that data is about
* @param representations
Expand All @@ -827,6 +830,9 @@ public static FormDocument makeFormDocument(FormIdValue formIdValue,
/**
* Creates an {@link SenseDocument}.
*
* If you plan to add this sense to a specific lexeme,
* it might be easier to use {@link LexemeDocument#createSense)}.
*
* @param senseIdValue
* the id of the form that data is about
* @param glosses
Expand Down
Expand Up @@ -28,11 +28,9 @@
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Jackson implementation of {@link LexemeDocument}.
Expand All @@ -52,6 +50,10 @@ public class LexemeDocumentImpl extends StatementDocumentImpl implements LexemeD

private final List<SenseDocument> senses;

private int nextFormId;

private int nextSenseId;

/**
* Constructor.
*
Expand Down Expand Up @@ -96,6 +98,9 @@ public class LexemeDocumentImpl extends StatementDocumentImpl implements LexemeD
this.lemmas = constructTermMap(lemmas);
this.forms = (forms == null) ? Collections.emptyList() : forms;
this.senses = (senses == null) ? Collections.emptyList() : senses;

nextFormId = nextChildEntityId(this.forms);
nextSenseId = nextChildEntityId(this.senses);
}

/**
Expand Down Expand Up @@ -125,6 +130,9 @@ public class LexemeDocumentImpl extends StatementDocumentImpl implements LexemeD
this.lemmas = lemmas;
this.forms = (forms == null) ? Collections.emptyList() : forms;
this.senses = (senses == null) ? Collections.emptyList() : senses;

nextFormId = nextChildEntityId(this.forms);
nextSenseId = nextChildEntityId(this.senses);
}

/**
Expand All @@ -138,13 +146,17 @@ private LexemeDocumentImpl(
Map<String, List<Statement>> statements,
List<FormDocument> forms,
List<SenseDocument> senses,
long revisionId) {
long revisionId,
int nextFormId,
int nextSenseId) {
super(id, statements, revisionId);
this.lexicalCategory = lexicalCategory;
this.language = language;
this.lemmas = lemmas;
this.forms = forms;
this.senses = senses;
this.nextFormId = nextFormId;
this.nextSenseId = nextSenseId;
}

private static Map<String, MonolingualTextValue> constructTermMap(List<MonolingualTextValue> terms) {
Expand All @@ -161,6 +173,21 @@ private static Map<String, MonolingualTextValue> constructTermMap(List<Monolingu
return map;
}

private static final Pattern CHILD_ID_PATTERN = Pattern.compile("^L\\d+-[FS]([1-9]\\d*)$");

private static int nextChildEntityId(List<? extends EntityDocument> childrenDocuments) {
int maxId = 0;
for(EntityDocument document : childrenDocuments) {
Matcher matcher = CHILD_ID_PATTERN.matcher(document.getEntityId().getId());
if(matcher.matches()) {
maxId = Math.max(maxId, Integer.parseInt(matcher.group(1)));
} else {
throw new IllegalArgumentException("Invalid child entity id " + document.getEntityId());
}
}
return maxId + 1;
}

@JsonIgnore
@Override
public LexemeIdValue getEntityId() {
Expand Down Expand Up @@ -248,40 +275,89 @@ public String toString() {
@Override
public LexemeDocument withLexicalCategory(ItemIdValue newLexicalCategory) {
return new LexemeDocumentImpl(getEntityId(), newLexicalCategory,
language, lemmas, claims, forms, senses, revisionId);
language, lemmas, claims, forms, senses,
revisionId, nextFormId, nextSenseId);
}

@Override
public LexemeDocument withLanguage(ItemIdValue newLanguage) {
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
newLanguage, lemmas, claims, forms, senses, revisionId);
newLanguage, lemmas, claims, forms, senses,
revisionId, nextFormId, nextSenseId);
}

@Override
public LexemeDocument withLemma(MonolingualTextValue lemma) {
Map<String, MonolingualTextValue> newLemmas = new HashMap<>(lemmas);
newLemmas.put(lemma.getLanguageCode(), lemma);
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, newLemmas, claims, forms, senses, revisionId);
language, newLemmas, claims, forms, senses,
revisionId, nextFormId, nextSenseId);
}

@Override
public LexemeDocument withStatement(Statement statement) {
Map<String, List<Statement>> newGroups = addStatementToGroups(statement, claims);
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, lemmas, newGroups, forms, senses, revisionId);
language, lemmas, newGroups, forms, senses,
revisionId, nextFormId, nextSenseId);
}

@Override
public LexemeDocument withoutStatementIds(Set<String> statementIds) {
Map<String, List<Statement>> newGroups = removeStatements(statementIds, claims);
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, lemmas, newGroups, forms, senses, revisionId);
language, lemmas, newGroups, forms, senses,
revisionId, nextFormId, nextSenseId);
}


@Override
public FormDocument createForm(List<MonolingualTextValue> representations) {
FormIdValue newFormId = new FormIdValueImpl(entityId + "-F" + nextFormId, siteIri);
nextFormId++;

return new FormDocumentImpl(newFormId, representations, Collections.emptyList(),
Collections.emptyList(), revisionId);
}

@Override
public LexemeDocument withForm(FormDocument form) {
if(!form.getEntityId().getLexemeId().equals(getEntityId())) {
throw new IllegalArgumentException("The form " + form.getEntityId() + " does not belong to lexeme " + getEntityId());
}

List<FormDocument> newForms = new ArrayList<>(forms);
newForms.add(form);
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, lemmas, claims, newForms, senses,
revisionId, nextFormId, nextSenseId);
}

@Override
public SenseDocument createSense(List<MonolingualTextValue> glosses) {
SenseIdValue newSenseId = new SenseIdValueImpl(entityId + "-S" + nextSenseId, siteIri);
nextSenseId++;

return new SenseDocumentImpl(newSenseId, glosses, Collections.emptyList(), revisionId);
}

@Override
public LexemeDocument withSense(SenseDocument sense) {
if(!sense.getEntityId().getLexemeId().equals(getEntityId())) {
throw new IllegalArgumentException("The sense " + sense.getEntityId() + " does not belong to lexeme " + getEntityId());
}

List<SenseDocument> newSenses = new ArrayList<>(senses);
newSenses.add(sense);
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, lemmas, claims, forms, newSenses,
revisionId, nextFormId, nextSenseId);
}

@Override
public LexemeDocument withRevisionId(long newRevisionId) {
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, lemmas, claims, forms, senses, newRevisionId);
language, lemmas, claims, forms, senses,
newRevisionId, nextFormId, nextSenseId);
}
}
Expand Up @@ -127,4 +127,31 @@ public interface LexemeDocument extends StatementDocument {
*/
@Override
LexemeDocument withoutStatementIds(Set<String> statementIds);

/**
* Creates a new {@link FormDocument} for this lexeme.
* The form is not added to the {@link LexemeDocument} object,
* it should be done with {@link LexemeDocument#withForm}.
*/
FormDocument createForm(List<MonolingualTextValue> representations);

/**
* Adds a {@link FormDocument} to this lexeme.
* The form id should be prefixed with the lexeme id.
*/
LexemeDocument withForm(FormDocument form);

/**
* Creates a new {@link SenseDocument} for this Lexeme.
* The form is not added to the {@link LexemeDocument} object,
* it should be done with {@link LexemeDocument#withSense}.
*/
SenseDocument createSense(List<MonolingualTextValue> glosses);

/**
* Adds a {@link SenseDocument} to this lexeme.
* The sense id should be prefixed with the lexeme id.
*/
LexemeDocument withSense(SenseDocument sense);

}
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.helpers.DatamodelMapper;
import org.wikidata.wdtk.datamodel.interfaces.*;

Expand Down Expand Up @@ -211,6 +212,20 @@ public void testWithRevisionId() {
assertEquals(ld1, ld1.withRevisionId(1325L).withRevisionId(ld1.getRevisionId()));
}

@Test
public void testWithLexicalCategory() {
ItemIdValue newLexicalCategory = new ItemIdValueImpl("Q142", "http://example.com/entity/");
LexemeDocument withLexicalCategory = ld1.withLexicalCategory(newLexicalCategory);
assertEquals(newLexicalCategory, withLexicalCategory.getLexicalCategory());
}

@Test
public void testWithLanguage() {
ItemIdValue newLanguage = new ItemIdValueImpl("Q242", "http://example.com/entity/");
LexemeDocument withLanguage = ld1.withLanguage(newLanguage);
assertEquals(newLanguage, withLanguage.getLanguage());
}

@Test
public void testWithLemmaInNewLanguage() {
MonolingualTextValue newLemma = new MonolingualTextValueImpl("Foo", "fr");
Expand Down Expand Up @@ -240,6 +255,40 @@ public void testDeleteStatements() {
assertNotEquals(withoutStatement, ld1);
}

@Test
public void testWithForm() {
FormDocument newForm = ld1.createForm(Collections.singletonList(new TermImpl("en", "add1")));
assertEquals(lid, newForm.getEntityId().getLexemeId());
assertEquals(ld1.getForms().size() + 1, ld1.withForm(newForm).getForms().size());
assertEquals(newForm, ld1.withForm(newForm).getForm(newForm.getEntityId()));
}
@Test(expected = IllegalArgumentException.class)
public void testWithWrongFormId() {
ld1.withForm(Datamodel.makeFormDocument(
Datamodel.makeFormIdValue("L444-F32","http://example.com/entity/"),
Collections.singletonList(new TermImpl("en", "add1")),
Collections.emptyList(),
Collections.emptyList()
));
}

@Test
public void testWithSense() {
SenseDocument newSense = ld1.createSense(Collections.singletonList(new TermImpl("en", "add1")));
assertEquals(lid, newSense.getEntityId().getLexemeId());
assertEquals(ld1.getSenses().size() + 1, ld1.withSense(newSense).getSenses().size());
assertEquals(newSense, ld1.withSense(newSense).getSense(newSense.getEntityId()));
}

@Test(expected = IllegalArgumentException.class)
public void testWithWrongSenseId() {
ld1.withSense(Datamodel.makeSenseDocument(
Datamodel.makeSenseIdValue("L444-S32","http://example.com/entity/"),
Collections.singletonList(new TermImpl("en", "add1")),
Collections.emptyList()
));
}

@Test
public void testLexemeToJson() throws JsonProcessingException {
JsonComparator.compareJsonStrings(JSON_LEXEME, mapper.writeValueAsString(ld1));
Expand Down

0 comments on commit c599342

Please sign in to comment.