Revision: 20279 http://sourceforge.net/p/gate/code/20279 Author: ian_roberts Date: 2017-12-04 22:58:58 +0000 (Mon, 04 Dec 2017) Log Message: ----------- Reinstated PatternMentionDescriber from 4.2 that got lost in the 5.0 transition
Added Paths: ----------- mimir/trunk/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java Copied: mimir/trunk/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java (from rev 20278, mimir/branches/4.2/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java) =================================================================== --- mimir/trunk/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java (rev 0) +++ mimir/trunk/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java 2017-12-04 22:58:58 UTC (rev 20279) @@ -0,0 +1,113 @@ +/* + * PatternMentionDescriber.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, 06 Feb 2014 + * + * $Id$ + */ +package gate.mimir.util; + +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import gate.mimir.AbstractSemanticAnnotationHelper; +import gate.mimir.AbstractSemanticAnnotationHelper.MentionDescriber; + + +/** + * A {@link MentionDescriber} that uses a user-given pattern to describe + * annotation mentions. + */ +public class PatternMentionDescriber implements MentionDescriber{ + + private static final long serialVersionUID = -3310472212294302781L; + + /** + * Regex used to find feature names in the pattern. + */ + protected static final Pattern FEATURE_FINDER = Pattern.compile("\\$\\{(.+?)\\}"); + + /** + * The pattern used to generate mention descriptions. + */ + protected String pattern; + + /** + * The set of feature names that actually occur in the pattern. + */ + protected Set<String> featureNames; + + + /** + * Construct a new pattern-based mention describer. When using this + * constructor make sure to alter set the pattern by calling + * {@link #setPattern(String)}. + */ + public PatternMentionDescriber() { + featureNames = new HashSet<String>(); + } + + + /** + * Construct a new pattern-based mention describer. + * + * @param pattern the pattern used to generate mentions. To describe an + * annotation mention, all occurrences of "${name}" in the pattern + * are replaced with the values of the <code>name</code> feature for the given + * annotation. + */ + public PatternMentionDescriber(String pattern) { + this(); + setPattern(pattern); + } + + + public String getPattern() { + return pattern; + } + + /** + * Sets the pattern to be used when describing annotation mentions. + * + * @param pattern the pattern used to generate mentions. To describe an + * annotation mention, all occurrences of "${name}" in the pattern + * are replaced with the values of the <code>name</code> feature for the given + * annotation. */ + public void setPattern(String pattern) { + this.pattern = pattern; + int pos = 0; + Matcher m = FEATURE_FINDER.matcher(pattern); + while(m.find(pos)) { + featureNames.add(m.group(1)); + pos = m.end(); + } + } + + @Override + public String describeMention(AbstractSemanticAnnotationHelper helper, + String mentionUri, String[] descriptiveFeatureNames, + String[] descriptiveFeatureValues) { + String res = pattern; + for(int i = 0; i < descriptiveFeatureNames.length; i++) { + if(featureNames.contains(descriptiveFeatureNames[i])) { + res = res.replace("${" + descriptiveFeatureNames[i] + "}", + (descriptiveFeatureValues[i] != null ? + descriptiveFeatureValues[i] : "")); + } + } + if(res.length() == 0) { + res = "{" + helper.getAnnotationType() + "}"; + } + return res; + } + +} Property changes on: mimir/trunk/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Id \ No newline at end of property Added: svn:mergeinfo ## -0,0 +1,3 ## +/mimir/branches/3.4/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java:14623,14634-14643 +/mimir/branches/4.0/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java:15380-15383,15385-15386,15388 +/mimir/branches/4.x/mimir-core/src/gate/mimir/util/PatternMentionDescriber.java:14299-14316 \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs