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

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

Introduction

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

Prototype

int count(@Nullable Object element);

Source Link

Document

Returns the number of occurrences of an element in this multiset (the count of the element).

Usage

From source file:edu.umd.cs.submitServer.servlets.AnalyzeArchives.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Connection conn = null;//  w ww.  j  a  v  a2  s.c o m
    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();

    Project project = (Project) request.getAttribute("project");
    Course course = (Course) request.getAttribute("course");

    long totalArchiveSpace = 0;
    long totalDistinctArchiveSpace = 0;
    HashSet<Integer> seen = new HashSet<Integer>();

    HashMap<String, FileContents> archiveContents = new HashMap<String, FileContents>();
    Multiset<String> files = HashMultiset.create();
    Multiset<String> checksums = HashMultiset.create();
    try {
        conn = getConnection();
        List<Integer> archives = Submission.getAllArchivesForProject(project.getProjectPK(), conn);
        writer.printf("Analyzing %d submissions for %s project %s%n", archives.size(), course.getCourseName(),
                project.getProjectNumber());
        for (Integer archivePK : archives) {
            byte[] bytes = Archive.downloadBytesFromArchive((String) Submission.SUBMISSION_ARCHIVES,
                    (Integer) archivePK, (Connection) conn);
            totalArchiveSpace += bytes.length;
            if (!seen.add(archivePK))
                continue;
            totalDistinctArchiveSpace += bytes.length;
            TreeMap<String, byte[]> contents = Archive.unzip(new ByteArrayInputStream(bytes));

            for (Map.Entry<String, byte[]> e : contents.entrySet()) {
                byte[] archiveBytes = e.getValue();
                String checksum = Checksums.getChecksum(archiveBytes);
                String name = e.getKey();
                files.add(name);
                checksums.add(checksum);
                FileContents info = archiveContents.get(checksum);
                if (info == null) {

                    info = new FileContents(name, TextUtilities.isText(TextUtilities.simpleName(name)),
                            archiveBytes.length, checksum, null);
                    archiveContents.put(checksum, info);
                }

            }

        }

    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        releaseConnection(conn);
    }
    long totalSize = 0;
    TreeSet<FileContents> ordered = new TreeSet<FileContents>(archiveContents.values());
    writer.printf("%5s %9s %s%n", "#", "size", "name");

    String prevName = null;
    for (FileContents info : ordered) {
        if (prevName == null || !prevName.equals(info.name)) {
            if (prevName != null)
                writer.println();
            writer.printf("%5d %9s %s%n", files.count(info.name), " ", info.name);
            prevName = info.name;
        }
        int count = checksums.count(info.checksum);
        writer.printf("%5d %9d %s%n", count, info.size, info.name);
        totalSize += info.size;
    }
    writer.printf("%n");
    writer.printf("%d distinct archives%n", seen.size());
    writer.printf("%d distinct files%n", files.elementSet().size());
    writer.printf("%d total files%n", files.size());

    writer.printf("%d bytes in distinct archives%n", totalDistinctArchiveSpace);
    writer.printf("%d bytes in repeated archives%n", totalArchiveSpace);
    writer.printf("%d bytes as files%n", totalSize);
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

private TEIValidator.Errors extractPreparationSteps(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = preparationSchemeURL == null ? new TEIPreparationValidator()
            : new FromURLValidator(preparationSchemeURL);
    Multiset<String> knownPreparationTitles = HashMultiset.create();

    File documentDir = new File(outputDirectory, "PreparationHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }//from w w  w  .  j av a  2s.c  o  m

    Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));
    Element preparationRoot = Preconditions
            .checkNotNull(doc.getRootElement().getChild("teiCorpus", null).getChild("teiCorpus", null));

    for (Element preparationHeader : preparationRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("PreparationHeader".equals(preparationHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(preparationHeader.clone());
        Document newDoc = new Document(tei);

        if (preparationSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIPreparationValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0,
                    new ProcessingInstruction("xml-model", "href=\"" + preparationSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));
        Element title = Preconditions.checkNotNull(titleStmt.getChild("title", null));
        String corresp = title.getAttributeValue("corresp");
        if (corresp != null) {
            if (knownPreparationTitles.contains(corresp)) {
                knownPreparationTitles.add(corresp);
                outName = corresp + "_" + knownPreparationTitles.count(corresp);
                log.warn(messages.getString("MORE THAN ONE PREPARATION HEADER"), corresp);
            } else {
                outName = corresp;
                knownPreparationTitles.add(corresp);
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN PREPARATION HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:org.dllearner.algorithms.qtl.experiments.PRConvergenceExperiment.java

private RDFResourceTree applyBaseLine(ExamplesWrapper examples, Baseline baselineApproach) {
    logger.info("Computing baseline...");
    Collection<RDFResourceTree> posExamples = examples.posExamplesMapping.values();
    Collection<RDFResourceTree> negExamples = examples.negExamplesMapping.values();

    RDFResourceTree solution = null;// www.j a  v a2s .com

    switch (baselineApproach) {
    case RANDOM:// 1.
        String query = "SELECT ?cls WHERE {?cls a owl:Class .} ORDER BY RAND() LIMIT 1";
        QueryExecution qe = qef.createQueryExecution(query);
        ResultSet rs = qe.execSelect();
        if (rs.hasNext()) {
            QuerySolution qs = rs.next();
            Resource cls = qs.getResource("cls");
            solution = new RDFResourceTree();
            solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode());
        }
        break;
    case MOST_POPULAR_TYPE_IN_KB:// 2.
        query = "SELECT ?cls WHERE {?cls a owl:Class . ?s a ?cls .} ORDER BY DESC(COUNT(?s)) LIMIT 1";
        qe = qef.createQueryExecution(query);
        rs = qe.execSelect();
        if (rs.hasNext()) {
            QuerySolution qs = rs.next();
            Resource cls = qs.getResource("cls");
            solution = new RDFResourceTree();
            solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode());
        }
        break;
    case MOST_FREQUENT_TYPE_IN_EXAMPLES:// 3.
        Multiset<Node> types = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            List<RDFResourceTree> children = ex.getChildren(RDF.type.asNode());
            for (RDFResourceTree child : children) {
                types.add(child.getData());
            }
        }
        Node mostFrequentType = Ordering.natural().onResultOf(new Function<Multiset.Entry<Node>, Integer>() {
            @Override
            public Integer apply(Multiset.Entry<Node> entry) {
                return entry.getCount();
            }
        }).max(types.entrySet()).getElement();
        solution = new RDFResourceTree();
        solution.addChild(new RDFResourceTree(mostFrequentType), RDF.type.asNode());
        break;
    case MOST_FREQUENT_EDGE_IN_EXAMPLES:// 4.
        Multiset<Pair<Node, Node>> pairs = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            SortedSet<Node> edges = ex.getEdges();
            for (Node edge : edges) {
                List<RDFResourceTree> children = ex.getChildren(edge);
                for (RDFResourceTree child : children) {
                    pairs.add(new Pair<>(edge, child.getData()));
                }
            }
        }
        Pair<Node, Node> mostFrequentPair = Ordering.natural()
                .onResultOf(new Function<Multiset.Entry<Pair<Node, Node>>, Integer>() {
                    @Override
                    public Integer apply(Multiset.Entry<Pair<Node, Node>> entry) {
                        return entry.getCount();
                    }
                }).max(pairs.entrySet()).getElement();
        solution = new RDFResourceTree();
        solution.addChild(new RDFResourceTree(mostFrequentPair.getValue()), mostFrequentPair.getKey());
        break;
    case MOST_INFORMATIVE_EDGE_IN_EXAMPLES:
        // get all p-o in pos examples
        Multiset<Pair<Node, Node>> edgeObjectPairs = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            SortedSet<Node> edges = ex.getEdges();
            for (Node edge : edges) {
                List<RDFResourceTree> children = ex.getChildren(edge);
                for (RDFResourceTree child : children) {
                    edgeObjectPairs.add(new Pair<>(edge, child.getData()));
                }
            }
        }

        double bestAccuracy = -1;
        solution = new RDFResourceTree();

        for (Pair<Node, Node> pair : edgeObjectPairs.elementSet()) {
            Node edge = pair.getKey();
            Node childValue = pair.getValue();

            // compute accuracy
            int tp = edgeObjectPairs.count(pair);
            int fn = posExamples.size() - tp;
            int fp = 0;
            for (RDFResourceTree ex : negExamples) { // compute false positives
                List<RDFResourceTree> children = ex.getChildren(edge);
                if (children != null) {
                    for (RDFResourceTree child : children) {
                        if (child.getData().equals(childValue)) {
                            fp++;
                            break;
                        }
                    }
                }
            }
            int tn = negExamples.size() - fp;

            double accuracy = Heuristics.getPredictiveAccuracy(posExamples.size(), negExamples.size(), tp, tn,
                    1.0);
            // update best solution
            if (accuracy >= bestAccuracy) {
                solution = new RDFResourceTree();
                solution.addChild(new RDFResourceTree(childValue), edge);
                bestAccuracy = accuracy;
            }
        }
        break;
    case LGG:
        LGGGenerator lggGenerator = new LGGGeneratorSimple();
        solution = lggGenerator.getLGG(Lists.newArrayList(posExamples));
        break;
    default:
        break;
    }
    logger.info("Baseline solution:\n" + owlRenderer.render(QueryTreeUtils.toOWLClassExpression(solution)));

    return solution;
}

From source file:it.cnr.isti.hpc.dexter.disambiguation.TurkishEntityDisambiguator.java

@Override
public EntityMatchList disambiguate(DexterLocalParams localParams, SpotMatchList sml) {
    entityScoreMap = new HashMap<String, EntityScores>();
    selectedEntities = new HashSet<String>();
    Multiset<String> entityFrequencyMultiset = HashMultiset.create();

    EntityMatchList entities = sml.getEntities();
    String inputText = localParams.getParams().get("text");
    String algorithm = Property.getInstance().get("algorithm");

    String ambigious = Property.getInstance().get("algorithm.ambigious");

    List<Token> inputTokens = Zemberek.getInstance().disambiguateFindTokens(inputText, false, true);
    List<Double> documentVector = DescriptionEmbeddingAverage.getAverageVectorList(inputText);
    Multiset<String> inputTokensMultiset = HashMultiset.create();
    for (Token token : inputTokens) {
        inputTokensMultiset.add(token.getMorphText());
    }//  w ww .j av a2 s  .c o m

    Multiset<String> domainMultiset = HashMultiset.create();
    Multiset<String> typeMultiset = HashMultiset.create();
    HashMap<String, Double> entitySimMap = new HashMap<String, Double>();
    // if (printCandidateEntities) {
    // printEntities(entities);
    // }
    HashSet<String> words = new HashSet<String>();
    Multiset<String> leskWords = HashMultiset.create();

    // first pass for finding number of types and domains
    for (int i = 0; i < entities.size(); i++) {
        EntityMatch em = entities.get(i);
        String id = em.getId();
        if (!entityFrequencyMultiset.contains(id)) {
            entityFrequencyMultiset.add(id);
            Entity entity = em.getEntity();
            words.add(entity.getShingle().getText());
            String type = entity.getPage().getType();
            if (type != null && type.length() > 0) {
                typeMultiset.add(type);
            }
            String domain = entity.getPage().getDomain();
            if (domain != null && domain.length() > 0) {
                domainMultiset.add(domain);
            }

            String desc = entity.getPage().getDescription();
            List<Token> tokens = Zemberek.getInstance().disambiguateFindTokens(desc, false, true);
            for (Token token : tokens) {
                leskWords.add(token.getMorphText());
            }

        } else {
            entityFrequencyMultiset.add(id);
        }
    }

    int maxDomainCount = 0;
    for (String domain : Multisets.copyHighestCountFirst(domainMultiset).elementSet()) {
        maxDomainCount = domainMultiset.count(domain);
        break;
    }
    int maxTypeCount = 0;
    for (String type : Multisets.copyHighestCountFirst(typeMultiset).elementSet()) {
        maxTypeCount = typeMultiset.count(type);
        break;
    }

    double maxSuffixScore = 0, maxLeskScore = 0, maxSimpleLeskScore = 0, maxLinkScore = 0,
            maxHashInfoboxScore = 0, maxwordvecDescriptionLocalScore = 0, maxHashDescriptionScore = 0,
            maxPopularityScore = 0, maxWordvectorAverage = 0, maxWordvecLinksScore = 0;
    // second pass compute similarities between entities in a window
    int currentSpotIndex = -1;
    SpotMatch currentSpot = null;
    for (int i = 0; i < entities.size(); i++) {
        EntityMatch em = entities.get(i);
        SpotMatch spot = em.getSpot();
        if (currentSpot == null || spot != currentSpot) {
            currentSpotIndex++;
            currentSpot = spot;
        }

        String id = em.getId();
        Entity entity = entities.get(i).getEntity();
        EntityPage page = entities.get(i).getEntity().getPage();
        String domain = page.getDomain();
        String type = page.getType();
        Shingle shingle = entity.getShingle();

        /* windowing algorithms stars */
        int left = currentSpotIndex - window;
        int right = currentSpotIndex + window;
        if (left < 0) {
            right -= left;
            left = 0;
        }
        if (right > sml.size()) {
            left += (sml.size()) - right;
            right = sml.size();
            if (left < 0) {
                left = 0;
            }
        }

        double linkScore = 0, hashInfoboxScore = 0, wordvecDescriptionLocalScore = 0, hashDescriptionScore = 0,
                wordvecLinksScore = 0;
        for (int j = left; j < right; j++) {
            SpotMatch sm2 = sml.get(j);
            EntityMatchList entities2 = sm2.getEntities();
            for (EntityMatch em2 : entities2) {
                String id2 = em2.getId();
                EntityPage page2 = em2.getEntity().getPage();
                int counter = 0;
                if (!ambigious.equals("true")) {
                    for (EntityMatch entityMatch : entities2) {
                        if (entityMatch.getId().startsWith("w")) {
                            counter++;
                        }
                    }
                }

                if ((ambigious.equals("true") || counter == 1) && em.getSpot() != em2.getSpot()
                        && !id.equals(id2)) {
                    // Link Similarity calculation starts
                    double linkSim = 0;
                    if (id.startsWith("w") && id2.startsWith("w")) {
                        if (entitySimMap.containsKey("link" + id + id2)) {
                            linkSim = entitySimMap.get("link" + id + id2);
                        } else {
                            HashSet<String> set1 = Sets.newHashSet(page.getLinks().split(" "));
                            HashSet<String> set2 = Sets.newHashSet(page2.getLinks().split(" "));
                            linkSim = JaccardCalculator.calculateSimilarity(set1, set2);
                            entitySimMap.put("link" + id + id2, linkSim);
                        }
                        linkScore += linkSim;
                        // Link Similarity calculation ends
                    }
                    // Entity embedding similarity calculation starts
                    double eeSim = 0;
                    if (id.startsWith("w") && id2.startsWith("w")) {
                        if (entitySimMap.containsKey("ee" + id + id2)) {
                            eeSim = entitySimMap.get("ee" + id + id2);
                        } else {
                            eeSim = EntityEmbeddingSimilarity.getInstance().getSimilarity(page, page2);
                            entitySimMap.put("ee" + id + id2, eeSim);
                        }
                        hashInfoboxScore += eeSim;
                    }
                    double w2veclinksSim = 0;
                    if (id.startsWith("w") && id2.startsWith("w")) {
                        if (entitySimMap.containsKey("wl" + id + id2)) {
                            w2veclinksSim = entitySimMap.get("wl" + id + id2);
                        } else {
                            w2veclinksSim = AveragePooling.getInstance().getSimilarity(page.getWord2vec(),
                                    page2.getWord2vec());
                            entitySimMap.put("wl" + id + id2, w2veclinksSim);
                        }
                        wordvecLinksScore += w2veclinksSim;
                    }

                    // Entity embedding similarity calculation ends

                    // Description word2vec similarity calculation
                    // starts
                    double word2vecSim = 0;

                    if (entitySimMap.containsKey("w2v" + id + id2)) {
                        word2vecSim = entitySimMap.get("w2v" + id + id2);
                    } else {
                        word2vecSim = AveragePooling.getInstance().getSimilarity(page2.getDword2vec(),
                                page.getDword2vec());
                        entitySimMap.put("w2v" + id + id2, word2vecSim);
                    }
                    wordvecDescriptionLocalScore += word2vecSim;
                    // Description word2vec similarity calculation ends

                    // Description autoencoder similarity calculation
                    // starts
                    double autoVecSim = 0;

                    if (entitySimMap.containsKey("a2v" + id + id2)) {
                        autoVecSim = entitySimMap.get("a2v" + id + id2);
                    } else {
                        autoVecSim = AveragePooling.getInstance().getSimilarity(page2.getDautoencoder(),
                                page.getDautoencoder());
                        entitySimMap.put("a2v" + id + id2, autoVecSim);
                    }
                    hashDescriptionScore += autoVecSim;
                    // Description autoencoder similarity calculation
                    // ends

                }
            }
        }
        if (linkScore > maxLinkScore) {
            maxLinkScore = linkScore;
        }
        if (hashInfoboxScore > maxHashInfoboxScore) {
            maxHashInfoboxScore = hashInfoboxScore;
        }
        if (wordvecDescriptionLocalScore > maxwordvecDescriptionLocalScore) {
            maxwordvecDescriptionLocalScore = wordvecDescriptionLocalScore;
        }
        if (hashDescriptionScore > maxHashDescriptionScore) {
            maxHashDescriptionScore = hashDescriptionScore;
        }
        if (wordvecLinksScore > maxWordvecLinksScore) {
            maxWordvecLinksScore = wordvecLinksScore;
        }

        /* windowing algorithms ends */

        double domainScore = 0;
        if (domainMultiset.size() > 0 && maxDomainCount > 1 && domainMultiset.count(domain) > 1) {
            domainScore = (double) domainMultiset.count(domain) / maxDomainCount;
        }
        double typeScore = 0;
        if (typeMultiset.size() > 0 && maxTypeCount > 1 && typeMultiset.count(type) > 1) {
            typeScore = (double) typeMultiset.count(type) / maxTypeCount;
        }
        if (typeBlackList.contains(type)) {
            typeScore /= 10;
        }

        double typeContentScore = 0;
        if (type.length() > 0 && StringUtils.containsIgnoreCase(words.toString(), type)) {
            typeContentScore = 1;
        }

        double typeClassifierScore = TypeClassifier.getInstance().predict(page, page.getTitle(), page.getType(),
                entity.getShingle().getSentence());

        double wordvecDescriptionScore = AveragePooling.getInstance().getSimilarity(documentVector,
                page.getDword2vec());
        if (wordvecDescriptionScore > maxWordvectorAverage) {
            maxWordvectorAverage = wordvecDescriptionScore;
        }

        double suffixScore = 0;

        if (type != null && type.length() > 0) {
            Set<String> suffixes = new HashSet<String>();
            String t = entity.getTitle().toLowerCase(new Locale("tr", "TR"));

            for (int x = 0; x < entities.size(); x++) {
                EntityMatch e2 = entities.get(x);
                if (e2.getId().equals(entity.getId())) {
                    suffixes.add(e2.getMention());
                }
            }
            suffixes.remove(t);
            suffixes.remove(entity.getTitle());
            // String inputTextLower = inputText.toLowerCase(new
            // Locale("tr",
            // "TR"));
            // while (inputTextLower.contains(t)) {
            // int start = inputTextLower.indexOf(t);
            // int end = inputTextLower.indexOf(" ", start + t.length());
            // if (end > start) {
            // String suffix = inputTextLower.substring(start, end);
            // // .replaceAll("\\W", "");
            // if (suffix.contains("'")
            // || (Zemberek.getInstance().hasMorph(suffix)
            // && !suffix.equals(t) && suffix.length() > 4)) {
            // suffixes.add(suffix);
            // }
            // inputTextLower = inputTextLower.substring(end);
            // } else {
            // break;
            // }
            // }
            if (suffixes.size() >= minSuffix) {
                for (String suffix : suffixes) {
                    double sim = gd.calculateSimilarity(suffix, type);
                    suffixScore += sim;
                }
            }
        }

        // String entitySuffix = page.getSuffix();
        // String[] inputSuffix = shingle.getSuffix().split(" ");
        // for (int j = 0; j < inputSuffix.length; j++) {
        // if (entitySuffix.contains(inputSuffix[j])) {
        // suffixScore += 0.25f;
        // }
        // }

        if (suffixScore > maxSuffixScore) {
            maxSuffixScore = suffixScore;
        }
        // if (id.equals("w691538")) {
        // LOGGER.info("");
        // }
        double letterCaseScore = 0;
        int lc = page.getLetterCase();
        if (StringUtils.isAllLowerCase(em.getMention()) && lc == 0 && id.startsWith("t")) {
            letterCaseScore = 1;
        } else if (StringUtils.isAllUpperCase(em.getMention()) && lc == 1 && id.startsWith("w")) {
            letterCaseScore = 1;
        } else if (Character.isUpperCase(em.getMention().charAt(0)) && lc == 2 && id.startsWith("w")) {
            letterCaseScore = 1;
        } else if (StringUtils.isAllLowerCase(em.getMention()) && id.startsWith("t")) {
            letterCaseScore = 1;
        }

        double nameScore = 1 - LevenshteinDistanceCalculator.calculateDistance(page.getTitle(),
                Zemberek.removeAfterSpostrophe(em.getMention()));

        double popularityScore = page.getRank();
        if (id.startsWith("w")) {
            popularityScore = Math.log10(popularityScore + 1);
            if (popularityScore > maxPopularityScore) {
                maxPopularityScore = popularityScore;
            }
        }

        double leskScore = 0, simpleLeskScore = 0;

        String desc = em.getEntity().getPage().getDescription();
        if (desc != null) {
            List<Token> tokens = Zemberek.getInstance().disambiguateFindTokens(desc, false, true);
            for (Token token : tokens) {
                if (inputTokensMultiset.contains(token.getMorphText())
                        && !TurkishNLP.isStopWord(token.getMorphText())) {
                    simpleLeskScore += inputTokensMultiset.count(token.getMorphText());
                }
                if (leskWords.contains(token.getMorphText()) && !TurkishNLP.isStopWord(token.getMorphText())) {
                    leskScore += leskWords.count(token.getMorphText());
                }

            }
            leskScore /= Math.log(tokens.size() + 1);
            simpleLeskScore /= Math.log(tokens.size() + 1);
            if (leskScore > maxLeskScore) {
                maxLeskScore = leskScore;
            }
            if (simpleLeskScore > maxSimpleLeskScore) {
                maxSimpleLeskScore = simpleLeskScore;
            }

            if (!entityScoreMap.containsKey(id)) {
                EntityScores scores = new EntityScores(em, id, popularityScore, nameScore, letterCaseScore,
                        suffixScore, wordvecDescriptionScore, typeContentScore, typeScore, domainScore,
                        hashDescriptionScore, wordvecDescriptionLocalScore, hashInfoboxScore, linkScore,
                        wordvecLinksScore, leskScore, simpleLeskScore, typeClassifierScore);
                entityScoreMap.put(id, scores);
            } else {
                EntityScores entityScores = entityScoreMap.get(id);
                entityScores.setHashInfoboxScore((entityScores.getHashInfoboxScore() + hashInfoboxScore) / 2);
                entityScores.setHashDescriptionScore(
                        (entityScores.getHashInfoboxScore() + hashDescriptionScore) / 2);
                entityScores.setLinkScore((entityScores.getLinkScore() + linkScore) / 2);
                entityScores.setWordvecDescriptionLocalScore(
                        (entityScores.getWordvecDescriptionLocalScore() + wordvecDescriptionLocalScore) / 2);
                entityScores
                        .setWordvecLinksScore((entityScores.getWordvecLinksScore() + wordvecLinksScore) / 2);
                entityScores.setLeskScore((entityScores.getLeskScore() + leskScore) / 2);

            }

        }
    }
    /* normalization and total score calculation starts */
    Set<String> set = new HashSet<String>();
    for (int i = 0; i < entities.size(); i++) {
        EntityMatch em = entities.get(i);
        String id = em.getId();
        EntityScores entityScores = entityScoreMap.get(id);
        if (set.contains(id)) {
            continue;
        }
        if (id.startsWith("w")) {
            if (maxLinkScore > 0 && entityScores.getLinkScore() > 0) {
                entityScores.setLinkScore(entityScores.getLinkScore() / maxLinkScore);
            }
            if (maxHashInfoboxScore > 0 && entityScores.getHashInfoboxScore() > 0) {
                entityScores.setHashInfoboxScore(entityScores.getHashInfoboxScore() / maxHashInfoboxScore);
            }
            if (maxWordvecLinksScore > 0 && entityScores.getWordvecLinksScore() > 0) {
                entityScores.setWordvecLinksScore(entityScores.getWordvecLinksScore() / maxWordvecLinksScore);
            }
            if (maxPopularityScore > 0 && entityScores.getPopularityScore() > 0) {
                entityScores.setPopularityScore(entityScores.getPopularityScore() / maxPopularityScore);
            }
        }
        if (maxwordvecDescriptionLocalScore > 0 && entityScores.getWordvecDescriptionLocalScore() > 0) {
            entityScores.setWordvecDescriptionLocalScore(
                    entityScores.getWordvecDescriptionLocalScore() / maxwordvecDescriptionLocalScore);
        }
        if (maxHashDescriptionScore > 0 && entityScores.getHashDescriptionScore() > 0) {
            entityScores
                    .setHashDescriptionScore(entityScores.getHashDescriptionScore() / maxHashDescriptionScore);
        }
        if (maxWordvectorAverage > 0 && entityScores.getWordvecDescriptionScore() > 0) {
            entityScores.setWordvecDescriptionScore(
                    entityScores.getWordvecDescriptionScore() / maxWordvectorAverage);
        }
        if (maxLeskScore > 0 && entityScores.getLeskScore() > 0) {
            entityScores.setLeskScore(entityScores.getLeskScore() / maxLeskScore);
        }
        if (maxSimpleLeskScore > 0 && entityScores.getSimpleLeskScore() > 0) {
            entityScores.setSimpleLeskScore(entityScores.getSimpleLeskScore() / maxSimpleLeskScore);
        }
        if (maxSuffixScore > 0 && entityScores.getSuffixScore() > 0) {
            entityScores.setSuffixScore(entityScores.getSuffixScore() / maxSuffixScore);
        }
        set.add(id);
    }

    LOGGER.info("\t"
            + "id\tTitle\tURL\tScore\tPopularity\tName\tLesk\tSimpeLesk\tCase\tNoun\tSuffix\tTypeContent\tType\tDomain\twordvecDescription\twordvecDescriptionLocal\thashDescription\thashInfobox\tword2vecLinks\tLink\t\ttypeClassifier\tDescription");
    for (int i = 0; i < entities.size(); i++) {
        EntityMatch em = entities.get(i);
        String id = em.getId();
        EntityScores e = entityScoreMap.get(id);
        double wikiScore = 0;
        if (id.startsWith("w") && Character.isUpperCase(em.getMention().charAt(0))) {
            wikiScore = wikiWeight;
        } else if (id.startsWith("t") && Character.isLowerCase(em.getMention().charAt(0))) {
            wikiScore = wikiWeight;
        }
        // if(id.equals("w508792")){
        // LOGGER.info("");
        // }
        double totalScore = wikiScore + e.getPopularityScore() * popularityWeight
                + e.getNameScore() * nameWeight + e.getLeskScore() * leskWeight
                + e.getSimpleLeskScore() * simpleLeskWeight + e.getLetterCaseScore() * letterCaseWeight
                + e.getSuffixScore() * suffixWeight + e.getTypeContentScore() * typeContentWeight
                + e.getTypeScore() * typeWeight + e.getDomainScore() * domainWeight
                + e.getWordvecDescriptionScore() * wordvecDescriptionWeight
                + e.getWordvecDescriptionLocalScore() * wordvecDescriptionLocalWeight
                + e.getHashDescriptionScore() * hashDescriptionWeight
                + e.getHashInfoboxScore() * hashInfoboxWeight + e.getWordvecLinksScore() * word2vecLinksWeight
                + e.getLinkScore() * linkWeight + e.getTypeClassifierkScore() * typeClassifierkWeight;
        if (ranklib == true) {
            totalScore = RankLib.getInstance().score(e);
        }

        if (em.getEntity().getPage().getUrlTitle().contains("(")) {
            totalScore /= 2;
        }
        em.setScore(totalScore);
        e.setScore(totalScore);

        LOGGER.info("\t" + id + "\t" + em.getEntity().getPage().getTitle() + "\t"
                + em.getEntity().getPage().getUrlTitle() + "\t" + em.getScore() + "\t"
                + e.getPopularityScore() * popularityWeight + "\t" + e.getNameScore() * nameWeight + "\t"
                + e.getLeskScore() * leskWeight + "\t" + e.getSimpleLeskScore() * simpleLeskWeight + "\t"
                + e.getLetterCaseScore() * letterCaseWeight + "\t" + e.getSuffixScore() * suffixWeight + "\t"
                + e.getTypeContentScore() * typeContentWeight + "\t" + e.getTypeScore() * typeWeight + "\t"
                + e.getDomainScore() * domainWeight + "\t"
                + e.getWordvecDescriptionScore() * wordvecDescriptionWeight + "\t"
                + e.getWordvecDescriptionLocalScore() * wordvecDescriptionLocalWeight + "\t"
                + e.getHashDescriptionScore() * hashDescriptionWeight + "\t"
                + e.getHashInfoboxScore() * hashInfoboxWeight + "\t"
                + e.getWordvecLinksScore() * word2vecLinksWeight + "\t" + e.getLinkScore() * linkWeight + "\t"
                + e.getTypeClassifierkScore() * typeClassifierkWeight + "\t"
                + em.getEntity().getPage().getDescription());
    }

    // if (annotateEntities) {
    // annotateEntities(localParams.getParams().get("originalText"), sml);
    // }

    EntityMatchList eml = new EntityMatchList();
    for (SpotMatch match : sml) {
        EntityMatchList list = match.getEntities();
        if (!list.isEmpty()) {
            list.sort();
            eml.add(list.get(0));
            selectedEntities.add(list.get(0).getId());
        }
    }
    return eml;
}

From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java

private RDFResourceTree applyBaseLine(ExamplesWrapper examples, Baseline baselineApproach) {
    Collection<RDFResourceTree> posExamples = examples.posExamplesMapping.values();
    Collection<RDFResourceTree> negExamples = examples.negExamplesMapping.values();

    switch (baselineApproach) {
    case RANDOM:// 1.
        String query = "SELECT ?cls WHERE {?cls a owl:Class .} ORDER BY RAND() LIMIT 1";
        QueryExecution qe = qef.createQueryExecution(query);
        ResultSet rs = qe.execSelect();
        if (rs.hasNext()) {
            QuerySolution qs = rs.next();
            Resource cls = qs.getResource("cls");
            RDFResourceTree solution = new RDFResourceTree();
            solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode());
            return solution;
        }/*w  w w.ja  v  a2  s.  c om*/
    case MOST_POPULAR_TYPE_IN_KB:// 2.
        query = "SELECT ?cls WHERE {?cls a owl:Class . ?s a ?cls .} ORDER BY DESC(COUNT(?s)) LIMIT 1";
        qe = qef.createQueryExecution(query);
        rs = qe.execSelect();
        if (rs.hasNext()) {
            QuerySolution qs = rs.next();
            Resource cls = qs.getResource("cls");
            RDFResourceTree solution = new RDFResourceTree();
            solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode());
            return solution;
        }
    case MOST_FREQUENT_TYPE_IN_EXAMPLES:// 3.
        Multiset<Node> types = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            List<RDFResourceTree> children = ex.getChildren(RDF.type.asNode());
            for (RDFResourceTree child : children) {
                types.add(child.getData());
            }
        }
        Node mostFrequentType = Ordering.natural().onResultOf(new Function<Multiset.Entry<Node>, Integer>() {
            @Override
            public Integer apply(Multiset.Entry<Node> entry) {
                return entry.getCount();
            }
        }).max(types.entrySet()).getElement();
        RDFResourceTree solution = new RDFResourceTree();
        solution.addChild(new RDFResourceTree(mostFrequentType), RDF.type.asNode());
        return solution;
    case MOST_FREQUENT_EDGE_IN_EXAMPLES:// 4.
    {
        Multiset<Pair<Node, Node>> pairs = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            SortedSet<Node> edges = ex.getEdges();
            for (Node edge : edges) {
                List<RDFResourceTree> children = ex.getChildren(edge);
                for (RDFResourceTree child : children) {
                    pairs.add(new Pair<>(edge, child.getData()));
                }
            }
        }
        Pair<Node, Node> mostFrequentPair = Ordering.natural()
                .onResultOf(new Function<Multiset.Entry<Pair<Node, Node>>, Integer>() {
                    @Override
                    public Integer apply(Multiset.Entry<Pair<Node, Node>> entry) {
                        return entry.getCount();
                    }
                }).max(pairs.entrySet()).getElement();
        solution = new RDFResourceTree();
        solution.addChild(new RDFResourceTree(mostFrequentPair.getValue()), mostFrequentPair.getKey());
        return solution;
    }
    case MOST_INFORMATIVE_EDGE_IN_EXAMPLES:
        // get all p-o in pos examples
        Multiset<Pair<Node, Node>> edgeObjectPairs = HashMultiset.create();
        for (RDFResourceTree ex : posExamples) {
            SortedSet<Node> edges = ex.getEdges();
            for (Node edge : edges) {
                List<RDFResourceTree> children = ex.getChildren(edge);
                for (RDFResourceTree child : children) {
                    edgeObjectPairs.add(new Pair<>(edge, child.getData()));
                }
            }
        }

        double bestAccuracy = -1;
        solution = new RDFResourceTree();

        for (Pair<Node, Node> pair : edgeObjectPairs.elementSet()) {
            Node edge = pair.getKey();
            Node childValue = pair.getValue();

            // compute accuracy
            int tp = edgeObjectPairs.count(pair);
            int fn = posExamples.size() - tp;
            int fp = 0;
            for (RDFResourceTree ex : negExamples) { // compute false positives
                List<RDFResourceTree> children = ex.getChildren(edge);
                if (children != null) {
                    for (RDFResourceTree child : children) {
                        if (child.getData().equals(childValue)) {
                            fp++;
                            break;
                        }
                    }
                }
            }
            int tn = negExamples.size() - fp;

            double accuracy = Heuristics.getPredictiveAccuracy(posExamples.size(), negExamples.size(), tp, tn,
                    1.0);
            // update best solution
            if (accuracy >= bestAccuracy) {
                solution = new RDFResourceTree();
                solution.addChild(new RDFResourceTree(childValue), edge);
                bestAccuracy = accuracy;
            }
        }
        return solution;
    case LGG:
        LGGGenerator lggGenerator = new LGGGeneratorSimple();
        RDFResourceTree lgg = lggGenerator.getLGG(Lists.newArrayList(posExamples));
        return lgg;

    default:
        break;

    }
    return null;
}

From source file:org.apache.mahout.classifier.sgd.TrainASFEmail.java

@Override
public int run(String[] args) throws Exception {
    addInputOption();//from   ww  w . ja  v a  2 s  . c  om
    addOutputOption();
    addOption("categories", "nc", "The number of categories to train on", true);
    addOption("cardinality", "c", "The size of the vectors to use", "100000");
    addOption("threads", "t", "The number of threads to use in the learner", "20");
    addOption("poolSize", "p", "The number of CrossFoldLearners to use in the AdaptiveLogisticRegression. "
            + "Higher values require more memory.", "5");
    if (parseArguments(args) == null) {
        return -1;
    }

    File base = new File(getInputPath().toString());

    Multiset<String> overallCounts = HashMultiset.create();
    File output = new File(getOutputPath().toString());
    output.mkdirs();
    int numCats = Integer.parseInt(getOption("categories"));
    int cardinality = Integer.parseInt(getOption("cardinality", "100000"));
    int threadCount = Integer.parseInt(getOption("threads", "20"));
    int poolSize = Integer.parseInt(getOption("poolSize", "5"));
    Dictionary asfDictionary = new Dictionary();
    AdaptiveLogisticRegression learningAlgorithm = new AdaptiveLogisticRegression(numCats, cardinality,
            new L1(), threadCount, poolSize);
    learningAlgorithm.setInterval(800);
    learningAlgorithm.setAveragingWindow(500);

    //We ran seq2encoded and split input already, so let's just build up the dictionary
    Configuration conf = new Configuration();
    PathFilter trainFilter = new PathFilter() {
        @Override
        public boolean accept(Path path) {
            return path.getName().contains("training");
        }
    };
    SequenceFileDirIterator<Text, VectorWritable> iter = new SequenceFileDirIterator<Text, VectorWritable>(
            new Path(base.toString()), PathType.LIST, trainFilter, null, true, conf);
    long numItems = 0;
    while (iter.hasNext()) {
        Pair<Text, VectorWritable> next = iter.next();
        asfDictionary.intern(next.getFirst().toString());
        numItems++;
    }

    System.out.println(numItems + " training files");

    SGDInfo info = new SGDInfo();

    iter = new SequenceFileDirIterator<Text, VectorWritable>(new Path(base.toString()), PathType.LIST,
            trainFilter, null, true, conf);
    int k = 0;
    while (iter.hasNext()) {
        Pair<Text, VectorWritable> next = iter.next();
        String ng = next.getFirst().toString();
        int actual = asfDictionary.intern(ng);
        //we already have encoded
        learningAlgorithm.train(actual, next.getSecond().get());
        k++;
        State<AdaptiveLogisticRegression.Wrapper, CrossFoldLearner> best = learningAlgorithm.getBest();

        SGDHelper.analyzeState(info, 0, k, best);
    }
    learningAlgorithm.close();
    //TODO: how to dissection since we aren't processing the files here
    //SGDHelper.dissect(leakType, asfDictionary, learningAlgorithm, files, overallCounts);
    System.out.println("exiting main, writing model to " + output);

    ModelSerializer.writeBinary(output + "/asf.model",
            learningAlgorithm.getBest().getPayload().getLearner().getModels().get(0));

    List<Integer> counts = Lists.newArrayList();
    System.out.println("Word counts");
    for (String count : overallCounts.elementSet()) {
        counts.add(overallCounts.count(count));
    }
    Collections.sort(counts, Ordering.natural().reverse());
    k = 0;
    for (Integer count : counts) {
        System.out.println(k + "\t" + count);
        k++;
        if (k > 1000) {
            break;
        }
    }
    return 0;
}

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];
        }//w w  w.ja v a 2  s. co  m

        //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];
        }/*  w w w  . jav a 2 s  . co m*/

        //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");
    }
}

From source file:com.wasteofplastic.acidisland.commands.AdminCmd.java

public boolean onCommand(final CommandSender sender, final Command command, final String label,
        final String[] split) {
    // Console commands
    Player player;//from w w w  .ja  v a 2s . c om
    if (sender instanceof Player) {
        player = (Player) sender;
        if (split.length > 0) {
            // Admin-only commands : reload, register, delete and purge
            if (split[0].equalsIgnoreCase("reload") || split[0].equalsIgnoreCase("register")
                    || split[0].equalsIgnoreCase("delete") || split[0].equalsIgnoreCase("purge")
                    || split[0].equalsIgnoreCase("confirm") || split[0].equalsIgnoreCase("setspawn")
                    || split[0].equalsIgnoreCase("deleteisland") || split[0].equalsIgnoreCase("setrange")
                    || split[0].equalsIgnoreCase("unregister")) {
                if (!checkAdminPerms(player, split)) {
                    player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoPermission);
                    return true;
                }
            } else {
                // Mod commands
                if (!checkModPerms(player, split)) {
                    player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoPermission);
                    return true;
                }
            }
        }
    }
    // Check for zero parameters e.g., /asadmin
    switch (split.length) {
    case 0:
        help(sender, label);
        return true;
    case 1:
        if (Settings.teamChat && split[0].equalsIgnoreCase("spy")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminLockerrorInGame);
                return true;
            }
            player = (Player) sender;
            if (VaultHelper.checkPerm(player, Settings.PERMPREFIX + "mod.spy") || player.isOp()) {
                if (plugin.getChatListener().toggleSpy(player.getUniqueId())) {
                    sender.sendMessage(ChatColor.GREEN + plugin.myLocale().teamChatStatusOn);
                } else {
                    sender.sendMessage(ChatColor.GREEN + plugin.myLocale().teamChatStatusOff);
                }
                return true;
            }
        } else if (split[0].equalsIgnoreCase("lock")) {
            // Just /asadmin lock
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminLockerrorInGame);
                return true;
            }
            player = (Player) sender;
            Island island = plugin.getGrid().getIslandAt(player.getLocation());
            // Check if island exists
            if (island == null) {
                player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNotOnIsland);
                return true;
            } else {
                Player owner = plugin.getServer().getPlayer(island.getOwner());
                if (island.isLocked()) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().lockUnlocking);
                    island.setLocked(false);
                    if (owner != null) {
                        owner.sendMessage(plugin.myLocale(owner.getUniqueId()).adminLockadminUnlockedIsland);
                    } else {
                        plugin.getMessages().setMessage(island.getOwner(),
                                plugin.myLocale(island.getOwner()).adminLockadminUnlockedIsland);
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().lockLocking);
                    island.setLocked(true);
                    if (owner != null) {
                        owner.sendMessage(plugin.myLocale(owner.getUniqueId()).adminLockadminLockedIsland);
                    } else {
                        plugin.getMessages().setMessage(island.getOwner(),
                                plugin.myLocale(island.getOwner()).adminLockadminLockedIsland);
                    }
                }
                return true;
            }
        } else
        // Find farms
        if (split[0].equalsIgnoreCase("topbreeders")) {
            // Go through each island and find how many farms there are
            sender.sendMessage(plugin.myLocale().adminTopBreedersFinding);
            //TreeMap<Integer, List<UUID>> topEntityIslands = new TreeMap<Integer, List<UUID>>();
            // Generate the stats
            sender.sendMessage(plugin.myLocale().adminTopBreedersChecking.replace("[number]",
                    String.valueOf(plugin.getGrid().getOwnershipMap().size())));
            // Try just finding every entity
            final List<Entity> allEntities = ASkyBlock.getIslandWorld().getEntities();
            final World islandWorld = ASkyBlock.getIslandWorld();
            final World netherWorld = ASkyBlock.getNetherWorld();
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                @Override
                public void run() {
                    Map<UUID, Multiset<EntityType>> result = new HashMap<UUID, Multiset<EntityType>>();
                    // Find out where the entities are
                    for (Entity entity : allEntities) {
                        //System.out.println("DEBUG " + entity.getType().toString());
                        if (entity.getLocation().getWorld().equals(islandWorld)
                                || entity.getLocation().getWorld().equals(netherWorld)) {
                            //System.out.println("DEBUG in world");
                            if (entity instanceof Creature && !(entity instanceof Player)) {
                                //System.out.println("DEBUG creature");
                                // Find out where it is
                                Island island = plugin.getGrid().getIslandAt(entity.getLocation());
                                if (island != null && !island.isSpawn()) {
                                    //System.out.println("DEBUG on island");
                                    // Add to result
                                    UUID owner = island.getOwner();
                                    Multiset<EntityType> count = result.get(owner);
                                    if (count == null) {
                                        // New entry for owner
                                        //System.out.println("DEBUG new entry for owner");
                                        count = HashMultiset.create();
                                    }
                                    count.add(entity.getType());
                                    result.put(owner, count);
                                }
                            }
                        }
                    }
                    // Sort by the number of entities on each island
                    TreeMap<Integer, List<UUID>> topEntityIslands = new TreeMap<Integer, List<UUID>>();
                    for (Entry<UUID, Multiset<EntityType>> entry : result.entrySet()) {
                        int numOfEntities = entry.getValue().size();
                        List<UUID> players = topEntityIslands.get(numOfEntities);
                        if (players == null) {
                            players = new ArrayList<UUID>();
                        }
                        players.add(entry.getKey());
                        topEntityIslands.put(numOfEntities, players);
                    }
                    final TreeMap<Integer, List<UUID>> topBreeders = topEntityIslands;
                    final Map<UUID, Multiset<EntityType>> finalResult = result;
                    // Now display results in sync thread
                    plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                        @Override
                        public void run() {
                            if (topBreeders.isEmpty()) {
                                sender.sendMessage(plugin.myLocale().adminTopBreedersNothing);
                                return;
                            }
                            int rank = 1;
                            // Display, largest first
                            for (int numOfEntities : topBreeders.descendingKeySet()) {
                                // Only bother if there's more that 5 animals
                                if (numOfEntities > 5) {
                                    // There can be multiple owners in the same position
                                    List<UUID> owners = topBreeders.get(numOfEntities);
                                    // Go through the owners one by one
                                    for (UUID owner : owners) {
                                        sender.sendMessage("#" + rank + " " + plugin.getPlayers().getName(owner)
                                                + " = " + numOfEntities);
                                        String content = "";
                                        Multiset<EntityType> entityCount = finalResult.get(owner);
                                        for (EntityType entity : entityCount.elementSet()) {
                                            int num = entityCount.count(entity);
                                            String color = ChatColor.GREEN.toString();
                                            if (num > 10 && num <= 20) {
                                                color = ChatColor.YELLOW.toString();
                                            } else if (num > 20 && num <= 40) {
                                                color = ChatColor.GOLD.toString();
                                            } else if (num > 40) {
                                                color = ChatColor.RED.toString();
                                            }
                                            content += Util.prettifyText(entity.toString()) + " x " + color
                                                    + num + ChatColor.WHITE + ", ";
                                        }
                                        int lastComma = content.lastIndexOf(",");
                                        // plugin.getLogger().info("DEBUG: last comma " +
                                        // lastComma);
                                        if (lastComma > 0) {
                                            content = content.substring(0, lastComma);
                                        }
                                        sender.sendMessage("  " + content);

                                    }
                                    rank++;
                                    if (rank > 10) {
                                        break;
                                    }
                                }
                            }
                            // If we didn't show anything say so
                            if (rank == 1) {
                                sender.sendMessage(plugin.myLocale().adminTopBreedersNothing);
                            }

                        }
                    });

                }
            });
            return true;
        }
        // Delete island
        if (split[0].equalsIgnoreCase("deleteisland")) {
            sender.sendMessage(ChatColor.RED + plugin.myLocale().adminDeleteIslandError);
            return true;
        }
        // Set spawn
        if (split[0].equalsIgnoreCase("setspawn")) {
            // Find the closest island
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUseInGame);
                return true;
            }
            player = (Player) sender;
            // Island spawn must be in the island world
            if (!player.getLocation().getWorld().getName().equals(Settings.worldName)) {
                player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorWrongWorld);
                return true;
            }
            // The island location is calculated based on the grid
            Location closestIsland = getClosestIsland(player.getLocation());
            Island oldSpawn = plugin.getGrid().getSpawn();
            Island newSpawn = plugin.getGrid().getIslandAt(closestIsland);
            if (newSpawn != null && newSpawn.isSpawn()) {
                // Already spawn, so just set the world spawn coords
                plugin.getGrid().setSpawnPoint(player.getLocation());
                //ASkyBlock.getIslandWorld().setSpawnLocation(player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminSetSpawnset);
                return true;
            }
            // Space otherwise occupied - find if anyone owns it
            if (newSpawn != null && newSpawn.getOwner() != null) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetSpawnownedBy.replace("[name]",
                        plugin.getPlayers().getName(newSpawn.getOwner())));
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetSpawnmove);
                return true;
            }
            if (oldSpawn != null) {
                sender.sendMessage(ChatColor.GOLD
                        + "Changing spawn island location. Warning: old spawn island location at "
                        + oldSpawn.getCenter().getBlockX() + "," + oldSpawn.getCenter().getBlockZ()
                        + " will be at risk of being overwritten with new islands. Recommend to clear that old area.");
                plugin.getGrid().deleteSpawn();
            }
            // New spawn site is free, so make it official
            if (newSpawn == null) {
                // Make the new spawn
                newSpawn = plugin.getGrid().addIsland(closestIsland.getBlockX(), closestIsland.getBlockZ());
            }
            plugin.getGrid().setSpawn(newSpawn);
            plugin.getGrid().setSpawnPoint(player.getLocation());
            //ASkyBlock.getIslandWorld().setSpawnLocation(player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
            player.sendMessage(ChatColor.GREEN + plugin.myLocale().adminSetSpawnsetting.replace("[location]",
                    player.getLocation().getBlockX() + "," + player.getLocation().getBlockZ()));
            player.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawncenter.replace("[location]",
                    newSpawn.getCenter().getBlockX() + "," + newSpawn.getCenter().getBlockZ()));
            player.sendMessage(ChatColor.YELLOW + (plugin.myLocale().adminSetSpawnlimits.replace("[min]",
                    newSpawn.getMinX() + "," + newSpawn.getMinZ())).replace("[max]",
                            (newSpawn.getMinX() + newSpawn.getIslandDistance() - 1) + ","
                                    + (newSpawn.getMinZ() + newSpawn.getIslandDistance() - 1)));
            player.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawnrange.replace("[number]",
                    String.valueOf(newSpawn.getProtectionSize())));
            player.sendMessage(ChatColor.YELLOW + (plugin.myLocale().adminSetSpawncoords.replace("[min]",
                    newSpawn.getMinProtectedX() + ", " + newSpawn.getMinProtectedZ())).replace("[max]",
                            +(newSpawn.getMinProtectedX() + newSpawn.getProtectionSize() - 1) + ", "
                                    + (newSpawn.getMinProtectedZ() + newSpawn.getProtectionSize() - 1)));
            if (newSpawn.isLocked()) {
                player.sendMessage(ChatColor.RED + plugin.myLocale().adminSetSpawnlocked);
            }
            return true;
        } else if (split[0].equalsIgnoreCase("info") || split[0].equalsIgnoreCase("setrange")) {
            // Find the closest island
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUseInGame);
                return true;
            }
            Location closestIsland = getClosestIsland(((Player) sender).getLocation());
            if (closestIsland == null) {
                sender.sendMessage(ChatColor.RED + "Sorry, could not find an island. Move closer?");
                return true;
            }
            Island island = plugin.getGrid().getIslandAt(closestIsland);
            if (island != null && island.isSpawn()) {
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminInfotitle);
                sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawncenter.replace(
                        "[location]", island.getCenter().getBlockX() + "," + island.getCenter().getBlockZ()));
                sender.sendMessage(ChatColor.YELLOW + (plugin.myLocale().adminSetSpawnlimits.replace("[min]",
                        island.getMinX() + "," + island.getMinZ())).replace("[max]",
                                (island.getMinX() + island.getIslandDistance() - 1) + ","
                                        + (island.getMinZ() + island.getIslandDistance() - 1)));
                sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawnrange.replace("[number]",
                        String.valueOf(island.getProtectionSize())));
                sender.sendMessage(ChatColor.YELLOW + (plugin.myLocale().adminSetSpawncoords.replace("[min]",
                        island.getMinProtectedX() + ", " + island.getMinProtectedZ())).replace("[max]",
                                +(island.getMinProtectedX() + island.getProtectionSize() - 1) + ", "
                                        + (island.getMinProtectedZ() + island.getProtectionSize() - 1)));
                if (island.isLocked()) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetSpawnlocked);
                }
                return true;
            }
            if (island == null) {
                plugin.getLogger().info("Get island at was null" + closestIsland);
            }
            UUID target = plugin.getPlayers().getPlayerFromIslandLocation(closestIsland);
            if (target == null) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminInfounowned);
                return true;
            }
            showInfo(target, sender);
            return true;
        } else if (split[0].equalsIgnoreCase("resetsign")) {
            // Find the closest island
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUseInGame);
                return true;
            }
            player = (Player) sender;
            if (!VaultHelper.checkPerm(player, Settings.PERMPREFIX + "mod.signadmin") && !player.isOp()) {
                player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoPermission);
                return true;
            }
            // Find out whether the player is looking at a warp sign
            // Look at what the player was looking at
            BlockIterator iter = new BlockIterator(player, 10);
            Block lastBlock = iter.next();
            while (iter.hasNext()) {
                lastBlock = iter.next();
                if (lastBlock.getType() == Material.AIR)
                    continue;
                break;
            }
            if (!lastBlock.getType().equals(Material.SIGN_POST)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).adminResetSignNoSign);
                return true;
            }
            // Check if it is a warp sign
            Sign sign = (Sign) lastBlock.getState();
            sender.sendMessage(ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).adminResetSignFound);
            // Find out whose island this is
            // plugin.getLogger().info("DEBUG: closest bedrock: " +
            // closestBedRock.toString());
            UUID target = plugin.getPlayers().getPlayerFromIslandLocation(player.getLocation());
            if (target == null) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).adminInfounowned);
                return true;
            }
            if (plugin.getWarpSignsListener().addWarp(target, lastBlock.getLocation())) {
                // Change sign color to green
                sign.setLine(0, ChatColor.GREEN + plugin.myLocale().warpswelcomeLine);
                sign.update();
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).adminResetSignRescued
                        .replace("[name]", plugin.getPlayers().getName(target)));
                return true;
            }
            // Warp already exists
            sender.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).adminResetSignErrorExists
                    .replace("[name]", plugin.getWarpSignsListener().getWarpOwner(lastBlock.getLocation())));
            return true;

        } else if (split[0].equalsIgnoreCase("reload")) {
            plugin.reloadConfig();
            plugin.loadPluginConfig();
            plugin.getChallenges().reloadChallengeConfig();
            if (Settings.useEconomy && VaultHelper.setupEconomy()) {
                ControlPanel.loadShop();
            } else {
                Settings.useEconomy = false;
            }
            ControlPanel.loadControlPanel();
            if (Settings.updateCheck) {
                plugin.checkUpdates();
            } else {
                plugin.setUpdateCheck(null);
            }
            plugin.getIslandCmd().loadSchematics();
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().reloadconfigReloaded);
            return true;
        } else if (split[0].equalsIgnoreCase("topten")) {
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminTopTengenerating);
            TopTen.topTenCreate(sender);
            return true;
        } else if (split[0].equalsIgnoreCase("purge")) {
            if (purgeFlag) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().purgealreadyRunning);
                return true;
            }
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().purgeusage.replace("[label]", label));
            return true;
        } else if (split[0].equalsIgnoreCase("confirm")) {
            if (!confirmReq) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().confirmerrorTimeLimitExpired);
                return true;
            } else {
                // Tell purge routine to go
                confirmOK = true;
                confirmReq = false;
            }
            return true;
        } else {
            sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
            return false;
        }
    case 2:
        // Resetsign <player> - makes a warp sign for player
        if (split[0].equalsIgnoreCase("resetsign")) {
            // Find the closest island
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUseInGame);
                return true;
            }
            Player p = (Player) sender;
            if (!VaultHelper.checkPerm(p, Settings.PERMPREFIX + "mod.signadmin") && !p.isOp()) {
                p.sendMessage(ChatColor.RED + plugin.myLocale(p.getUniqueId()).errorNoPermission);
                return true;
            }
            // Convert target name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
            } else {
                // Check if this player has an island
                if (!plugin.getPlayers().hasIsland(playerUUID) && !plugin.getPlayers().inTeam(playerUUID)) {
                    // No island
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoIslandOther);
                    return true;
                }
                // Has an island
                // Find out whether the player is looking at a warp sign
                // Look at what the player was looking at
                BlockIterator iter = new BlockIterator(p, 10);
                Block lastBlock = iter.next();
                while (iter.hasNext()) {
                    lastBlock = iter.next();
                    if (lastBlock.getType() == Material.AIR)
                        continue;
                    break;
                }
                // Check if it is a sign
                if (!lastBlock.getType().equals(Material.SIGN_POST)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale(p.getUniqueId()).adminResetSignNoSign);
                    return true;
                }
                Sign sign = (Sign) lastBlock.getState();
                // Check if the sign is within the right island boundary
                Location islandLoc = plugin.getPlayers().getIslandLocation(playerUUID);
                if (!plugin.getGrid().getIslandAt(islandLoc).inIslandSpace(sign.getLocation())) {
                    p.sendMessage(
                            ChatColor.RED + plugin.myLocale(p.getUniqueId()).adminSetHomeNotOnPlayersIsland);
                } else {
                    sender.sendMessage(ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).adminResetSignFound);
                    // Find out if this player is allowed to have a sign on this island
                    if (plugin.getWarpSignsListener().addWarp(playerUUID, lastBlock.getLocation())) {
                        // Change sign color to green
                        sign.setLine(0, ChatColor.GREEN + plugin.myLocale().warpswelcomeLine);
                        sign.update();
                        p.sendMessage(ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).adminResetSignRescued
                                .replace("[name]", plugin.getPlayers().getName(playerUUID)));
                        return true;
                    }
                    // Warp already exists
                    sender.sendMessage(ChatColor.RED
                            + plugin.myLocale(p.getUniqueId()).adminResetSignErrorExists.replace("[name]",
                                    plugin.getWarpSignsListener().getWarpOwner(lastBlock.getLocation())));
                }
            }
            return true;
        }
        // Delete the island you are on
        else if (split[0].equalsIgnoreCase("deleteisland")) {
            if (!split[1].equalsIgnoreCase("confirm")) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminDeleteIslandError);
                return true;
            }
            // Get the island I am on
            Island island = plugin.getGrid().getIslandAt(((Player) sender).getLocation());
            if (island == null) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminDeleteIslandnoid);
                return true;
            }
            // Try to get the owner of this island
            UUID owner = island.getOwner();
            String name = "unknown";
            if (owner != null) {
                name = plugin.getPlayers().getName(owner);
                sender.sendMessage(
                        ChatColor.RED + plugin.myLocale().adminSetSpawnownedBy.replace("[name]", name));
                sender.sendMessage(
                        ChatColor.RED + plugin.myLocale().adminDeleteIslanduse.replace("[name]", name));
                return true;
            } else {
                sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().deleteremoving.replace("[name]", name));
                deleteIslands(island, sender);
                return true;
            }
        } else if (split[0].equalsIgnoreCase("resethome")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
            } else {
                // Check if this player has an island
                if (!plugin.getPlayers().hasIsland(playerUUID) && !plugin.getPlayers().inTeam(playerUUID)) {
                    // No island
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoIslandOther);
                    return true;
                }
                // Has an island
                Location safeHome = plugin.getGrid().getSafeHomeLocation(playerUUID, 1);
                if (safeHome == null) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetHomeNoneFound);
                } else {
                    plugin.getPlayers().setHomeLocation(playerUUID, safeHome);
                    sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminSetHomeHomeSet.replace(
                            "[location]",
                            safeHome.getBlockX() + ", " + safeHome.getBlockY() + "," + safeHome.getBlockZ()));
                }
            }
            return true;
        } else if (split[0].equalsIgnoreCase("sethome")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminLockerrorInGame);
                return true;
            }
            player = (Player) sender;
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
            } else {
                // Check if this player has an island
                if (!plugin.getPlayers().hasIsland(playerUUID) && !plugin.getPlayers().inTeam(playerUUID)) {
                    // No island
                    player.sendMessage(
                            ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorNoIslandOther);
                    return true;
                }
                // Has an island
                Location islandLoc = plugin.getPlayers().getIslandLocation(playerUUID);
                // Check the player is within the island boundaries
                if (!plugin.getGrid().getIslandAt(islandLoc).inIslandSpace(player.getLocation())) {
                    player.sendMessage(ChatColor.RED
                            + plugin.myLocale(player.getUniqueId()).adminSetHomeNotOnPlayersIsland);
                } else {
                    // Check that the location is safe
                    if (!GridManager.isSafeLocation(player.getLocation())) {
                        // Not safe
                        player.sendMessage(
                                ChatColor.RED + plugin.myLocale(player.getUniqueId()).adminSetHomeNoneFound);
                    } else {
                        // Success
                        plugin.getPlayers().setHomeLocation(playerUUID, player.getLocation());
                        player.sendMessage(
                                ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).adminSetHomeHomeSet
                                        .replace("[location]",
                                                player.getLocation().getBlockX() + ", "
                                                        + player.getLocation().getBlockY() + ","
                                                        + player.getLocation().getBlockZ()));
                    }
                }
            }
            return true;
        } else
        // Set protection for the island the player is on
        if (split[0].equalsIgnoreCase("setrange")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminLockerrorInGame);
                return true;
            }
            player = (Player) sender;
            UUID playerUUID = player.getUniqueId();
            Island island = plugin.getGrid().getIslandAt(player.getLocation());
            // Check if island exists
            if (island == null) {
                player.sendMessage(ChatColor.RED + plugin.myLocale().errorNotOnIsland);
                return true;
            } else {
                int newRange = 10;
                int maxRange = Settings.islandDistance;
                // If spawn do something different
                if (island.isSpawn()) {
                    try {
                        newRange = Integer.valueOf(split[1]);
                    } catch (Exception e) {
                        player.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).adminSetRangeInvalid);
                        return true;
                    }
                    player.sendMessage(ChatColor.GREEN + plugin.myLocale(playerUUID).adminSetRangeSet
                            .replace("[number]", String.valueOf(newRange)));
                    if (newRange > maxRange) {
                        player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD
                                + plugin.myLocale(playerUUID).adminSetRangeWarning.replace("[max]",
                                        String.valueOf(maxRange)));
                        player.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).adminSetRangeWarning2);
                    }
                    island.setProtectionSize(newRange);
                    player.sendMessage(
                            ChatColor.YELLOW + plugin.myLocale().adminSetSpawncenter.replace("[location]",
                                    island.getCenter().getBlockX() + "," + island.getCenter().getBlockZ()));
                    player.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawnlimits
                            .replace("[min]", island.getMinX() + "," + island.getMinZ())
                            .replace("[max]", (island.getMinX() + island.getIslandDistance() - 1) + ","
                                    + (island.getMinZ() + island.getIslandDistance() - 1)));
                    player.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawnrange
                            .replace("[number]", String.valueOf(island.getProtectionSize())));
                    player.sendMessage(ChatColor.YELLOW + plugin.myLocale().adminSetSpawncoords
                            .replace("[min]", island.getMinProtectedX() + ", " + island.getMinProtectedZ())
                            .replace("[max]", +(island.getMinProtectedX() + island.getProtectionSize() - 1)
                                    + ", " + (island.getMinProtectedZ() + island.getProtectionSize() - 1)));
                    if (island.isLocked()) {
                        player.sendMessage(ChatColor.RED + plugin.myLocale().adminSetSpawnlocked);
                    }
                } else {
                    if (!plugin.getConfig().getBoolean("island.overridelimit")) {
                        maxRange -= 16;
                    }
                    try {
                        newRange = Integer.valueOf(split[1]);
                    } catch (Exception e) {
                        player.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).adminSetRangeInvalid
                                + " " + plugin.myLocale(playerUUID).adminSetRangeTip.replace("[max]",
                                        String.valueOf(maxRange)));
                        return true;
                    }
                    if (newRange < 10 || newRange > maxRange) {
                        player.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).adminSetRangeInvalid
                                + " " + plugin.myLocale(playerUUID).adminSetRangeTip.replace("[max]",
                                        String.valueOf(maxRange)));
                        return true;
                    }
                    island.setProtectionSize(newRange);
                    player.sendMessage(ChatColor.GREEN + plugin.myLocale(playerUUID).adminSetRangeSet
                            .replace("[number]", String.valueOf(newRange)));
                    showInfo(island.getOwner(), sender);
                }
                return true;
            }
        }
        if (split[0].equalsIgnoreCase("purge")) {
            // PURGE Command
            // Check for "allow" or "disallow" flags
            // Protect island from purging
            if (split[1].equalsIgnoreCase("allow") || split[1].equalsIgnoreCase("disallow")) {
                // Find the closest island
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminLockerrorInGame);
                    return true;
                }
                Player p = (Player) sender;
                // Island spawn must be in the island world
                if (!p.getLocation().getWorld().equals(ASkyBlock.getIslandWorld())
                        && !p.getLocation().getWorld().equals(ASkyBlock.getNetherWorld())) {
                    p.sendMessage(ChatColor.RED + plugin.myLocale(p.getUniqueId()).errorWrongWorld);
                    return true;
                }
                Island island = plugin.getGrid().getIslandAt(p.getLocation());
                if (island == null) {
                    p.sendMessage(ChatColor.RED + plugin.myLocale(p.getUniqueId()).errorNoIslandOther);
                    return true;
                }
                if (split[1].equalsIgnoreCase("allow")) {
                    island.setPurgeProtected(true);
                } else {
                    island.setPurgeProtected(false);
                }
                if (island.isPurgeProtected()) {
                    p.sendMessage(ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).adminAllowPurge);
                } else {
                    p.sendMessage(ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).adminPreventPurge);
                }
                return true;
            }

            // Purge runs in the background so if one is already running
            // this flag stops a repeat
            if (purgeFlag) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().purgealreadyRunning);
                return true;
            }

            if (split[1].equalsIgnoreCase("unowned")) {
                countUnowned(sender);
                return true;
            }
            // Set the flag
            purgeFlag = true;
            // See if this purge unowned

            // Convert days to hours - no other limit checking?
            final int time;
            try {
                time = Integer.parseInt(split[1]) * 24;
            } catch (Exception e) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().purgeusage.replace("[label]", label));
                purgeFlag = false;
                return true;
            }
            sender.sendMessage(
                    ChatColor.YELLOW + plugin.myLocale().purgecalculating.replace("[time]", split[1]));
            // Check who has not been online since the time
            for (Entry<UUID, Island> entry : plugin.getGrid().getOwnershipMap().entrySet()) {
                //plugin.getLogger().info("UUID = " + entry.getKey());
                // Only do this if it isn't protected
                if (entry.getKey() != null && !entry.getValue().isPurgeProtected()) {
                    if (Bukkit.getOfflinePlayer(entry.getKey()).hasPlayedBefore()) {
                        long offlineTime = Bukkit.getOfflinePlayer(entry.getKey()).getLastPlayed();
                        offlineTime = (System.currentTimeMillis() - offlineTime) / 3600000L;
                        if (offlineTime > time) {
                            //if (plugin.getPlayers().getIslandLevel(entry.getKey()) < Settings.abandonedIslandLevel) {
                            // Check level later
                            removeList.add(entry.getKey());
                            //}
                        }
                    } else {
                        removeList.add(entry.getKey());
                    }
                }
            }
            if (removeList.isEmpty()) {
                sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().purgenoneFound);
                purgeFlag = false;
                return true;
            }
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().purgethisWillRemove
                    .replace("[number]", String.valueOf(removeList.size()))
                    .replace("[level]", String.valueOf(Settings.abandonedIslandLevel)));
            sender.sendMessage(ChatColor.RED + plugin.myLocale().purgewarning);
            sender.sendMessage(ChatColor.RED + plugin.myLocale().purgetypeConfirm.replace("[label]", label));
            confirmReq = true;
            confirmOK = false;
            confirmTimer = 0;
            new BukkitRunnable() {
                @Override
                public void run() {
                    // This waits for 10 seconds and if no
                    // confirmation received, then it
                    // cancels
                    if (confirmTimer++ > 10) {
                        // Ten seconds is up!
                        confirmReq = false;
                        confirmOK = false;
                        purgeFlag = false;
                        removeList.clear();
                        sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().purgepurgeCancelled);
                        this.cancel();
                    } else if (confirmOK) {
                        // Set up a repeating task to run every 2
                        // seconds to remove
                        // islands one by one and then cancel when
                        // done
                        final int total = removeList.size();
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                if (removeList.isEmpty() && purgeFlag) {
                                    purgeFlag = false;
                                    sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().purgefinished);
                                    this.cancel();
                                }

                                if (removeList.size() > 0 && purgeFlag) {
                                    // Check the level
                                    if (plugin.getPlayers().getIslandLevel(
                                            removeList.get(0)) < Settings.abandonedIslandLevel) {
                                        sender.sendMessage(ChatColor.YELLOW + "["
                                                + (total - removeList.size() + 1) + "/" + total + "] "
                                                + plugin.myLocale().purgeremovingName.replace("[name]",
                                                        plugin.getPlayers().getName(removeList.get(0))));
                                        plugin.deletePlayerIsland(removeList.get(0), true);
                                    }
                                    removeList.remove(0);
                                }
                                //sender.sendMessage("Now waiting...");
                            }
                        }.runTaskTimer(plugin, 0L, 20L);
                        confirmReq = false;
                        confirmOK = false;
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, 0L, 40L);
            return true;
        } else if (split[0].equalsIgnoreCase("lock")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                Island island = plugin.getGrid().getIsland(playerUUID);
                if (island != null) {
                    Player owner = plugin.getServer().getPlayer(island.getOwner());
                    if (island.isLocked()) {
                        sender.sendMessage(ChatColor.RED + plugin.myLocale().lockUnlocking);
                        island.setLocked(false);
                        if (owner != null) {
                            owner.sendMessage(
                                    plugin.myLocale(owner.getUniqueId()).adminLockadminUnlockedIsland);
                        } else {
                            plugin.getMessages().setMessage(island.getOwner(),
                                    plugin.myLocale(island.getOwner()).adminLockadminUnlockedIsland);
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + plugin.myLocale().lockLocking);
                        island.setLocked(true);
                        if (owner != null) {
                            owner.sendMessage(plugin.myLocale(owner.getUniqueId()).adminLockadminLockedIsland);
                        } else {
                            plugin.getMessages().setMessage(island.getOwner(),
                                    plugin.myLocale(island.getOwner()).adminLockadminLockedIsland);
                        }
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoIslandOther);
                }
                return true;
            }
        } else if (split[0].equalsIgnoreCase("clearreset")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                plugin.getPlayers().setResetsLeft(playerUUID, Settings.resetLimit);
                sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().clearedResetLimit + " ["
                        + Settings.resetLimit + "]");
                return true;
            }
        } else if (split[0].equalsIgnoreCase("tp")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
                return true;
            }
            player = (Player) sender;
            // Convert name to a UUID
            final UUID targetUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(targetUUID)) {
                player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorUnknownPlayer);
                return true;
            } else {
                if (plugin.getPlayers().hasIsland(targetUUID) || plugin.getPlayers().inTeam(targetUUID)) {
                    // Teleport to the over world
                    Location warpSpot = plugin.getPlayers().getIslandLocation(targetUUID).toVector()
                            .toLocation(ASkyBlock.getIslandWorld());
                    String failureMessage = ChatColor.RED
                            + plugin.myLocale(player.getUniqueId()).adminTpManualWarp.replace("[location]",
                                    warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
                                            + warpSpot.getBlockZ());
                    // Try the player's home first
                    Location home = plugin.getPlayers().getHomeLocation(targetUUID);
                    plugin.getGrid();
                    if (home.getWorld().equals(ASkyBlock.getIslandWorld())
                            && GridManager.isSafeLocation(home)) {
                        player.teleport(home);
                        return true;
                    }
                    // Other wise, go to a safe spot
                    new SafeSpotTeleport(plugin, player, warpSpot, failureMessage);
                    return true;
                }
                sender.sendMessage(plugin.myLocale().errorNoIslandOther);
                return true;
            }
        } else if (split[0].equalsIgnoreCase("tpnether")) {
            if (!Settings.createNether || !Settings.newNether) {
                return false;
            }
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
                return true;
            }
            player = (Player) sender;
            // Convert name to a UUID
            final UUID targetUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(targetUUID)) {
                player.sendMessage(ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorUnknownPlayer);
                return true;
            } else {
                if (plugin.getPlayers().hasIsland(targetUUID) || plugin.getPlayers().inTeam(targetUUID)) {
                    // Teleport to the nether
                    Location warpSpot = plugin.getPlayers().getIslandLocation(targetUUID).toVector()
                            .toLocation(ASkyBlock.getNetherWorld());
                    String failureMessage = ChatColor.RED
                            + plugin.myLocale(player.getUniqueId()).adminTpManualWarp.replace("[location]",
                                    warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
                                            + warpSpot.getBlockZ());
                    // Try the player's home first
                    Location home = plugin.getPlayers().getHomeLocation(targetUUID);
                    plugin.getGrid();
                    if (home.getWorld().equals(ASkyBlock.getNetherWorld())
                            && GridManager.isSafeLocation(home)) {
                        player.teleport(home);
                        return true;
                    }
                    new SafeSpotTeleport(plugin, player, warpSpot, failureMessage);
                    return true;
                }
                sender.sendMessage(plugin.myLocale().errorNoIslandOther);
                return true;
            }
        } else if (split[0].equalsIgnoreCase("delete")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                // This now deletes the player and cleans them up even if
                // they don't have an island
                sender.sendMessage(
                        ChatColor.YELLOW + plugin.myLocale().deleteremoving.replace("[name]", split[1]));
                // If they are online and in ASkyBlock then delete their
                // stuff too
                Player target = plugin.getServer().getPlayer(playerUUID);
                if (target != null) {
                    // Clear any coop inventories
                    // CoopPlay.getInstance().returnAllInventories(target);
                    // Remove any of the target's coop invitees and grab
                    // their stuff
                    CoopPlay.getInstance().clearMyInvitedCoops(target);
                    CoopPlay.getInstance().clearMyCoops(target);
                    plugin.resetPlayer(target);
                }
                // plugin.getLogger().info("DEBUG: deleting player");
                plugin.deletePlayerIsland(playerUUID, true);
                return true;
            }
        } else if (split[0].equalsIgnoreCase("register")) {
            if (sender instanceof Player) {
                // Convert name to a UUID
                final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
                if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                    return true;
                } else {
                    if (adminSetPlayerIsland(sender, ((Player) sender).getLocation(), playerUUID)) {
                        sender.sendMessage(ChatColor.GREEN
                                + plugin.myLocale().registersettingIsland.replace("[name]", split[1]));
                        plugin.getGrid().saveGrid();
                    } else {
                        sender.sendMessage(ChatColor.RED + plugin.myLocale().registererrorBedrockNotFound);
                    }
                    return true;
                }
            } else {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
            }
            return true;
        } else if (split[0].equalsIgnoreCase("unregister")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                if (plugin.getPlayers().inTeam(playerUUID)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminUnregisterOnTeam);
                    return true;
                }
                Location island = plugin.getPlayers().getIslandLocation(playerUUID);
                if (island == null) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoIslandOther);
                    return true;
                }
                // Delete player, but keep blocks
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminUnregisterKeepBlocks
                        .replace("[location]", +plugin.getPlayers().getIslandLocation(playerUUID).getBlockX()
                                + "," + plugin.getPlayers().getIslandLocation(playerUUID).getBlockZ()));
                plugin.deletePlayerIsland(playerUUID, false);
                plugin.getGrid().saveGrid();
                return true;
            }
        } else if (split[0].equalsIgnoreCase("info")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            // plugin.getLogger().info("DEBUG: console player info UUID = "
            // + playerUUID);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                showInfo(playerUUID, sender);
                return true;
            }
        } else if (split[0].equalsIgnoreCase("resetallchallenges")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            plugin.getPlayers().resetAllChallenges(playerUUID);
            sender.sendMessage(
                    ChatColor.YELLOW + plugin.myLocale().resetChallengessuccess.replace("[name]", split[1]));
            return true;
        } else {
            return false;
        }
    case 3:
        // Confirm purge unowned
        if (split[0].equalsIgnoreCase("purge")) {
            if (purgeFlag) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().purgealreadyRunning);
                return true;
            }
            // Check if this is purge unowned
            if (split[1].equalsIgnoreCase("unowned") && split[2].equalsIgnoreCase("confirm")) {
                if (!purgeUnownedConfirm) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().confirmerrorTimeLimitExpired);
                    return true;
                } else {
                    purgeUnownedConfirm = false;
                    // Purge the unowned islands
                    purgeUnownedIslands(sender);
                    return true;
                }
            }

        }
        // Set protection
        if (split[0].equalsIgnoreCase("setrange")) {
            // Convert name to a UUID
            UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            // Check if player exists
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            // Check if the target is in a team and if so, the leader needs to be adjusted
            if (plugin.getPlayers().inTeam(playerUUID)) {
                playerUUID = plugin.getPlayers().getTeamLeader(playerUUID);
            }
            // Get the range that this player has now
            Island island = plugin.getGrid().getIsland(playerUUID);
            if (island == null) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoIslandOther);
                return true;
            } else {
                int newRange = 0;
                int maxRange = Settings.islandDistance;
                if (!plugin.getConfig().getBoolean("island.overridelimit")) {
                    maxRange -= 16;
                }
                try {
                    newRange = Integer.valueOf(split[2]);
                } catch (Exception e) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetRangeInvalid + " "
                            + plugin.myLocale().adminSetRangeTip.replace("[max]", String.valueOf(maxRange)));

                    return true;
                }
                if (newRange < 10 || newRange > maxRange) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminSetRangeInvalid + " "
                            + plugin.myLocale().adminSetRangeTip.replace("[max]", String.valueOf(maxRange)));

                    return true;
                }
                island.setProtectionSize(newRange);
                sender.sendMessage(ChatColor.GREEN
                        + plugin.myLocale().adminSetRangeSet.replace("[number]", String.valueOf(newRange)));
                showInfo(playerUUID, sender);
                plugin.getGrid().saveGrid();
                return true;
            }
        }
        // Change biomes
        if (split[0].equalsIgnoreCase("setbiome")) {
            // Convert name to a UUID
            UUID playerUUID = plugin.getPlayers().getUUID(split[1]);
            // Check if player exists
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            // Check if the target is in a team and if so, the leader
            if (plugin.getPlayers().inTeam(playerUUID)) {
                playerUUID = plugin.getPlayers().getTeamLeader(playerUUID);
            }
            // Check if biome is valid
            Biome biome = null;
            String biomeName = split[2].toUpperCase();
            try {
                biome = Biome.valueOf(biomeName);
                biomeName = biome.name();
                if (!plugin.getConfig().contains("biomes." + biomeName)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().biomeUnknown);
                    // Doing it this way ensures that only valid biomes are
                    // shown
                    for (Biome b : Biome.values()) {
                        if (plugin.getConfig().contains("biomes." + b.name())) {
                            sender.sendMessage(b.name());
                        }
                    }
                    return true;
                }
                // Get friendly name
                biomeName = plugin.getConfig().getString("biomes." + biomeName + ".friendlyname",
                        Util.prettifyText(biomeName));

            } catch (Exception e) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().biomeUnknown);
                for (Biome b : Biome.values()) {
                    if (plugin.getConfig().contains("biomes." + b.name())) {
                        sender.sendMessage(b.name());
                    }
                }
                return true;
            }
            // Okay clear to set biome
            // Actually set the biome
            if (plugin.getPlayers().inTeam(playerUUID)
                    && plugin.getPlayers().getTeamIslandLocation(playerUUID) != null) {
                plugin.getBiomes().setIslandBiome(plugin.getPlayers().getTeamIslandLocation(playerUUID), biome);
            } else {
                plugin.getBiomes().setIslandBiome(plugin.getPlayers().getIslandLocation(playerUUID), biome);
            }
            sender.sendMessage(ChatColor.GREEN + plugin.myLocale().biomeSet.replace("[biome]", biomeName));
            Player targetPlayer = plugin.getServer().getPlayer(playerUUID);
            if (targetPlayer != null) {
                // Online
                targetPlayer.sendMessage("[Admin] " + ChatColor.GREEN
                        + plugin.myLocale(playerUUID).biomeSet.replace("[biome]", biomeName));
            } else {
                plugin.getMessages().setMessage(playerUUID, "[Admin] " + ChatColor.GREEN
                        + plugin.myLocale(playerUUID).biomeSet.replace("[biome]", biomeName));
            }
            return true;
        } else
        // team kick <player> and team delete <leader>
        if (split[0].equalsIgnoreCase("team")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[2]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            if (split[1].equalsIgnoreCase("kick")) {
                // Remove player from team
                if (!plugin.getPlayers().inTeam(playerUUID)) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().errorNoTeam);
                    return true;
                }
                UUID teamLeader = plugin.getPlayers().getTeamLeader(playerUUID);
                if (teamLeader == null) {
                    // Player is apparently in a team, but there is no team leader
                    // Remove their team status
                    // Clear the player of all team-related items
                    plugin.getPlayers().setLeaveTeam(playerUUID);
                    plugin.getPlayers().setHomeLocation(playerUUID, null);
                    plugin.getPlayers().setIslandLocation(playerUUID, null);
                    // Remove any warps
                    plugin.getWarpSignsListener().removeWarp(playerUUID);
                    sender.sendMessage(
                            ChatColor.RED + plugin.myLocale().kicknameRemoved.replace("[name]", split[2]));
                    return true;
                }
                // Payer is not a team leader
                if (!teamLeader.equals(playerUUID)) {
                    // Clear the player of all team-related items
                    plugin.getPlayers().setLeaveTeam(playerUUID);
                    plugin.getPlayers().setHomeLocation(playerUUID, null);
                    plugin.getPlayers().setIslandLocation(playerUUID, null);
                    // Clear the leader of this player and if they now have
                    // no team, remove the team
                    plugin.getPlayers().removeMember(teamLeader, playerUUID);
                    if (plugin.getPlayers().getMembers(teamLeader).size() < 2) {
                        plugin.getPlayers().setLeaveTeam(teamLeader);
                    }
                    // Remove any warps
                    plugin.getWarpSignsListener().removeWarp(playerUUID);
                    sender.sendMessage(
                            ChatColor.RED + plugin.myLocale().kicknameRemoved.replace("[name]", split[2]));
                    return true;
                } else {
                    sender.sendMessage(
                            ChatColor.RED + (plugin.myLocale().adminTeamKickLeader.replace("[label]", label))
                                    .replace("[name]", split[2]));
                    return true;
                }
            } else {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
                return false;
            }
        } else if (split[0].equalsIgnoreCase("completechallenge")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[2]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            if (plugin.getPlayers().checkChallenge(playerUUID, split[1].toLowerCase())
                    || !plugin.getPlayers().get(playerUUID).challengeExists(split[1].toLowerCase())) {
                sender.sendMessage(
                        ChatColor.RED + plugin.myLocale().completeChallengeerrorChallengeDoesNotExist);
                return true;
            }
            plugin.getPlayers().get(playerUUID).completeChallenge(split[1].toLowerCase());
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().completeChallengechallangeCompleted
                    .replace("[challengename]", split[1].toLowerCase()).replace("[name]", split[2]));
            return true;
        } else if (split[0].equalsIgnoreCase("resetchallenge")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[2]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            if (!plugin.getPlayers().checkChallenge(playerUUID, split[1].toLowerCase())
                    || !plugin.getPlayers().get(playerUUID).challengeExists(split[1].toLowerCase())) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().resetChallengeerrorChallengeDoesNotExist);
                return true;
            }
            plugin.getPlayers().resetChallenge(playerUUID, split[1].toLowerCase());
            sender.sendMessage(ChatColor.YELLOW + plugin.myLocale().resetChallengechallengeReset
                    .replace("[challengename]", split[1].toLowerCase()).replace("[name]", split[2]));
            return true;
        } else if (split[0].equalsIgnoreCase("info") && split[1].equalsIgnoreCase("challenges")) {
            // Convert name to a UUID
            final UUID playerUUID = plugin.getPlayers().getUUID(split[2]);
            // plugin.getLogger().info("DEBUG: console player info UUID = "
            // + playerUUID);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            } else {
                showInfoChallenges(playerUUID, sender);
                return true;
            }
        }
        return false;
    case 4:
        // Team add <player> <leader>
        if (split[0].equalsIgnoreCase("team") && split[1].equalsIgnoreCase("add")) {
            // Convert names to UUIDs
            final UUID playerUUID = plugin.getPlayers().getUUID(split[2]);
            final Player targetPlayer = plugin.getServer().getPlayer(playerUUID);
            final UUID teamLeader = plugin.getPlayers().getUUID(split[3]);
            if (!plugin.getPlayers().isAKnownPlayer(playerUUID)
                    || !plugin.getPlayers().isAKnownPlayer(teamLeader)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownPlayer);
                return true;
            }
            if (playerUUID.equals(teamLeader)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminTeamAddLeaderToOwn);
                return true;
            }
            // See if leader has an island
            if (!plugin.getPlayers().hasIsland(teamLeader)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().adminTeamAddLeaderNoIsland);
                return true;
            }
            // Check to see if this player is already in a team
            if (plugin.getPlayers().inTeam(playerUUID)) {
                sender.sendMessage(ChatColor.RED + plugin.myLocale().inviteerrorThatPlayerIsAlreadyInATeam);
                return true;
            }
            // If the leader's member list does not contain their own name
            // then
            // add it
            if (!plugin.getPlayers().getMembers(teamLeader).contains(teamLeader)) {
                // Set up the team leader
                plugin.getPlayers().setJoinTeam(teamLeader, teamLeader,
                        plugin.getPlayers().getIslandLocation(teamLeader));
                plugin.getPlayers().addTeamMember(teamLeader, teamLeader);
                sender.sendMessage(ChatColor.GOLD + plugin.myLocale().adminTeamAddedLeader);
            }
            // This is a hack to clear any pending invitations
            if (targetPlayer != null) {
                targetPlayer.performCommand(Settings.ISLANDCOMMAND + " decline");
            }
            // If the invitee has an island of their own
            if (plugin.getPlayers().hasIsland(playerUUID)) {
                Location islandLoc = plugin.getPlayers().getIslandLocation(playerUUID);
                if (islandLoc != null) {
                    sender.sendMessage(ChatColor.RED + plugin.myLocale().adminTeamNowUnowned
                            .replace("[name]", plugin.getPlayers().getName(playerUUID))
                            .replace("[location]", islandLoc.getBlockX() + " " + islandLoc.getBlockZ()));
                }
            }
            // Remove their old island affiliation - do not delete the
            // island just in case
            plugin.deletePlayerIsland(playerUUID, false);
            // Join the team and set the team island location and leader
            plugin.getPlayers().setJoinTeam(playerUUID, teamLeader,
                    plugin.getPlayers().getIslandLocation(teamLeader));
            // Configure the best home location for this player
            if (plugin.getPlayers().getHomeLocation(teamLeader) != null) {
                plugin.getPlayers().setHomeLocation(playerUUID,
                        plugin.getPlayers().getHomeLocation(teamLeader));
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminTeamSettingHome);
            } else {
                plugin.getPlayers().setHomeLocation(playerUUID,
                        plugin.getPlayers().getIslandLocation(teamLeader));
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminTeamSettingHome);
            }
            // If the leader's member list does not contain player then add
            // it
            if (!plugin.getPlayers().getMembers(teamLeader).contains(playerUUID)) {
                plugin.getPlayers().addTeamMember(teamLeader, playerUUID);
                sender.sendMessage(ChatColor.GREEN + plugin.myLocale().adminTeamAddingPlayer);
            } else {
                sender.sendMessage(ChatColor.GOLD + plugin.myLocale().adminTeamAlreadyOnTeam);
            }
            // Teleport the player if they are online
            if (targetPlayer != null) {
                plugin.getGrid().homeTeleport(targetPlayer);
            }
            plugin.getGrid().saveGrid();
            return true;
        } else {
            sender.sendMessage(ChatColor.RED + plugin.myLocale().errorUnknownCommand);
            return false;
        }
    default:
        return false;
    }
}

From source file:org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat.java

/**
 * Process all the nodes and create splits that are local to a node.
 * Generate one split per node iteration, and walk over nodes multiple times
 * to distribute the splits across nodes.
 * <p>/*from  w w  w.j a  v a2s .  c  o m*/
 * Note: The order of processing the nodes is undetermined because the
 * implementation of nodeToBlocks is {@link java.util.HashMap} and its order
 * of the entries is undetermined.
 * @param nodeToBlocks Mapping from a node to the list of blocks that
 *                     it contains.
 * @param blockToNodes Mapping from a block to the nodes on which
 *                     it has replicas.
 * @param rackToBlocks Mapping from a rack name to the list of blocks it has.
 * @param totLength Total length of the input files.
 * @param maxSize Max size of each split.
 *                If set to 0, disable smoothing load.
 * @param minSizeNode Minimum split size per node.
 * @param minSizeRack Minimum split size per rack.
 * @param splits New splits created by this method are added to the list.
 */
@VisibleForTesting
void createSplits(Map<String, Set<OneBlockInfo>> nodeToBlocks, Map<OneBlockInfo, String[]> blockToNodes,
        Map<String, List<OneBlockInfo>> rackToBlocks, long totLength, long maxSize, long minSizeNode,
        long minSizeRack, List<InputSplit> splits) {
    ArrayList<OneBlockInfo> validBlocks = new ArrayList<OneBlockInfo>();
    long curSplitSize = 0;

    int totalNodes = nodeToBlocks.size();
    long totalLength = totLength;

    Multiset<String> splitsPerNode = HashMultiset.create();
    Set<String> completedNodes = new HashSet<String>();

    while (true) {
        for (Iterator<Map.Entry<String, Set<OneBlockInfo>>> iter = nodeToBlocks.entrySet().iterator(); iter
                .hasNext();) {
            Map.Entry<String, Set<OneBlockInfo>> one = iter.next();

            String node = one.getKey();

            // Skip the node if it has previously been marked as completed.
            if (completedNodes.contains(node)) {
                continue;
            }

            Set<OneBlockInfo> blocksInCurrentNode = one.getValue();

            // for each block, copy it into validBlocks. Delete it from
            // blockToNodes so that the same block does not appear in
            // two different splits.
            Iterator<OneBlockInfo> oneBlockIter = blocksInCurrentNode.iterator();
            while (oneBlockIter.hasNext()) {
                OneBlockInfo oneblock = oneBlockIter.next();

                // Remove all blocks which may already have been assigned to other
                // splits.
                if (!blockToNodes.containsKey(oneblock)) {
                    oneBlockIter.remove();
                    continue;
                }

                validBlocks.add(oneblock);
                blockToNodes.remove(oneblock);
                curSplitSize += oneblock.length;

                // if the accumulated split size exceeds the maximum, then
                // create this split.
                if (maxSize != 0 && curSplitSize >= maxSize) {
                    // create an input split and add it to the splits array
                    addCreatedSplit(splits, Collections.singleton(node), validBlocks);
                    totalLength -= curSplitSize;
                    curSplitSize = 0;

                    splitsPerNode.add(node);

                    // Remove entries from blocksInNode so that we don't walk these
                    // again.
                    blocksInCurrentNode.removeAll(validBlocks);
                    validBlocks.clear();

                    // Done creating a single split for this node. Move on to the next
                    // node so that splits are distributed across nodes.
                    break;
                }

            }
            if (validBlocks.size() != 0) {
                // This implies that the last few blocks (or all in case maxSize=0)
                // were not part of a split. The node is complete.

                // if there were any blocks left over and their combined size is
                // larger than minSplitNode, then combine them into one split.
                // Otherwise add them back to the unprocessed pool. It is likely
                // that they will be combined with other blocks from the
                // same rack later on.
                // This condition also kicks in when max split size is not set. All
                // blocks on a node will be grouped together into a single split.
                if (minSizeNode != 0 && curSplitSize >= minSizeNode && splitsPerNode.count(node) == 0) {
                    // haven't created any split on this machine. so its ok to add a
                    // smaller one for parallelism. Otherwise group it in the rack for
                    // balanced size create an input split and add it to the splits
                    // array
                    addCreatedSplit(splits, Collections.singleton(node), validBlocks);
                    totalLength -= curSplitSize;
                    splitsPerNode.add(node);
                    // Remove entries from blocksInNode so that we don't walk this again.
                    blocksInCurrentNode.removeAll(validBlocks);
                    // The node is done. This was the last set of blocks for this node.
                } else {
                    // Put the unplaced blocks back into the pool for later rack-allocation.
                    for (OneBlockInfo oneblock : validBlocks) {
                        blockToNodes.put(oneblock, oneblock.hosts);
                    }
                }
                validBlocks.clear();
                curSplitSize = 0;
                completedNodes.add(node);
            } else { // No in-flight blocks.
                if (blocksInCurrentNode.size() == 0) {
                    // Node is done. All blocks were fit into node-local splits.
                    completedNodes.add(node);
                } // else Run through the node again.
            }
        }

        // Check if node-local assignments are complete.
        if (completedNodes.size() == totalNodes || totalLength == 0) {
            // All nodes have been walked over and marked as completed or all blocks
            // have been assigned. The rest should be handled via rackLock assignment.
            LOG.info("DEBUG: Terminated node allocation with : CompletedNodes: " + completedNodes.size()
                    + ", size left: " + totalLength);
            break;
        }
    }

    // if blocks in a rack are below the specified minimum size, then keep them
    // in 'overflow'. After the processing of all racks is complete, these 
    // overflow blocks will be combined into splits.
    ArrayList<OneBlockInfo> overflowBlocks = new ArrayList<OneBlockInfo>();
    Set<String> racks = new HashSet<String>();

    // Process all racks over and over again until there is no more work to do.
    while (blockToNodes.size() > 0) {

        // Create one split for this rack before moving over to the next rack. 
        // Come back to this rack after creating a single split for each of the 
        // remaining racks.
        // Process one rack location at a time, Combine all possible blocks that
        // reside on this rack as one split. (constrained by minimum and maximum
        // split size).

        // iterate over all racks 
        for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = rackToBlocks.entrySet().iterator(); iter
                .hasNext();) {

            Map.Entry<String, List<OneBlockInfo>> one = iter.next();
            racks.add(one.getKey());
            List<OneBlockInfo> blocks = one.getValue();

            // for each block, copy it into validBlocks. Delete it from 
            // blockToNodes so that the same block does not appear in 
            // two different splits.
            boolean createdSplit = false;
            for (OneBlockInfo oneblock : blocks) {
                if (blockToNodes.containsKey(oneblock)) {
                    validBlocks.add(oneblock);
                    blockToNodes.remove(oneblock);
                    curSplitSize += oneblock.length;

                    // if the accumulated split size exceeds the maximum, then 
                    // create this split.
                    if (maxSize != 0 && curSplitSize >= maxSize) {
                        // create an input split and add it to the splits array
                        addCreatedSplit(splits, getHosts(racks), validBlocks);
                        createdSplit = true;
                        break;
                    }
                }
            }

            // if we created a split, then just go to the next rack
            if (createdSplit) {
                curSplitSize = 0;
                validBlocks.clear();
                racks.clear();
                continue;
            }

            if (!validBlocks.isEmpty()) {
                if (minSizeRack != 0 && curSplitSize >= minSizeRack) {
                    // if there is a minimum size specified, then create a single split
                    // otherwise, store these blocks into overflow data structure
                    addCreatedSplit(splits, getHosts(racks), validBlocks);
                } else {
                    // There were a few blocks in this rack that 
                    // remained to be processed. Keep them in 'overflow' block list. 
                    // These will be combined later.
                    overflowBlocks.addAll(validBlocks);
                }
            }
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    assert blockToNodes.isEmpty();
    assert curSplitSize == 0;
    assert validBlocks.isEmpty();
    assert racks.isEmpty();

    // Process all overflow blocks
    for (OneBlockInfo oneblock : overflowBlocks) {
        validBlocks.add(oneblock);
        curSplitSize += oneblock.length;

        // This might cause an exiting rack location to be re-added,
        // but it should be ok.
        for (int i = 0; i < oneblock.racks.length; i++) {
            racks.add(oneblock.racks[i]);
        }

        // if the accumulated split size exceeds the maximum, then 
        // create this split.
        if (maxSize != 0 && curSplitSize >= maxSize) {
            // create an input split and add it to the splits array
            addCreatedSplit(splits, getHosts(racks), validBlocks);
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    // Process any remaining blocks, if any.
    if (!validBlocks.isEmpty()) {
        addCreatedSplit(splits, getHosts(racks), validBlocks);
    }
}