public static void main(String[] args) { Map<String, String> map1 = Generics.newHashMap(); map1.put("a", "1"); map1.put("b", "2"); map1.put("c", "2"); map1.put("d", "4"); Map<String, String> map2 = Generics.newHashMap(); map2.put("1", "x"); map2.put("2", "y"); map2.put("3", "z"); System.out.println("map1: " + map1); System.out.println("invert(map1): " + Maps.invert(map1)); System.out.println("invertSet(map1): " + Maps.invertSet(map1)); System.out.println("map2: " + map2); System.out.println("compose(map1,map2): " + Maps.compose(map1, map2)); Map<String, Set<String>> setValues = Generics.newHashMap(); Map<String, List<String>> listValues = Generics.newHashMap(); Maps.putIntoValueArrayList(listValues, "a", "1"); Maps.putIntoValueArrayList(listValues, "a", "1"); Maps.putIntoValueArrayList(listValues, "a", "2"); Maps.putIntoValueHashSet(setValues, "a", "1"); Maps.putIntoValueHashSet(setValues, "a", "1"); Maps.putIntoValueHashSet(setValues, "a", "2"); System.out.println("listValues: " + listValues); System.out.println("setValues: " + setValues); }
public Set<K2> secondKeySet() { Set<K2> keys = Generics.newHashSet(); for (K1 k1 : map.keySet()) { keys.addAll(get(k1).firstKeySet()); } return keys; }
public Collection<V> values() { List<V> s = Generics.newArrayList(); for (TwoDimensionalMap<K2, K3, V> innerMap : map.values()) { s.addAll(innerMap.values()); } return s; }
/** * Compose two maps map1:x->y and map2:y->z to get a map x->z * * @return The composed map */ public static <X, Y, Z> Map<X, Z> compose(Map<X, Y> map1, Map<Y, Z> map2) { Map<X, Z> composedMap = Generics.newHashMap(); for (Entry<X, Y> xyEntry : map1.entrySet()) { composedMap.put(xyEntry.getKey(), map2.get(xyEntry.getValue())); } return composedMap; }
@Override public Set<Requirement> requirementsSatisfied() { Set<Requirement> satisfied = Generics.newHashSet(); for (Annotator annotator : annotators) { satisfied.addAll(annotator.requirementsSatisfied()); } return satisfied; }
/** * Inverts a map x->y to a map y->pow(x) not assuming unique preimages. * * @return The inverted set */ public static <X, Y> Map<Y, Set<X>> invertSet(Map<X, Y> map) { Map<Y, Set<X>> invertedMap = Generics.newHashMap(); for (Map.Entry<X, Y> entry : map.entrySet()) { X key = entry.getKey(); Y value = entry.getValue(); putIntoValueHashSet(invertedMap, value, key); } return invertedMap; }
/** * Inverts a map x->y to a map y->x assuming unique preimages. If they are not unique, you * get an arbitrary ones as the values in the inverted map. * * @return The inverted map */ public static <X, Y> Map<Y, X> invert(Map<X, Y> map) { Map<Y, X> invertedMap = Generics.newHashMap(); for (Map.Entry<X, Y> entry : map.entrySet()) { X key = entry.getKey(); Y value = entry.getValue(); invertedMap.put(value, key); } return invertedMap; }
public Set<K3> thirdKeySet() { Set<K3> keys = Generics.newHashSet(); for (K1 k1 : map.keySet()) { TwoDimensionalMap<K2, K3, V> m = map.get(k1); for (K2 k2 : m.firstKeySet()) { keys.addAll(m.get(k2).keySet()); } } return keys; }
public TokensRegexNERAnnotator(String name, Properties properties) { String prefix = (name != null && !name.isEmpty()) ? name + '.' : ""; String backgroundSymbol = properties.getProperty(prefix + "backgroundSymbol", DEFAULT_BACKGROUND_SYMBOL); String[] backgroundSymbols = backgroundSymbol.split("\\s*,\\s*"); String mappingFiles = properties.getProperty(prefix + "mapping", DefaultPaths.DEFAULT_REGEXNER_RULES); String[] mappings = mappingFiles.split("\\s*[,;]\\s*"); String validPosRegex = properties.getProperty(prefix + "validpospattern"); this.posMatchType = PosMatchType.valueOf( properties.getProperty(prefix + "posmatchtype", DEFAULT_POS_MATCH_TYPE.name())); String noDefaultOverwriteLabelsProp = properties.getProperty(prefix + "noDefaultOverwriteLabels"); this.noDefaultOverwriteLabels = (noDefaultOverwriteLabelsProp != null) ? Collections.unmodifiableSet( CollectionUtils.asSet(noDefaultOverwriteLabelsProp.split("\\s*,\\s*"))) : Collections.unmodifiableSet(new HashSet<>()); this.ignoreCase = PropertiesUtils.getBool(properties, prefix + "ignorecase", false); this.verbose = PropertiesUtils.getBool(properties, prefix + "verbose", false); if (validPosRegex != null && !validPosRegex.isEmpty()) { validPosPattern = Pattern.compile(validPosRegex); } else { validPosPattern = null; } entries = Collections.unmodifiableList( readEntries(name, noDefaultOverwriteLabels, ignoreCase, verbose, mappings)); IdentityHashMap<SequencePattern<CoreMap>, Entry> patternToEntry = new IdentityHashMap<>(); multiPatternMatcher = createPatternMatcher(patternToEntry); this.patternToEntry = Collections.unmodifiableMap(patternToEntry); Set<String> myLabels = Generics.newHashSet(); // Can always override background or none. Collections.addAll(myLabels, backgroundSymbols); myLabels.add(null); // Always overwrite labels for (Entry entry : entries) myLabels.add(entry.type); this.myLabels = Collections.unmodifiableSet(myLabels); }
/** * Creates a new Index. * * @param capacity Initial capacity of Index. */ public HashIndex(int capacity) { super(); objects = new ArrayList<E>(capacity); indexes = Generics.newHashMap(capacity); }
/** Creates a new Index. */ public HashIndex() { super(); objects = new ArrayList<E>(); indexes = Generics.newHashMap(); }
@Override public <K1, V1> Map<K1, V1> setMap(Map<K1, V1> map, int initCapacity) { map = Generics.newHashMap(initCapacity); return map; }
@Override public <K1, V1> Map<K1, V1> setMap(Map<K1, V1> map) { map = Generics.newHashMap(); return map; }
/** * Reads a list of Entries from a mapping file and update the given entries. Line numbers start * from 1. * * @return the updated list of Entries */ private static List<Entry> readEntries( String annotatorName, List<Entry> entries, TrieMap<String, Entry> seenRegexes, String mappingFilename, BufferedReader mapping, Set<String> noDefaultOverwriteLabels, boolean ignoreCase, boolean verbose) throws IOException { int origEntriesSize = entries.size(); int isTokensRegex = 0; int lineCount = 0; for (String line; (line = mapping.readLine()) != null; ) { lineCount++; String[] split = line.split("\t"); if (split.length < 2 || split.length > 5) { throw new IllegalArgumentException( "Provided mapping file is in wrong format. This line is bad: " + line); } String regex = split[0].trim(); String tokensRegex = null; String[] regexes = null; if (regex.startsWith("( ") && regex.endsWith(" )")) { // Tokens regex (remove start and end parenthesis) tokensRegex = regex.substring(1, regex.length() - 1).trim(); } else { regexes = regex.split("\\s+"); } String[] key = (regexes != null) ? regexes : new String[] {tokensRegex}; if (ignoreCase) { String[] norm = new String[key.length]; for (int i = 0; i < key.length; i++) { norm[i] = key[i].toLowerCase(); } key = norm; } String type = split[1].trim(); Set<String> overwritableTypes = Generics.newHashSet(); double priority = 0.0; if (split.length >= 3) { overwritableTypes.addAll(Arrays.asList(split[2].trim().split("\\s*,\\s*"))); } if (split.length >= 4) { try { priority = Double.parseDouble(split[3].trim()); } catch (NumberFormatException e) { throw new IllegalArgumentException( "ERROR: Invalid priority in line " + lineCount + " in regexner file " + mappingFilename + ": \"" + line + "\"!", e); } } int annotateGroup = 0; // Get annotate group from input.... if (split.length >= 5) { // Which group to take (allow for context) String context = split[4].trim(); try { annotateGroup = Integer.parseInt(context); } catch (NumberFormatException e) { throw new IllegalArgumentException( "ERROR: Invalid group in line " + lineCount + " in regexner file " + mappingFilename + ": \"" + line + "\"!", e); } } // Print some warning about the type int commaPos = type.indexOf(','); if (commaPos > 0) { // Strip the "," and just take first type String newType = type.substring(0, commaPos).trim(); logger.warn( "TokensRegexNERAnnotator " + annotatorName + ": Entry has multiple types: " + line + ". Taking type to be " + newType); type = newType; } Entry entry = new Entry(tokensRegex, regexes, type, overwritableTypes, priority, annotateGroup); if (seenRegexes.containsKey(key)) { Entry oldEntry = seenRegexes.get(key); if (priority > oldEntry.priority) { logger.warn( "TokensRegexNERAnnotator " + annotatorName + ": Replace duplicate entry (higher priority): old=" + oldEntry + ", new=" + entry); } else { if (!oldEntry.type.equals(type)) { if (verbose) { logger.warn( "TokensRegexNERAnnotator " + annotatorName + ": Ignoring duplicate entry: " + split[0] + ", old type = " + oldEntry.type + ", new type = " + type); } // } else { // if (verbose) { // logger.warn("TokensRegexNERAnnotator " + annotatorName + // ": Duplicate entry [ignored]: " + split[0] + ", old type = " + // oldEntry.type + ", new type = " + type); // } } continue; } } // Print some warning if label belongs to noDefaultOverwriteLabels but there is no // overwritable types if (entry.overwritableTypes.isEmpty() && noDefaultOverwriteLabels.contains(entry.type)) { logger.warn( "TokensRegexNERAnnotator " + annotatorName + ": Entry doesn't have overwriteable types " + entry + ", but entry type is in noDefaultOverwriteLabels"); } entries.add(entry); seenRegexes.put(key, entry); if (entry.tokensRegex != null) isTokensRegex++; } logger.log( "TokensRegexNERAnnotator " + annotatorName + ": Read " + (entries.size() - origEntriesSize) + " unique entries out of " + lineCount + " from " + mappingFilename + ", " + isTokensRegex + " TokensRegex patterns."); return entries; }
@Override public Map<K, V> newMap() { return Generics.newHashMap(); }
public ThreeDimensionalMap() { this.map = Generics.newHashMap(); }
@Override public Map<K, V> newMap(int initCapacity) { return Generics.newHashMap(initCapacity); }