<commit_before>/** 
 * Copyright (c) 2007-2008, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */
package org.cleartk.timeml.tlink;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.cleartk.classifier.CleartkAnnotator;
import org.cleartk.classifier.CleartkAnnotatorDescriptionFactory;
import org.cleartk.classifier.Instance;
import org.cleartk.classifier.feature.extractor.simple.BagExtractor;
import org.cleartk.classifier.feature.extractor.simple.NamingExtractor;
import org.cleartk.classifier.feature.extractor.simple.SimpleFeatureExtractor;
import org.cleartk.classifier.feature.extractor.simple.SpannedTextExtractor;
import org.cleartk.classifier.opennlp.DefaultMaxentDataWriterFactory;
import org.cleartk.syntax.constituent.type.TopTreebankNode;
import org.cleartk.syntax.constituent.type.TreebankNode;
import org.cleartk.timeml.TimeMLComponents;
import org.cleartk.timeml.type.Anchor;
import org.cleartk.timeml.type.Event;
import org.cleartk.timeml.type.TemporalLink;
import org.cleartk.timeml.util.CleartkInternalModelLocator;
import org.cleartk.timeml.util.PrecedingTokenTextBagExtractor;
import org.cleartk.timeml.util.TargetPathExtractor;
import org.cleartk.timeml.util.TokenPOSExtractor;
import org.cleartk.timeml.util.TokenStemExtractor;
import org.cleartk.token.type.Sentence;
import org.cleartk.token.type.Token;
import org.cleartk.util.AnnotationRetrieval;
import org.uimafit.descriptor.ConfigurationParameter;
import org.uimafit.descriptor.TypeCapability;
import org.uimafit.factory.ConfigurationParameterFactory;

/**
 * <br>
 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * 
 * 
 * @author <NAME>
 */
@TypeCapability(outputs = { "org.cleartk.timeml.type.TemporalLink", "org.cleartk.timeml.type.Event" })
public class VerbClauseTemporalAnnotator extends CleartkAnnotator<String> {

  public static final CleartkInternalModelLocator MODEL_LOCATOR = new CleartkInternalModelLocator(
      VerbClauseTemporalAnnotator.class);

  private static final Map<String, String[]> headMap = new HashMap<String, String[]>();
  static {
    headMap.put("S", "VP S SBAR ADJP".split(" "));
    headMap.put("SBAR", "VP S SBAR ADJP".split(" "));
    headMap.put("VP", ("VP VB VBZ VBP VBG VBN VBD JJ JJR JJS "
        + "NNS NN PRP NNPS NNP ADJP NP S SBAR").split(" "));
    headMap.put("ADJP", "ADJP VB VBZ VBP VBG VBN VBD JJ JJR JJS".split(" "));
    headMap.put("NP", "NP NNS NN PRP NNPS NNP QP ADJP".split(" "));
    headMap.put("QP", "NP NNS NN PRP NNPS NNP QP ADJP".split(" "));
  }

  private static final Set<String> stopWords = new HashSet<String>(
      Arrays.asList("be been is 's am are was were has had have".split(" ")));

  private List<SimpleFeatureExtractor> sourceFeatureExtractors;

  private List<SimpleFeatureExtractor> targetFeatureExtractors;

  private List<SimpleFeatureExtractor> betweenAnchorsFeatureExtractors;

  private TargetPathExtractor pathExtractor;

  private int eventID;

  @ConfigurationParameter(defaultValue = "false", description = "Create events for all verbs in "
      + "verb-clause relations (using existing events if present, but adding new ones "
      + "wherever they are not present).")
  private boolean createEvents;

  public static final String PARAM_CREATE_EVENTS = ConfigurationParameterFactory
      .createConfigurationParameterName(VerbClauseTemporalAnnotator.class, "createEvents");

  public static AnalysisEngineDescription getWriterDescription()
      throws ResourceInitializationException {
    return CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        DefaultMaxentDataWriterFactory.class,
        MODEL_LOCATOR.getTrainingDirectory());
  }

  public static AnalysisEngineDescription getAnnotatorDescription()
      throws ResourceInitializationException {
    return CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        MODEL_LOCATOR.getClassifierJarURL().getFile());
  }

  public static AnalysisEngineDescription getEventCreatingAnnotatorDescription()
      throws ResourceInitializationException {
    AnalysisEngineDescription desc = CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        MODEL_LOCATOR.getClassifierJarURL().getFile());
    ConfigurationParameterFactory.addConfigurationParameters(
        desc,
        VerbClauseTemporalAnnotator.PARAM_CREATE_EVENTS,
        true);
    return desc;
  }

  public VerbClauseTemporalAnnotator() {
    this.eventID = 1;

    PrecedingTokenTextBagExtractor precedingAuxiliaries;
    precedingAuxiliaries = new PrecedingTokenTextBagExtractor(3, "MD", "TO", "IN", "VB", "RB");

    this.sourceFeatureExtractors = new ArrayList<SimpleFeatureExtractor>();
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new SpannedTextExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new TokenPOSExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new TokenStemExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", precedingAuxiliaries));

    this.targetFeatureExtractors = new ArrayList<SimpleFeatureExtractor>();<commit_msg>Fix bug in VerbClauseTemporalAnnotator that was making it always look on the file system instead of using the Resource URL when possible.<commit_after>/** 
 * Copyright (c) 2007-2008, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */
package org.cleartk.timeml.tlink;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.cleartk.classifier.CleartkAnnotator;
import org.cleartk.classifier.CleartkAnnotatorDescriptionFactory;
import org.cleartk.classifier.Instance;
import org.cleartk.classifier.feature.extractor.simple.BagExtractor;
import org.cleartk.classifier.feature.extractor.simple.NamingExtractor;
import org.cleartk.classifier.feature.extractor.simple.SimpleFeatureExtractor;
import org.cleartk.classifier.feature.extractor.simple.SpannedTextExtractor;
import org.cleartk.classifier.opennlp.DefaultMaxentDataWriterFactory;
import org.cleartk.syntax.constituent.type.TopTreebankNode;
import org.cleartk.syntax.constituent.type.TreebankNode;
import org.cleartk.timeml.TimeMLComponents;
import org.cleartk.timeml.type.Anchor;
import org.cleartk.timeml.type.Event;
import org.cleartk.timeml.type.TemporalLink;
import org.cleartk.timeml.util.CleartkInternalModelLocator;
import org.cleartk.timeml.util.PrecedingTokenTextBagExtractor;
import org.cleartk.timeml.util.TargetPathExtractor;
import org.cleartk.timeml.util.TokenPOSExtractor;
import org.cleartk.timeml.util.TokenStemExtractor;
import org.cleartk.token.type.Sentence;
import org.cleartk.token.type.Token;
import org.cleartk.util.AnnotationRetrieval;
import org.uimafit.descriptor.ConfigurationParameter;
import org.uimafit.descriptor.TypeCapability;
import org.uimafit.factory.ConfigurationParameterFactory;

/**
 * <br>
 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * 
 * 
 * @author <NAME>
 */
@TypeCapability(outputs = { "org.cleartk.timeml.type.TemporalLink", "org.cleartk.timeml.type.Event" })
public class VerbClauseTemporalAnnotator extends CleartkAnnotator<String> {

  public static final CleartkInternalModelLocator MODEL_LOCATOR = new CleartkInternalModelLocator(
      VerbClauseTemporalAnnotator.class);

  private static final Map<String, String[]> headMap = new HashMap<String, String[]>();
  static {
    headMap.put("S", "VP S SBAR ADJP".split(" "));
    headMap.put("SBAR", "VP S SBAR ADJP".split(" "));
    headMap.put("VP", ("VP VB VBZ VBP VBG VBN VBD JJ JJR JJS "
        + "NNS NN PRP NNPS NNP ADJP NP S SBAR").split(" "));
    headMap.put("ADJP", "ADJP VB VBZ VBP VBG VBN VBD JJ JJR JJS".split(" "));
    headMap.put("NP", "NP NNS NN PRP NNPS NNP QP ADJP".split(" "));
    headMap.put("QP", "NP NNS NN PRP NNPS NNP QP ADJP".split(" "));
  }

  private static final Set<String> stopWords = new HashSet<String>(
      Arrays.asList("be been is 's am are was were has had have".split(" ")));

  private List<SimpleFeatureExtractor> sourceFeatureExtractors;

  private List<SimpleFeatureExtractor> targetFeatureExtractors;

  private List<SimpleFeatureExtractor> betweenAnchorsFeatureExtractors;

  private TargetPathExtractor pathExtractor;

  private int eventID;

  @ConfigurationParameter(defaultValue = "false", description = "Create events for all verbs in "
      + "verb-clause relations (using existing events if present, but adding new ones "
      + "wherever they are not present).")
  private boolean createEvents;

  public static final String PARAM_CREATE_EVENTS = ConfigurationParameterFactory
      .createConfigurationParameterName(VerbClauseTemporalAnnotator.class, "createEvents");

  public static AnalysisEngineDescription getWriterDescription()
      throws ResourceInitializationException {
    return CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        DefaultMaxentDataWriterFactory.class,
        MODEL_LOCATOR.getTrainingDirectory());
  }

  public static AnalysisEngineDescription getAnnotatorDescription()
      throws ResourceInitializationException {
    return CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        MODEL_LOCATOR.getClassifierJarURL().toString());
  }

  public static AnalysisEngineDescription getEventCreatingAnnotatorDescription()
      throws ResourceInitializationException {
    AnalysisEngineDescription desc = CleartkAnnotatorDescriptionFactory.createCleartkAnnotator(
        VerbClauseTemporalAnnotator.class,
        TimeMLComponents.TYPE_SYSTEM_DESCRIPTION,
        MODEL_LOCATOR.getClassifierJarURL().toString());
    ConfigurationParameterFactory.addConfigurationParameters(
        desc,
        VerbClauseTemporalAnnotator.PARAM_CREATE_EVENTS,
        true);
    return desc;
  }

  public VerbClauseTemporalAnnotator() {
    this.eventID = 1;

    PrecedingTokenTextBagExtractor precedingAuxiliaries;
    precedingAuxiliaries = new PrecedingTokenTextBagExtractor(3, "MD", "TO", "IN", "VB", "RB");

    this.sourceFeatureExtractors = new ArrayList<SimpleFeatureExtractor>();
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new SpannedTextExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new TokenPOSExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", new TokenStemExtractor()));
    this.sourceFeatureExtractors.add(new NamingExtractor("Source", precedingAuxiliaries));

    this.targetFeatureExtractors = new ArrayList<SimpleFeatureExtractor>();
{'fraction_non_alphanumeric': 0.08300846888022584, 'fraction_numerical': 0.0024196800645248017, 'mean_word_length': 5.270122208175305, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4944399', 'n_tokens_mistral': 4470, 'n_tokens_neox': 4219, 'n_words': 1126}