List of usage examples for java.util TreeSet TreeSet
public TreeSet()
From source file:de.knowwe.visualization.util.Utils.java
public static String prepareLabel(String string) { // if (true) return string; String lb = LINE_BREAK;//ww w . j a v a 2 s . co m int length = string.length(); if (length < 13) return clean(string, lb); // find possible line break positions Set<Integer> possibleLBs = new TreeSet<>(); // possible line breaks are before the following chars: // _ >= <= = . ( [ and white spaces Matcher m = Pattern.compile("_|>=|<=|=|\\.|\\([^\\)]{1}|\\[[^\\]]{1}").matcher(string); while (m.find()) { possibleLBs.add(m.start(0)); } // line breaks at whitespace only if they are not in range of = or > or // < m = Pattern.compile("(?<=[^=<>]){3}( )(?=[^=<>]{3})").matcher(string); while (m.find()) { possibleLBs.add(m.start(1)); } if (possibleLBs.isEmpty()) return clean(string, lb); // add the line breaks were it makes sense List<Integer> desiredLBs = new LinkedList<>(); Set<Integer> addedLBs = new TreeSet<>(); // optimal length is determined by the length of the given String double optimalLength = (double) length / Math.sqrt(length / 5); for (int i = 1; i < string.length() / optimalLength; i++) { // having the line breaks on these position would be optimal desiredLBs.add((int) Math.round(i * optimalLength)); } //todo: remove creation of trailing linebreaks // try to find those possible line breaks that closest to the optimal // line breaks int d = 0; for (Integer desLB : desiredLBs) { int bestCandiadate = 0; // to avoid breaks for only a few chars at the end, we make // extra efforts for the last line break // we get the line break that produces the smallest variance // we should actually calculate the best break via variance for // all line breaks, but that seems rather complex and not yet // justified right now, since the current simple algorithm // already produces nice results if (d == desiredLBs.size() - 1) { double bestVar = Double.MAX_VALUE; for (Integer posLB : possibleLBs) { Set<Integer> temp = new TreeSet<>(addedLBs); temp.add(posLB); TreeSet<Integer> varianceCheck = new TreeSet<>(temp); varianceCheck.add(length); double variance = getVariance(varianceCheck); if (variance <= bestVar) { bestVar = variance; bestCandiadate = posLB; } } } // for all other breakpoints, just get the one closest to the // desired position else { for (Integer posLB : possibleLBs) { if (Math.abs(desLB - posLB) <= Math.abs(desLB - bestCandiadate)) { bestCandiadate = posLB; } } } if (bestCandiadate != 0 && bestCandiadate != length) { addedLBs.add(bestCandiadate); } d++; } // but in the line breaks StringBuilder labelBuilder = new StringBuilder(); List<String> split = new ArrayList<>(addedLBs.size() + 1); int last = 0; for (Integer addedLB : addedLBs) { split.add(string.substring(last, addedLB)); last = addedLB; } split.add(string.substring(last, string.length())); for (String s : split) { // clean the substrings labelBuilder.append(clean(s.trim(), lb)).append(lb); } String label = labelBuilder.toString(); return label; }
From source file:annis.CSVHelper.java
public static SortedMap<Integer, SortedSet<String>> exportCSVHeader(Iterator<AnnotatedMatch> matches, PrintWriter w) {/*from w ww . j a va2 s.c o m*/ // figure out what annotations are used at each match position SortedMap<Integer, SortedSet<String>> columnsByNodePos = new TreeMap<>(); while (matches.hasNext()) { AnnotatedMatch match = matches.next(); for (int j = 0; j < match.size(); ++j) { AnnotatedSpan span = match.get(j); if (columnsByNodePos.get(j) == null) { columnsByNodePos.put(j, new TreeSet<String>()); } for (Annotation annotation : span.getAnnotations()) { columnsByNodePos.get(j).add("anno_" + annotation.getQualifiedName()); } for (Annotation meta : span.getMetadata()) { columnsByNodePos.get(j).add("meta_" + meta.getQualifiedName()); } } } CSVWriter csvWriter = new CSVWriter(w, '\t', CSVWriter.NO_QUOTE_CHARACTER, '\\'); // print column names and data types int count = columnsByNodePos.keySet().size(); ArrayList<String> headerLine = new ArrayList<>(); for (int j = 0; j < count; ++j) { headerLine.add(fullColumnName(j + 1, "id")); headerLine.add(fullColumnName(j + 1, "span")); SortedSet<String> annotationNames = columnsByNodePos.get(j); for (String name : annotationNames) { headerLine.add(fullColumnName(j + 1, name)); } } csvWriter.writeNext(headerLine.toArray(new String[headerLine.size()])); return columnsByNodePos; }
From source file:de.shadowhunt.sonar.plugins.ignorecode.model.AbstractPattern.java
static SortedSet<Integer> parseLineValues(final String lineValues) { final SortedSet<Integer> lines = new TreeSet<>(); if ("*".equals(lineValues)) { return lines; }// w w w.j a v a2 s.c om final String s = StringUtils.substringBetween(StringUtils.trim(lineValues), "[", "]"); final String[] parts = StringUtils.split(s, ','); for (final String part : parts) { if (StringUtils.contains(part, '-')) { final String[] range = StringUtils.split(part, '-'); final int from = Integer.parseInt(range[0]); final int to = Integer.parseInt(range[1]); addLines(lines, from, to); } else { lines.add(Integer.parseInt(part)); } } return lines; }
From source file:ch.entwine.weblounge.common.url.UrlUtils.java
/** * Sorts the given urls by path./* w w w .ja v a 2s.com*/ * * @param urls * the urls to sort * @return the sorted urls */ public static String[] sort(String[] urls) { TreeSet<String> set = new TreeSet<String>(); for (int i = 0; i < urls.length; i++) set.add(urls[i]); String[] result = new String[urls.length]; Iterator<String> i = set.iterator(); int index = 0; while (i.hasNext()) { result[index++] = i.toString(); } return result; }
From source file:com.spotify.heroic.filter.impl.OrFilterImpl.java
private static SortedSet<Filter> flatten(final Collection<Filter> statements) { final SortedSet<Filter> result = new TreeSet<>(); for (final Filter f : statements) { final Filter o = f.optimize(); if (o == null) { continue; }/* ww w . j a v a 2 s . c o m*/ if (o instanceof Filter.Or) { result.addAll(((Filter.Or) o).terms()); continue; } if (o instanceof Filter.Not) { final Filter.Not not = (Filter.Not) o; if (not.first() instanceof Filter.And) { result.addAll(collapseNotAnd((Filter.And) not.first())); continue; } } result.add(o); } return result; }
From source file:ch.zhaw.ias.dito.ui.util.HistogramFrame.java
public HistogramFrame(Question q) { String title = Translation.INSTANCE.get("misc.graphic.histogram") + " " + q.getName(); setTitle(title);/*from w w w .j a v a 2 s . c o m*/ HistogramDataset hist = new HistogramDataset(); hist.setType(HistogramType.FREQUENCY); Set<Double> values = new TreeSet<Double>(); q.getData().addValuesToCollection(values); int numOfBins = Math.min(values.size(), (q.getData().filteredLength() / 20) + 10); hist.addSeries(q.getName(), q.getData().getValues(), numOfBins); JFreeChart chart = ChartFactory.createHistogram(title, Translation.INSTANCE.get("misc.graphic.value"), Translation.INSTANCE.get("misc.graphic.frequency"), hist, PlotOrientation.VERTICAL, false, true, false); this.add(new ChartPanel(chart), BorderLayout.CENTER); this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); }
From source file:de.dhbw.vetaraus.NetFactory.java
/** * Create a new Netica net from an existing CSV file. Cases are learned through gradient descent learning algorithm. * * @param path//w ww . j a v a 2 s . c om * Filepath of the CSV file * @return A Netica net. * @throws NeticaException * Netica problems. * @throws IOException * I/O problems. */ public static Net fromCases(String path) throws NeticaException, IOException { List<Case> cases = SanitizeUtils.sanitizeCases(CSV.parse(path)); Set<String> ageGroupSet = new TreeSet<>(); Set<String> degreeSet = new TreeSet<>(); Set<String> occupationSet = new TreeSet<>(); Set<String> incomeSet = new TreeSet<>(); Set<String> tariffSet = new TreeSet<>(); // Find all states for (Case c : cases) { ageGroupSet.add(c.getAge()); degreeSet.add(c.getDegree()); occupationSet.add(c.getOccupation()); incomeSet.add(c.getIncome()); tariffSet.add(c.getTariff()); } Net net = new Net(NeticaUtils.getEnvironment()); Caseset caseset = getCaseset(cases); // Create nodes in net: NodeList nodeList = new NodeList(net); Node ageGroupNode = new Node(Constants.NODE_AGE, StringUtils.join(ageGroupSet, ','), net); Node genderNode = new Node(Constants.NODE_GENDER, "m,w", net); Node marriedNode = new Node(Constants.NODE_MARRIED, "ja,nein", net); Node childCountNode = new Node(Constants.NODE_CHILDCOUNT, "_0,_1,_2,_3,_4", net); Node degreeNode = new Node(Constants.NODE_DEGREE, StringUtils.join(degreeSet, ','), net); Node occupationNode = new Node(Constants.NODE_OCCUPATION, StringUtils.join(occupationSet, ','), net); Node incomeNode = new Node(Constants.NODE_INCOME, StringUtils.join(incomeSet, ','), net); Node tariffNode = new Node(Constants.NODE_INSURANCE, StringUtils.join(tariffSet, ','), net); // Link nodes: tariffNode.addLink(ageGroupNode); tariffNode.addLink(genderNode); tariffNode.addLink(marriedNode); tariffNode.addLink(childCountNode); tariffNode.addLink(incomeNode); incomeNode.addLink(occupationNode); occupationNode.addLink(degreeNode); nodeList.add(ageGroupNode); nodeList.add(genderNode); nodeList.add(marriedNode); nodeList.add(childCountNode); nodeList.add(degreeNode); nodeList.add(occupationNode); nodeList.add(incomeNode); nodeList.add(tariffNode); Learner learner = new Learner(Learner.GRADIENT_DESCENT_LEARNING); learner.learnCPTs(nodeList, caseset, 1.0); return net; }
From source file:edu.cmu.sphinx.speakerid.SpeakerCluster.java
public SpeakerCluster() { this.segmentSet = new TreeSet<Segment>(); }
From source file:net.eusashead.hateoas.header.impl.AllowHeaderImpl.java
public AllowHeaderImpl(HttpMethod... methods) { Set<HttpMethod> allowed = new TreeSet<HttpMethod>(); allowed.addAll(Arrays.asList(methods)); this.methods = allowed; }
From source file:net.sf.jabref.importer.ImportFormats.java
private static JFileChooser createImportFileChooser(String currentDir) { SortedSet<ImportFormat> importers = Globals.IMPORT_FORMAT_READER.getImportFormats(); String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_IMPORT); FileFilter defaultFilter = null; JFileChooser fc = new JFileChooser(currentDir); Set<ImportFileFilter> filters = new TreeSet<>(); for (ImportFormat format : importers) { ImportFileFilter filter = new ImportFileFilter(format); filters.add(filter);// w w w .ja v a2s . com if (format.getFormatName().equals(lastUsedFormat)) { defaultFilter = filter; } } for (ImportFileFilter filter : filters) { fc.addChoosableFileFilter(filter); } if (defaultFilter == null) { fc.setFileFilter(fc.getAcceptAllFileFilter()); } else { fc.setFileFilter(defaultFilter); } return fc; }