Esempio n. 1
0
  /**
   * Adds the given POP application satisfier set as a subset of this set, with the given
   * exceptions. The exceptions set must be a subset of the given satisfier set. If the given POP
   * application was already a subset of this set, then the new exceptions set is the intersection
   * of the given exceptions set with the old one. Otherwise, the exceptions set is the given one
   * minus any individual elements of this set that satisfy the given POP application.
   */
  public void addSatisfiers(NumberVar popApp, ObjectSet satisfiers, Set newExceptions) {
    if (popAppSatisfiers.containsKey(popApp)) {
      // already in set; assume satisfiers the same
      Set curExceptions = (Set) popAppExceptions.get(popApp);
      int oldNumExceptions = curExceptions.size();
      curExceptions.retainAll(newExceptions);
      size += (oldNumExceptions - curExceptions.size());
    } else {
      popAppSatisfiers.put(popApp, satisfiers);
      Set oldIndivs = (Set) popAppIndivs.remove(popApp);
      for (Iterator iter = oldIndivs.iterator(); iter.hasNext(); ) {
        individuals.remove(iter.next());
      }

      Set curExceptions = new HashSet(newExceptions);
      curExceptions.removeAll(oldIndivs);
      popAppExceptions.put(popApp, curExceptions);

      size +=
          (satisfiers.size()
              - oldIndivs.size() // because they were already
              // here
              - curExceptions.size()); // because they weren't added
    }
  }
Esempio n. 2
0
  /**
   * 如果第二个字符串为空则返回第一个字符串,否则取两个字符串的交集
   *
   * @return "34,777"
   */
  public static String justInFirstString(String str1, String str2) {
    if (CommonUtil.isEmptyString(str2)) {
      return str1;
    }
    String[] str1Arr = str1.split(",");
    String[] str2Arr = str2.split(",");
    Set<String> set1 = new HashSet<String>();
    Set<String> set2 = new HashSet<String>();
    for (String str : str1Arr) {
      set1.add(str);
    }
    for (String str : str2Arr) {
      set2.add(str);
    }
    Set<String> retainSet = new HashSet<String>();
    retainSet.addAll(set1);
    retainSet.retainAll(set2);

    StringBuffer strB = new StringBuffer();
    for (String str : retainSet) {
      strB.append(str).append(",");
    }
    String resultStr = strB.toString();
    if (resultStr.endsWith(",")) {
      return resultStr.substring(0, resultStr.length() - 1);
    }
    return resultStr;
  }
Esempio n. 3
0
 public static void intersectSet(Set<String> target, Set<String> set) {
   if (target.isEmpty()) {
     target.addAll(set);
   } else {
     target.retainAll(set);
   }
 }
Esempio n. 4
0
  public void validateAlternateAlleles() {
    if (!hasGenotypes()) return;

    List<Allele> reportedAlleles = getAlleles();
    Set<Allele> observedAlleles = new HashSet<Allele>();
    observedAlleles.add(getReference());
    for (final Genotype g : getGenotypes()) {
      if (g.isCalled()) observedAlleles.addAll(g.getAlleles());
    }

    if (reportedAlleles.size() != observedAlleles.size())
      throw new TribbleException.InternalCodecException(
          String.format(
              "the ALT allele(s) for the record at position %s:%d do not match what is observed in the per-sample genotypes",
              getChr(), getStart()));

    int originalSize = reportedAlleles.size();
    // take the intersection and see if things change
    observedAlleles.retainAll(reportedAlleles);
    if (observedAlleles.size() != originalSize)
      throw new TribbleException.InternalCodecException(
          String.format(
              "the ALT allele(s) for the record at position %s:%d do not match what is observed in the per-sample genotypes",
              getChr(), getStart()));
  }
Esempio n. 5
0
  private boolean isConcordant(VariantContext vc, Collection<VariantContext> compVCs) {
    if (vc == null || compVCs == null || compVCs.isEmpty()) return false;

    // if we're not looking for specific samples then the fact that we have both VCs is enough to
    // call it concordant.
    if (NO_SAMPLES_SPECIFIED) return true;

    // make a list of all samples contained in this variant VC that are being tracked by the user
    // command line arguments.
    Set<String> variantSamples = vc.getSampleNames();
    variantSamples.retainAll(samples);

    // check if we can find all samples from the variant rod in the comp rod.
    for (String sample : variantSamples) {
      boolean foundSample = false;
      for (VariantContext compVC : compVCs) {
        Genotype varG = vc.getGenotype(sample);
        Genotype compG = compVC.getGenotype(sample);
        if (haveSameGenotypes(varG, compG)) {
          foundSample = true;
          break;
        }
      }
      // if at least one sample doesn't have the same genotype, we don't have concordance
      if (!foundSample) {
        return false;
      }
    }
    return true;
  }
Esempio n. 6
0
  @Override
  public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
    _fieldLocations = new HashMap<String, GlobalStreamId>();
    _collector = collector;
    int timeout = ((Number) conf.get(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS)).intValue();
    _pending =
        new TimeCacheMap<List<Object>, Map<GlobalStreamId, Tuple>>(timeout, new ExpireCallback());
    _numSources = context.getThisSources().size();
    Set<String> idFields = null;
    for (GlobalStreamId source : context.getThisSources().keySet()) {
      Fields fields =
          context.getComponentOutputFields(source.get_componentId(), source.get_streamId());
      Set<String> setFields = new HashSet<String>(fields.toList());
      if (idFields == null) idFields = setFields;
      else idFields.retainAll(setFields);

      for (String outfield : _outFields) {
        for (String sourcefield : fields) {
          if (outfield.equals(sourcefield)) {
            _fieldLocations.put(outfield, source);
          }
        }
      }
    }
    _idFields = new Fields(new ArrayList<String>(idFields));

    if (_fieldLocations.size() != _outFields.size()) {
      throw new RuntimeException("Cannot find all outfields among sources");
    }
  }
  private Set<Vertex> computeMinVertexCover(Set<Vertex> side1, Set<Vertex> side2) {
    Set<Vertex> konigSet = new HashSet<Vertex>();
    Set<Vertex> unmatched = new TreeSet<Vertex>(side1);
    unmatched.removeAll(matches);
    // System.out.println("Matches: " + matches);
    // System.out.println("side 1 unmatched set: " + unmatched);

    for (Vertex v : unmatched) {
      konigDFS(konigSet, v, false);
    }

    // System.out.println("Konig set: " + konigSet);

    Set<Vertex> result = new HashSet<Vertex>(side2);
    result.retainAll(konigSet);
    // System.out.println("side 2 intersect konigSet: " + result);

    Set<Vertex> side1notInKonigSet = new HashSet<Vertex>(side1);
    side1notInKonigSet.removeAll(konigSet);
    // System.out.println("side 1 not in Konig set: " + side1notInKonigSet);

    result.addAll(side1notInKonigSet);

    return result;
  }
Esempio n. 8
0
  /**
   * A dumb vector space model that counts each word's co-occurences with a predefined set of
   * content words and uses these co-occurence vectors directly as word representations. The context
   * in which a word occurs is the set of content words in an entire sentence.
   *
   * <p>N.B. Most people would probably not consider this an embedding model, since the words have
   * not been embedded in a lower dimensional subspace. However, it is a good starting point.
   *
   * <p>Since this approach does not share any information between representations of different
   * words, we can filter the training data to only include sentences that contain words of
   * interest. In other approaches this may not be a good idea.
   *
   * @param dataPath
   * @param targetVocab
   * @param contentVocab
   * @return
   */
  private static HashMap<String, float[]> getEmbeddings(
      String dataPath, HashMap<String, Integer> contentVocab, Set<String> targetVocab) {

    HashMap<String, float[]> embeddingMatrix = new HashMap<String, float[]>();
    for (String target_word : targetVocab) {
      embeddingMatrix.put(target_word, new float[contentVocab.size()]);
    }

    Collection<List<String>> sentenceCollection =
        SentenceCollection.Reader.readSentenceCollection(dataPath);

    for (List<String> sentence : sentenceCollection) {
      Set<String> sw = new HashSet<String>(sentence);
      sw.retainAll(targetVocab);
      for (String word : sentence) {
        if (!contentVocab.containsKey(word)) continue;
        int contentWordId = contentVocab.get(word);
        for (String targetWord : sw) {
          embeddingMatrix.get(targetWord)[contentWordId] =
              embeddingMatrix.get(targetWord)[contentWordId] + 1;
        }
      }
    }

    return embeddingMatrix;
  }
Esempio n. 9
0
  public static double repairAccuracy(Set<RepairedCell> truth, Set<RepairedCell> found) {
    if (found.size() != 0) {
      Set<RepairedCell> tAndF = new HashSet<RepairedCell>();
      tAndF.addAll(truth);
      tAndF.retainAll(found);
      double precision = tAndF.size() * 1.0 / found.size(),
          recall = tAndF.size() * 1.0 / truth.size();
      if (debug)
        System.out.println("repair precision = " + precision + ", repair recall = " + recall);

      return 2 * precision * recall / (precision + recall);
    }
    return 0;
  }
  /**
   * Compares the covariate report lists.
   *
   * @param diffs map where to annotate the difference.
   * @param other the argument collection to compare against.
   * @param thisRole the name for this argument collection that makes sense to the user.
   * @param otherRole the name for the other argument collection that makes sense to the end user.
   * @return <code>true</code> if a difference was found.
   */
  @Requires("diffs != null && other != null && thisRole != null && otherRole != null")
  private boolean compareRequestedCovariates(
      final Map<String, String> diffs,
      final RecalibrationArgumentCollection other,
      final String thisRole,
      final String otherRole) {

    final Set<String> beforeNames = new HashSet<>(this.COVARIATES.length);
    final Set<String> afterNames = new HashSet<>(other.COVARIATES.length);
    Utils.addAll(beforeNames, this.COVARIATES);
    Utils.addAll(afterNames, other.COVARIATES);
    final Set<String> intersect = new HashSet<>(Math.min(beforeNames.size(), afterNames.size()));
    intersect.addAll(beforeNames);
    intersect.retainAll(afterNames);

    String diffMessage = null;
    if (intersect.size()
        == 0) { // In practice this is not possible due to required covariates but...
      diffMessage =
          String.format(
              "There are no common covariates between '%s' and '%s'"
                  + " recalibrator reports. Covariates in '%s': {%s}. Covariates in '%s': {%s}.",
              thisRole,
              otherRole,
              thisRole,
              Utils.join(", ", this.COVARIATES),
              otherRole,
              Utils.join(",", other.COVARIATES));
    } else if (intersect.size() != beforeNames.size() || intersect.size() != afterNames.size()) {
      beforeNames.removeAll(intersect);
      afterNames.removeAll(intersect);
      diffMessage =
          String.format(
              "There are differences in the set of covariates requested in the"
                  + " '%s' and '%s' recalibrator reports. "
                  + " Exclusive to '%s': {%s}. Exclusive to '%s': {%s}.",
              thisRole,
              otherRole,
              thisRole,
              Utils.join(", ", beforeNames),
              otherRole,
              Utils.join(", ", afterNames));
    }
    if (diffMessage != null) {
      diffs.put("covariate", diffMessage);
      return true;
    } else {
      return false;
    }
  }
Esempio n. 11
0
 /**
  * Remove the segment's data from the data container and cache store because we no longer own it.
  *
  * @param removedSegments to be cancelled and discarded
  */
 private void cancelTransfers(Set<Integer> removedSegments) {
   synchronized (this) {
     List<Integer> segmentsToCancel = new ArrayList<Integer>(removedSegments);
     while (!segmentsToCancel.isEmpty()) {
       int segmentId = segmentsToCancel.remove(0);
       InboundTransferTask inboundTransfer = transfersBySegment.remove(segmentId);
       if (inboundTransfer != null) { // we need to check the transfer was not already completed
         Set<Integer> cancelledSegments = new HashSet<Integer>(removedSegments);
         cancelledSegments.retainAll(inboundTransfer.getSegments());
         segmentsToCancel.removeAll(cancelledSegments);
         inboundTransfer.cancelSegments(
             cancelledSegments); // this will also remove it from transfersBySource if the entire
                                 // task gets cancelled
       }
     }
   }
 }
  @Override
  public Set<Person> getChildren(Person parent1, Person parent2) {
    // get the children of both parents
    // look in the birth table and find person objects
    // match both parents' ids in birth.parents
    Set<Person> children = new HashSet<Person>();
    em.getTransaction().begin();
    Query q =
        em.createQuery("select b.person from Birth b, IN(b.parents) p WHERE p.id = :parentId");
    q.setParameter("parentId", parent1.getId());
    children.addAll((Collection<? extends Person>) q.getResultList());

    q.setParameter("parentId", parent2.getId());
    children.retainAll((Collection<? extends Person>) q.getResultList());
    em.getTransaction().commit();
    return children;
  }
  /**
   * Returns a local hosts blacklist, or a blacklist with a single host to be removed, while
   * cleaning up expired records from the global blacklist.
   *
   * @return A local hosts blacklist.
   */
  public synchronized Map<String, Long> getGlobalBlacklist() {
    if (!isGlobalBlacklistEnabled()) {
      String localHostToRemove = this.hostToRemove;

      if (this.hostToRemove != null) {
        HashMap<String, Long> fakedBlacklist = new HashMap<String, Long>();
        fakedBlacklist.put(localHostToRemove, System.currentTimeMillis() + 5000);
        return fakedBlacklist;
      }

      return new HashMap<String, Long>(1);
    }

    // Make a local copy of the blacklist
    Map<String, Long> blacklistClone = new HashMap<String, Long>(globalBlacklist.size());
    // Copy everything from synchronized global blacklist to local copy for manipulation
    synchronized (globalBlacklist) {
      blacklistClone.putAll(globalBlacklist);
    }
    Set<String> keys = blacklistClone.keySet();

    // We're only interested in blacklisted hosts that are in the hostList
    keys.retainAll(this.hostList);

    // Don't need to synchronize here as we using a local copy
    for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
      String host = i.next();
      // OK if null is returned because another thread already purged Map entry.
      Long timeout = globalBlacklist.get(host);
      if (timeout != null && timeout < System.currentTimeMillis()) {
        // Timeout has expired, remove from blacklist
        synchronized (globalBlacklist) {
          globalBlacklist.remove(host);
        }
        i.remove();
      }
    }
    if (keys.size() == this.hostList.size()) {
      // return an empty blacklist, let the BalanceStrategy implementations try to connect to
      // everything since it appears that all hosts are
      // unavailable - we don't want to wait for loadBalanceBlacklistTimeout to expire.
      return new HashMap<String, Long>(1);
    }

    return blacklistClone;
  }
 private Set<List<Set<Integer>>> combineClusters(
     Set<Set<Integer>> ESeeds, List<Set<Integer>> CSeeds) {
   Set<Set<Integer>> EClusters = finishESeeds(ESeeds);
   Set<Integer> Cs = new HashSet();
   for (int i = 0; i < variables.size(); i++) Cs.add(i);
   Set<Integer> Es = new HashSet();
   for (Set<Integer> ECluster : EClusters) Es.addAll(ECluster);
   Cs.removeAll(Es);
   List<List<Set<Integer>>> Clusters = new ArrayList();
   for (Set<Integer> ECluster : EClusters) {
     List<Set<Integer>> newCluster = new ArrayList<Set<Integer>>();
     newCluster.add(1, ECluster);
     Clusters.add(newCluster);
   }
   List<Set<Integer>> EClustersArray = new ArrayList<Set<Integer>>();
   for (Set<Integer> ECluster : EClusters) EClustersArray.add(ECluster);
   for (Integer c : Cs) {
     int match = -1;
     int overlap = 0;
     boolean pass = false;
     for (int i = 0; i < EClusters.size(); i++) {
       Set<Integer> ECluster = EClustersArray.get(i);
       Set<Integer> intersection = ECluster;
       intersection.retainAll(CSeeds.get(c));
       int _overlap = intersection.size();
       if (_overlap > overlap) {
         overlap = _overlap;
         match = i;
         if (overlap / ECluster.size() > CIparameter) {
           pass = true;
         }
       }
     }
     if (pass) {
       List<Set<Integer>> modCluster = new ArrayList<Set<Integer>>();
       Set<Integer> newCs = Clusters.get(match).get(0);
       newCs.add(c);
       modCluster.add(newCs);
       modCluster.add(EClustersArray.get(match));
       Clusters.set(match, modCluster);
     }
   }
   Set<List<Set<Integer>>> ClusterSet = new HashSet<List<Set<Integer>>>(Clusters);
   return ClusterSet;
 }
Esempio n. 15
0
  /**
   * WaveformSnapshot constructor.
   *
   * @param correlation The pulse's correlation event from which we extract the waveforms.
   * @param pvMap The map of entries each containing a waveform id as the key and PV name as the
   *     value.
   * @param timeMap The map of entries each containing a waveform id as the key and the associated
   *     WaveformTime as the value
   */
  public WaveformSnapshot(
      final Correlation<ChannelTimeRecord> correlation,
      final Map<String, String> pvMap,
      final Map<String, WaveformTime> timeMap) {
    timestamp = correlation.meanDate();

    final Set<String> keys = new HashSet<>(pvMap.keySet());
    keys.retainAll(correlation.names());

    int count = keys.size();
    waveforms = new Waveform[count];
    int index = 0;
    for (final String key : keys) {
      final ChannelRecord record = correlation.getRecord(key);
      final WaveformTime timeInfo = timeMap.get(key);
      final String pvName = pvMap.get(key);
      waveforms[index] = new Waveform(pvName, record.doubleArray(), timeInfo);
      ++index;
    }
  }
  private void writeWeights(
      String orig, GeneBranch from, GeneBranch to, String edgeType, Writer writer)
      throws IOException {
    Set<String> dwstr = to.getAllGenes();
    dwstr.retainAll(downstream.get(orig));
    assert !dwstr.isEmpty();
    double cumPval = calcPVal(orig, dwstr);
    boolean upreg = calcChangeDirection(orig, to.gene);

    String key = from.gene + " " + edgeType + " " + to.gene;
    writer.write("edge\t" + key + "\tcolor\t" + val2Color(cumPval, 0) + "\n");
    writer.write("edge\t" + key + "\twidth\t2\n");

    if (affectedDw.get(orig).contains(to.gene)) {
      double pval = calcPVal(orig, Collections.singleton(to.gene));
      writer.write("node\t" + to.gene + "\tcolor\t" + val2Color(pval, upreg ? 1 : -1) + "\n");
    } else {
      writer.write("node\t" + to.gene + "\tcolor\t255 255 255\n");
    }
  }
Esempio n. 17
0
  /**
   * create a <code>Map</code> from each common item in orig and rev to the index of its first
   * occurrence in orig
   *
   * @param orig the original sequence of items
   * @param rev the revised sequence of items
   */
  protected Map buildEqSet(Object[] orig, Object[] rev) {
    // construct a set of the objects that orig and rev have in common

    // first construct a set containing all the elements in orig
    Set items = new HashSet(Arrays.asList(orig));

    // then remove all those not in rev
    items.retainAll(Arrays.asList(rev));

    Map eqs = new HashMap();
    for (int i = 0; i < orig.length; i++) {
      // if its a common item and hasn't been found before
      if (items.contains(orig[i])) {
        // add it to the map
        eqs.put(orig[i], Integer.valueOf(i));
        // and make sure its not considered again
        items.remove(orig[i]);
      }
    }
    return eqs;
  }
  private static void mergePhiVersions(SSAConstructorSparseEx ssa, DirectGraph graph) {
    // collect phi versions
    List<Set<VarVersionPair>> lst = new ArrayList<Set<VarVersionPair>>();
    for (Entry<VarVersionPair, FastSparseSet<Integer>> ent : ssa.getPhi().entrySet()) {
      Set<VarVersionPair> set = new HashSet<VarVersionPair>();
      set.add(ent.getKey());
      for (Integer version : ent.getValue()) {
        set.add(new VarVersionPair(ent.getKey().var, version.intValue()));
      }

      for (int i = lst.size() - 1; i >= 0; i--) {
        Set<VarVersionPair> tset = lst.get(i);
        Set<VarVersionPair> intersection = new HashSet<VarVersionPair>(set);
        intersection.retainAll(tset);

        if (!intersection.isEmpty()) {
          set.addAll(tset);
          lst.remove(i);
        }
      }

      lst.add(set);
    }

    Map<VarVersionPair, Integer> phiVersions = new HashMap<VarVersionPair, Integer>();
    for (Set<VarVersionPair> set : lst) {
      int min = Integer.MAX_VALUE;
      for (VarVersionPair paar : set) {
        if (paar.version < min) {
          min = paar.version;
        }
      }

      for (VarVersionPair paar : set) {
        phiVersions.put(new VarVersionPair(paar.var, paar.version), min);
      }
    }

    updateVersions(graph, phiVersions);
  }
  public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter one of file names
    System.out.print("Enter a file name for baby name ranking: ");
    String fileName = input.next();

    // Create to sets
    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();

    try {
      java.net.URL url = new java.net.URL("http://www.cs.armstrong.edu/liang/data/" + fileName);

      // Create input file from url and add names to sets
      Scanner inputStream = new Scanner(url.openStream());
      while (inputStream.hasNext()) {
        inputStream.next();
        set1.add(inputStream.next());
        inputStream.next();
        set2.add(inputStream.next());
        inputStream.next();
      }
    } catch (java.net.MalformedURLException ex) {
      System.out.println("Invalid URL");
    } catch (java.io.IOException ex) {
      System.out.println("I/O Errors; no such file");
    }

    // Display the names that are used for both genders
    set1.retainAll(set2);
    System.out.println(set1.size() + " names used for both genders");
    System.out.print("They are ");
    for (String name : set1) {
      System.out.print(name + " ");
    }
    System.out.println();
  }
  private void processUnsuccessfulSelections(
      final Object[] toSelect, Function<Object, Object> restore, Set<Object> originallySelected) {
    final Set<Object> selected = myUi.getSelectedElements();

    boolean wasFullyRejected = false;
    if (toSelect.length > 0 && !selected.isEmpty() && !originallySelected.containsAll(selected)) {
      final Set<Object> successfulSelections = new HashSet<Object>();
      ContainerUtil.addAll(successfulSelections, toSelect);

      successfulSelections.retainAll(selected);
      wasFullyRejected = successfulSelections.isEmpty();
    } else if (selected.isEmpty() && originallySelected.isEmpty()) {
      wasFullyRejected = true;
    }

    if (wasFullyRejected && !selected.isEmpty()) return;

    for (Object eachToSelect : toSelect) {
      if (!selected.contains(eachToSelect)) {
        restore.fun(eachToSelect);
      }
    }
  }
    /* evaluates precision and recall by calling makeObjects() to make a
     * set of structures for guess Tree and gold Tree, and compares them
     * with each other.  */
    public void evaluate(Tree<L> guess, Tree<L> gold, PrintWriter pw) {
      Set<Object> guessedSet = makeObjects(guess);
      Set<Object> goldSet = makeObjects(gold);
      Set<Object> correctSet = new HashSet<Object>();
      correctSet.addAll(goldSet);
      correctSet.retainAll(guessedSet);

      correctEvents += correctSet.size();
      guessedEvents += guessedSet.size();
      goldEvents += goldSet.size();

      int currentExact = 0;
      if (correctSet.size() == guessedSet.size() && correctSet.size() == goldSet.size()) {
        exact++;
        currentExact = 1;
      }
      total++;

      //      guess.pennPrint(pw);
      //      gold.pennPrint(pw);
      displayPRF(
          str + " [Current] ",
          correctSet.size(),
          guessedSet.size(),
          goldSet.size(),
          currentExact,
          1,
          pw);
      displayPRF(
          str + " [Average (up to " + total + ")] ",
          correctEvents,
          guessedEvents,
          goldEvents,
          exact,
          total,
          pw);
    }
 @Override
 public void processNextSentence(Sentence sentence) {
   assert abbreviationDictionary != null;
   String strLine = sentence.getText();
   int abbrevConflicts = 0;
   Set<String> abbrevs = abbreviationDictionary.getAbbrevSet();
   for (String abbrev : abbrevs) {
     int pos = 0;
     for (String word : sentence.getTokens()) {
       if (word.equals(abbrev)) {
         String position = sentence.getIndex() + "-" + pos;
         for (Map.Entry<String, String> entry : position2abbrev.entrySet()) {
           String aPos = entry.getKey();
           String aAbbrev = entry.getValue();
           if (aAbbrev != abbrev) { // not the same one
             // find how close they are by meaning
             Set<String> meaningSetA =
                 new HashSet<String>(abbreviationDictionary.getMeaningSetOfAbbreviation(aAbbrev));
             Set<String> meaningSetB = abbreviationDictionary.getMeaningSetOfAbbreviation(abbrev);
             meaningSetA.retainAll(meaningSetB);
             if (meaningSetA.size() > 0) {
               abbrevConflicts++;
             }
           }
         }
       }
     }
   }
   sentence.setValue("abbrev_conflicts", abbrevConflicts); // number of conflicts
   if (position2abbrev.size() > 0) {
     sentence.setValue(
         "abbrev_conflicts_divided_by_count", abbrevConflicts * 1.0 / position2abbrev.size());
   } else {
     sentence.setValue("abbrev_conflicts_divided_by_count", 0.0);
   }
 }
  @Override
  public double similarity(List<String> a, List<String> b) {
    if (a.size() == 0 || b.size() == 0) return 0.0;
    List<String> aTemp = new ArrayList<>();
    List<String> bTemp = new ArrayList<>();

    for (String s : a) {
      String[] tokens = s.split("_");
      aTemp.addAll(Arrays.asList(tokens));
    }

    for (String s : b) {
      String[] tokens = s.split("_");
      bTemp.addAll(Arrays.asList(tokens));
    }

    Set<String> unionLst = new HashSet<>(aTemp);
    unionLst.addAll(bTemp);

    Set<String> intersectionLst = new HashSet<>(aTemp);
    intersectionLst.retainAll(bTemp);

    return (double) intersectionLst.size() / unionLst.size();
  }
  public double similarity(String a, String b) {
    double simi = 0.0;
    try {
      List<String> aTemp = new ArrayList<>();
      List<String> bTemp = new ArrayList<>();

      String[] tokensA = a.split("_");
      aTemp.addAll(Arrays.asList(tokensA));

      String[] tokensB = b.split("_");
      bTemp.addAll(Arrays.asList(tokensB));

      Set<String> unionLst = new HashSet<>(aTemp);
      unionLst.addAll(bTemp);

      Set<String> intersectionLst = new HashSet<>(aTemp);
      intersectionLst.retainAll(bTemp);

      simi = (double) intersectionLst.size() / unionLst.size();
    } catch (Exception e) {
      logger.error("error calulate simi jaccard for pn: " + a + "\t" + b);
    }
    return simi;
  }
Esempio n. 25
0
 public static <T> Set<T> intersection(Set<T> a, Set<T> b) {
   Set<T> result = new HashSet<T>(a);
   result.retainAll(b);
   return result;
 }
Esempio n. 26
0
  /**
   * 多表路由
   *
   * @param schema
   * @param ctx
   * @param tables
   * @param rrs
   * @param isSelect
   * @return
   * @throws SQLNonTransientException
   */
  public static RouteResultset tryRouteForTables(
      SchemaConfig schema,
      DruidShardingParseInfo ctx,
      RouteCalculateUnit routeUnit,
      RouteResultset rrs,
      boolean isSelect,
      LayerCachePool cachePool)
      throws SQLNonTransientException {
    List<String> tables = ctx.getTables();
    if (schema.isNoSharding() || (tables.size() >= 1 && isNoSharding(schema, tables.get(0)))) {
      return routeToSingleNode(rrs, schema.getDataNode(), ctx.getSql());
    }

    // 只有一个表的
    if (tables.size() == 1) {
      return RouterUtil.tryRouteForOneTable(
          schema, ctx, routeUnit, tables.get(0), rrs, isSelect, cachePool);
    }

    Set<String> retNodesSet = new HashSet<String>();
    // 每个表对应的路由映射
    Map<String, Set<String>> tablesRouteMap = new HashMap<String, Set<String>>();

    // 分库解析信息不为空
    Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions =
        routeUnit.getTablesAndConditions();
    if (tablesAndConditions != null && tablesAndConditions.size() > 0) {
      // 为分库表找路由
      RouterUtil.findRouteWithcConditionsForTables(
          schema, rrs, tablesAndConditions, tablesRouteMap, ctx.getSql(), cachePool, isSelect);
      if (rrs.isFinishedRoute()) {
        return rrs;
      }
    }

    // 为全局表和单库表找路由
    for (String tableName : tables) {
      TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase());
      if (tableConfig == null) {
        String msg =
            "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
      }
      if (tableConfig.isGlobalTable()) { // 全局表
        if (tablesRouteMap.get(tableName) == null) {
          tablesRouteMap.put(tableName, new HashSet<String>());
        }
        tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
      } else if (tablesRouteMap.get(tableName) == null) { // 余下的表都是单库表
        tablesRouteMap.put(tableName, new HashSet<String>());
        tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
      }
    }

    boolean isFirstAdd = true;
    for (Map.Entry<String, Set<String>> entry : tablesRouteMap.entrySet()) {
      if (entry.getValue() == null || entry.getValue().size() == 0) {
        throw new SQLNonTransientException("parent key can't find any valid datanode ");
      } else {
        if (isFirstAdd) {
          retNodesSet.addAll(entry.getValue());
          isFirstAdd = false;
        } else {
          retNodesSet.retainAll(entry.getValue());
          if (retNodesSet.size() == 0) { // 两个表的路由无交集
            String errMsg =
                "invalid route in sql, multi tables found but datanode has no intersection "
                    + " sql:"
                    + ctx.getSql();
            LOGGER.warn(errMsg);
            throw new SQLNonTransientException(errMsg);
          }
        }
      }
    }

    if (retNodesSet != null && retNodesSet.size() > 0) {
      if (retNodesSet.size() > 1 && isAllGlobalTable(ctx, schema)) {
        // mulit routes ,not cache route result
        if (isSelect) {
          rrs.setCacheAble(false);
          routeToSingleNode(rrs, retNodesSet.iterator().next(), ctx.getSql());
        } else { // delete 删除全局表的记录
          routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql(), true);
        }

      } else {
        routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql());
      }
    }
    return rrs;
  }
Esempio n. 27
0
  /**
   * Returns the most specific common superclass or interface of the given classes.
   *
   * @param class1 the first class.
   * @param class2 the second class.
   * @param interfaces specifies whether to look for a superclass or for an interface.
   * @return the common class.
   */
  private Clazz findCommonClass(Clazz class1, Clazz class2, boolean interfaces) {
    // Collect the superclasses or the interfaces of this class.
    Set superClasses1 = new HashSet();
    class1.hierarchyAccept(
        !interfaces, !interfaces, interfaces, false, new ClassCollector(superClasses1));

    int superClasses1Count = superClasses1.size();
    if (superClasses1Count == 0) {
      if (interfaces) {
        return null;
      } else if (class1.getSuperName() != null) {
        throw new IllegalArgumentException(
            "Can't find any super classes of ["
                + class1.getName()
                + "] (not even immediate super class ["
                + class1.getSuperName()
                + "])");
      }
    }

    // Collect the superclasses or the interfaces of the other class.
    Set superClasses2 = new HashSet();
    class2.hierarchyAccept(
        !interfaces, !interfaces, interfaces, false, new ClassCollector(superClasses2));

    int superClasses2Count = superClasses2.size();
    if (superClasses2Count == 0) {
      if (interfaces) {
        return null;
      } else if (class2.getSuperName() != null) {
        throw new IllegalArgumentException(
            "Can't find any super classes of ["
                + class2.getName()
                + "] (not even immediate super class ["
                + class2.getSuperName()
                + "])");
      }
    }

    if (DEBUG) {
      System.out.println(
          "ReferenceValue.generalize this ["
              + class1.getName()
              + "] with other ["
              + class2.getName()
              + "] (interfaces = "
              + interfaces
              + ")");
      System.out.println("  This super classes:  " + superClasses1);
      System.out.println("  Other super classes: " + superClasses2);
    }

    // Find the common superclasses.
    superClasses1.retainAll(superClasses2);

    if (DEBUG) {
      System.out.println("  Common super classes: " + superClasses1);
    }

    if (interfaces && superClasses1.isEmpty()) {
      return null;
    }

    // Find a class that is a subclass of all common superclasses,
    // or that at least has the maximum number of common superclasses.
    Clazz commonClass = null;

    int maximumSuperClassCount = -1;

    // Go over all common superclasses to find it. In case of
    // multiple subclasses, keep the lowest one alphabetically,
    // in order to ensure that the choice is deterministic.
    Iterator commonSuperClasses = superClasses1.iterator();
    while (commonSuperClasses.hasNext()) {
      Clazz commonSuperClass = (Clazz) commonSuperClasses.next();

      int superClassCount = superClassCount(commonSuperClass, superClasses1);
      if (maximumSuperClassCount < superClassCount
          || (maximumSuperClassCount == superClassCount
              && commonClass != null
              && commonClass.getName().compareTo(commonSuperClass.getName()) > 0)) {
        commonClass = commonSuperClass;
        maximumSuperClassCount = superClassCount;
      }
    }

    if (commonClass == null) {
      throw new IllegalArgumentException(
          "Can't find common super class of ["
              + class1.getName()
              + "] (with "
              + superClasses1Count
              + " known super classes) and ["
              + class2.getName()
              + "] (with "
              + superClasses2Count
              + " known super classes)");
    }

    if (DEBUG) {
      System.out.println("  Best common class: [" + commonClass.getName() + "]");
    }

    return commonClass;
  }
 /**
  * Get the set of fields from the filter's field ordering which were omitted due to being empty.
  *
  * @return the set of empty fields which were omitted from the ordering
  */
 public Set<Field> getOmittedEmptyFields() {
   Set<Field> empties = EnumSet.copyOf(columnOrdering.getFields());
   empties.retainAll(emptyFields);
   return empties;
 }
Esempio n. 29
0
  public void step() {
    double chancetoTakeBest =
        Math.random(); // I don't like this, it s/b based upon pg. 222, but temp for random movement
    movementsMade++;

    discoveredFood.retainAll(antColony.getFood());

    if (this.goToNest) // has found ALL food and is working way back home.
    {
      if (this.antNetwork[this.x][this.y].hasNest()) die(); // ant "dies" upon return to the nest
      else {
        double currentMaxNestPheromone = 0;
        Map<WorldGridSquare, Double> maxFoodMap = new HashMap<WorldGridSquare, Double>();
        List<WorldGridSquare> maxNestGridSquaresList = new ArrayList<WorldGridSquare>();
        List<WorldGridSquare> allNeighborGridSquaresList = new ArrayList<WorldGridSquare>();
        double totalNeighborPheromones = 0;

        for (int c = -1; c <= 1; c++) {
          if (this.x + c < 0 || this.x + c >= antNetwork.length) continue;

          for (int r = -1; r <= 1; r++) {
            // ignore self, edges
            if (c == 0 && r == 0) continue;
            if (y + r < 0 || y + r >= antNetwork[0].length) continue;

            if (!antNetwork[this.x + c][this.y + r].isBlocked()) {
              allNeighborGridSquaresList.add(antNetwork[this.x + c][this.y + r]);
              totalNeighborPheromones += antNetwork[this.x + c][this.y + r].nestPheromoneLevel;

              if (antNetwork[this.x + c][this.y + r].getNestPheromoneLevel()
                  > currentMaxNestPheromone) {
                currentMaxNestPheromone =
                    antNetwork[this.x + c][this.y + r].getNestPheromoneLevel();
                maxNestGridSquaresList.clear();
                maxNestGridSquaresList.add(antNetwork[this.x + c][this.y + r]);
              } else if (antNetwork[this.x + c][this.y + r].getNestPheromoneLevel()
                  == currentMaxNestPheromone)
                maxNestGridSquaresList.add(antNetwork[this.x + c][this.y + r]);

              for (WorldGridSquare food : discoveredFood) {
                if (!maxFoodMap.containsKey(food)
                    || antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food)
                        > maxFoodMap.get(food))
                  maxFoodMap.put(
                      food, antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food));
              }
            }
          }
        }

        if (antNetwork[x][y].isFood())
          maxFoodMap.put(antNetwork[this.x][this.y], WorldGridSquare.maxFoodPheromoneLevel);

        for (WorldGridSquare food : discoveredFood) {
          antNetwork[this.x][this.y].setFoodPheromone(food, maxFoodMap.get(food) * Ant.dropOffRate);
        }

        // There's a % chance, essentially, that the Ant will choose the best route
        if (Ant.bestNextSquare > chancetoTakeBest) {
          if (!maxNestGridSquaresList.isEmpty()) {
            int randBestGSIndex = (int) (maxNestGridSquaresList.size() * Math.random());
            WorldGridSquare bestGridSquare = maxNestGridSquaresList.get(randBestGSIndex);

            this.x = bestGridSquare.column;
            this.y = bestGridSquare.row;
          }
        } else // if random didn't result in the best route, choose a (partially) random alternate
        {
          double currentPheromones = 0;
          double randPheromoneLevel = totalNeighborPheromones * Math.random();
          for (WorldGridSquare neighbor : allNeighborGridSquaresList) {
            currentPheromones += neighbor.getNestPheromoneLevel();
            if (currentPheromones > randPheromoneLevel) {
              this.x = neighbor.column;
              this.y = neighbor.row;
              break;
            }
          }
        }
      }
    } else // go hunting for food in the wild
    {
      if (antNetwork[this.x][this.y].isFood()) {
        discoveredFood.add(antNetwork[this.x][this.y]);
        if (discoveredFood.size() >= antColony.getFood().size()) {
          movementsMade = 0; // reset the track
          goToNest = true; // start heading home
          return;
        }
      } else if (antNetwork[this.x][this.y].hasNest()) {
        if (movementsMade > 1) {
          die();
          return;
        }
      }

      double currentMaxFoodPheromone = 0;
      double currentMaxNestPheromone = 0;

      Map<WorldGridSquare, Double> maxFoodMap = new HashMap<WorldGridSquare, Double>();
      List<WorldGridSquare> maxFoodGridSquaresList = new ArrayList<WorldGridSquare>();
      List<WorldGridSquare> allNeighborGridSquaresList = new ArrayList<WorldGridSquare>();
      double totalNeighborPheromones = 0;

      for (int c = -1; c <= 1; c++) {
        if (this.x + c < 0 || this.x + c >= antNetwork.length) continue;

        for (int r = -1; r <= 1; r++) {
          // ignore self, edges
          if (c == 0 && r == 0) continue;
          if (this.y + r < 0 || this.y + r >= antNetwork[0].length) continue;

          if (!antNetwork[this.x + c][this.y + r].isBlocked()) {
            allNeighborGridSquaresList.add(antNetwork[this.x + c][this.y + r]);

            if (currentMaxFoodPheromone == 0)
              maxFoodGridSquaresList.add(antNetwork[this.x + c][this.y + r]);

            for (WorldGridSquare food : discoveredFood) {
              if (!maxFoodMap.containsKey(food)
                  || antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food)
                      > maxFoodMap.get(food))
                maxFoodMap.put(
                    food, antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food));
            }

            if (antNetwork[this.x][this.y].isFood())
              maxFoodMap.put(antNetwork[this.x][this.y], WorldGridSquare.maxFoodPheromoneLevel);

            for (WorldGridSquare food : discoveredFood) {
              antNetwork[this.x][this.y].setFoodPheromone(
                  food, maxFoodMap.get(food) * Ant.dropOffRate);
            }

            if (antNetwork[this.x + c][this.y + r].getNestPheromoneLevel()
                > currentMaxNestPheromone) {
              currentMaxNestPheromone = antNetwork[this.x + c][this.y + r].getNestPheromoneLevel();
            }

            if (antColony.getFood().isEmpty()) totalNeighborPheromones += 1;
            else {
              for (WorldGridSquare food : antColony.getFood()) {
                if (discoveredFood.contains(food)) continue;

                totalNeighborPheromones +=
                    antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food);

                if (antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food)
                    > currentMaxFoodPheromone) {
                  currentMaxFoodPheromone =
                      antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food);
                  maxFoodGridSquaresList.clear();
                  maxFoodGridSquaresList.add(antNetwork[this.x + c][this.y + r]);
                } else if (antNetwork[this.x + c][this.y + r].getFoodPheromoneLevel(food)
                    == currentMaxFoodPheromone) {
                  maxFoodGridSquaresList.add(antNetwork[this.x + c][this.y + r]);
                }
              }
            }
          }
        }
      }

      if (antNetwork[this.x][this.y].hasNest())
        currentMaxNestPheromone = WorldGridSquare.maxNestPheromoneLevel;

      antNetwork[this.x][this.y].setNestPheromone(currentMaxNestPheromone * Ant.dropOffRate);

      if (Ant.bestNextSquare > chancetoTakeBest) {
        if (!maxFoodGridSquaresList.isEmpty()) {
          int randBestGSIndex = (int) (maxFoodGridSquaresList.size() * Math.random());
          WorldGridSquare bestGridSquare = maxFoodGridSquaresList.get(randBestGSIndex);

          this.x = bestGridSquare.column;
          this.y = bestGridSquare.row;
        }
      } else // if random didn't result in the best route, choose a (partially) random alternate
      {
        double currentPheromones = 0;
        double randPheromoneLevel = totalNeighborPheromones * Math.random();

        for (WorldGridSquare neighbor : allNeighborGridSquaresList) {
          if (antColony.getFood().isEmpty()) {
            currentPheromones += 1;
            if (currentPheromones > randPheromoneLevel) {
              this.x = neighbor.column;
              this.y = neighbor.row;
              break;
            }
          } else {
            for (WorldGridSquare food : antColony.getFood()) {
              if (discoveredFood.contains(food)) continue;
              currentPheromones += neighbor.getFoodPheromoneLevel(food);
              if (currentPheromones > randPheromoneLevel) {
                this.x = neighbor.column;
                this.y = neighbor.row;
                break;
              }
            }
          }
        }
      }
    }
  }
 @Override
 public boolean retainAll(Collection<?> c) {
   return s.retainAll(c);
 }