Example usage for java.util TreeMap entrySet

List of usage examples for java.util TreeMap entrySet

Introduction

In this page you can find the example usage for java.util TreeMap entrySet.

Prototype

EntrySet entrySet

To view the source code for java.util TreeMap entrySet.

Click Source Link

Document

Fields initialized to contain an instance of the entry set view the first time this view is requested.

Usage

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step8GoldDataAggregator.java

public static void main(String[] args) throws Exception {
    String inputDir = args[0] + "/";
    // output dir
    File outputDir = new File(args[1]);
    File turkersConfidence = new File(args[2]);
    if (outputDir.exists()) {
        outputDir.delete();/*from  w  ww. j  ava 2  s  . c  o m*/
    }
    outputDir.mkdir();

    List<String> annotatorsIDs = new ArrayList<>();
    //        for (File f : FileUtils.listFiles(new File(inputDir), new String[] { "xml" }, false)) {
    //            QueryResultContainer queryResultContainer = QueryResultContainer
    //                    .fromXML(FileUtils.readFileToString(f, "utf-8"));
    //            for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
    //                for (QueryResultContainer.MTurkRelevanceVote relevanceVote : rankedResults.mTurkRelevanceVotes) {
    //                    if (!annotatorsIDs.contains(relevanceVote.turkID))
    //                        annotatorsIDs.add(relevanceVote.turkID);
    //                }
    //            }
    //        }
    HashMap<String, Integer> countVotesForATurker = new HashMap<>();
    // creates temporary file with format for mace
    // Hashmap annotations: key is the id of a document and a sentence
    // Value is an array votes[] of turkers decisions: true or false (relevant or not)
    // the length of this array equals the number of annotators in List<String> annotatorsIDs.
    // If an annotator worked on the task his decision is written in the array otherwise the value is NULL

    // key: queryID + clueWebID + sentenceID
    // value: true and false annotations
    TreeMap<String, Annotations> annotations = new TreeMap<>();

    for (File f : FileUtils.listFiles(new File(inputDir), new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));
        System.out.println("Reading " + f.getName());
        for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
            String documentID = rankedResults.clueWebID;
            for (QueryResultContainer.MTurkRelevanceVote relevanceVote : rankedResults.mTurkRelevanceVotes) {
                Integer turkerID;
                if (!annotatorsIDs.contains(relevanceVote.turkID)) {
                    annotatorsIDs.add(relevanceVote.turkID);
                    turkerID = annotatorsIDs.size() - 1;
                } else {
                    turkerID = annotatorsIDs.indexOf(relevanceVote.turkID);
                }
                Integer count = countVotesForATurker.get(relevanceVote.turkID);
                if (count == null) {
                    count = 0;
                }
                count++;
                countVotesForATurker.put(relevanceVote.turkID, count);

                String id;
                List<Integer> trueVotes;
                List<Integer> falseVotes;
                for (QueryResultContainer.SingleSentenceRelevanceVote singleSentenceRelevanceVote : relevanceVote.singleSentenceRelevanceVotes)
                    if (!"".equals(singleSentenceRelevanceVote.sentenceID)) {

                        id = f.getName() + "_" + documentID + "_" + singleSentenceRelevanceVote.sentenceID;
                        Annotations turkerVotes = annotations.get(id);
                        if (turkerVotes == null) {
                            trueVotes = new ArrayList<>();
                            falseVotes = new ArrayList<>();
                            turkerVotes = new Annotations(trueVotes, falseVotes);
                        }
                        trueVotes = turkerVotes.trueAnnotations;
                        falseVotes = turkerVotes.falseAnnotations;
                        if ("true".equals(singleSentenceRelevanceVote.relevant)) {
                            // votes[turkerID] = true;
                            trueVotes.add(turkerID);
                        } else if ("false".equals(singleSentenceRelevanceVote.relevant)) {
                            //   votes[turkerID] = false;
                            falseVotes.add(turkerID);
                        } else {
                            throw new IllegalStateException("Annotation value of sentence "
                                    + singleSentenceRelevanceVote.sentenceID + " in " + rankedResults.clueWebID
                                    + " equals " + singleSentenceRelevanceVote.relevant);
                        }
                        try {
                            int allVotesCount = trueVotes.size() + falseVotes.size();
                            if (allVotesCount > 5) {
                                System.err.println(id + " doesn't have 5 annotators: true: " + trueVotes.size()
                                        + " false: " + falseVotes.size());

                                // nasty hack, we're gonna strip some data; true votes first
                                /* we can't do that, it breaks something down the line
                                int toRemove = allVotesCount - 5;
                                if (trueVotes.size() >= toRemove) {
                                trueVotes = trueVotes
                                        .subList(0, trueVotes.size() - toRemove);
                                }
                                else if (
                                    falseVotes.size() >= toRemove) {
                                falseVotes = falseVotes
                                        .subList(0, trueVotes.size() - toRemove);
                                }
                                */
                                System.err.println("Adjusted: " + id + " doesn't have 5 annotators: true: "
                                        + trueVotes.size() + " false: " + falseVotes.size());
                            }
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        }
                        turkerVotes.trueAnnotations = trueVotes;
                        turkerVotes.falseAnnotations = falseVotes;
                        annotations.put(id, turkerVotes);
                    } else {
                        throw new IllegalStateException(
                                "Empty Sentence ID in " + f.getName() + " for turker " + turkerID);
                    }

            }
        }

    }
    File tmp = printHashMap(annotations, annotatorsIDs.size());

    String file = TEMP_DIR + "/" + tmp.getName();
    MACE.main(new String[] { "--prefix", file });

    //gets the keys of the documents and sentences
    ArrayList<String> lines = (ArrayList<String>) FileUtils.readLines(new File(file + ".prediction"));
    int i = 0;
    TreeMap<String, TreeMap<String, ArrayList<HashMap<String, String>>>> ids = new TreeMap<>();
    ArrayList<HashMap<String, String>> sentences;
    if (lines.size() != annotations.size()) {
        throw new IllegalStateException(
                "The size of prediction file is " + lines.size() + "but expected " + annotations.size());
    }
    for (Map.Entry entry : annotations.entrySet()) { //1001.xml_clueweb12-1905wb-13-07360_8783
        String key = (String) entry.getKey();
        String[] IDs = key.split("_");
        if (IDs.length > 2) {
            String queryID = IDs[0];
            String clueWebID = IDs[1];
            String sentenceID = IDs[2];
            TreeMap<String, ArrayList<HashMap<String, String>>> clueWebIDs = ids.get(queryID);
            if (clueWebIDs == null) {
                clueWebIDs = new TreeMap<>();
            }
            sentences = clueWebIDs.get(clueWebID);
            if (sentences == null) {
                sentences = new ArrayList<>();
            }
            HashMap<String, String> sentence = new HashMap<>();
            sentence.put(sentenceID, lines.get(i));
            sentences.add(sentence);
            clueWebIDs.put(clueWebID, sentences);
            ids.put(queryID, clueWebIDs);
        } else {
            throw new IllegalStateException("Wrong ID " + key);
        }

        i++;
    }

    for (Map.Entry entry : ids.entrySet()) {
        TreeMap<Integer, String> value = (TreeMap<Integer, String>) entry.getValue();
        String queryID = (String) entry.getKey();
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(new File(inputDir, queryID), "utf-8"));
        for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
            for (Map.Entry val : value.entrySet()) {
                String clueWebID = (String) val.getKey();
                if (clueWebID.equals(rankedResults.clueWebID)) {
                    List<QueryResultContainer.SingleSentenceRelevanceVote> goldEstimatedLabels = new ArrayList<>();
                    List<QueryResultContainer.SingleSentenceRelevanceVote> turkersVotes = new ArrayList<>();
                    int size = 0;
                    int hitSize = 0;
                    String hitID = "";
                    for (QueryResultContainer.MTurkRelevanceVote vote : rankedResults.mTurkRelevanceVotes) {
                        if (!hitID.equals(vote.hitID)) {
                            hitID = vote.hitID;
                            hitSize = vote.singleSentenceRelevanceVotes.size();
                            size = size + hitSize;
                            turkersVotes.addAll(vote.singleSentenceRelevanceVotes);
                        } else {
                            if (vote.singleSentenceRelevanceVotes.size() != hitSize) {
                                hitSize = vote.singleSentenceRelevanceVotes.size();
                                size = size + hitSize;
                                turkersVotes.addAll(vote.singleSentenceRelevanceVotes);
                            }
                        }
                    }
                    ArrayList<HashMap<String, String>> sentenceList = (ArrayList<HashMap<String, String>>) val
                            .getValue();
                    if (sentenceList.size() != turkersVotes.size()) {
                        try {
                            throw new IllegalStateException("Expected size of annotations is "
                                    + turkersVotes.size() + "but found " + sentenceList.size()
                                    + " for document " + rankedResults.clueWebID + " in " + queryID);
                        } catch (IllegalStateException ex) {
                            ex.printStackTrace();
                        }
                    }
                    for (QueryResultContainer.SingleSentenceRelevanceVote s : turkersVotes) {
                        String valSentence = null;
                        for (HashMap<String, String> anno : sentenceList) {
                            if (anno.keySet().contains(s.sentenceID)) {
                                valSentence = anno.get(s.sentenceID);
                            }
                        }
                        QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote();
                        singleSentenceVote.sentenceID = s.sentenceID;
                        if (("false").equals(valSentence)) {
                            singleSentenceVote.relevant = "false";
                        } else if (("true").equals(valSentence)) {
                            singleSentenceVote.relevant = "true";
                        } else {
                            throw new IllegalStateException("Annotation value of sentence "
                                    + singleSentenceVote.sentenceID + " equals " + val.getValue());
                        }
                        goldEstimatedLabels.add(singleSentenceVote);
                    }
                    rankedResults.goldEstimatedLabels = goldEstimatedLabels;
                }
            }
        }
        File outputFile = new File(outputDir, queryID);
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }

    ArrayList<String> annotators = (ArrayList<String>) FileUtils.readLines(new File(file + ".competence"));
    FileWriter fileWriter;
    StringBuilder sb = new StringBuilder();
    for (int j = 0; j < annotatorsIDs.size(); j++) {
        String[] s = annotators.get(0).split("\t");
        Float score = Float.parseFloat(s[j]);
        String turkerID = annotatorsIDs.get(j);
        System.out.println(turkerID + " " + score + " " + countVotesForATurker.get(turkerID));
        sb.append(turkerID).append(" ").append(score).append(" ").append(countVotesForATurker.get(turkerID))
                .append("\n");
    }
    fileWriter = new FileWriter(turkersConfidence);
    fileWriter.append(sb.toString());
    fileWriter.close();

}

From source file:Main.java

private static String getValue(TreeMap<String, String> map, String key) {
    for (Map.Entry<String, String> entry : map.entrySet()) {
        if (entry.getKey().equalsIgnoreCase(key)) {
            return entry.getValue();
        }/*from  w  w w .j a v a2  s.  com*/
    }
    return "";
}

From source file:Main.java

private static void MyPut(TreeMap<String, String> map, String key, String value) {
    for (Entry<String, String> entry : map.entrySet()) {
        String keyOld = entry.getKey();
        String valueOld = entry.getValue();
        if (keyOld.equals(key)) {
            value = valueOld + "&" + value;
            break;
        }// w ww. jav a  2 s. com
    }
    map.put(key, value);
}

From source file:Main.java

public static void log(Map<?, ?> map) {

    TreeMap<Object, Object> tree = new TreeMap<Object, Object>(map);

    for (Map.Entry<Object, Object> entry : tree.entrySet()) {
        System.err.println(String.format("%s = %s", entry.getKey(), entry.getValue()));
    }/*www.  j  a  va2  s.c  o  m*/

}

From source file:io.fabric8.mq.controller.util.BrokerJmxUtils.java

public static String getOrderedProperties(Hashtable<String, String> properties) {
    TreeMap<String, String> map = new TreeMap<>(properties);
    String result = "";
    String separator = "";
    for (Map.Entry<String, String> entry : map.entrySet()) {
        result += separator;/*from w ww  . java2  s  .  c  om*/
        result += entry.getKey() + "=" + entry.getValue();
        separator = ",";
    }
    return result;
}

From source file:edu.umass.cs.gnsserver.utils.ParametersAndOptions.java

/**
 * Shows all the options on the console.
 *
 * @param options//from   w  ww. ja va 2 s .c  o  m
 */
public static void printOptions(Map<String, String> options) {
    StringBuilder result = new StringBuilder();
    TreeMap<String, String> tree = new TreeMap<String, String>(options);
    for (Entry<String, String> entry : tree.entrySet()) {
        result.append(entry.getKey());
        result.append(" => ");
        result.append(entry.getValue());
        result.append("\n");
    }
    System.out.print(result.toString());
}

From source file:io.warp10.continuum.DirectoryUtil.java

private static long computeHash(long k0, long k1, long timestamp, List<String> classSelectors,
        List<Map<String, String>> labelsSelectors) {
    ////from w  w  w  . j av a2 s  . com
    // Create a ByteArrayOutputStream into which the content will be dumped
    //

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Add timestamp

    try {
        baos.write(Longs.toByteArray(timestamp));

        if (null != classSelectors) {
            for (String classSelector : classSelectors) {
                baos.write(classSelector.getBytes(Charsets.UTF_8));
            }
        }

        if (null != labelsSelectors) {
            for (Map<String, String> map : labelsSelectors) {
                TreeMap<String, String> tm = new TreeMap<String, String>();
                tm.putAll(map);
                for (Entry<String, String> entry : tm.entrySet()) {
                    baos.write(entry.getKey().getBytes(Charsets.UTF_8));
                    baos.write(entry.getValue().getBytes(Charsets.UTF_8));
                }
            }
        }
    } catch (IOException ioe) {
        return 0L;
    }

    // Compute hash

    byte[] data = baos.toByteArray();

    long hash = SipHashInline.hash24(k0, k1, data, 0, data.length);

    return hash;
}

From source file:kr.co.aim.nanoframe.orm.SQLLogUtil.java

public static String getLogFormatSqlStatement(String sql, Object args, Log log) {
    try {//  w w  w .j a  v  a2s  .co m
        if (args instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) args;

            TreeMap<String, Object> treeMap = new TreeMap<String, Object>(stringComparator);
            treeMap.putAll(map);

            Iterator<Entry<String, Object>> iter = treeMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<String, Object> entry = iter.next();
                sql = sql.replace(":" + entry.getKey(), getLogFormatArgument(entry.getValue()));
            }
        } else if (args instanceof Object[]) {
            Object[] objs = (Object[]) args;

            for (Object obj : objs) {
                sql = StringUtils.replaceOnce(sql, "?", getLogFormatArgument(obj));
            }
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled())
            log.error(t, t);
        else
            log.error(t);
    }

    return sql;
}

From source file:com.azaptree.services.spring.application.SpringApplicationService.java

private static void logEnvironment(final Logger log) {
    if (log.isDebugEnabled()) {
        final TreeMap<String, String> sysProps = new TreeMap<>(System.getenv());
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final PrintStream ps = new PrintStream(bos);
        for (final Map.Entry<String, String> entry : sysProps.entrySet()) {
            ps.print(entry.getKey());/*ww  w . java2 s.  co m*/
            ps.print('=');
            ps.println(entry.getValue());
        }
        log.debug("Environment:\n{}", bos);
    }
}

From source file:io.apiman.tools.i18n.TemplateScanner.java

/**
 * Output the sorted map of strings to the specified output file.
 * @param strings//  ww w .j a v  a 2s  . c  o  m
 * @param outputFile
 * @throws FileNotFoundException
 */
private static void outputMessages(TreeMap<String, String> strings, File outputFile)
        throws FileNotFoundException {
    PrintWriter writer = new PrintWriter(new FileOutputStream(outputFile));
    for (Entry<String, String> entry : strings.entrySet()) {
        String key = entry.getKey();
        String val = entry.getValue();
        writer.append(key);
        writer.append('=');
        writer.append(val);
        writer.append("\n");
    }
    writer.flush();
    writer.close();
}