Revision: 16105 http://gate.svn.sourceforge.net/gate/?rev=16105&view=rev Author: valyt Date: 2012-10-02 16:08:35 +0000 (Tue, 02 Oct 2012) Log Message: ----------- Support for pluggable mention describers in AbstractS-A-H.
New Default and Ontology mention describers. Modified Paths: -------------- mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java Added Paths: ----------- mimir/trunk/mimir-core/src/gate/mimir/util/DefaultMentionDescriber.java mimir/trunk/mimir-core/src/gate/mimir/util/OntologyMentionDescriber.java Modified: mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java =================================================================== --- mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java 2012-10-02 16:06:58 UTC (rev 16104) +++ mimir/trunk/mimir-core/src/gate/mimir/AbstractSemanticAnnotationHelper.java 2012-10-02 16:08:35 UTC (rev 16105) @@ -18,7 +18,9 @@ import gate.mimir.index.Indexer; import gate.mimir.index.Mention; import gate.mimir.search.QueryEngine; +import gate.mimir.util.DefaultMentionDescriber; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -47,8 +49,20 @@ * treat URI features as if they were simply text features with no semantics.</p> */ public abstract class AbstractSemanticAnnotationHelper implements - SemanticAnnotationHelper { + SemanticAnnotationHelper { + /** + * Interface for supporting classes used by + * {@link AbstractSemanticAnnotationHelper} and its sub-classes to provide a + * pluggable implementation for + * {@link SemanticAnnotationHelper#describeMention(String)}. + */ + public static interface MentionDescriber extends Serializable { + public String describeMention(AbstractSemanticAnnotationHelper helper, + String mentionUri, String[] descriptiveFeatureNames, + String[] descriptiveFeatureValues); + } + private static final long serialVersionUID = -5432862771431426914L; private transient boolean isInited = false; @@ -94,6 +108,9 @@ */ protected String[] descriptiveFeatures; + protected MentionDescriber mentionDescriber; + + /** * The working mode for this helper (defaults to {@link Mode#ANNOTATION}). */ @@ -171,7 +188,7 @@ * * @return the descriptiveFeatures */ - public String[] getDescriptiveFeatures() { + protected String[] getDescriptiveFeatures() { return descriptiveFeatures; } @@ -189,6 +206,27 @@ this.descriptiveFeatures = descriptiveFeatures; } + + + /** + * @return the mentionDescriber + */ + public MentionDescriber getMentionDescriber() { + return mentionDescriber; + } + + /** + * Sets the {@link MentionDescriber} to be used as the implementation of + * {@link SemanticAnnotationHelper#describeMention(String)}. + * If set to <code>null</code>, then an instance of + * {@link DefaultMentionDescriber} is automatically created and used. + * + * @param mentionDescriber the custom mentionDescriber to use + */ + public void setMentionDescriber(MentionDescriber mentionDescriber) { + this.mentionDescriber = mentionDescriber; + } + /* (non-Javadoc) * @see gate.mimir.SemanticAnnotationHelper#documentEnd() */ @@ -217,20 +255,11 @@ */ @Override public String describeMention(String mentionUri) { - String[] values = getDescriptiveFeatureValues(mentionUri); - if(values == null) { - return mentionUri; - } else { - StringBuilder res = new StringBuilder("{").append(annotationType); - for(int i = 0; i < descriptiveFeatures.length; i++) { - if(values[i] != null && values[i].length() > 0) { - res.append(' ').append(descriptiveFeatures[i]).append(" = "); - res.append(values[i]); - } - } - res.append('}'); - return res.toString(); + if(mentionDescriber == null) { + mentionDescriber = new DefaultMentionDescriber(); } + return mentionDescriber.describeMention(this, mentionUri, + getDescriptiveFeatures(), getDescriptiveFeatureValues(mentionUri)); } /** Added: mimir/trunk/mimir-core/src/gate/mimir/util/DefaultMentionDescriber.java =================================================================== --- mimir/trunk/mimir-core/src/gate/mimir/util/DefaultMentionDescriber.java (rev 0) +++ mimir/trunk/mimir-core/src/gate/mimir/util/DefaultMentionDescriber.java 2012-10-02 16:08:35 UTC (rev 16105) @@ -0,0 +1,60 @@ +/* + * DefaultMentionDescriber.java + * + * Copyright (c) 2007-2011, The University of Sheffield. + * + * This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html), + * and is free software, licenced under the GNU Lesser General Public License, + * Version 3, June 2007 (also included with this distribution as file + * LICENCE-LGPL3.html). + * + * Valentin Tablan, 2 Oct 2012 + * + * $Id$ + */ +package gate.mimir.util; + +import gate.mimir.AbstractSemanticAnnotationHelper; +import gate.mimir.AbstractSemanticAnnotationHelper.MentionDescriber; + +/** + * Default implementation of a {@link MentionDescriber} that simply lists + * the annotation type and the descriptive features (see + * {@link AbstractSemanticAnnotationHelper#getDescriptiveFeatures()} and their + * values. The generated mention descriptions look like "{AnnType feat1 = + * val1, feat2 = val2 ...}" + */ +public class DefaultMentionDescriber implements MentionDescriber { + /** + * + */ + private static final long serialVersionUID = 4818070659434784582L; + + + /* (non-Javadoc) + * @see gate.mimir.AbstractSemanticAnnotationHelper.MentionDescriber#describeMention(gate.mimir.AbstractSemanticAnnotationHelper, java.lang.String) + */ + @Override + public String describeMention(AbstractSemanticAnnotationHelper helper, + String mentionUri, String[] descriptiveFeatureNames, + String[] descriptiveFeatureValues) { + if(descriptiveFeatureValues == null || + descriptiveFeatureNames == null || + descriptiveFeatureValues.length < descriptiveFeatureNames.length) { + return mentionUri; + } else { + StringBuilder res = new StringBuilder("{"); + res.append(helper.getAnnotationType()); + for(int i = 0; i < descriptiveFeatureNames.length; i++) { + if(descriptiveFeatureValues[i] != null && + descriptiveFeatureValues[i].length() > 0) { + res.append(i == 0 ? ' ' : ", "); + res.append(descriptiveFeatureNames[i]).append(" = "); + res.append(descriptiveFeatureValues[i]); + } + } + res.append('}'); + return res.toString(); + } + } +} Property changes on: mimir/trunk/mimir-core/src/gate/mimir/util/DefaultMentionDescriber.java ___________________________________________________________________ Added: svn:keywords + Id Added: svn:eol-style + native Added: mimir/trunk/mimir-core/src/gate/mimir/util/OntologyMentionDescriber.java =================================================================== --- mimir/trunk/mimir-core/src/gate/mimir/util/OntologyMentionDescriber.java (rev 0) +++ mimir/trunk/mimir-core/src/gate/mimir/util/OntologyMentionDescriber.java 2012-10-02 16:08:35 UTC (rev 16105) @@ -0,0 +1,128 @@ +/* + * OntologyMentionDescriber.java + * + * Copyright (c) 2007-2011, The University of Sheffield. + * + * This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html), + * and is free software, licenced under the GNU Lesser General Public License, + * Version 3, June 2007 (also included with this distribution as file + * LICENCE-LGPL3.html). + * + * Valentin Tablan, 2 Oct 2012 + * + * $Id$ + */ +package gate.mimir.util; + +import gate.mimir.AbstractSemanticAnnotationHelper; +import gate.mimir.AbstractSemanticAnnotationHelper.MentionDescriber; + +/** + * A {@link MentionDescriber} for annotations that represent ontology entities. + * The generated description for a given entity looks like + * "Class (Instance)", e.g. "City (London)". + * + * For this describer to work, the array of descriptive features (see + * {@link AbstractSemanticAnnotationHelper#getDescriptiveFeatures()}) must start + * with the name of the <code>class</code> feature, followed optionally by the + * name of the <code>feature</code> feature. All subsequent feature names are + * ignored. + */ +public class OntologyMentionDescriber implements MentionDescriber { + + protected String nameSpaceSeparator = "#"; + + protected boolean localNamesOnly = true; + + /** + * Gets the string used to split the ontology URIs into name space and local + * name. Defaults to "#". + * + * @return the nameSpaceSeparator + */ + public String getNameSpaceSeparator() { + return nameSpaceSeparator; + } + + /** + * When this describer is set to use {@link #localNamesOnly} (<code>true<code> + * by default), the name space separator is used to split ontology URIs into + * name space and local name. + * + * Call this method to change the separator string used if the default + * separator ("#") is not suitable. + * + * @param nameSpaceSeparator the new separator to use. + */ + public void setNameSpaceSeparator(String nameSpaceSeparator) { + this.nameSpaceSeparator = nameSpaceSeparator; + } + + /** + * Is this describer set to use local (short) names only? + * @return <code>true</code> if local names should be used instead of full + * URIs. + */ + public boolean isLocalNamesOnly() { + return localNamesOnly; + } + + /** + * Set this to <code>true</code> to use local (short) names in the + * description, or <code>false</code> to use full URIs. Defaults to + * <code>true</code>. + * + * @param the new value. + */ + public void setLocalNamesOnly(boolean localNamesOnly) { + this.localNamesOnly = localNamesOnly; + } + + /** + * + */ + private static final long serialVersionUID = 7995628810321612593L; + + /* (non-Javadoc) + * @see gate.mimir.AbstractSemanticAnnotationHelper.MentionDescriber#describeMention(gate.mimir.AbstractSemanticAnnotationHelper, java.lang.String, java.lang.String[], java.lang.String[]) + */ + @Override + public String describeMention(AbstractSemanticAnnotationHelper helper, + String mentionUri, + String[] descriptiveFeatureNames, + String[] descriptiveFeatureValues) { + if(descriptiveFeatureValues == null || + descriptiveFeatureNames == null || + descriptiveFeatureValues.length < descriptiveFeatureNames.length || + descriptiveFeatureNames.length == 0 || + descriptiveFeatureValues[0] == null) { + return mentionUri; + } else { + StringBuilder res = new StringBuilder( + getName(descriptiveFeatureValues[0])); + if(descriptiveFeatureValues.length > 1 && + descriptiveFeatureValues[1] != null) { + res.append(" ("); + res.append(getName(descriptiveFeatureValues[1])); + res.append(')'); + } + return res.toString(); + } + } + + /** + * Calculates the class/instance name according to the settings of this + * describer. + * @param uri + * @return + */ + protected String getName(String uri) { + if(localNamesOnly) { + // we need to shorten the full name + int pos = uri.indexOf(nameSpaceSeparator); + return pos < 0 ? uri : uri.substring(pos + nameSpaceSeparator.length()); + } else { + return uri; + } + } +} \ No newline at end of file Property changes on: mimir/trunk/mimir-core/src/gate/mimir/util/OntologyMentionDescriber.java ___________________________________________________________________ Added: svn:keywords + Id Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Don't let slow site performance ruin your business. Deploy New Relic APM Deploy New Relic app performance management and know exactly what is happening inside your Ruby, Python, PHP, Java, and .NET app Try New Relic at no cost today and get our sweet Data Nerd shirt too! http://p.sf.net/sfu/newrelic-dev2dev _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs