Revision: 14447
http://gate.svn.sourceforge.net/gate/?rev=14447&view=rev
Author: valyt
Date: 2011-10-27 13:13:51 +0000 (Thu, 27 Oct 2011)
Log Message:
-----------
Support for document metadata.
Modified Paths:
--------------
mimir/trunk/plugins/db-h2/src/gate/mimir/db/AnnotationTemplateCache.java
mimir/trunk/plugins/db-h2/src/gate/mimir/db/DBSemanticAnnotationHelper.java
Modified:
mimir/trunk/plugins/db-h2/src/gate/mimir/db/AnnotationTemplateCache.java
===================================================================
--- mimir/trunk/plugins/db-h2/src/gate/mimir/db/AnnotationTemplateCache.java
2011-10-27 13:13:28 UTC (rev 14446)
+++ mimir/trunk/plugins/db-h2/src/gate/mimir/db/AnnotationTemplateCache.java
2011-10-27 13:13:51 UTC (rev 14447)
@@ -20,6 +20,7 @@
package gate.mimir.db;
import gate.Annotation;
+import gate.FeatureMap;
import gate.mimir.AbstractSemanticAnnotationHelper;
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
@@ -72,14 +73,13 @@
* The key that goes into the level 1 cache map.
*/
protected class NominalFeatures {
- public NominalFeatures(Annotation ann) {
+ public NominalFeatures(FeatureMap annFeats) {
// nominalValues is guaranteed to be non-null and to have the same size
// as owner.getNominalFeatureNames() (i.e. 0, if no nominal features)
features = new short[nominalvalues.length];
for(int i = 0; i < nominalvalues.length; i++) {
String value =
- (String)ann.getFeatures()
- .get(owner.getNominalFeatureNames()[i]);
+ (String)annFeats.get(owner.getNominalFeatureNames()[i]);
if(value == null) {
features[i] = NULL;
} else {
@@ -114,7 +114,7 @@
* The type of keys that go into the level2 cache.
*/
protected class NonNominalFeatures {
- public NonNominalFeatures(Annotation ann) {
+ public NonNominalFeatures(FeatureMap annFeats) {
int length = 0;
if(owner.getIntegerFeatureNames() != null)
length += owner.getIntegerFeatureNames().length;
@@ -128,22 +128,22 @@
int i = 0;
if(owner.getIntegerFeatureNames() != null) {
for(String aFeature : owner.getIntegerFeatureNames()) {
- values[i++] = ann.getFeatures().get(aFeature);
+ values[i++] = annFeats.get(aFeature);
}
}
if(owner.getFloatFeatureNames() != null) {
for(String aFeature : owner.getFloatFeatureNames()) {
- values[i++] = ann.getFeatures().get(aFeature);
+ values[i++] = annFeats.get(aFeature);
}
}
if(owner.getTextFeatureNames() != null) {
for(String aFeature : owner.getTextFeatureNames()) {
- values[i++] = ann.getFeatures().get(aFeature);
+ values[i++] = annFeats.get(aFeature);
}
}
if(owner.getUriFeatureNames() != null) {
for(String aFeature : owner.getUriFeatureNames()) {
- values[i++] = ann.getFeatures().get(aFeature);
+ values[i++] = annFeats.get(aFeature);
}
}
// cache the hash code.
@@ -335,9 +335,9 @@
* an ID value of {@link #NO_ID} - it is the responsibility of the client
code
* to obtain the correct ID and set it on the tag.
*/
- public Tag getLevel1Tag(Annotation ann) {
+ public Tag getLevel1Tag(FeatureMap annFeats) {
// build the nominal features value
- NominalFeatures nomFeats = new NominalFeatures(ann);
+ NominalFeatures nomFeats = new NominalFeatures(annFeats);
Level2Cache l1tag = level1Cache.get(nomFeats);
if(l1tag == null) {
l1CacheMisses++;
@@ -356,9 +356,9 @@
* - it is the responsibility of the client code to obtain the correct ID and
* set it on the tag.
*/
- public Tag getLevel2Tag(Annotation ann, Tag level1Tag) {
+ public Tag getLevel2Tag(FeatureMap annFeats, Tag level1Tag) {
Level2Cache l1Value = (Level2Cache)level1Tag;
- NonNominalFeatures nonNonFeats = new NonNominalFeatures(ann);
+ NonNominalFeatures nonNonFeats = new NonNominalFeatures(annFeats);
LongTag level2Tag = (LongTag)l1Value.level2Cache.get(nonNonFeats);
if(level2Tag == null) {
l2CacheMisses++;
Modified:
mimir/trunk/plugins/db-h2/src/gate/mimir/db/DBSemanticAnnotationHelper.java
===================================================================
--- mimir/trunk/plugins/db-h2/src/gate/mimir/db/DBSemanticAnnotationHelper.java
2011-10-27 13:13:28 UTC (rev 14446)
+++ mimir/trunk/plugins/db-h2/src/gate/mimir/db/DBSemanticAnnotationHelper.java
2011-10-27 13:13:51 UTC (rev 14447)
@@ -22,6 +22,7 @@
import static gate.mimir.db.AnnotationTemplateCache.Tag.NO_ID;
import gate.Annotation;
import gate.Document;
+import gate.FeatureMap;
import gate.Gate;
import gate.creole.ANNIEConstants;
import gate.mimir.AbstractSemanticAnnotationHelper;
@@ -40,6 +41,7 @@
import gate.mimir.search.QueryEngine;
import gate.mimir.search.QueryRunner;
import gate.mimir.search.query.Binding;
+import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.mg4j.index.DowncaseTermProcessor;
import it.unimi.dsi.mg4j.index.NullTermProcessor;
@@ -175,6 +177,13 @@
private static transient Logger logger =
Logger.getLogger(DBSemanticAnnotationHelper.class);
+ /**
+ * When in document mode (see
+ * {@link SemanticAnnotationHelper#isInDocumentMode()}), stores the
features
+ * for the current document.
+ */
+ private transient FeatureMap documentFeatures;
+
public DBSemanticAnnotationHelper(String annotationType,
String[] nominalFeatureNames, String[] integerFeatureNames,
String[] floatFeatureNames, String[] textFeatureNames) {
@@ -519,14 +528,21 @@
}
@Override
- public String[] getMentionUris(Annotation annotation, int length,
+ public String[] getMentionUris(Annotation ann, int length,
Indexer indexer) {
+ FeatureMap featuresToIndex;
+ if(isInDocumentMode()) {
+ length = -1;
+ featuresToIndex = documentFeatures;
+ } else {
+ featuresToIndex = ann.getFeatures();
+ }
try {
// find the level 1 ID
- Tag level1Tag = cache.getLevel1Tag(annotation);
+ Tag level1Tag = cache.getLevel1Tag(featuresToIndex);
while(level1Tag.getId() == NO_ID){
- setStatementParameters(level1SelectStmt, annotation);
+ setStatementParameters(level1SelectStmt, featuresToIndex);
ResultSet res = level1SelectStmt.executeQuery();
if(res.next()) {
// we have found the level 1 ID
@@ -534,10 +550,10 @@
// sanity check
if(res.next()) throw new RuntimeException(
"Multiple Unique IDs foud in level 1 table for annotation "
+
- annotation.toString());
+ ann.toString());
} else {
// insert the new row
- setStatementParameters(level1InsertStmt, annotation);
+ setStatementParameters(level1InsertStmt, featuresToIndex);
if(level1InsertStmt.executeUpdate() != 1) {
// the update failed
logger.error("Error while inserting into database. Annotation was
lost!");
@@ -565,7 +581,7 @@
// sanity check
if(res.next()) throw new RuntimeException(
"Multiple Unique IDs foud in mentions table for annotation "
+
- "(of length "+ length + "):\n" +annotation.toString());
+ "(of length "+ length + "):\n" +ann.toString());
} else {
// insert the new row
mentionsInsertStmt.setLong(1, level1Tag.getId());
@@ -586,10 +602,10 @@
if(level2Used){
// find the level 2 ID
- Tag level2Tag = cache.getLevel2Tag(annotation, level1Tag);
+ Tag level2Tag = cache.getLevel2Tag(featuresToIndex, level1Tag);
while(level2Tag.getId() == NO_ID){
level2SelectStmt.setLong(1, level1Tag.getId());
- setStatementParameters(level2SelectStmt, annotation);
+ setStatementParameters(level2SelectStmt, featuresToIndex);
ResultSet res = level2SelectStmt.executeQuery();
if(res.next()) {
@@ -598,11 +614,11 @@
// sanity check
if(res.next()) throw new RuntimeException(
"Multiple Unique IDs found in level 2 table for annotation
" +
- annotation.toString());
+ ann.toString());
} else {
// insert the new row
level2InsertStmt.setLong(1, level1Tag.getId());
- setStatementParameters(level2InsertStmt, annotation);
+ setStatementParameters(level2InsertStmt, featuresToIndex);
if(level2InsertStmt.executeUpdate() != 1) {
// the update failed
logger.error("Error while inserting into database. Annotation
was lost!");
@@ -625,7 +641,7 @@
// sanity check
if(res.next()) throw new RuntimeException(
"Multiple Unique IDs foud in mentions table for annotation
" +
- "(of length "+ length + "):\n" +annotation.toString());
+ "(of length "+ length + "):\n" +ann.toString());
} else {
// insert the new row
mentionsInsertStmt.setLong(1, level1Tag.getId());
@@ -667,12 +683,12 @@
* @throws SQLException
*/
protected void setStatementParameters(PreparedStatement stmt,
- Annotation annotation) throws SQLException {
+ FeatureMap annFeats) throws SQLException {
if(stmt == level1InsertStmt || stmt == level1SelectStmt) {
if(nominalFeatureNames != null){
int paramIdx = 1;
for(String aFeatureName : nominalFeatureNames) {
- Object value = annotation.getFeatures().get(aFeatureName);
+ Object value = annFeats.get(aFeatureName);
if(value != null) {
stmt.setString(paramIdx++, value.toString());
} else {
@@ -686,7 +702,7 @@
int paramIdx = 2;
if(integerFeatureNames != null){
for(String aFeatureName : integerFeatureNames) {
- Object valueObj = annotation.getFeatures().get(aFeatureName);
+ Object valueObj = annFeats.get(aFeatureName);
Long value = null;
if(valueObj != null){
if(valueObj instanceof Number) {
@@ -714,7 +730,7 @@
}
if(floatFeatureNames != null){
for(String aFeatureName : floatFeatureNames) {
- Object valueObj = annotation.getFeatures().get(aFeatureName);
+ Object valueObj = annFeats.get(aFeatureName);
Double value = null;
if(valueObj != null){
if(valueObj instanceof Number) {
@@ -742,7 +758,7 @@
}
if(textFeatureNames != null) {
for(String aFeatureName : textFeatureNames) {
- Object valueObj = annotation.getFeatures().get(aFeatureName);
+ Object valueObj = annFeats.get(aFeatureName);
if(valueObj != null) {
stmt.setString(paramIdx++, valueObj.toString());
} else {
@@ -988,7 +1004,7 @@
ResultSet res =
dbConnection.createStatement().executeQuery(selectStr.toString());
while(res.next()) {
long id = res.getLong(1);
- int length = res.getInt(2);
+ int length = isInDocumentMode()? Mention.NO_LENGTH : res.getInt(2);
mentions.add(new Mention(annotationType + ":" + id, length));
}
} catch(SQLException e) {
@@ -998,8 +1014,18 @@
return mentions;
}
+
+
@Override
+ public void documentStart(Document document) {
+ if(isInDocumentMode()) {
+ documentFeatures = document.getFeatures();
+ }
+ }
+
+ @Override
public void documentEnd() {
+ documentFeatures = null;
if(cache != null) {
double l1ratio = cache.getL1CacheHitRatio();
double l2ratio = cache.getL2CacheHitRatio();
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs