Example usage for com.google.common.base Functions forMap

List of usage examples for com.google.common.base Functions forMap

Introduction

In this page you can find the example usage for com.google.common.base Functions forMap.

Prototype

public static <K, V> Function<K, V> forMap(Map<K, V> map) 

Source Link

Document

Returns a function which performs a map lookup.

Usage

From source file:org.apache.ctakes.temporal.ae.baselines.TimidBaselineEventTimeRelationAnnotator.java

@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas jCas, Annotation sentence) {

    List<EventMention> events = JCasUtil.selectCovered(jCas, EventMention.class, sentence);
    List<TimeMention> times = JCasUtil.selectCovered(jCas, TimeMention.class, sentence);

    if (times.size() != 1 || events.size() < 1) {
        return Lists.newArrayList();
    }//from   w  w  w  .  ja va2s.c o m

    // compute token distance for each time-event pair
    HashMap<IdentifiedAnnotationPair, Integer> distanceLookup = new HashMap<IdentifiedAnnotationPair, Integer>();
    for (EventMention event : events) {
        // ignore subclasses like Procedure and Disease/Disorder
        if (event.getClass().equals(EventMention.class)) {
            for (TimeMention time : times) {
                IdentifiedAnnotationPair pair = new IdentifiedAnnotationPair(time, event);
                List<BaseToken> baseTokens = JCasUtil.selectBetween(jCas, BaseToken.class, pair.getArg1(),
                        pair.getArg2());
                int distance = baseTokens.size();
                distanceLookup.put(pair, distance);
            }
        }
    }

    // find the pair where the distance between entities is the smallest and return it
    List<IdentifiedAnnotationPair> rankedPairs = new ArrayList<IdentifiedAnnotationPair>(
            distanceLookup.keySet());
    Function<IdentifiedAnnotationPair, Integer> getValue = Functions.forMap(distanceLookup);
    Collections.sort(rankedPairs, Ordering.natural().onResultOf(getValue));

    List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
    result.add(rankedPairs.get(0));

    return result;
}

From source file:org.apache.ctakes.temporal.ae.baselines.RecallBaselineEventTimeRelationAnnotator.java

@Override
protected List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas jCas, Annotation sentence) {
    List<EventMention> events = JCasUtil.selectCovered(jCas, EventMention.class, sentence);
    List<TimeMention> times = JCasUtil.selectCovered(jCas, TimeMention.class, sentence);

    if (times.size() < 1 || events.size() < 1) {
        return Lists.newArrayList();
    }/*w  ww .j  a va 2  s  .c o  m*/

    // compute token distance for each time-event pair
    HashMap<IdentifiedAnnotationPair, Integer> distanceLookup = new HashMap<IdentifiedAnnotationPair, Integer>();
    for (EventMention event : events) {
        // ignore subclasses like Procedure and Disease/Disorder
        if (event.getClass().equals(EventMention.class)) {
            for (TimeMention time : times) {
                IdentifiedAnnotationPair pair = new IdentifiedAnnotationPair(time, event);
                List<BaseToken> baseTokens = JCasUtil.selectBetween(jCas, BaseToken.class, pair.getArg1(),
                        pair.getArg2());
                int distance = baseTokens.size();
                distanceLookup.put(pair, distance);
            }
        }
    }

    // find the pair where the distance between entities is the smallest and return it
    List<IdentifiedAnnotationPair> rankedPairs = new ArrayList<IdentifiedAnnotationPair>(
            distanceLookup.keySet());
    Function<IdentifiedAnnotationPair, Integer> getValue = Functions.forMap(distanceLookup);
    Collections.sort(rankedPairs, Ordering.natural().onResultOf(getValue));

    List<IdentifiedAnnotationPair> results = new ArrayList<IdentifiedAnnotationPair>();
    Set<EventMention> relTimes = new HashSet<EventMention>();
    for (IdentifiedAnnotationPair result : rankedPairs) {
        if (!relTimes.contains(result.getArg2())) {
            relTimes.add((EventMention) result.getArg2());
            results.add(result);
        }
    }
    return results;
}

From source file:org.apache.ctakes.temporal.ae.baselines.PrecisionBaselineEventTimeRelationAnnotator.java

@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas jCas, Annotation sentence) {

    List<EventMention> events = JCasUtil.selectCovered(jCas, EventMention.class, sentence);
    List<TimeMention> times = JCasUtil.selectCovered(jCas, TimeMention.class, sentence);

    if (times.size() < 1 || events.size() < 1) {
        return Lists.newArrayList();
    }/*  www  .  j av  a 2 s  .  c om*/

    // compute token distance for each time-event pair
    HashMap<IdentifiedAnnotationPair, Integer> distanceLookup = new HashMap<IdentifiedAnnotationPair, Integer>();
    for (EventMention event : events) {
        // ignore subclasses like Procedure and Disease/Disorder
        if (event.getClass().equals(EventMention.class)) {
            for (TimeMention time : times) {
                IdentifiedAnnotationPair pair = new IdentifiedAnnotationPair(time, event);
                List<BaseToken> baseTokens = JCasUtil.selectBetween(jCas, BaseToken.class, pair.getArg1(),
                        pair.getArg2());
                int distance = baseTokens.size();
                distanceLookup.put(pair, distance);
            }
        }
    }

    // find the pair where the distance between entities is the smallest and return it
    List<IdentifiedAnnotationPair> rankedPairs = new ArrayList<IdentifiedAnnotationPair>(
            distanceLookup.keySet());
    Function<IdentifiedAnnotationPair, Integer> getValue = Functions.forMap(distanceLookup);
    Collections.sort(rankedPairs, Ordering.natural().onResultOf(getValue));

    List<IdentifiedAnnotationPair> results = new ArrayList<IdentifiedAnnotationPair>();

    Set<TimeMention> relTimes = new HashSet<TimeMention>();
    for (IdentifiedAnnotationPair result : rankedPairs) {
        if (!relTimes.contains(result.getArg1())) {
            relTimes.add((TimeMention) result.getArg1());
            results.add(result);
        }
    }

    return results;
}

From source file:tile80.world80.World80Graph.java

@Override
public Iterable<Tile80> getTileByRect(Pair<Integer, Integer> topLeft, Pair<Integer, Integer> bottomRight) {
    Preconditions.checkNotNull(topLeft, bottomRight);
    return FluentIterable.from(coord.rightSet()).filter(Range.closed(topLeft, bottomRight))
            .transform(Functions.forMap(coord.rightMap())).transform(toTile80);
}

From source file:org.apache.ctakes.relationextractor.ae.baselines.Baseline2DegreeOfRelationExtractorAnnotator.java

@Override
public List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas identifiedAnnotationView,
        Annotation sentence) {/*from  w w  w.  java  2 s  .  c o  m*/

    List<EntityMention> entities = JCasUtil.selectCovered(identifiedAnnotationView, EntityMention.class,
            sentence);
    List<Modifier> modifiers = JCasUtil.selectCovered(identifiedAnnotationView, Modifier.class, sentence);

    // look for sentences with multiple modifiers/arg2s and a single entity/arg1
    if (!(modifiers.size() >= 1 && entities.size() == 1)) {
        return new ArrayList<IdentifiedAnnotationPair>();
    }

    List<IdentifiedAnnotationPair> pairs = new ArrayList<IdentifiedAnnotationPair>();
    for (EntityMention entity : entities) {
        for (Modifier modifier : modifiers) {
            pairs.add(new IdentifiedAnnotationPair(entity, modifier));
        }
    }

    // compute distance between entities for the pairs where entity types are correct
    HashMap<IdentifiedAnnotationPair, Integer> distanceLookup = new HashMap<IdentifiedAnnotationPair, Integer>();
    for (IdentifiedAnnotationPair pair : pairs) {
        if (Utils.validateDegreeOfArgumentTypes(pair)) {
            try {
                int distance = Utils.getDistance(identifiedAnnotationView.getView(CAS.NAME_DEFAULT_SOFA), pair);
                distanceLookup.put(pair, distance);
            } catch (CASException e) {
                System.out.println("couldn't get default sofa");
                break;
            }
        }
    }
    if (distanceLookup.isEmpty()) {
        return new ArrayList<IdentifiedAnnotationPair>(); // no pairs with suitable argument types
    }

    // find the pair where the distance between entities is the smallest and return it
    List<IdentifiedAnnotationPair> rankedPairs = new ArrayList<IdentifiedAnnotationPair>(
            distanceLookup.keySet());
    Function<IdentifiedAnnotationPair, Integer> getValue = Functions.forMap(distanceLookup);
    Collections.sort(rankedPairs, Ordering.natural().onResultOf(getValue));

    List<IdentifiedAnnotationPair> result = new ArrayList<IdentifiedAnnotationPair>();
    result.add(rankedPairs.get(0));

    System.out.println(sentence.getCoveredText());
    System.out.println("arg1: " + result.get(0).getArg1().getCoveredText());
    System.out.println("arg2: " + result.get(0).getArg2().getCoveredText());
    System.out.println();

    return result;
}

From source file:org.atlasapi.query.content.parser.QueryStringBackedQueryBuilder.java

@SuppressWarnings("unchecked")
public ContentQuery build(HttpServletRequest request) {
    ContentQuery contentQuery = build(request.getParameterMap())
            .copyWithSelection(selectionBuilder.build(request));
    String annotationsParam = request.getParameter(ANNOTATIONS_PARAM) != null
            ? request.getParameter(ANNOTATIONS_PARAM)
            : "";
    Set<Annotation> annotations = ImmutableSet.copyOf(
            Iterables.transform(csvSplitter.split(annotationsParam), Functions.forMap(Annotation.LOOKUP)));
    if (annotations.isEmpty()) {
        return contentQuery;
    } else {/*from   w  ww  .j  a v  a2s.  c om*/
        return contentQuery.copyWithAnnotations(annotations);
    }
}

From source file:org.jclouds.byon.config.YamlNodeStoreModule.java

@Provides
@Singleton// w ww. j a v  a  2  s .  c  o m
protected LoadingCache<String, Node> provideNodeStore(Map<String, YamlNode> backing,
        Function<Node, YamlNode> yamlSerializer, Function<YamlNode, Node> yamlDeserializer) {
    return CacheBuilder.newBuilder().build(CacheLoader.from(Functions
            .forMap(new TransformingMap<String, YamlNode, Node>(backing, yamlDeserializer, yamlSerializer))));
}

From source file:org.apache.ctakes.temporal.ae.baselines.F1BaselineEventTimeRelationAnnotator.java

@Override
protected List<IdentifiedAnnotationPair> getCandidateRelationArgumentPairs(JCas jCas, Annotation sentence) {
    List<EventMention> events = JCasUtil.selectCovered(jCas, EventMention.class, sentence);

    if (events.size() == 0) {
        return Lists.newArrayList();
    }//from  w w w  .j av  a2  s. co  m

    // compute token distance for each time-event pair
    HashMap<IdentifiedAnnotationPair, Integer> distanceLookup = new HashMap<IdentifiedAnnotationPair, Integer>();
    for (EventMention event : events) {
        if (event.getClass().equals(EventMention.class)) {
            // get only times preceding this event:
            List<TimeMention> times = JCasUtil.selectCovered(jCas, TimeMention.class, sentence.getBegin(),
                    event.getBegin());
            if (times.size() == 0)
                continue;
            for (TimeMention time : times) {
                // ignore subclasses like Procedure and Disease/Disorder
                IdentifiedAnnotationPair pair = new IdentifiedAnnotationPair(time, event);
                List<BaseToken> baseTokens = JCasUtil.selectBetween(jCas, BaseToken.class, pair.getArg1(),
                        pair.getArg2());
                int distance = baseTokens.size();
                distanceLookup.put(pair, distance);
            }
        }

    }

    if (distanceLookup.size() == 0)
        return Lists.newArrayList();

    // find the pair where the distance between entities is the smallest and return it
    List<IdentifiedAnnotationPair> rankedPairs = new ArrayList<IdentifiedAnnotationPair>(
            distanceLookup.keySet());
    Function<IdentifiedAnnotationPair, Integer> getValue = Functions.forMap(distanceLookup);
    Collections.sort(rankedPairs, Ordering.natural().onResultOf(getValue));

    List<IdentifiedAnnotationPair> results = new ArrayList<IdentifiedAnnotationPair>();
    Set<EventMention> relTimes = new HashSet<EventMention>();
    for (IdentifiedAnnotationPair result : rankedPairs) {
        if (!relTimes.contains(result.getArg2())) {
            relTimes.add((EventMention) result.getArg2());
            results.add(result);
            Sentence sent = JCasUtil.selectCovering(jCas, Sentence.class, result.getArg1().getBegin(),
                    result.getArg1().getEnd()).get(0);
            System.out.println(sent.getCoveredText());
            System.out.println("Relation found: CONTAINS(" + result.getArg1().getCoveredText() + ","
                    + result.getArg2().getCoveredText() + ")\n");
        }
    }
    return results;
}

From source file:uk.ac.open.kmi.iserve.discovery.disco.impl.OperationDataflowMatcher.java

/**
 * Perform a match between two URIs (from {@code origin} to {@code destination})
 * and returns the result.//w w  w .  j a v  a 2  s . c o m
 *
 * @param origin      URI of the element to match
 * @param destination URI of the element to match against
 * @return {@link uk.ac.open.kmi.iserve.discovery.api.MatchResult} with the result of the matching.
 */
@Override
public MatchResult match(URI origin, URI destination) {

    Set<URI> originOutputs = this.serviceManager.listOutputs(origin);
    Set<URI> destinationInputs = this.serviceManager.listInputs(destination);

    // Obtain all combinations of matches
    Table<URI, URI, MatchResult> matches = this.conceptMatcher.match(originOutputs, destinationInputs);
    // Collect the best match results for each of the destination's inputs
    // We should try and maximise the fulfillment of destination's inputs rather than be guided by the origin outputs
    ImmutableSet.Builder<MatchResult> builder = ImmutableSet.builder();

    for (Map.Entry<URI, Map<URI, MatchResult>> entry : matches.columnMap().entrySet()) {
        Map<URI, MatchResult> matchesMap = entry.getValue();
        Ordering<URI> valueComparator = Ordering.from(MatchResultComparators.BY_TYPE)
                .onResultOf(Functions.forMap(matchesMap)) // Order by value
                .compound(Ordering.natural()); // Order by URI eventually

        URI bestMatchUri = valueComparator.max(matchesMap.keySet());
        log.info("The best match for {} is {}, with a Match Result of {}", entry.getKey(), bestMatchUri,
                matchesMap.get(bestMatchUri).getMatchType());

        builder.add(matchesMap.get(bestMatchUri));
    }

    MatchResult result = INTERSECTION.apply(builder.build());
    log.info("Combined match result - {}", result);
    return result;
}

From source file:org.apache.ctakes.relationextractor.ae.baselines.Baseline3EntityMentionPairRelationExtractorAnnotator.java

private static EntityMention getNearestEntity(JCas jCas, EntityMention anatomicalSite,
        List<EntityMention> entityMentions) {

    // token distance from anatomical site to other entity mentions
    Map<EntityMention, Integer> distanceToEntities = new HashMap<EntityMention, Integer>();

    for (EntityMention entityMention : entityMentions) {
        List<BaseToken> baseTokens = JCasUtil.selectBetween(jCas, BaseToken.class, anatomicalSite,
                entityMention);/*from w  ww.  ja  va  2s.c  o  m*/
        distanceToEntities.put(entityMention, baseTokens.size());
    }

    List<EntityMention> sortedEntityMentions = new ArrayList<EntityMention>(distanceToEntities.keySet());
    Function<EntityMention, Integer> getValue = Functions.forMap(distanceToEntities);
    Collections.sort(sortedEntityMentions, Ordering.natural().onResultOf(getValue));

    return sortedEntityMentions.get(0);
}