Author: cpetroaca
Date: Fri Sep 11 21:40:04 2015
New Revision: 1702559
URL: http://svn.apache.org/r1702559
Log:
Added unit tests for the entity coreference engine.
Added:
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/EntityCoReferenceEngineTest.java
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockComponentContext.java
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockEntityCorefDbpediaSite.java
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockSiteManager.java
Modified:
stanbol/trunk/enhancement-engines/entitycoreference/pom.xml
Modified: stanbol/trunk/enhancement-engines/entitycoreference/pom.xml
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/entitycoreference/pom.xml?rev=1702559&r1=1702558&r2=1702559&view=diff
==============================================================================
--- stanbol/trunk/enhancement-engines/entitycoreference/pom.xml (original)
+++ stanbol/trunk/enhancement-engines/entitycoreference/pom.xml Fri Sep 11
21:40:04 2015
@@ -107,6 +107,7 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@@ -123,6 +124,20 @@
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.entityhub.core</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.core</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
Added:
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/EntityCoReferenceEngineTest.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/EntityCoReferenceEngineTest.java?rev=1702559&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/EntityCoReferenceEngineTest.java
(added)
+++
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/EntityCoReferenceEngineTest.java
Fri Sep 11 21:40:04 2015
@@ -0,0 +1,137 @@
+package org.apache.stanbol.enhancer.engines.entitycoreference;
+
+import static
org.apache.stanbol.enhancer.servicesapi.rdf.Properties.DC_LANGUAGE;
+import static org.apache.stanbol.enhancer.servicesapi.rdf.Properties.DC_TYPE;
+import static
org.apache.stanbol.enhancer.servicesapi.rdf.Properties.ENHANCER_CONFIDENCE;
+import static
org.apache.stanbol.enhancer.servicesapi.rdf.TechnicalClasses.DCTERMS_LINGUISTIC_SYSTEM;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map.Entry;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import
org.apache.stanbol.enhancer.contentitem.inmemory.InMemoryContentItemFactory;
+import org.apache.stanbol.enhancer.nlp.NlpAnnotations;
+import org.apache.stanbol.enhancer.nlp.coref.CorefFeature;
+import org.apache.stanbol.enhancer.nlp.model.AnalysedText;
+import org.apache.stanbol.enhancer.nlp.model.AnalysedTextFactory;
+import org.apache.stanbol.enhancer.nlp.model.Chunk;
+import org.apache.stanbol.enhancer.nlp.model.Sentence;
+import org.apache.stanbol.enhancer.nlp.model.Token;
+import org.apache.stanbol.enhancer.nlp.model.annotation.Value;
+import org.apache.stanbol.enhancer.nlp.ner.NerTag;
+import org.apache.stanbol.enhancer.nlp.phrase.PhraseTag;
+import org.apache.stanbol.enhancer.nlp.pos.LexicalCategory;
+import org.apache.stanbol.enhancer.nlp.pos.Pos;
+import org.apache.stanbol.enhancer.nlp.pos.PosTag;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.EngineException;
+import org.apache.stanbol.enhancer.servicesapi.EnhancementEngine;
+import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
+import org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.apache.stanbol.enhancer.servicesapi.rdf.OntologicalClasses;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.cm.ConfigurationException;
+
+/**
+ * Main test class
+ *
+ * @author Cristian Petroaca
+ *
+ */
+public class EntityCoReferenceEngineTest {
+ private static final String SPATIAL_SENTENCE_1 = "Angela Merkel visited
China.";
+ private static final String SPATIAL_SENTENCE_2 = "The German politician
met the Chinese prime minister.";
+ private static final String SPATIAL_TEXT = SPATIAL_SENTENCE_1 +
SPATIAL_SENTENCE_2;
+
+ private static final ContentItemFactory ciFactory =
InMemoryContentItemFactory.getInstance();
+ private static final AnalysedTextFactory atFactory =
AnalysedTextFactory.getDefaultInstance();
+
+ private EntityCoReferenceEngine engine;
+
+ @Before
+ public void setUpServices() throws IOException, ConfigurationException {
+ engine = new EntityCoReferenceEngine();
+ // we need to set some fields that would otherwise be injected
by the
+ // container
+ engine.siteManager = new MockSiteManager();
+
+ Dictionary<String, Object> config = new Hashtable<String,
Object>();
+ config.put(EnhancementEngine.PROPERTY_NAME,
"entity-coreference");
+ config.put(EntityCoReferenceEngine.CONFIG_LANGUAGES, "en");
+ config.put(EntityCoReferenceEngine.REFERENCED_SITE_ID,
MockEntityCorefDbpediaSite.SITE_ID);
+ config.put(EntityCoReferenceEngine.MAX_DISTANCE, 1);
+ config.put(EntityCoReferenceEngine.ENTITY_URI_BASE,
"http://dbpedia.org/resource/");
+ config.put(EntityCoReferenceEngine.SPATIAL_ATTR_FOR_PERSON,
Constants.DEFAULT_SPATIAL_ATTR_FOR_PERSON);
+
config.put(EntityCoReferenceEngine.SPATIAL_ATTR_FOR_ORGANIZATION,
+
Constants.DEFAULT_SPATIAL_ATTR_FOR_ORGANIZATION);
+ config.put(EntityCoReferenceEngine.SPATIAL_ATTR_FOR_PLACE,
Constants.DEFAULT_SPATIAL_ATTR_FOR_PLACE);
+ config.put(EntityCoReferenceEngine.ORG_ATTR_FOR_PERSON,
Constants.DEFAULT_ORG_ATTR_FOR_PERSON);
+ config.put(EntityCoReferenceEngine.ENTITY_CLASSES_TO_EXCLUDE,
Constants.DEFAULT_ENTITY_CLASSES_TO_EXCLUDE);
+
+ engine.activate(new MockComponentContext(config));
+ }
+
+ @Test
+ public void testSpatialCoref() throws EngineException, IOException {
+ ContentItem ci = ciFactory.createContentItem(new
StringSource(SPATIAL_TEXT));
+ MGraph graph = ci.getMetadata();
+ UriRef textEnhancement =
EnhancementEngineHelper.createTextEnhancement(ci, engine);
+ graph.add(new TripleImpl(textEnhancement, DC_LANGUAGE, new
PlainLiteralImpl("en")));
+ graph.add(new TripleImpl(textEnhancement, ENHANCER_CONFIDENCE,
new PlainLiteralImpl("100.0")));
+ graph.add(new TripleImpl(textEnhancement, DC_TYPE,
DCTERMS_LINGUISTIC_SYSTEM));
+
+ Entry<UriRef, Blob> textBlob = ContentItemHelper.getBlob(ci,
Collections.singleton("text/plain"));
+ AnalysedText at = atFactory.createAnalysedText(ci,
textBlob.getValue());
+
+ Sentence sentence1 = at.addSentence(0,
SPATIAL_SENTENCE_1.indexOf(".") + 1);
+ Chunk angelaMerkel = sentence1.addChunk(0, "Angela
Merkel".length());
+ angelaMerkel.addAnnotation(NlpAnnotations.NER_ANNOTATION,
+ Value.value(new NerTag("Angela Merkel",
OntologicalClasses.DBPEDIA_PERSON)));
+
+ Sentence sentence2 =
at.addSentence(SPATIAL_SENTENCE_1.indexOf(".") + 1,
+ SPATIAL_SENTENCE_1.length() +
SPATIAL_SENTENCE_2.indexOf(".") + 1);
+ int theStartIdx = sentence2.getSpan().indexOf("The");
+ int germanStartIdx = sentence2.getSpan().indexOf("German");
+ int chancellorStartIdx =
sentence2.getSpan().indexOf("politician");
+ Token the = sentence2.addToken(theStartIdx, theStartIdx +
"The".length());
+ the.addAnnotation(NlpAnnotations.POS_ANNOTATION,
+ Value.value(new PosTag("The",
LexicalCategory.PronounOrDeterminer, Pos.Determiner)));
+
+ Token german = sentence2.addToken(germanStartIdx,
germanStartIdx + "German".length());
+ german.addAnnotation(NlpAnnotations.POS_ANNOTATION,
+ Value.value(new PosTag("German",
LexicalCategory.Adjective)));
+
+ Token politician = sentence2.addToken(chancellorStartIdx,
chancellorStartIdx + "politician".length());
+ politician.addAnnotation(NlpAnnotations.POS_ANNOTATION,
+ Value.value(new PosTag("politician",
LexicalCategory.Noun)));
+
+ Chunk theGermanChancellor = sentence2.addChunk(theStartIdx,
chancellorStartIdx + "politician".length());
+
theGermanChancellor.addAnnotation(NlpAnnotations.PHRASE_ANNOTATION,
+ Value.value(new PhraseTag("The German
politician", LexicalCategory.Noun)));
+
+ engine.computeEnhancements(ci);
+
+ Value<CorefFeature> representativeCorefValue =
angelaMerkel.getAnnotation(NlpAnnotations.COREF_ANNOTATION);
+ Assert.assertNotNull(representativeCorefValue);
+ CorefFeature representativeCoref =
representativeCorefValue.value();
+ Assert.assertTrue(representativeCoref.isRepresentative());
+
Assert.assertTrue(representativeCoref.getMentions().contains(theGermanChancellor));
+
+ Value<CorefFeature> subordinateCorefValue =
theGermanChancellor.getAnnotation(NlpAnnotations.COREF_ANNOTATION);
+ Assert.assertNotNull(subordinateCorefValue);
+ CorefFeature subordinateCoref = subordinateCorefValue.value();
+ Assert.assertTrue(!subordinateCoref.isRepresentative());
+
Assert.assertTrue(subordinateCoref.getMentions().contains(angelaMerkel));
+ }
+}
Added:
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockComponentContext.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockComponentContext.java?rev=1702559&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockComponentContext.java
(added)
+++
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockComponentContext.java
Fri Sep 11 21:40:04 2015
@@ -0,0 +1,79 @@
+package org.apache.stanbol.enhancer.engines.entitycoreference;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.ComponentInstance;
+
+/**
+ *
+ * @author Cristian Petroaca
+ *
+ */
+public class MockComponentContext implements ComponentContext {
+
+ private final Dictionary<String, Object> properties;
+
+ public MockComponentContext() {
+ properties = new Hashtable<String, Object>();
+ }
+
+ public MockComponentContext(Dictionary<String, Object> properties) {
+ this.properties = properties;
+ }
+
+ @Override
+ public Dictionary<String, Object> getProperties() {
+ return properties;
+ }
+
+ @Override
+ public Object locateService(String name) {
+ return null;
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Object locateService(String name, ServiceReference reference) {
+ return null;
+ }
+
+ @Override
+ public Object[] locateServices(String name) {
+ return null;
+ }
+
+ @Override
+ public BundleContext getBundleContext() {
+ return null;
+ }
+
+ @Override
+ public Bundle getUsingBundle() {
+ return null;
+ }
+
+ @Override
+ public ComponentInstance getComponentInstance() {
+ return null;
+ }
+
+ @Override
+ public void enableComponent(String name) {
+ }
+
+ @Override
+ public void disableComponent(String name) {
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public ServiceReference getServiceReference() {
+ return null;
+ }
+
+}
Added:
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockEntityCorefDbpediaSite.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockEntityCorefDbpediaSite.java?rev=1702559&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockEntityCorefDbpediaSite.java
(added)
+++
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockEntityCorefDbpediaSite.java
Fri Sep 11 21:40:04 2015
@@ -0,0 +1,129 @@
+package org.apache.stanbol.enhancer.engines.entitycoreference;
+
+import static
org.apache.stanbol.enhancer.servicesapi.rdf.Properties.RDFS_LABEL;
+import static org.apache.stanbol.enhancer.servicesapi.rdf.Properties.RDF_TYPE;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.stanbol.entityhub.core.model.EntityImpl;
+import org.apache.stanbol.entityhub.core.model.InMemoryValueFactory;
+import org.apache.stanbol.entityhub.core.query.DefaultQueryFactory;
+import org.apache.stanbol.entityhub.core.query.QueryResultListImpl;
+import org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper;
+import org.apache.stanbol.entityhub.servicesapi.model.Entity;
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.apache.stanbol.entityhub.servicesapi.model.Text;
+import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
+import org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory;
+import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList;
+import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
+import org.apache.stanbol.entityhub.servicesapi.site.Site;
+import org.apache.stanbol.entityhub.servicesapi.site.SiteConfiguration;
+import org.apache.stanbol.entityhub.servicesapi.site.SiteException;
+
+/**
+ *
+ * @author Cristian Petroaca
+ *
+ */
+public class MockEntityCorefDbpediaSite implements Site {
+ public static final String SITE_ID = "entity-coref-dbpedia";
+
+ private Map<String, Entity> entities;
+
+ public MockEntityCorefDbpediaSite() {
+ entities = new HashMap<>();
+ InMemoryValueFactory valueFactory =
InMemoryValueFactory.getInstance();
+
+ Representation merkelRep =
valueFactory.createRepresentation("http://dbpedia.org/page/Angela_Merkel");
+ merkelRep.set("http://dbpedia.org/ontology/country",
"http://dbpedia.org/resource/Germany");
+ merkelRep.set(RDF_TYPE.getUnicodeString(),
"http://dbpedia.org/class/yago/Politician110451263");
+ merkelRep.set(RDFS_LABEL.getUnicodeString(),
valueFactory.createText("Angela Merkel", "en"));
+
+ entities.put("http://dbpedia.org/page/Angela_Merkel", new
EntityImpl(SITE_ID, merkelRep, null));
+
+ Representation politicianRep = valueFactory
+
.createRepresentation("http://dbpedia.org/class/yago/Politician110451263");
+ politicianRep.set(RDFS_LABEL.getUnicodeString(),
valueFactory.createText("politician", "en"));
+
entities.put("http://dbpedia.org/class/yago/Politician110451263", new
EntityImpl(SITE_ID, politicianRep, null));
+
+ }
+
+ @Override
+ public String getId() {
+ return null;
+ }
+
+ @Override
+ public QueryResultList<String> findReferences(FieldQuery query) throws
SiteException {
+ return null;
+ }
+
+ @Override
+ public QueryResultList<Representation> find(FieldQuery query) throws
SiteException {
+ return null;
+ }
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public QueryResultList<Entity> findEntities(FieldQuery query) throws
SiteException {
+ TextConstraint labelConstraint = (TextConstraint)
query.getConstraint(RDFS_LABEL.getUnicodeString());
+
+ for (Entity entity : entities.values()) {
+ Iterator<Object> entityAttributes =
entity.getRepresentation().get(RDFS_LABEL.getUnicodeString());
+
+ while (entityAttributes.hasNext()) {
+ Text entityAttribute = (Text)
entityAttributes.next();
+
+ if
(entityAttribute.getText().equals(labelConstraint.getText())) {
+ Collection<Entity> retEntities = new
ArrayList<>(1);
+ retEntities.add(entity);
+ return new
QueryResultListImpl<Entity>(null, retEntities, Entity.class);
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public Entity getEntity(String id) throws SiteException {
+ return entities.get(id);
+ }
+
+ @Override
+ public InputStream getContent(String id, String contentType) throws
SiteException {
+ return null;
+ }
+
+ @Override
+ public FieldMapper getFieldMapper() {
+ return null;
+ }
+
+ @Override
+ public FieldQueryFactory getQueryFactory() {
+ return DefaultQueryFactory.getInstance();
+ }
+
+ @Override
+ public SiteConfiguration getConfiguration() {
+ return null;
+ }
+
+ @Override
+ public boolean supportsLocalMode() {
+ return false;
+ }
+
+ @Override
+ public boolean supportsSearch() {
+ return false;
+ }
+
+}
Added:
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockSiteManager.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockSiteManager.java?rev=1702559&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockSiteManager.java
(added)
+++
stanbol/trunk/enhancement-engines/entitycoreference/src/test/java/org/apache/stanbol/enhancer/engines/entitycoreference/MockSiteManager.java
Fri Sep 11 21:40:04 2015
@@ -0,0 +1,69 @@
+package org.apache.stanbol.enhancer.engines.entitycoreference;
+
+import java.io.InputStream;
+import java.util.Collection;
+
+import org.apache.stanbol.entityhub.servicesapi.model.Entity;
+import org.apache.stanbol.entityhub.servicesapi.model.Representation;
+import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
+import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList;
+import org.apache.stanbol.entityhub.servicesapi.site.Site;
+import org.apache.stanbol.entityhub.servicesapi.site.SiteManager;
+
+/**
+ *
+ * @author Cristian Petroaca
+ *
+ */
+public class MockSiteManager implements SiteManager {
+
+ @Override
+ public boolean isReferred(String id) {
+ return false;
+ }
+
+ @Override
+ public Site getSite(String id) {
+ if (id.equals(MockEntityCorefDbpediaSite.SITE_ID)) {
+ return new MockEntityCorefDbpediaSite();
+ }
+
+ return null;
+ }
+
+ @Override
+ public Collection<Site> getSitesByEntityPrefix(String entityUri) {
+ return null;
+ }
+
+ @Override
+ public Entity getEntity(String reference) {
+ return null;
+ }
+
+ @Override
+ public QueryResultList<Entity> findEntities(FieldQuery query) {
+ return null;
+ }
+
+ @Override
+ public QueryResultList<Representation> find(FieldQuery query) {
+ return null;
+ }
+
+ @Override
+ public QueryResultList<String> findIds(FieldQuery query) {
+ return null;
+ }
+
+ @Override
+ public InputStream getContent(String entity, String contentType) {
+ return null;
+ }
+
+ @Override
+ public Collection<String> getSiteIds() {
+ return null;
+ }
+
+}