Hi Philip,
Hi All,
I've just copied code from KIM web page: http://www.ontotext.com/kim/doc/sys-
doc/ExploreEntitiesExamples.html
but neither EntityQueryResult nor SemanticQueryResult I can use... the first
I cannot find in Sesame libraries, the second generates runtime error.
Please, check them... I've attached the two version.... I just want to try
Exploring Entities Examples....
Thanks so much,
greetings,
Valentina
/*
* To view the entities for a document and their properties the document has to
be
* annotated first. For every document annotation the 'inst' feature gives an
URI
* to a resource holding an entity. The example below uses a SeRQL query to get
the
* main label of each entity, it counts how many times the current entity
occurred
* in the annotated document and fills a map with labels and counts that is
viewed
* at the end of the example.
* */
import com.ontotext.kim.client.KIMService;
import com.ontotext.kim.client.GetService;
import com.ontotext.kim.client.corpora.CorporaAPI;
import com.ontotext.kim.client.documentrepository.DocumentRepositoryAPI;
import com.ontotext.kim.client.semanticannotation.SemanticAnnotationAPI;
import com.ontotext.kim.client.query.QueryAPI;
import com.ontotext.kim.client.query.SemanticQueryResult;
import com.ontotext.kim.client.semanticrepository.SemanticRepositoryAPI;
import com.ontotext.kim.client.coredb.CoreDbAPI;
import org.openrdf.model.Resource;
import org.openrdf.model.impl.BNodeImpl;
import org.openrdf.model.impl.URIImpl;
import com.ontotext.kim.client.corpora.KIMDocument;
import com.ontotext.kim.client.corpora.KIMFeatureMap;
import com.ontotext.kim.client.corpora.KIMFeatureMapImpl;
import com.ontotext.kim.client.corpora.KIMAnnotation;
import com.ontotext.kim.client.corpora.KIMAnnotationSet;
import com.ontotext.kim.client.corpora.KIMCorporaException;
import com.ontotext.kim.client.model.FeatureConstants;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import java.util.List;
import java.util.HashSet;
import java.util.Vector;
import java.io.*;
import java.util.Map;
import java.util.HashMap;
public class Decoder2 {
public static final String RMI_HOST= "localhost";
public static final int RMI_PORT= 1099;
public static final String start= "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>";
public static void main(String[] args) {
try {
// connessione al KIMservice
//KIMService serviceKim= GetService.from(RMI_HOST, RMI_PORT);
KIMService serviceKim= GetService.from();
System.out.println("KIM Server connesso.");
// otteniamo componenti CorporaAPI e SemanticAnnotationAPI
CorporaAPI apiCorpora= serviceKim.getCorporaAPI();
System.out.println("CorporaAPI ottenuto con successo.");
SemanticAnnotationAPI apiSemAnn= serviceKim.getSemanticAnnotationAPI();
System.out.println("SemanticAnnotationAPI ottenuto con successo.");
// DocumentRepositoryAPI apiDR = serviceKim.getDocumentRepositoryAPI();
// spit("DocumentRepositoryAPI ottenuto con successo.");
// QueryAPI apiQuery = serviceKim.getQueryAPI();
// spit("QueryAPI ottenuto con successo.");
SemanticRepositoryAPI apiSesame =
serviceKim.getSemanticRepositoryAPI();
System.out.println("SemanticRepositoryAPI ottenuto con successo.");
// CoreDbAPI apiCoreDb = serviceKim.getCoreDbAPI();
// spit("CoreDbAPI ottenuto con successo.");
// create and annotate document
URL url2= new URL ("http://www.ontotext.com/kim");
KIMDocument kdoc= apiCorpora.createDocument(url2, "UTF-8");
apiSemAnn.execute(kdoc);
// view document's entities
KIMAnnotationSet kimASet= kdoc.getAnnotations();
Iterator annIterator= kimASet.iterator();
System.out.println("--> Entità del documento...");
Map entCount= new HashMap();
while( annIterator.hasNext()) {
// get annotation and annotations features
KIMAnnotation kimAnnotation = (KIMAnnotation)
annIterator.next();
KIMFeatureMap kimFeatures = kimAnnotation.getFeatures();
if(kimFeatures != null) {
String resourceUri = (String)
kimFeatures.get(FeatureConstants.INSTANCE);
Resource uriRes = null;
if(resourceUri.startsWith("_")) {
// anonymous resource
uriRes= new BNodeImpl(resourceUri);
}
else {
uriRes= new URIImpl(resourceUri);
}
// get the labels of the entities
SemanticQueryResult properties=
apiSesame.evaluateSelectSeRQL("select distinct MainLabel "
+ "from {<" + uriRes.toString() + ">} <" +
URIImpl.RDFS_LABEL.getURI()
+ "> {MainLabel}");
// count entities and fill a map with number and names
while(((Iterator) properties).hasNext()){
String curLabel = ((List) ((Iterator)
properties).next()).get(0).toString();
if(entCount.containsKey(curLabel)){
Long prev= (Long) entCount.get(curLabel);
entCount.put(curLabel, new Long
(prev.longValue()+1));
}else {
entCount.put(curLabel, new Long(1));
}
}
}
}
// view entities with count and name
Iterator cntIterator= entCount.keySet().iterator();
while(cntIterator.hasNext()) {
String entLabel= cntIterator.next().toString();
long entOccurs= ((Long ) entCount.get(entLabel)).longValue();
System.out.println("num: " + entOccurs + "\t\t name: " + entLabel);
}
System.out.println("\n");
} catch (Exception e) {e.printStackTrace();}
}
}/*
* To view the entities for a document and their properties the document has to
be
* annotated first. For every document annotation the 'inst' feature gives an
URI
* to a resource holding an entity. The example below uses a SeRQL query to get
the
* main label of each entity, it counts how many times the current entity
occurred
* in the annotated document and fills a map with labels and counts that is
viewed
* at the end of the example.
* */
import com.ontotext.kim.client.KIMService;
import com.ontotext.kim.client.GetService;
import com.ontotext.kim.client.corpora.CorporaAPI;
import com.ontotext.kim.client.documentrepository.DocumentRepositoryAPI;
import com.ontotext.kim.client.semanticannotation.SemanticAnnotationAPI;
import com.ontotext.kim.client.query.QueryAPI;
import com.ontotext.kim.client.semanticrepository.SemanticRepositoryAPI;
import com.ontotext.kim.client.coredb.CoreDbAPI;
import com.ontotext.kim.client.corpora.KIMDocument;
import com.ontotext.kim.client.corpora.KIMFeatureMap;
import com.ontotext.kim.client.corpora.KIMFeatureMapImpl;
import com.ontotext.kim.client.corpora.KIMAnnotation;
import com.ontotext.kim.client.corpora.KIMAnnotationSet;
import com.ontotext.kim.client.corpora.KIMCorporaException;
import com.ontotext.kim.client.model.FeatureConstants;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import java.util.List;
import java.util.HashSet;
import java.util.Vector;
import java.io.*;
public class Decoder {
public static final String RMI_HOST= "localhost";
public static final int RMI_PORT= 1099;
public static final String start= "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>";
public static void main(String[] args) {
try {
// connessione al KIMservice
//KIMService serviceKim= GetService.from(RMI_HOST, RMI_PORT);
KIMService serviceKim= GetService.from();
System.out.println("KIM Server connesso.");
// otteniamo componenti CorporaAPI e SemanticAnnotationAPI
CorporaAPI apiCorpora= serviceKim.getCorporaAPI();
System.out.println("CorporaAPI ottenuto con successo.");
SemanticAnnotationAPI apiSemAnn= serviceKim.getSemanticAnnotationAPI();
System.out.println("SemanticAnnotationAPI ottenuto con successo.");
// DocumentRepositoryAPI apiDR = serviceKim.getDocumentRepositoryAPI();
// spit("DocumentRepositoryAPI ottenuto con successo.");
// QueryAPI apiQuery = serviceKim.getQueryAPI();
// spit("QueryAPI ottenuto con successo.");
SemanticRepositoryAPI apiSesame =
serviceKim.getSemanticRepositoryAPI();
System.out.println("SemanticRepositoryAPI ottenuto con successo.");
// CoreDbAPI apiCoreDb = serviceKim.getCoreDbAPI();
// spit("CoreDbAPI ottenuto con successo.");
// create and annotate document
URL url2= new URL ("http://www.ontotext.com/kim");
KIMDocument kdoc= apiCorpora.createDocument(url2, "UTF-8");
apiSemAnn.execute(kdoc);
// view document's entities
KIMAnnotationSet kimASet= kdoc.getAnnotations();
Iterator annIterator= kimASet.iterator();
System.out.println("--> Entità del documento...");
while( annIterator.hasNext()) {
// get annotation and annotations features
KIMAnnotation kimAnnotation = (KIMAnnotation)
annIterator.next();
KIMFeatureMap kimFeatures = kimAnnotation.getFeatures();
if(kimFeatures != null) {
String resourceUri = (String)
kimFeatures.get(FeatureConstants.INSTANCE);
Resource uriRes = null;
if(resourceUri.startsWith("_")) {
// anonymous resource
uriRes= new BNodeImpl(resourceUri);
}
else {
uriRes= new URIImpl(resourceUri);
}
Map entCount= new HashMap();
// get the labels of the entities
EntityQueryResult properties=
apiSesame.evaluateSelectSeRQL("select distinct MainLabel "
+ "from {<" + uriRes.toString() + ">} <" +
URIImpl.RDFS_LABEL.getURI()
+ "> {MainLabel}");
// count entities and fill a map with number and names
while(properties.hasNext()){
String curLabel = ((List)
properties.next()).get(0).toString();
if(entCount.containsKey(curLabel)){
Long prev= (Long) entCount.get(curLabel);
entCount.put(curLabel, new Long
(prev.longValue()+1));
}else {
entCount.put(curLabel, new Long(1));
}
}
}
}
// view entities with count and name
Iterator cntIterator= entCount.keySet().iterator();
while(cntIterator.hasNext()) {
String entLabel= cntIterator.next().toString();
long entOccurs= ((Long ) entCount.get(entLabel)).longValue();
System.out.println("num: " + entOccurs + "\t\t name: " + entLabel);
}
System.out.println("\n");
} catch (Exception e) {e.printStackTrace();}
}
}_______________________________________________
Kim-discussion mailing list
[email protected]
http://ontotext.com/mailman/listinfo/kim-discussion