use of org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory in project stanbol by apache.
the class TestLocationEnhancementEngine method getTextAnnotation.
public static void getTextAnnotation(ContentItem ci, String name, String context, IRI type) {
String content;
try {
content = IOUtils.toString(ci.getStream(), "UTF-8");
} catch (IOException e) {
//should never happen anyway!
content = "";
}
RdfEntityFactory factory = RdfEntityFactory.createInstance(ci.getMetadata());
TextAnnotation testAnnotation = factory.getProxy(new IRI("urn:org.apache:stanbol.enhancer:test:text-annotation:person"), TextAnnotation.class);
testAnnotation.setCreator(new IRI("urn:org.apache:stanbol.enhancer:test:dummyEngine"));
testAnnotation.setCreated(new Date());
testAnnotation.setSelectedText(name);
testAnnotation.setSelectionContext(context);
testAnnotation.getDcType().add(type);
Integer start = content.indexOf(name);
if (start < 0) {
//if not found in the content
//set some random numbers for start/end
start = (int) (Math.random() * 100);
}
testAnnotation.setStart(start);
testAnnotation.setEnd(start + name.length());
}
use of org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory in project stanbol by apache.
the class RdfEntityFactoryTest method testInterfaceHierarchies.
@Test
public void testInterfaceHierarchies() throws Exception {
Graph graph = new SimpleGraph();
RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
String testUri = "urn:RdfEntityFactoryTest:SubTestEntity";
String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
String testUri3 = "urn:RdfEntityFactoryTest:TestEntity";
IRI node = new IRI(testUri);
IRI node2 = new IRI(testUri2);
IRI node3 = new IRI(testUri3);
SubTestRdfEntity entity = factory.getProxy(node, SubTestRdfEntity.class);
TestRdfEntity entity2 = factory.getProxy(node2, TestRdfEntity.class, SubTestRdfEntity.class, TestRdfEntity2.class);
TestRdfEntity entity3 = factory.getProxy(node3, TestRdfEntity.class);
//Start with checking the types for entity2
//first type cast to the hierarchy
assertTrue(entity instanceof TestRdfEntity);
assertTrue(entity instanceof RdfEntity);
// test if the rdf:type triples are present in the Graph
Set<String> typeStrings = getRdfTypes(graph, node);
assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
typeStrings = null;
//now the same for entity2
//first type cast to the hierarchy
assertTrue(entity2 instanceof SubTestRdfEntity);
assertTrue(entity2 instanceof TestRdfEntity2);
assertTrue(entity2 instanceof RdfEntity);
// test if the rdf:type triples are present in the Graph
typeStrings = getRdfTypes(graph, node2);
assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
typeStrings = null;
//Now check Entity3
assertTrue(!(entity3 instanceof SubTestRdfEntity));
assertTrue(entity3 instanceof TestRdfEntity);
//Now create an new Entity for the same Node that implements SubEntity2
SubTestRdfEntity entity4 = factory.getProxy(node3, SubTestRdfEntity.class);
//check if entity4 implements SubTestRefEntity
assertTrue(entity4 instanceof SubTestRdfEntity);
//now check if the additional type was added to node3
typeStrings = getRdfTypes(graph, node3);
assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
//and that entity3 still dose not implement SubTestEntity
// ... because adding/removing rdf:type triples in the graph can not affect existing proxy instances!
assertTrue(!(entity3 instanceof SubTestRdfEntity));
}
use of org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory in project stanbol by apache.
the class RdfEntityFactoryTest method testObjectProperties.
@Test
public void testObjectProperties() throws Exception {
Graph graph = new SimpleGraph();
RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
String testUri = "urn:RdfEntityFactoryTest:TestEntity";
String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
IRI node = new IRI(testUri);
IRI node2 = new IRI(testUri2);
TestRdfEntity entity = factory.getProxy(node, TestRdfEntity.class);
TestRdfEntity2 entity2 = factory.getProxy(node2, TestRdfEntity2.class);
URI testURI = new URI("urn:test:URI");
entity.setURI(testURI);
assertEquals(testURI, entity.getURI());
URL testURL = new URL("http://www.iks-project.eu");
entity.setURL(testURL);
assertEquals(testURL, entity.getURL());
entity.setIRI(node2);
assertEquals(node2, entity.getIRI());
entity2.setTestEntity(entity);
assertEquals(entity, entity2.getTestEntity());
Collection<TestRdfEntity> testEntities = entity2.getTestEntities();
//check that entity is not in the collection
assertTrue(testEntities.isEmpty());
Set<IRI> testIRIs = new HashSet<IRI>();
int NUM = 10;
for (int i = 0; i < NUM; i++) {
IRI testNode = new IRI(testUri + ':' + '_' + i);
testIRIs.add(testNode);
testEntities.add(factory.getProxy(testNode, TestRdfEntity.class));
}
//now get a new collection and test if the added entities are there
//add to a list to check for duplicates
Collection<IRI> resultIRIs = new ArrayList<IRI>();
for (TestRdfEntity e : entity2.getTestEntities()) {
//I used IRIs for the generation ...
assertTrue(e.getId() instanceof IRI);
resultIRIs.add((IRI) e.getId());
}
//now cross check
assertTrue(testIRIs.containsAll(resultIRIs));
assertTrue(resultIRIs.containsAll(testIRIs));
//now one could try to remove some Elements ...
// ... but things like that are already tested for Integers in testPrimitiveDataTypes
}
use of org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory in project stanbol by apache.
the class TestEnhancementInterfaces method testEnhancementInterfaces.
@Test
public void testEnhancementInterfaces() throws Exception {
ContentItem ci = createContentItem(SINGLE_SENTENCE_SOURCE);
IRI ciUri = new IRI(ci.getUri().getUnicodeString());
RdfEntityFactory factory = RdfEntityFactory.createInstance(ci.getMetadata());
long start = System.currentTimeMillis();
//create an Text Annotation representing an extracted Person
TextAnnotation personAnnotation = factory.getProxy(createEnhancementURI(), TextAnnotation.class);
personAnnotation.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
personAnnotation.setCreated(new Date());
personAnnotation.setExtractedFrom(ciUri);
personAnnotation.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/text#Person"));
personAnnotation.setConfidence(0.8);
personAnnotation.setSelectedText("Patrick Marshall");
personAnnotation.setStart(SINGLE_SENTENCE.indexOf(personAnnotation.getSelectedText()));
personAnnotation.setEnd(personAnnotation.getStart() + personAnnotation.getSelectedText().length());
personAnnotation.setSelectionContext(SINGLE_SENTENCE);
//create an Text Annotation representing an extracted Location
TextAnnotation locationAnnotation = factory.getProxy(createEnhancementURI(), TextAnnotation.class);
locationAnnotation.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
locationAnnotation.setCreated(new Date());
locationAnnotation.setExtractedFrom(ciUri);
locationAnnotation.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/text#Location"));
locationAnnotation.setConfidence(0.78);
locationAnnotation.setSelectedText("New Zealand");
locationAnnotation.setStart(SINGLE_SENTENCE.indexOf(locationAnnotation.getSelectedText()));
locationAnnotation.setEnd(locationAnnotation.getStart() + locationAnnotation.getSelectedText().length());
locationAnnotation.setSelectionContext(SINGLE_SENTENCE);
//create an Text Annotation representing an extracted Organisation
TextAnnotation orgAnnotation = factory.getProxy(createEnhancementURI(), TextAnnotation.class);
orgAnnotation.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
orgAnnotation.setCreated(new Date());
orgAnnotation.setExtractedFrom(ciUri);
orgAnnotation.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/text#Organisation"));
orgAnnotation.setConfidence(0.78);
orgAnnotation.setSelectedText("University of Otago");
orgAnnotation.setStart(SINGLE_SENTENCE.indexOf(orgAnnotation.getSelectedText()));
orgAnnotation.setEnd(orgAnnotation.getStart() + orgAnnotation.getSelectedText().length());
orgAnnotation.setSelectionContext(SINGLE_SENTENCE);
// create an Entity Annotation for the person TextAnnotation
EntityAnnotation patrickMarshall = factory.getProxy(createEnhancementURI(), EntityAnnotation.class);
patrickMarshall.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
patrickMarshall.setCreated(new Date());
patrickMarshall.setExtractedFrom(ciUri);
patrickMarshall.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/entity#Entity"));
patrickMarshall.setConfidence(0.56);
patrickMarshall.getRelations().add(personAnnotation);
patrickMarshall.setEntityLabel("Patrick Marshall");
patrickMarshall.setEntityReference(new IRI("http://rdf.freebase.com/rdf/en/patrick_marshall"));
patrickMarshall.getEntityTypes().addAll(Arrays.asList(new IRI("http://rdf.freebase.com/ns/people.person"), new IRI("http://rdf.freebase.com/ns/common.topic"), new IRI("http://rdf.freebase.com/ns/education.academic")));
// and an other for New Zealand
EntityAnnotation newZealand = factory.getProxy(createEnhancementURI(), EntityAnnotation.class);
newZealand.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
newZealand.setCreated(new Date());
newZealand.setExtractedFrom(ciUri);
newZealand.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/entity#Entity"));
newZealand.setConfidence(0.98);
newZealand.getRelations().add(locationAnnotation);
newZealand.setEntityLabel("New Zealand");
newZealand.setEntityReference(new IRI("http://rdf.freebase.com/rdf/en/new_zealand"));
newZealand.getEntityTypes().addAll(Arrays.asList(new IRI("http://rdf.freebase.com/ns/location.location"), new IRI("http://rdf.freebase.com/ns/common.topic"), new IRI("http://rdf.freebase.com/ns/location.country")));
// and an other option for New Zealand
EntityAnnotation airNewZealand = factory.getProxy(createEnhancementURI(), EntityAnnotation.class);
airNewZealand.setCreator(TEST_ENHANCEMENT_ENGINE_URI);
airNewZealand.setCreated(new Date());
airNewZealand.setExtractedFrom(ciUri);
airNewZealand.getDcType().add(new IRI("http://www.example.org/cv/annotatation-types/entity#Entity"));
airNewZealand.setConfidence(0.36);
airNewZealand.getRelations().add(locationAnnotation);
airNewZealand.setEntityLabel("New Zealand");
airNewZealand.setEntityReference(new IRI("http://rdf.freebase.com/rdf/en/air_new_zealand"));
airNewZealand.getEntityTypes().addAll(Arrays.asList(new IRI("http://rdf.freebase.com/ns/business.sponsor"), new IRI("http://rdf.freebase.com/ns/common.topic"), new IRI("http://rdf.freebase.com/ns/travel.transport_operator"), new IRI("http://rdf.freebase.com/ns/aviation.airline"), new IRI("http://rdf.freebase.com/ns/aviation.aircraft_owner"), new IRI("http://rdf.freebase.com/ns/business.employer"), new IRI("http://rdf.freebase.com/ns/freebase.apps.hosts.com.appspot.acre.juggle.juggle"), new IRI("http://rdf.freebase.com/ns/business.company")));
System.out.println("creation time " + (System.currentTimeMillis() - start) + "ms");
//now test the enhancement
int numberOfTextAnnotations = checkAllTextAnnotations(ci.getMetadata());
assertEquals(3, numberOfTextAnnotations);
int numberOfEntityAnnotations = checkAllEntityAnnotations(ci.getMetadata());
assertEquals(3, numberOfEntityAnnotations);
}
use of org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory in project stanbol by apache.
the class RdfEntityFactoryTest method testTypeStatements.
@Test
public void testTypeStatements() throws Exception {
Graph graph = new SimpleGraph();
RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
String testUri = "urn:RdfEntityFactoryTest:TestEntity";
IRI node = new IRI(testUri);
TestRdfEntity entity = factory.getProxy(node, TestRdfEntity.class, new Class[] { TestRdfEntity2.class });
// test the if the proxy implements both interfaces
assertTrue(entity instanceof TestRdfEntity);
assertTrue(entity instanceof TestRdfEntity2);
Set<String> typeStrings = getRdfTypes(graph, node);
assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
}
Aggregations