001/* 002 * Copyright (c) 2011, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * 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. 010 * 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. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.timeml.util; 025 026import java.util.ArrayList; 027import java.util.Arrays; 028import java.util.HashMap; 029import java.util.HashSet; 030import java.util.List; 031import java.util.Map; 032import java.util.Set; 033 034import org.apache.uima.jcas.JCas; 035import org.apache.uima.jcas.tcas.Annotation; 036import org.cleartk.ml.Feature; 037import org.cleartk.ml.feature.extractor.NamedFeatureExtractor1; 038 039import com.google.common.base.Joiner; 040 041/** 042 * <br> 043 * Copyright (c) 2011, Regents of the University of Colorado <br> 044 * All rights reserved. 045 * 046 * @author Steven Bethard 047 */ 048public class TimeWordsExtractor<T extends Annotation> implements NamedFeatureExtractor1<T> { 049 050 private String featureName = "TimeType"; 051 052 private Map<String, Set<String>> groupedWords; 053 054 public TimeWordsExtractor() { 055 this.groupedWords = new HashMap<String, Set<String>>(); 056 this.groupedWords.put("Now", new HashSet<String>(Arrays.asList("now", "current", "currently"))); 057 this.groupedWords.put( 058 "TimeOfDay", 059 new HashSet<String>(Arrays.asList( 060 "morning", 061 "noon", 062 "afternoon", 063 "evening", 064 "night", 065 "midnight"))); 066 this.groupedWords.put( 067 "Day", 068 new HashSet<String>(Arrays.asList( 069 "Monday", 070 "Tuesday", 071 "Wednesday", 072 "Thursday", 073 "Friday", 074 "Saturday", 075 "Sunday", 076 "today", 077 "tomorrow", 078 "yesterday"))); 079 this.groupedWords.put( 080 "Month", 081 new HashSet<String>(Arrays.asList( 082 "January", 083 "February", 084 "March", 085 "April", 086 "May", 087 "June", 088 "July", 089 "August", 090 "September", 091 "October", 092 "November", 093 "December"))); 094 this.groupedWords.put( 095 "Season", 096 new HashSet<String>(Arrays.asList("spring", "summer", "fall", "autumn", "winter"))); 097 this.groupedWords.put( 098 "TimeDuration", 099 new HashSet<String>( 100 Arrays.asList("minute", "minutes", "second", "seconds", "hour", "hours"))); 101 this.groupedWords.put( 102 "DateDuration", 103 new HashSet<String>(Arrays.asList( 104 "day", 105 "days", 106 "week", 107 "weeks", 108 "month", 109 "months", 110 "quarter", 111 "quarters", 112 "season", 113 "seasons", 114 "year", 115 "years", 116 "decade", 117 "decades", 118 "century", 119 "centuries"))); 120 } 121 122 @Override 123 public String getFeatureName() { 124 return this.featureName; 125 } 126 127 @Override 128 public List<Feature> extract(JCas view, T focusAnnotation) { 129 List<String> types = new ArrayList<String>(); 130 String[] words = focusAnnotation.getCoveredText().split("\\W+"); 131 for (String word : words) { 132 for (String group : this.groupedWords.keySet()) { 133 if (this.groupedWords.get(group).contains(word)) { 134 types.add(group); 135 } 136 } 137 if (word.matches("^\\d{4}$")) { 138 types.add("Year"); 139 } 140 } 141 if (types.isEmpty()) { 142 for (String word : words) { 143 for (String group : this.groupedWords.keySet()) { 144 if (this.groupedWords.get(group).contains(word.toLowerCase())) { 145 types.add(group + "Lower"); 146 } 147 } 148 } 149 } 150 if (types.isEmpty()) { 151 types.add("None"); 152 } 153 return Arrays.asList(new Feature(this.featureName, Joiner.on('_').join(types))); 154 } 155 156}