Example usage for com.google.common.collect Multiset iterator

List of usage examples for com.google.common.collect Multiset iterator

Introduction

In this page you can find the example usage for com.google.common.collect Multiset iterator.

Prototype

@Override
Iterator<E> iterator();

Source Link

Document

Elements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.

Usage

From source file:com.music.service.text.TimelineToMusicService.java

private Variation getVariation(List<Tweet> tweets, TimelineMusic meta) {
    Morphology morphology = new Morphology(new StringReader(""));
    Multiset<String> words = HashMultiset.create();
    for (Tweet tweet : tweets) {
        String tweetText = tweet.getText().toLowerCase();
        List<String> urls = TimelineToMusicService.extractUrls(tweetText);
        for (String url : urls) {
            tweetText = tweetText.replace(url, "");
        }//from  w  w w . j  av  a2 s.com
        List<String> usernames = TimelineToMusicService.extractMentionedUsernames(tweetText);
        for (String username : usernames) {
            tweetText = tweetText.replace(username, "").replace("rt", "");
        }

        String[] wordsInTweet = tweetText.split("[^\\p{L}&&[^']]+");
        for (String word : wordsInTweet) {
            try {
                words.add(morphology.stem(word));
            } catch (Exception ex) {
                words.add(word);
            }
        }
    }
    words.removeAll(stopwords);

    // if a word is mentioned more times than is 4% of the tweets, it's considered a topic
    double topicThreshold = tweets.size() * 4 / 100;
    for (Iterator<String> it = words.iterator(); it.hasNext();) {
        String word = it.next();
        // remove stopwords not in the list (e.g. in a different language).
        // We consider all words less than 4 characters to be stop words
        if (word == null || word.length() < 4) {
            it.remove();
        } else if (words.count(word) < topicThreshold) {
            it.remove();
        }
    }

    meta.setTopKeywords(new HashSet<>(words.elementSet()));

    // the more topics you have, the more variative music
    if (meta.getTopKeywords().size() > 40) {
        return Variation.EXTREMELY_VARIATIVE;
    } else if (meta.getTopKeywords().size() > 30) {
        return Variation.VERY_VARIATIVE;
    } else if (meta.getTopKeywords().size() > 20) {
        return Variation.MOVING;
    } else if (meta.getTopKeywords().size() > 10) {
        return Variation.AVERAGE;
    } else {
        return Variation.MONOTONOUS;
    }
}

From source file:BibTex.IOmethods.java

public void writeCategoriesAndTheirPapersToFile(Set<BibTexRef> refs) throws IOException {
    BufferedWriter bw = new BufferedWriter(new FileWriter(folder + "refs per categories.csv"));
    StringBuilder sb = new StringBuilder();
    String sep = "|";

    //creation of 2 convenient data structures for I/O
    Map<String, Multiset<String>> categoriesToPapers = new TreeMap();
    List<String> categoryNames = new ArrayList();

    for (BibTexRef ref : refs) {
        Set<Category> categories = ref.getCategories();

        for (Category category : categories) {
            if (!categoryNames.contains(category.getCategoryName())) {
                categoryNames.add(category.getCategoryName());
            }/*from w w w. j a  v  a2 s.  c o  m*/
            if (categoriesToPapers.containsKey(category.getCategoryName())) {
                categoriesToPapers.get(category.getCategoryName()).add(ref.toBibliographicalFormattedString());
            } else {
                Multiset<String> papersForOneCategory = HashMultiset.create();
                papersForOneCategory.add(ref.toBibliographicalFormattedString());

                categoriesToPapers.put(category.getCategoryName(), papersForOneCategory);
            }
        }

    }
    Collections.sort(categoryNames);

    //writing of the first line of the csv: headers of the categories.
    for (String categoryName : categoryNames) {
        sb.append(categoryName);
        sb.append(sep);
    }
    sb.append("\n");

    //writing of all subsequent lines: one per year
    int countCategoriesdone = 0;
    boolean continueLoop = true;
    while (continueLoop) {

        for (Iterator<String> it = categoriesToPapers.keySet().iterator(); it.hasNext();) {
            String category = it.next();
            Multiset<String> papersForOneCategory = categoriesToPapers.get(category);
            Iterator<String> papersIterator = papersForOneCategory.iterator();
            if (papersIterator.hasNext()) {
                String string = papersIterator.next();
                sb.append(string).append(sep);
                papersIterator.remove();
            } else {
                sb.append(sep);
            }
        }
        sb.append("\n");

        for (String cat : categoriesToPapers.keySet()) {
            if (categoriesToPapers.get(cat).isEmpty()) {
                countCategoriesdone++;
            }
        }
        if (countCategoriesdone == categoryNames.size()) {
            continueLoop = false;
        } else {
            countCategoriesdone = 0;
        }

    }

    bw.write(sb.toString());
    bw.close();

}

From source file:org.cspoker.ai.opponentmodels.weka.Propositionalizer.java

public void signalCardShowdown(Object id, Card card1, Card card2) {
    //      PlayerData p = players.get(id);
    if (cards.size() == 5) {
        //showdown after river
        Multiset<Integer> ranks = new TreeMultiset<Integer>();
        //         int minSampleRank = Integer.MAX_VALUE;
        //         int maxSampleRank = Integer.MIN_VALUE;
        //         int sum = 0;

        int startRank = 53;
        for (Card card : cards) {
            startRank = handRanks[card.ordinal() + 1 + startRank];
        }//from w  ww .  j  a v  a  2 s .  com

        //add real rank
        int realRank = startRank;
        realRank = handRanks[card1.ordinal() + 1 + realRank];
        realRank = handRanks[card2.ordinal() + 1 + realRank];
        int realType = (realRank >>> 12) - 1;
        realRank = realRank & 0xFFF;
        realRank = offsets[realType] + realRank - 1;

        //take rank samples
        int nbBuckets = 6;
        int nbSamplesPerBucket = 6;
        int nbSamples = nbBuckets * nbSamplesPerBucket;
        for (int i = 0; i < nbSamples; i++) {

            int rank = startRank;
            Card sampleCard1;
            do {
                sampleCard1 = Card.values()[random.nextInt(Card.values().length)];
            } while (cards.contains(sampleCard1));
            rank = handRanks[sampleCard1.ordinal() + 1 + rank];

            Card sampleCard2;
            do {
                sampleCard2 = Card.values()[random.nextInt(Card.values().length)];
            } while (cards.contains(sampleCard2) || sampleCard2.equals(sampleCard1));
            rank = handRanks[sampleCard2.ordinal() + 1 + rank];

            int type = (rank >>> 12) - 1;
            rank = rank & 0xFFF;
            rank = offsets[type] + rank - 1;

            ranks.add(rank);

            //            if(rank<minSampleRank){
            //               minSampleRank = rank;
            //            }
            //            if(rank>maxSampleRank){
            //               maxSampleRank = rank;
            //            }
            //            sum += rank;
        }
        //         double var = 0;
        //         double mean = ((double)sum)/nbSamples;
        //         for (Multiset.Entry<Integer> entry : ranks.entrySet()) {
        //            double diff = mean - entry.getElement();
        //            var += diff * diff * entry.getCount();
        //         }
        //         var /= (nbSamples-1);
        //         int averageSampleRank = (int) Math.round(mean);
        //         int sigmaSampleRank = (int) Math.round(Math.sqrt(var));
        int[] bucketCounts = new int[nbBuckets];
        Iterator<Integer> iter = ranks.iterator();
        double realRankCount = ranks.count(realRank);
        //         long avgBucket = -1;
        double[] bucketDistr = new double[nbBuckets];
        if (realRankCount > 0) {
            for (int bucket = 0; bucket < nbBuckets; bucket++) {
                for (int i = 0; i < nbSamplesPerBucket; i++) {
                    int rank = iter.next();
                    if (rank == realRank) {
                        ++bucketCounts[bucket];
                    }
                }
            }
            int partitionSum = 0;
            for (int i = 0; i < nbBuckets; i++) {
                bucketDistr[i] = bucketCounts[i] / realRankCount;
                partitionSum += bucketCounts[i] * i;
            }
            //         avgBucket = Math.round(partitionSum/realRankCount);
        } else {
            boolean found = false;
            bucketIteration: for (int bucket = 0; bucket < nbBuckets; bucket++) {
                for (int i = 0; i < nbSamplesPerBucket; i++) {
                    int rank = iter.next();
                    if (rank > realRank) {
                        bucketDistr[bucket] = 1;
                        //            avgBucket = bucket;
                        found = true;
                        break bucketIteration;
                    }
                }
            }
            if (!found) {
                bucketCounts[nbBuckets - 1] = 1;
                //            avgBucket = nbBuckets-1;
            }
        }
        logShowdown(id, bucketDistr);
    } else {
        //ignore
        //throw new IllegalStateException("everybody went all-in before the river");
    }
}

From source file:bots.mctsbot.ai.opponentmodels.weka.Propositionalizer.java

public void signalCardShowdown(Object id, Card card1, Card card2) {
    //      PlayerData p = players.get(id);
    if (cards.size() == 5) {
        //showdown after river
        Multiset<Integer> ranks = new TreeMultiset<Integer>();
        //         int minSampleRank = Integer.MAX_VALUE;
        //         int maxSampleRank = Integer.MIN_VALUE;
        //         int sum = 0;

        int startRank = 53;
        for (int i = 0; i < cards.size(); i++) {
            startRank = handRanks[CardConverter.toSpears2p2Index(cards.getCard(i + 1)) + startRank];
        }// ww  w.j  a v  a  2  s.  com

        //add real rank
        int realRank = startRank;
        realRank = handRanks[CardConverter.toSpears2p2Index(card1) + realRank];
        realRank = handRanks[CardConverter.toSpears2p2Index(card2) + realRank];
        int realType = (realRank >>> 12) - 1;
        realRank = realRank & 0xFFF;
        realRank = offsets[realType] + realRank - 1;

        //take rank samples
        int nbBuckets = 6;
        int nbSamplesPerBucket = 6;
        int nbSamples = nbBuckets * nbSamplesPerBucket;
        for (int i = 0; i < nbSamples; i++) {

            int rank = startRank;
            Card sampleCard1;
            do {
                sampleCard1 = new Card(random.nextInt(52));
            } while (cards.contains(sampleCard1));
            rank = handRanks[CardConverter.toSpears2p2Index(sampleCard1) + rank];

            Card sampleCard2;
            do {
                sampleCard2 = new Card(random.nextInt(52));
            } while (cards.contains(sampleCard2) || sampleCard2.equals(sampleCard1));
            rank = handRanks[CardConverter.toSpears2p2Index(sampleCard2) + rank];

            int type = (rank >>> 12) - 1;
            rank = rank & 0xFFF;
            rank = offsets[type] + rank - 1;

            ranks.add(rank);

            //            if(rank<minSampleRank){
            //               minSampleRank = rank;
            //            }
            //            if(rank>maxSampleRank){
            //               maxSampleRank = rank;
            //            }
            //            sum += rank;
        }
        //         double var = 0;
        //         double mean = ((double)sum)/nbSamples;
        //         for (Multiset.Entry<Integer> entry : ranks.entrySet()) {
        //            double diff = mean - entry.getElement();
        //            var += diff * diff * entry.getCount();
        //         }
        //         var /= (nbSamples-1);
        //         int averageSampleRank = (int) Math.round(mean);
        //         int sigmaSampleRank = (int) Math.round(Math.sqrt(var));
        int[] bucketCounts = new int[nbBuckets];
        Iterator<Integer> iter = ranks.iterator();
        double realRankCount = ranks.count(realRank);
        //         long avgBucket = -1;
        double[] bucketDistr = new double[nbBuckets];
        if (realRankCount > 0) {
            for (int bucket = 0; bucket < nbBuckets; bucket++) {
                for (int i = 0; i < nbSamplesPerBucket; i++) {
                    int rank = iter.next();
                    if (rank == realRank) {
                        ++bucketCounts[bucket];
                    }
                }
            }
            int partitionSum = 0;
            for (int i = 0; i < nbBuckets; i++) {
                bucketDistr[i] = bucketCounts[i] / realRankCount;
                partitionSum += bucketCounts[i] * i;
            }
            //         avgBucket = Math.round(partitionSum/realRankCount);
        } else {
            boolean found = false;
            bucketIteration: for (int bucket = 0; bucket < nbBuckets; bucket++) {
                for (int i = 0; i < nbSamplesPerBucket; i++) {
                    int rank = iter.next();
                    if (rank > realRank) {
                        bucketDistr[bucket] = 1;
                        //            avgBucket = bucket;
                        found = true;
                        break bucketIteration;
                    }
                }
            }
            if (!found) {
                bucketCounts[nbBuckets - 1] = 1;
                //            avgBucket = nbBuckets-1;
            }
        }
        logShowdown(id, bucketDistr);
    } else {
        //ignore
        //throw new IllegalStateException("everybody went all-in before the river");
    }
}