protected String clozeText(String txt, String ord, String type) { String regex = String.format(Locale.US, Mustache.clozeReg, ord); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(txt); if (!m.find()) { return ""; } // replace chosen cloze with type if (type.equals("q")) { if (m.group(3) != "") { txt = txt.replaceFirst( regex, String.format(Locale.US, "<span class=cloze>[%s...]</span>", m.group(3))); } else { txt = txt.replaceFirst( regex, String.format(Locale.US, "<span class=cloze>[...]</span>", txt)); } } else if (type.equals("a")) { txt = txt.replaceFirst( regex, String.format(Locale.US, "<span class=cloze>%s</span>", m.group(1))); } regex = String.format(Locale.US, Mustache.clozeReg, ".*?"); // and display other clozes normally p = Pattern.compile(regex); m = p.matcher(txt); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, m.group(1)); } m.appendTail(sb); return m.toString(); }
public String replaceAll(String subjectOfReplacement) { if (!findCalled) find(); int lastCondMatcherGroupCount = this.groupCount(); Matcher variableMatcher = variablePattern.matcher(subjectOfReplacement); StringBuffer sb = new StringBuffer(); while (variableMatcher.find()) { int groupCount = variableMatcher.groupCount(); if (groupCount < 1) { log.error("group count on variable finder regex is not as expected"); if (log.isDebugEnabled()) { log.error("variableMatcher: " + variableMatcher.toString()); } continue; } String varStr = variableMatcher.group(1); boolean validVariable = false; int varInt = 0; log.debug("found " + varStr); try { varInt = Integer.parseInt(varStr); if (varInt > lastCondMatcherGroupCount) { log.error("variable $" + varInt + " not found"); if (log.isDebugEnabled()) { log.debug("wildcard matcher: " + this.toString()); } } else { validVariable = true; } } catch (NumberFormatException nfe) { log.error("could not parse variable " + varStr + " to number"); } String conditionMatch = ""; if (validVariable) { conditionMatch = this.group(varInt); } variableMatcher.appendReplacement(sb, conditionMatch); } variableMatcher.appendTail(sb); if (log.isDebugEnabled()) log.debug("replaced sb is " + sb); String result = sb.toString(); Matcher escapedVariableMatcher = escapedVariablePattern.matcher(result); result = escapedVariableMatcher.replaceAll("$1"); return result; }
public static Command parceVoice(List<String> candidates) { Command command = new Command(); for (String voice : candidates) { Log.d(COMMAND_MASTER_TAG, String.format("raw voice => %s", voice)); Matcher m = COMMAND_PATTERN.matcher(voice.trim()); if (m.matches()) { Log.d(COMMAND_MASTER_TAG, m.toString()); String s = m.group(1); Log.d(COMMAND_MASTER_TAG, s); if ("拡大".equals(s)) { command.setMessage("ZOOM_IN\n"); command.setVoice("拡大"); break; } else if ("縮小".equals(s)) { command.setMessage("ZOOM_OUT\n"); command.setVoice("縮小"); break; } else if ("全体".equals(s)) { command.setMessage("ZOOM_FIT\n"); command.setVoice("全体"); break; } else if ("上".equals(s)) { command.setMessage(String.format("FLING,vx=%f,vy=%f\n", 0.0f, -VY)); command.setVoice("上"); break; } else if ("下".equals(s)) { command.setMessage(String.format("FLING,vx=%f,vy=%f\n", 0.0f, VY)); command.setVoice("下"); break; } else if ("右".equals(s)) { command.setMessage(String.format("FLING,vx=%f,vy=%f\n", VX, 0.0f)); command.setVoice("右"); break; } else if ("左".equals(s)) { command.setMessage(String.format("FLING,vx=%f,vy=%f\n", -VX, 0.0f)); command.setVoice("左"); break; } else { command.setMessage(String.format("GOTO_PAGE,page=%d\n\n", Integer.parseInt(s))); command.setVoice(String.format("%dページ", Integer.parseInt(s))); break; } } } return command; }
private void addFileToMap( Matcher matcher, Path file, boolean sawBase, Map<Integer, BucketTracker> splitToBucketMap) { if (!matcher.find()) { LOG.warn( "Found a non-bucket file that we thought matched the bucket pattern! " + file.toString() + " Matcher=" + matcher.toString()); } int bucketNum = Integer.valueOf(matcher.group()); BucketTracker bt = splitToBucketMap.get(bucketNum); if (bt == null) { bt = new BucketTracker(); splitToBucketMap.put(bucketNum, bt); } LOG.debug("Adding " + file.toString() + " to list of files for splits"); bt.buckets.add(file); bt.sawBase |= sawBase; }