List of usage examples for org.apache.commons.lang3 StringUtils countMatches
public static int countMatches(final CharSequence str, final char ch)
Counts how many times the char appears in the given string.
A null or empty ("") String input returns 0 .
StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", 0) = 0 StringUtils.countMatches("abba", 'a') = 2 StringUtils.countMatches("abba", 'b') = 2 StringUtils.countMatches("abba", 'x') = 0
From source file:org.xwiki.rendering.parser.xwiki10.util.CleanUtil.java
/** * Check the provided string contains enough new lines at the beginning and add the need ones. * /*from w w w . j a va 2 s . co m*/ * @param content the content to convert. * @param nb the number of new lines the string need to contains at the beginning. * @return the converted string. */ public static String setLeadingNewLines(String content, int nb) { String cleanedContent = content; Matcher matcher = STARTING_NLNOOUTPUT_GROUP_PATTERN.matcher(content); int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0; if (foundNb < nb) { cleanedContent = StringUtils.repeat("\n", nb - foundNb) + content; } return cleanedContent; }
From source file:org.xwiki.rendering.parser.xwiki10.util.CleanUtil.java
/** * Check the provided string contains enough new lines at the end and add the need ones. * //from w w w . ja va2 s.com * @param content the content to convert. * @param nb the number of new lines the string need to contains at the end. * @return the converted string. */ public static String setTrailingNewLines(String content, int nb) { String cleanedContent = content; Matcher matcher = ENDING_NLNOOUTPUT_GROUP_PATTERN.matcher(content); int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0; if (foundNb < nb) { cleanedContent = content + StringUtils.repeat("\n", nb - foundNb); } return cleanedContent; }
From source file:org.xwiki.rendering.parser.xwiki10.util.CleanUtil.java
/** * Check the provided string contains enough new lines at the end and add the need ones. * //w ww.ja va2s . c o m * @param content the content to convert. * @param nb the number of new lines the string need to contains at the end. */ public static void setTrailingNewLines(StringBuffer content, int nb) { Matcher matcher = ENDING_NLNOOUTPUT_GROUP_PATTERN.matcher(content); int foundNb = matcher.find() ? StringUtils.countMatches(matcher.group(0), "\n") : 0; if (foundNb < nb) { content.append(StringUtils.repeat("\n", nb - foundNb)); } }
From source file:password.pwm.config.value.UserPermissionValue.java
private void validateLdapSearchFilter(final String filter) { if (filter == null || filter.isEmpty()) { return;//w w w . j a v a 2s. c om } final int leftParens = StringUtils.countMatches(filter, "("); final int rightParens = StringUtils.countMatches(filter, ")"); if (leftParens != rightParens) { throw new IllegalArgumentException("unbalanced parentheses"); } }
From source file:personality_prediction.LIWCDictionary.java
public Map<String, Double> getJustCounts(String text, boolean absoluteCounts) { Map<String, Double> counts = new LinkedHashMap<String, Double>(map.size()); String[] words = tokenize(text); String[] sentences = splitSentences(text); System.err.println(//w w w . ja v a2s . c o m "Input text splitted into " + words.length + " words and " + sentences.length + " sentences"); // word count (NOT A PROPER FEATURE) if (absoluteCounts) { counts.put("WC", new Double(words.length)); } counts.put("WPS", new Double(sentences.length)); // type token ratio, words with more than 6 letters, abbreviations, // emoticons, numbers int sixletters = 0; int numbers = 0; for (int i = 0; i < words.length; i++) { String word = words[i].toLowerCase(); if (word.length() > 6) { sixletters++; } if (word.matches("-?[,\\d+]*\\.?\\d+")) { numbers++; } } Set<String> types = new LinkedHashSet<String>(Arrays.asList(words)); counts.put("UNIQUE", new Double(types.size())); counts.put("SIXLTR", new Double(sixletters)); // abbreviations int abbrev = StringUtils.countMatches("\\w\\.(\\w\\.)+", text); counts.put("ABBREVIATIONS", new Double(abbrev)); // emoticons int emoticons = StringUtils.countMatches("[:;8%]-[\\)\\(\\@\\[\\]\\|]+", text); counts.put("EMOTICONS", new Double(emoticons)); // text ending with a question mark int qmarks = StringUtils.countMatches("\\w\\s*\\?", text); counts.put("QMARKS", new Double(qmarks)); // punctuation int period = StringUtils.countMatches("\\.", text); counts.put("PERIOD", new Double(period)); int comma = StringUtils.countMatches(",", text); counts.put("COMMA", new Double(comma)); int colon = StringUtils.countMatches(":", text); counts.put("COLON", new Double(colon)); int semicolon = StringUtils.countMatches(";", text); counts.put("SEMIC", new Double(semicolon)); int qmark = StringUtils.countMatches("\\?", text); counts.put("QMARK", new Double(qmark)); int exclam = StringUtils.countMatches("!", text); counts.put("EXCLAM", new Double(exclam)); int dash = StringUtils.countMatches("-", text); counts.put("DASH", new Double(dash)); int quote = StringUtils.countMatches("\"", text); counts.put("QUOTE", new Double(quote)); int apostr = StringUtils.countMatches("'", text); counts.put("APOSTRO", new Double(apostr)); int parent = StringUtils.countMatches("[\\(\\[{]", text); counts.put("PARENTH", new Double(parent)); int otherp = StringUtils.countMatches("[^\\w\\d\\s\\.:;\\?!\"'\\(\\{\\[,-]", text); counts.put("OTHERP", new Double(otherp)); int allp = period + comma + colon + semicolon + qmark + exclam + dash + quote + apostr + parent + otherp; counts.put("ALLPCT", new Double(allp)); // PATTERN MATCHING // store word in dic boolean[] indic = new boolean[words.length]; for (int i = 0; i < indic.length; i++) { indic[i] = false; } // first get all lexical counts for (String cat : map.keySet()) { // add entry to output hash Pattern catRegex = map.get(cat); int catCount = 0; for (int i = 0; i < words.length; i++) { String word = words[i].toLowerCase(); Matcher m = catRegex.matcher(word); while (m.find()) { catCount++; indic[i] = true; } } counts.put(cat, new Double(catCount)); } // put ratio of words matched int wordsMatched = 0; for (int i = 0; i < indic.length; i++) { if (indic[i]) { wordsMatched++; } } counts.put("DIC", new Double(wordsMatched)); // add numerical numbers double nonNumeric = ((Double) counts.get("NUMBERS")).doubleValue(); counts.put("NUMBERS", new Double(nonNumeric + numbers)); return counts; }
From source file:picard.sam.markduplicates.UmiGraph.java
List<DuplicateSet> joinUmisIntoDuplicateSets(final int maxEditDistanceToJoin) { // Compare all UMIs to each other. If they are within maxEditDistanceToJoin // join them to the same duplicate set using the union-find algorithm. GraphUtils.Graph<Integer> umiGraph = new GraphUtils.Graph<>(); for (int i = 0; i < numUmis; i++) { umiGraph.addNode(i);/*from w ww . j av a2 s. c om*/ for (int j = i + 1; j < numUmis; j++) { if (StringUtil.isWithinHammingDistance(umi[i], umi[j], maxEditDistanceToJoin)) { umiGraph.addEdge(i, j); } } } final Map<Integer, Integer> umiClusterMap = umiGraph.cluster(); // This ensures that all duplicate sets have unique IDs. During Union-Find a tree is constructed // where each UMI points to parent UMI. This ensures that all UMIs that belong to the same duplicate // set point to the same parent UMI. Note that the parent UMI is only used as a representative UMI and // is not at all related to the assigned UMI. for (int i = 0; i < numUmis; i++) { duplicateSetID[i] = umiClusterMap.get(i); } final Map<Integer, List<SAMRecord>> duplicateSets = new HashMap<>(); // Assign UMIs to duplicateSets final Map<String, Integer> duplicateSetsFromUmis = getDuplicateSetsFromUmis(); for (SAMRecord rec : records) { final String umi = UmiUtil.getSanitizedUMI(rec, umiTag); final Integer duplicateSetIndex = duplicateSetsFromUmis.get(umi); if (duplicateSets.containsKey(duplicateSetIndex)) { duplicateSets.get(duplicateSetIndex).add(rec); } else { final List<SAMRecord> n = new ArrayList<>(); n.add(rec); duplicateSets.put(duplicateSetIndex, n); } } final List<DuplicateSet> duplicateSetList = new ArrayList<>(); for (final Map.Entry<Integer, List<SAMRecord>> entry : duplicateSets.entrySet()) { final DuplicateSet ds = new DuplicateSet(); final List<SAMRecord> recordList = entry.getValue(); // Add records to the DuplicateSet recordList.forEach(ds::add); // For a particular duplicate set, identify the most common UMI // and use this as an assigned UMI. long maxCount = 0; String assignedUmi = null; String fewestNUmi = null; long nCount = 0; for (SAMRecord rec : recordList) { final String umi = UmiUtil.getSanitizedUMI(rec, umiTag); // If there is another choice, we don't want to choose the UMI with a N // as the assignedUmi if (umi.contains("N")) { int count = StringUtils.countMatches(umi, "N"); // If an UMI containing a N hasn't been seen before, the current UMI is now the fewestNUmi if (nCount == 0) { nCount = count; fewestNUmi = umi; } else if (count < nCount) { // If N containing UMI already exists, reset it if we find a lower one nCount = count; fewestNUmi = umi; } } else if (umiCounts.get(umi) > maxCount) { maxCount = umiCounts.get(umi); assignedUmi = umi; } } // If we didn't find a Umi w/o a N to represent // then choose the one with the fewest Ns if (assignedUmi == null) { assignedUmi = fewestNUmi; } // Set the records to contain the assigned UMI for (final SAMRecord rec : recordList) { if (allowMissingUmis && rec.getStringAttribute(umiTag).isEmpty()) { // The SAM spec doesn't support empty tags, so we set it to null if it is empty. rec.setAttribute(umiTag, null); } else { rec.setAttribute(assignedUmiTag, assignedUmi); } } duplicateSetList.add(ds); } return duplicateSetList; }
From source file:pl.prutkowski.java.playground.java8.TestCollectors.java
/** * @param args the command line arguments *//*from w w w . j a v a 2s. c o m*/ public static void main(String[] args) { Map<String, Integer> monthByLen = months.stream() .collect(Collectors.toMap(String::toUpperCase, m -> StringUtils.countMatches(m, "e"))); monthByLen.forEach((month, eCount) -> System.out.println(month + " -> " + eCount)); System.out.println("---------------------------------"); Map<Object, List<String>> monthByLen2 = months.stream() .collect(Collectors.groupingBy(m -> StringUtils.countMatches(m, "e"))); monthByLen2.forEach((count, groupedMonths) -> System.out.println(count + " -> " + groupedMonths)); System.out.println("---------------------------------"); Double averageLength = months.stream().collect(Collectors.averagingDouble(String::length)); System.out.println("Average length: " + averageLength); System.out.println("---------------------------------"); Double max = months.stream().collect(Collectors.summarizingDouble(String::length)).getMax(); System.out.println("Max length: " + max); System.out.println("---------------------------------"); String reduced = months.stream().collect(Collectors.reducing((m1, m2) -> (m1 + ", " + m2))).get(); System.out.println("Reduced: " + reduced); System.out.println("---------------------------------"); System.out.println(String.join(", ", months)); System.out.println("---------------------------------"); List<String> monthsWithZ = months.stream().filter(m -> m.contains("z")).collect(new ListCollector<>()); System.out.println(monthsWithZ); }
From source file:rapternet.irc.bots.wheatley.listeners.DefListener2.java
@Override public void onMessage(MessageEvent event) throws FileNotFoundException, InterruptedException { String message = Colors.removeFormattingAndColors(event.getMessage()); String[] msgSplit = message.split(" "); if (message.equalsIgnoreCase("!randef") || message.equalsIgnoreCase("!randdef")) { String word = defs.getRandomWord(); event.getBot().sendIRC().message(event.getChannel().getName(), Colors.BOLD + defs.getWordWithCase(word) + ": " + Colors.NORMAL + defs.getDefOfWord(word)); } else if (msgSplit[0].equalsIgnoreCase("!whodef")) { String word = message.split(" ", 2)[1]; if (defs.containsDef(word)) { event.getBot().sendIRC().message(event.getChannel().getName(), Colors.BOLD + defs.getWordWithCase(word) + Colors.NORMAL + " was defined by " + defs.getOriginator(word) + " at " + IRCUtils.getTimestamp(String.valueOf(Long.parseLong(defs.getTimeOfDef(word)) * 1000))); } else if (!event.getBot().getUserChannelDao() .getChannels(event.getBot().getUserChannelDao().getUser("theTardis")) .contains(event.getChannel())) { event.getBot().sendIRC().notice(event.getUser().getNick(), Colors.BOLD + "WhoDef: " + Colors.NORMAL + "Definition not found"); }/* w ww. j av a 2 s .c o m*/ } if (message.endsWith("?") && message.split("\\?", 2)[0].length() > 0 && message.split("\\?").length == 1 && StringUtils.countMatches(message, "?") == 1) { if (defs.containsDef(message.split("\\?")[0])) { String word = message.split("\\?")[0]; // int index = indexOfIgnoreCase(words,message.split("\\?")[0]); event.getBot().sendIRC().message(event.getChannel().getName(), Colors.BOLD + defs.getWordWithCase(word) + Colors.NORMAL + ": " + defs.getDefOfWord(word)); } } //Global.getTimestamp(Long.toString(rs.getLong("TIME")*1000)) //event.getTimestamp() / 1000 if (message.equalsIgnoreCase("!list defs")) { ArrayList<String> sortedWords = defs.getDefList(); // sortedWords.addAll(words); Collections.sort(sortedWords, String.CASE_INSENSITIVE_ORDER); String wordList = ""; for (int i = 0; i < sortedWords.size(); i++) { wordList = wordList + sortedWords.get(i) + ", "; } event.getBot().sendIRC().message(event.getUser().getNick(), wordList); } if (msgSplit.length > 3 && msgSplit[0].equalsIgnoreCase("tell") && msgSplit[2].equalsIgnoreCase("about")) { String user = message.split(" ")[1]; if (event.getBot().getUserChannelDao().getAllUsers() .contains(event.getBot().getUserChannelDao().getUser(user))) { //If the user is in the same channel as the summon String defWord = message.split("(?i)about(?-i)")[1].trim();//.split(" ",2)[2]; if (defs.containsDef(defWord)) { String def = defs.getDefOfWord(defWord); event.getBot().sendIRC().notice(event.getUser().getNick(), user + " has been PMed"); event.getBot().sendIRC().message(event.getBot().getUserChannelDao().getUser(user).getNick(), event.getUser().getNick() + " wants me to tell you about: " + Colors.BOLD + defs.getWordWithCase(defWord) + Colors.NORMAL + ": " + Colors.NORMAL + def); } else if (!event.getBot().getUserChannelDao() .getChannels(event.getBot().getUserChannelDao().getUser("theTardis")) .contains(event.getChannel())) { event.getBot().sendIRC().notice(event.getUser().getNick(), Colors.BOLD + "tell " + Colors.NORMAL + "definition not found"); } } else if (!event.getBot().getUserChannelDao() .getChannels(event.getBot().getUserChannelDao().getUser("theTardis")) .contains(event.getChannel())) { event.getBot().sendIRC().notice(event.getUser().getNick(), Colors.BOLD + "tell " + Colors.NORMAL + "user not in channel"); } } // if(msgSplit[0].equalsIgnoreCase("!load")){ // // if (event.getUser().getNick().equalsIgnoreCase(Global.botOwner)&&event.getUser().isVerified()){ // // addDefsFromFile("definitions.txt", String.valueOf(event.getTimestamp()/1000)); // addDefLogFromFile("definitionLog.txt", String.valueOf(event.getTimestamp()/1000)); // } // } // ADDING DEFINITIONS if (msgSplit[0].equalsIgnoreCase("!mkdef")) {//||msgSplit[0].equalsIgnoreCase("!addef") if (event.getUser().getNick().equalsIgnoreCase(Global.botOwner) && event.getUser().isVerified()) { if (message.split("@").length != 2) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Improperly formed defintion add command: !mkdef word or phrase @ definition phrase"); } else if (StringUtils.countMatches(message.split("@")[0], "?") > 0) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Improperly formed defintion add command: Definition terms may not contain a \"?\""); } else if (defs.containsDef(message.split(" ", 2)[1].split("@")[0].trim())) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Definition already exists"); } else { String word = message.split(" ", 2)[1].split("@")[0].trim(); String def = message.split("@")[1].trim(); String user = event.getUser().getNick(); String time = String.valueOf(event.getTimestamp() / 1000); defs.createDef(word, def, user, time); event.getBot().sendIRC().notice(event.getUser().getNick(), "Success: " + word + " was added to the definitions file"); } } else event.getBot().sendIRC().notice(event.getUser().getNick(), "You do not have access to this function"); } // REMOVING DEFINITIONS if (msgSplit[0].equalsIgnoreCase("!rmdef")) {//||msgSplit[0].equalsIgnoreCase("!deletedef") if (event.getUser().getNick().equalsIgnoreCase(Global.botOwner) && event.getUser().isVerified()) { String word = message.split(" ", 2)[1]; if (!defs.containsDef(word)) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Definition not found"); } else { String logWord = defs.getWordWithCase(word); int i = 0; while (defsLog.containsDef(logWord)) { logWord = word + String.valueOf(i); i++; } defsLog.createDef(logWord, defs.getDefOfWord(word), defs.getOriginator(word), defs.getTimeOfDef(word)); boolean success = defs.deleteDef(word); if (success) event.getBot().sendIRC().notice(event.getUser().getNick(), "Success: " + word + " was removed from the definitions file"); else event.getBot().sendIRC().notice(event.getUser().getNick(), "SOMETHING BROKE: DEF NOT DELETED"); } } else event.getBot().sendIRC().notice(event.getUser().getNick(), "You do not have access to this function"); } // Updating definitions already in the db if (msgSplit[0].equalsIgnoreCase("!overdef")) {//||msgSplit[0].equalsIgnoreCase("!updef") if (event.getUser().getNick().equalsIgnoreCase(Global.botOwner) && event.getUser().isVerified()) { if (!(message.split("@").length == 2)) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Improperly formed update command: !overdef word phrase @ definition phrase"); } else if (!defs.containsDef(message.split(" ", 2)[1].split("@")[0].trim())) { event.getBot().sendIRC().notice(event.getUser().getNick(), "Def currently does not exist, please use !adddef"); } else { String word = message.split(" ", 2)[1].split("@")[0].trim(); System.out.println(word); String logWord = defs.getWordWithCase(word); int i = 0; while (defsLog.containsDef(logWord)) { logWord = word + String.valueOf(i); i++; } defsLog.createDef(logWord, defs.getDefOfWord(word), defs.getOriginator(word), defs.getTimeOfDef(word)); boolean success = defs.deleteDef(word); defs.createDef(word, message.split("@")[1], event.getUser().getNick(), String.valueOf(event.getTimestamp() / 1000)); if (success) event.getBot().sendIRC().notice(event.getUser().getNick(), "Success: " + message.split(" ", 2)[1].split("@")[0].trim() + " was updated in the definitions file"); else event.getBot().sendIRC().notice(event.getUser().getNick(), "SOMETHING BROKE: FILE NOT UPDATED"); } } else event.getBot().sendIRC().notice(event.getUser().getNick(), "You do not have access to this function"); } }
From source file:rapture.kernel.BlobApiImpl.java
@Override public Map<String, RaptureFolderInfo> listBlobsByUriPrefix(CallingContext context, String uriPrefix, int depth) { RaptureURI internalUri = new RaptureURI(uriPrefix, BLOB); String authority = internalUri.getAuthority(); Map<String, RaptureFolderInfo> ret = new HashMap<String, RaptureFolderInfo>(); // Schema level is special case. if (authority.isEmpty()) { --depth;/*from w w w. j av a 2 s .c o m*/ try { List<BlobRepoConfig> configs = getBlobRepoConfigs(context); for (BlobRepoConfig config : configs) { authority = config.getAuthority(); // NULL or empty string should not exist. if ((authority == null) || authority.isEmpty()) { log.warn("Invalid authority (null or empty string) found for " + JacksonUtil.jsonFromObject(config)); continue; } String uri = new RaptureURI(authority, BLOB).toString(); ret.put(uri, new RaptureFolderInfo(authority, true)); if (depth != 0) { ret.putAll(listBlobsByUriPrefix(context, uri, depth)); } } } catch (RaptureException e) { // permission denied log.debug("No read permission for " + uriPrefix); } return ret; } BlobRepo repo = getRepoFromCache(internalUri.getAuthority()); Boolean getAll = false; if (repo == null) { return ret; } String parentDocPath = internalUri.getDocPath() == null ? "" : internalUri.getDocPath(); int startDepth = StringUtils.countMatches(parentDocPath, "/"); if (log.isDebugEnabled()) { log.debug("Loading all children from repo " + internalUri.getAuthority() + " with " + internalUri.getDocPath()); } if (depth <= 0) getAll = true; Stack<String> parentsStack = new Stack<String>(); parentsStack.push(parentDocPath); while (!parentsStack.isEmpty()) { String currParentDocPath = parentsStack.pop(); int currDepth = StringUtils.countMatches(currParentDocPath, "/") - startDepth; if (!getAll && currDepth >= depth) continue; boolean top = currParentDocPath.isEmpty(); // Make sure that you have permission to read the folder. try { ListBlobsByUriPrefixPayload requestObj = new ListBlobsByUriPrefixPayload(); requestObj.setContext(context); requestObj.setBlobUri(currParentDocPath); ContextValidator.validateContext(context, EntitlementSet.Blob_listBlobsByUriPrefix, requestObj); } catch (RaptureException e) { // permission denied log.debug("No read permission on folder " + currParentDocPath); continue; } List<RaptureFolderInfo> children = repo.listMetaByUriPrefix(currParentDocPath); if ((children == null) || (children.isEmpty()) && (currDepth == 0) && (internalUri.hasDocPath())) { return ret; } else { for (RaptureFolderInfo child : children) { String childDocPath = currParentDocPath + (top ? "" : "/") + child.getName(); if (child.getName().isEmpty()) continue; String childUri = RaptureURI.builder(BLOB, authority).docPath(childDocPath).asString() + (child.isFolder() ? "/" : ""); ret.put(childUri, child); if (child.isFolder()) { parentsStack.push(childDocPath); } } } if (top) startDepth--; // special case } return ret; }
From source file:rapture.kernel.cache.config.FormatHelper.java
public int getNumExpected(String standardTemplate) { return StringUtils.countMatches(standardTemplate, "%s"); }