private ScoredCandidates<Container> scoreContainers(
      Multiset<String> parents, int children, ResultDescription desc) {
    Builder<Container> candidates = DefaultScoredCandidates.fromSource(NAME);

    ResolvedContent containers = resolver.findByCanonicalUris(parents.elementSet());

    for (Multiset.Entry<String> parent : parents.entrySet()) {
      Maybe<Identified> possibledContainer = containers.get(parent.getElement());
      if (possibledContainer.hasValue()) {
        Identified identified = possibledContainer.requireValue();
        if (identified instanceof Container) {
          Container container = (Container) identified;
          Score score = score(parent.getCount(), children);
          candidates.addEquivalent(container, score);
          desc.appendText(
              "%s: scored %s (%s)", container.getCanonicalUri(), score, container.getTitle());
        } else {
          desc.appendText("%s: %s not container", parent, identified.getClass().getSimpleName());
        }
      } else {
        desc.appendText("%s: missing", parent);
      }
    }

    return candidates.build();
  }
示例#2
0
 /**
  * Calculates a score representing the similarity of the candidate's title compared to the
  * subject's title.
  *
  * @param subject - subject content
  * @param candidate - candidate content
  * @return score representing how closely candidate's title matches subject's title.
  */
 public Score score(Content subject, Content candidate, ResultDescription desc) {
   if (subject.getTitle() == null || candidate.getTitle() == null) {
     return Score.NULL_SCORE;
   }
   String subjectTitle = sanitize(subject.getTitle());
   String contentTitle = sanitize(candidate.getTitle());
   double score = score(subjectTitle, contentTitle);
   desc.appendText(
       "%s vs. %s (%s): %s", subjectTitle, contentTitle, candidate.getCanonicalUri(), score);
   return Score.valueOf(score);
 }
示例#3
0
  public ScoredCandidates<T> scoreCandidates(
      T content, Iterable<? extends T> candidates, ResultDescription desc) {
    Builder<T> equivalents = DefaultScoredCandidates.fromSource(name);
    desc.appendText("Scoring %s candidates", Iterables.size(candidates));

    for (T found : ImmutableSet.copyOf(candidates)) {
      equivalents.addEquivalent(found, score(content, found, desc));
    }

    return equivalents.build();
  }